Commit 26653eb0 authored by Stan Hu's avatar Stan Hu Committed by Oswaldo Ferreira

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
parent 9ec37d3d
---
title: Don't create a temp reference for branch comparisons within project
merge_request:
author:
type: fixed
......@@ -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)
......
......@@ -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'
......
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