Don't create a temp reference for branch comparisons within project

A temp reference is only needed to fetch a branch from another project,
as in the case for forked repositories. For branch comparisons within
the same project, we can just use the existing branch names to do the
comparison.

Relates to https://gitlab.com/gitlab-org/gitlab-ce/issues/38689#note_126107862
This commit is contained in:
Stan Hu 2018-12-26 10:24:16 -08:00 committed by Oswaldo Ferreira
parent 9ec37d3dc1
commit 26653eb035
3 changed files with 48 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
title: Don't create a temp reference for branch comparisons within project
merge_request:
author:
type: fixed

View file

@ -732,6 +732,12 @@ module Gitlab
end
def compare_source_branch(target_branch_name, source_repository, source_branch_name, straight:)
if source_repository == self
return unless commit(source_branch_name).present?
return Gitlab::Git::Compare.new(self, target_branch_name, source_branch_name, straight: straight)
end
tmp_ref = "refs/tmp/#{SecureRandom.hex}"
return unless fetch_source_branch!(source_repository, source_branch_name, tmp_ref)
@ -743,7 +749,7 @@ module Gitlab
straight: straight
)
ensure
delete_refs(tmp_ref)
delete_refs(tmp_ref) if tmp_ref
end
def write_ref(ref_path, ref, old_ref: nil)

View file

@ -1966,6 +1966,42 @@ describe Gitlab::Git::Repository, :seed_helper do
end
end
describe '#compare_source_branch' do
context 'within same repository' do
it 'does not create a temp ref' do
expect(repository).not_to receive(:fetch_source_branch!)
expect(repository).not_to receive(:delete_refs)
compare = repository.compare_source_branch('master', repository, 'feature', straight: false)
expect(compare).to be_a(Gitlab::Git::Compare)
expect(compare.commits.count).to be > 0
end
it 'returns nil when source ref does not exist' do
compare = repository.compare_source_branch('master', repository, 'non-existent-branch', straight: false)
expect(compare).to be_nil
end
end
context 'with different repositories' do
it 'creates a temp ref' do
expect(repository).to receive(:fetch_source_branch!).and_call_original
expect(repository).to receive(:delete_refs).and_call_original
compare = repository.compare_source_branch('master', mutable_repository, 'feature', straight: false)
expect(compare).to be_a(Gitlab::Git::Compare)
expect(compare.commits.count).to be > 0
end
it 'returns nil when source ref does not exist' do
compare = repository.compare_source_branch('master', mutable_repository, 'non-existent-branch', straight: false)
expect(compare).to be_nil
end
end
end
describe '#checksum' do
it 'calculates the checksum for non-empty repo' do
expect(repository.checksum).to eq '51d0a9662681f93e1fee547a6b7ba2bcaf716059'