From 5ce1068a02630943780f583a776b690eda9a93eb Mon Sep 17 00:00:00 2001 From: Ash McKenzie Date: Tue, 27 Aug 2024 18:04:02 +1000 Subject: [PATCH] Write commits into temporary branches first --- ee/app/services/projects/update_mirror_service.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/ee/app/services/projects/update_mirror_service.rb b/ee/app/services/projects/update_mirror_service.rb index 291b2488cfc07a..59e6e65b761b7c 100644 --- a/ee/app/services/projects/update_mirror_service.rb +++ b/ee/app/services/projects/update_mirror_service.rb @@ -4,6 +4,8 @@ module Projects class UpdateMirrorService < BaseService include Gitlab::Utils::StrongMemoize + TEMP_BRANCH_PREFIX = "update-mirror/" + Error = Class.new(StandardError) UpdateError = Class.new(Error) @@ -32,12 +34,16 @@ def execute project.fetch_mirror(forced: true, check_tags_changed: true) end - update_branches + update_branches(branch_prefix: TEMP_BRANCH_PREFIX) # Updating LFS objects is expensive since it requires scanning for blobs with pointers. # Let's skip this if the repository hasn't changed. update_lfs_objects if update_lfs_objects?(checksum_before) + update_branches do |name| + ::Branches::DeleteService.new(project, current_user).execute("#{TEMP_BRANCH_PREFIX}#{name}") + end + # Running git fetch in the repository creates loose objects in the same # way running git push *to* the repository does, so ensure we run regular # garbage collection @@ -70,14 +76,14 @@ def normalized_url(url) end end - def update_branches + def update_branches(branch_prefix: "", &block) local_branches = repository.branches.index_by(&:name) errors = [] branches_to_create = {} repository.upstream_branches.each do |upstream_branch| - name = upstream_branch.name + name = "#{branch_prefix}#{upstream_branch.name}" next if skip_branch?(name) @@ -96,6 +102,8 @@ def update_branches errors << e.message end end + + yield(name) if block end result = ::Branches::CreateService.new(project, current_user).bulk_create(branches_to_create) -- GitLab