Commit bc92de8f authored by Rémy Coutable's avatar Rémy Coutable

Add a safeguard in MergeRequest#compute_diverged_commits_count

We have to ensure source_sha and target_sha are not nil before calling
Gitlab::Git::Commit.between.
parent 4a8a8282
...@@ -516,7 +516,7 @@ class MergeRequest < ActiveRecord::Base ...@@ -516,7 +516,7 @@ class MergeRequest < ActiveRecord::Base
end end
def target_sha def target_sha
@target_sha ||= target_project.repository.commit(target_branch).sha @target_sha ||= target_project.repository.commit(target_branch).try(:sha)
end end
def source_sha def source_sha
...@@ -572,8 +572,11 @@ class MergeRequest < ActiveRecord::Base ...@@ -572,8 +572,11 @@ class MergeRequest < ActiveRecord::Base
end end
def compute_diverged_commits_count def compute_diverged_commits_count
return 0 unless source_sha && target_sha
Gitlab::Git::Commit.between(target_project.repository.raw_repository, source_sha, target_sha).size Gitlab::Git::Commit.between(target_project.repository.raw_repository, source_sha, target_sha).size
end end
private :compute_diverged_commits_count
def diverged_from_target_branch? def diverged_from_target_branch?
diverged_commits_count > 0 diverged_commits_count > 0
......
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
%span into %span into
= link_to namespace_project_commits_path(@project.namespace, @project, @merge_request.target_branch), class: "label-branch" do = link_to namespace_project_commits_path(@project.namespace, @project, @merge_request.target_branch), class: "label-branch" do
= @merge_request.target_branch = @merge_request.target_branch
- if @merge_request.mergeable? && @merge_request.diverged_from_target_branch? - if @merge_request.open? && @merge_request.diverged_from_target_branch?
%span (#{pluralize(@merge_request.diverged_commits_count, 'commit')} behind) %span (#{pluralize(@merge_request.diverged_commits_count, 'commit')} behind)
= render "projects/merge_requests/show/how_to_merge" = render "projects/merge_requests/show/how_to_merge"
......
...@@ -86,6 +86,16 @@ describe MergeRequest, models: true do ...@@ -86,6 +86,16 @@ describe MergeRequest, models: true do
end end
end end
describe '#target_sha' do
context 'when the target branch does not exist anymore' do
subject { create(:merge_request).tap { |mr| mr.update_attribute(:target_branch, 'deleted') } }
it 'returns nil' do
expect(subject.target_sha).to be_nil
end
end
end
describe '#source_sha' do describe '#source_sha' do
let(:last_branch_commit) { subject.source_project.repository.commit(subject.source_branch) } let(:last_branch_commit) { subject.source_project.repository.commit(subject.source_branch) }
...@@ -310,6 +320,18 @@ describe MergeRequest, models: true do ...@@ -310,6 +320,18 @@ describe MergeRequest, models: true do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:fork_project) { create(:project, forked_from_project: project) } let(:fork_project) { create(:project, forked_from_project: project) }
context 'when the target branch does not exist anymore' do
subject { create(:merge_request).tap { |mr| mr.update_attribute(:target_branch, 'deleted') } }
it 'does not crash' do
expect{ subject.diverged_commits_count }.not_to raise_error
end
it 'returns 0' do
expect(subject.diverged_commits_count).to eq(0)
end
end
context 'diverged on same repository' do context 'diverged on same repository' do
subject(:merge_request_with_divergence) { create(:merge_request, :diverged, source_project: project, target_project: project) } subject(:merge_request_with_divergence) { create(:merge_request, :diverged, source_project: project, target_project: project) }
......
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