Commit 0187018e authored by Jacob Vosmaer (GitLab)'s avatar Jacob Vosmaer (GitLab) Committed by Douwe Maan

Simplify Git interactions in AfterImportService

parent 7d3e888d
module Projects module Projects
class AfterImportService class AfterImportService
RESERVED_REFS_REGEXP = RESERVED_REF_PREFIXES = Repository::RESERVED_REFS_NAMES.map { |n| File.join('refs', n, '/') }
%r{\Arefs/(?:#{Regexp.union(*Repository::RESERVED_REFS_NAMES)})/}
def initialize(project) def initialize(project)
@project = project @project = project
...@@ -9,7 +8,7 @@ module Projects ...@@ -9,7 +8,7 @@ module Projects
def execute def execute
Projects::HousekeepingService.new(@project).execute do Projects::HousekeepingService.new(@project).execute do
repository.delete_refs(*garbage_refs) repository.delete_all_refs_except(RESERVED_REF_PREFIXES)
end end
rescue Projects::HousekeepingService::LeaseTaken => e rescue Projects::HousekeepingService::LeaseTaken => e
Rails.logger.info( Rails.logger.info(
...@@ -18,10 +17,6 @@ module Projects ...@@ -18,10 +17,6 @@ module Projects
private private
def garbage_refs
@garbage_refs ||= repository.all_ref_names_except(RESERVED_REFS_REGEXP)
end
def repository def repository
@repository ||= @project.repository @repository ||= @project.repository
end end
......
...@@ -250,11 +250,17 @@ module Gitlab ...@@ -250,11 +250,17 @@ module Gitlab
branch_names + tag_names branch_names + tag_names
end end
def delete_all_refs_except(prefixes)
delete_refs(*all_ref_names_except(prefixes))
end
# Returns an Array of all ref names, except when it's matching pattern # Returns an Array of all ref names, except when it's matching pattern
# #
# regexp - The pattern for ref names we don't want # regexp - The pattern for ref names we don't want
def all_ref_names_except(regexp) def all_ref_names_except(prefixes)
rugged.references.reject { |ref| ref.name =~ regexp }.map(&:name) rugged.references.reject do |ref|
prefixes.any? { |p| ref.name.start_with?(p) }
end.map(&:name)
end end
# Discovers the default branch based on the repository's available branches # Discovers the default branch based on the repository's available branches
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment