diff --git a/ee/app/services/projects/update_mirror_service.rb b/ee/app/services/projects/update_mirror_service.rb index 291b2488cfc07a006a253030348dbfb97b567f63..59e6e65b761b7c57be8a002ae808733f4ab693da 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)