diff --git a/app/models/compare.rb b/app/models/compare.rb index 19ea2bc3065..feb4b89c781 100644 --- a/app/models/compare.rb +++ b/app/models/compare.rb @@ -41,21 +41,16 @@ class Compare end alias_method :commit, :head_commit - def base_commit - strong_memoize(:base_commit) do - return unless start_commit && head_commit - return OpenStruct.new(sha: @base_sha) if @base_sha - - project.merge_base_commit(start_commit.id, head_commit.id) - end - end - def start_commit_sha start_commit&.sha end def base_commit_sha - base_commit&.sha + strong_memoize(:base_commit) do + next unless start_commit && head_commit + + @base_sha || project.merge_base_commit(start_commit.id, head_commit.id)&.sha + end end def head_commit_sha diff --git a/spec/models/compare_spec.rb b/spec/models/compare_spec.rb index 4200af0e0f5..8e88bb81162 100644 --- a/spec/models/compare_spec.rb +++ b/spec/models/compare_spec.rb @@ -37,28 +37,6 @@ describe Compare do end end - describe '#base_commit' do - let(:base_commit) { Commit.new(another_sample_commit, project) } - - it 'returns project merge base commit' do - expect(project).to receive(:merge_base_commit).with(start_commit.id, head_commit.id).and_return(base_commit) - - expect(subject.base_commit).to eq(base_commit) - end - - it 'returns nil if there is no start_commit' do - expect(subject).to receive(:start_commit).and_return(nil) - - expect(subject.base_commit).to eq(nil) - end - - it 'returns nil if there is no head commit' do - expect(subject).to receive(:head_commit).and_return(nil) - - expect(subject.base_commit).to eq(nil) - end - end - describe '#base_commit_sha' do it 'returns @base_sha if it is present' do expect(project).not_to receive(:merge_base_commit) @@ -87,13 +65,23 @@ describe Compare do 3.times { subject.base_commit_sha } end + + it 'returns nil if there is no start_commit' do + expect(subject).to receive(:start_commit).and_return(nil) + + expect(subject.base_commit_sha).to eq(nil) + end + + it 'returns nil if there is no head commit' do + expect(subject).to receive(:head_commit).and_return(nil) + + expect(subject.base_commit_sha).to eq(nil) + end end describe '#diff_refs' do - it 'uses base_commit sha as base_sha' do - expect(subject).to receive(:base_commit).at_least(:once).and_call_original - - expect(subject.diff_refs.base_sha).to eq(subject.base_commit.id) + it 'uses base_commit_sha sha as base_sha' do + expect(subject.diff_refs.base_sha).to eq(subject.base_commit_sha) end it 'uses start_commit sha as start_sha' do