summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2023-04-06 13:41:41 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2023-04-06 13:41:41 +0000
commitc052cc60d2d816fd6506cc6fdacb680cfef85a2c (patch)
treefe18cab61222404b8d598bbd4023f772dd5e762a
parentae06966da16860568740fd607fdf98377fcada81 (diff)
downloadgitlab-ce-c052cc60d2d816fd6506cc6fdacb680cfef85a2c.tar.gz
Add latest changes from gitlab-org/gitlab@15-10-stable-ee
-rw-r--r--config/feature_flags/development/resolve_ambiguous_archives.yml8
-rw-r--r--lib/gitlab/git/repository.rb30
-rw-r--r--spec/lib/gitlab/git/repository_spec.rb108
3 files changed, 144 insertions, 2 deletions
diff --git a/config/feature_flags/development/resolve_ambiguous_archives.yml b/config/feature_flags/development/resolve_ambiguous_archives.yml
new file mode 100644
index 00000000000..ed4ed492105
--- /dev/null
+++ b/config/feature_flags/development/resolve_ambiguous_archives.yml
@@ -0,0 +1,8 @@
+---
+name: resolve_ambiguous_archives
+introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/116411
+rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/403650
+milestone: '15.11'
+type: development
+group: group::source code
+default_enabled: true
diff --git a/lib/gitlab/git/repository.rb b/lib/gitlab/git/repository.rb
index 95633400aee..40cb4521f6a 100644
--- a/lib/gitlab/git/repository.rb
+++ b/lib/gitlab/git/repository.rb
@@ -262,7 +262,15 @@ module Gitlab
def archive_metadata(ref, storage_path, project_path, format = "tar.gz", append_sha:, path: nil)
ref ||= root_ref
- commit = Gitlab::Git::Commit.find(self, ref)
+
+ if Feature.enabled?(:resolve_ambiguous_archives, container)
+ commit_id = extract_commit_id_from_ref(ref)
+ return {} if commit_id.nil?
+ else
+ commit_id = ref
+ end
+
+ commit = Gitlab::Git::Commit.find(self, commit_id)
return {} if commit.nil?
prefix = archive_prefix(ref, commit.id, project_path, append_sha: append_sha, path: path)
@@ -1223,6 +1231,26 @@ module Gitlab
def gitaly_delete_refs(*ref_names)
gitaly_ref_client.delete_refs(refs: ref_names) if ref_names.any?
end
+
+ # The order is based on git priority to resolve ambiguous references
+ #
+ # `git show <ref>`
+ #
+ # In case of name clashes, it uses this order:
+ # 1. Commit
+ # 2. Tag
+ # 3. Branch
+ def extract_commit_id_from_ref(ref)
+ return ref if Gitlab::Git.commit_id?(ref)
+
+ tag = find_tag(ref)
+ return tag.dereferenced_target.sha if tag
+
+ branch = find_branch(ref)
+ return branch.dereferenced_target.sha if branch
+
+ ref
+ end
end
end
end
diff --git a/spec/lib/gitlab/git/repository_spec.rb b/spec/lib/gitlab/git/repository_spec.rb
index 483140052f0..15bce16bd7f 100644
--- a/spec/lib/gitlab/git/repository_spec.rb
+++ b/spec/lib/gitlab/git/repository_spec.rb
@@ -116,7 +116,8 @@ RSpec.describe Gitlab::Git::Repository, feature_category: :source_code_managemen
let(:expected_extension) { 'tar.gz' }
let(:expected_filename) { "#{expected_prefix}.#{expected_extension}" }
let(:expected_path) { File.join(storage_path, cache_key, "@v2", expected_filename) }
- let(:expected_prefix) { "gitlab-git-test-#{ref}-#{TestEnv::BRANCH_SHA['master']}" }
+ let(:expected_prefix) { "gitlab-git-test-#{ref.tr('/', '-')}-#{expected_prefix_sha}" }
+ let(:expected_prefix_sha) { TestEnv::BRANCH_SHA['master'] }
subject(:metadata) { repository.archive_metadata(ref, storage_path, 'gitlab-git-test', format, append_sha: append_sha, path: path) }
@@ -173,6 +174,111 @@ RSpec.describe Gitlab::Git::Repository, feature_category: :source_code_managemen
it { expect(metadata['ArchivePath']).to eq(expected_path) }
end
end
+
+ context 'when references are ambiguous' do
+ let_it_be(:ambiguous_project) { create(:project, :repository) }
+ let_it_be(:repository) { ambiguous_project.repository.raw }
+ let_it_be(:branch_merged_commit_id) { ambiguous_project.repository.find_branch('branch-merged').dereferenced_target.id }
+ let_it_be(:branch_master_commit_id) { ambiguous_project.repository.find_branch('master').dereferenced_target.id }
+ let_it_be(:tag_1_0_0_commit_id) { ambiguous_project.repository.find_tag('v1.0.0').dereferenced_target.id }
+
+ context 'when tag is ambiguous' do
+ before do
+ ambiguous_project.repository.add_tag(user, ref, 'master', 'foo')
+ end
+
+ after do
+ ambiguous_project.repository.rm_tag(user, ref)
+ end
+
+ where(:ref, :expected_commit_id, :desc) do
+ 'refs/heads/branch-merged' | ref(:branch_master_commit_id) | 'when tag looks like a branch'
+ 'branch-merged' | ref(:branch_master_commit_id) | 'when tag has the same name as a branch'
+ ref(:branch_merged_commit_id) | ref(:branch_merged_commit_id) | 'when tag looks like a commit id'
+ 'v0.0.0' | ref(:branch_master_commit_id) | 'when tag looks like a normal tag'
+ end
+
+ with_them do
+ it 'selects the correct commit' do
+ expect(metadata['CommitId']).to eq(expected_commit_id)
+ end
+ end
+
+ context 'when resolve_ambiguous_archives is disabled' do
+ before do
+ stub_feature_flags(resolve_ambiguous_archives: false)
+ end
+
+ where(:ref, :expected_commit_id, :desc) do
+ 'refs/heads/branch-merged' | ref(:branch_merged_commit_id) | 'when tag looks like a branch (difference!)'
+ 'branch-merged' | ref(:branch_master_commit_id) | 'when tag has the same name as a branch'
+ ref(:branch_merged_commit_id) | ref(:branch_merged_commit_id) | 'when tag looks like a commit id'
+ 'v0.0.0' | ref(:branch_master_commit_id) | 'when tag looks like a normal tag'
+ end
+
+ with_them do
+ it 'selects the correct commit' do
+ expect(metadata['CommitId']).to eq(expected_commit_id)
+ end
+ end
+ end
+ end
+
+ context 'when branch is ambiguous' do
+ before do
+ ambiguous_project.repository.add_branch(user, ref, 'master')
+ end
+
+ where(:ref, :expected_commit_id, :desc) do
+ 'refs/tags/v1.0.0' | ref(:branch_master_commit_id) | 'when branch looks like a tag'
+ 'v1.0.0' | ref(:tag_1_0_0_commit_id) | 'when branch has the same name as a tag'
+ ref(:branch_merged_commit_id) | ref(:branch_merged_commit_id) | 'when branch looks like a commit id'
+ 'just-a-normal-branch' | ref(:branch_master_commit_id) | 'when branch looks like a normal branch'
+ end
+
+ with_them do
+ it 'selects the correct commit' do
+ expect(metadata['CommitId']).to eq(expected_commit_id)
+ end
+ end
+
+ context 'when resolve_ambiguous_archives is disabled' do
+ before do
+ stub_feature_flags(resolve_ambiguous_archives: false)
+ end
+
+ where(:ref, :expected_commit_id, :desc) do
+ 'refs/tags/v1.0.0' | ref(:tag_1_0_0_commit_id) | 'when branch looks like a tag (difference!)'
+ 'v1.0.0' | ref(:tag_1_0_0_commit_id) | 'when branch has the same name as a tag'
+ ref(:branch_merged_commit_id) | ref(:branch_merged_commit_id) | 'when branch looks like a commit id'
+ 'just-a-normal-branch' | ref(:branch_master_commit_id) | 'when branch looks like a normal branch'
+ end
+
+ with_them do
+ it 'selects the correct commit' do
+ expect(metadata['CommitId']).to eq(expected_commit_id)
+ end
+ end
+ end
+ end
+
+ context 'when ref is HEAD' do
+ let(:ref) { 'HEAD' }
+
+ it 'selects commit id from HEAD ref' do
+ expect(metadata['CommitId']).to eq(branch_master_commit_id)
+ expect(metadata['ArchivePrefix']).to eq(expected_prefix)
+ end
+ end
+
+ context 'when ref is not found' do
+ let(:ref) { 'unknown-ref-cannot-be-found' }
+
+ it 'returns empty metadata' do
+ expect(metadata).to eq({})
+ end
+ end
+ end
end
describe '#size' do