Add latest changes from gitlab-org/gitlab@15-10-stable-ee

This commit is contained in:
GitLab Bot 2023-04-06 13:41:41 +00:00
parent ae06966da1
commit c052cc60d2
3 changed files with 144 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -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