From 081e2fce8240f86f991b0bd3653ef6756f2cf9aa Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Fri, 25 Aug 2017 23:00:06 +0800 Subject: [PATCH] Try to make reserved ref names more obvious So that whenever we want to reserve more, we're aware, and don't mess it up. --- app/models/environment.rb | 2 +- app/models/merge_request.rb | 2 +- app/models/repository.rb | 18 +++++++++++++++--- app/services/projects/after_import_service.rb | 5 +---- .../projects/after_import_service_spec.rb | 2 +- 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/app/models/environment.rb b/app/models/environment.rb index e9ebf0637f3..435eeaf0e2e 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -114,7 +114,7 @@ class Environment < ActiveRecord::Base end def ref_path - "refs/environments/#{Shellwords.shellescape(name)}" + "refs/#{Repository::REF_ENVIRONMENTS}/#{Shellwords.shellescape(name)}" end def formatted_external_url diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index f028d2395c1..8361039f301 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -797,7 +797,7 @@ class MergeRequest < ActiveRecord::Base end def ref_path - "refs/merge-requests/#{iid}/head" + "refs/#{Repository::REF_MERGE_REQUEST}/#{iid}/head" end def ref_fetched? diff --git a/app/models/repository.rb b/app/models/repository.rb index c1e4fcf94a4..c247fb840ce 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -1,6 +1,18 @@ require 'securerandom' class Repository + REF_MERGE_REQUEST = 'merge-requests' + REF_KEEP_AROUND = 'keep-around' + REF_ENVIRONMENTS = 'environments' + + RESERVED_REFS_NAMES = %W[ + heads + tags + #{REF_ENVIRONMENTS} + #{REF_KEEP_AROUND} + #{REF_ENVIRONMENTS} + ].freeze + include Gitlab::ShellAdapter include RepositoryMirroring @@ -228,10 +240,10 @@ class Repository begin write_ref(keep_around_ref_name(sha), sha) rescue Rugged::ReferenceError => ex - Rails.logger.error "Unable to create keep-around reference for repository #{path}: #{ex}" + Rails.logger.error "Unable to create #{REF_KEEP_AROUND} reference for repository #{path}: #{ex}" rescue Rugged::OSError => ex raise unless ex.message =~ /Failed to create locked file/ && ex.message =~ /File exists/ - Rails.logger.error "Unable to create keep-around reference for repository #{path}: #{ex}" + Rails.logger.error "Unable to create #{REF_KEEP_AROUND} reference for repository #{path}: #{ex}" end end @@ -1152,7 +1164,7 @@ class Repository end def keep_around_ref_name(sha) - "refs/keep-around/#{sha}" + "refs/#{REF_KEEP_AROUND}/#{sha}" end def repository_event(event, tags = {}) diff --git a/app/services/projects/after_import_service.rb b/app/services/projects/after_import_service.rb index bbada7e2b1c..107856885f3 100644 --- a/app/services/projects/after_import_service.rb +++ b/app/services/projects/after_import_service.rb @@ -1,10 +1,7 @@ module Projects class AfterImportService - RESERVED_REFS_NAMES = - %w[heads tags merge-requests keep-around environments].freeze - RESERVED_REFS_REGEXP = - %r{\Arefs/(?:#{Regexp.union(*RESERVED_REFS_NAMES)})/} + %r{\Arefs/(?:#{Regexp.union(*Repository::RESERVED_REFS_NAMES)})/} def initialize(project) @project = project diff --git a/spec/services/projects/after_import_service_spec.rb b/spec/services/projects/after_import_service_spec.rb index 540d2191b2d..c6678fc1f5c 100644 --- a/spec/services/projects/after_import_service_spec.rb +++ b/spec/services/projects/after_import_service_spec.rb @@ -37,7 +37,7 @@ describe Projects::AfterImportService do end end - described_class::RESERVED_REFS_NAMES.each do |name| + Repository::RESERVED_REFS_NAMES.each do |name| context "with a ref in refs/#{name}/tmp" do before do repository.write_ref("refs/#{name}/tmp", sha)