Fix MergeRequest#source_sha when there is no diff

`MergeRequest#source_sha` is expected to return the sha of the source
branch last commit.

But when a open Merge Request has no diff (e.g. all commits have already
been merged to the target branch), `merge_request.source_sha`
incorrectly returns `nil`.

This was un-noticed before – but now that !2217 has been merged,
it makes `Gitlab::Git::Commit.between` raise an
"Unexpected nil argument" exception.

This fixes the crash, by making sure that `source_sha` returns a
correct result even when there is no diff available.
parent 7cbf8fd0
...@@ -33,6 +33,7 @@ v 8.6.0 (unreleased) ...@@ -33,6 +33,7 @@ v 8.6.0 (unreleased)
- Fix bug where Bitbucket `closed` issues were imported as `opened` (Iuri de Silvio) - Fix bug where Bitbucket `closed` issues were imported as `opened` (Iuri de Silvio)
- Don't show Issues/MRs from archived projects in Groups view - Don't show Issues/MRs from archived projects in Groups view
- Fix wrong "iid of max iid" in Issuable sidebar for some merged MRs - Fix wrong "iid of max iid" in Issuable sidebar for some merged MRs
- Fix empty source_sha on Merge Request when there is no diff (Pierre de La Morinerie)
- Increase the notes polling timeout over time (Roberto Dip) - Increase the notes polling timeout over time (Roberto Dip)
- Add shortcut to toggle markdown preview (Florent Baldino) - Add shortcut to toggle markdown preview (Florent Baldino)
- Show labels in dashboard and group milestone views - Show labels in dashboard and group milestone views
......
...@@ -520,7 +520,11 @@ class MergeRequest < ActiveRecord::Base ...@@ -520,7 +520,11 @@ class MergeRequest < ActiveRecord::Base
end end
def source_sha def source_sha
last_commit.try(:sha) last_commit.try(:sha) || source_tip.try(:sha)
end
def source_tip
source_branch && source_project.repository.commit(source_branch)
end end
def fetch_ref def fetch_ref
......
...@@ -51,6 +51,11 @@ FactoryGirl.define do ...@@ -51,6 +51,11 @@ FactoryGirl.define do
trait :with_diffs do trait :with_diffs do
end end
trait :without_diffs do
source_branch "improve/awesome"
target_branch "master"
end
trait :conflict do trait :conflict do
source_branch "feature_conflict" source_branch "feature_conflict"
target_branch "feature" target_branch "feature"
......
...@@ -96,6 +96,13 @@ describe MergeRequest, models: true do ...@@ -96,6 +96,13 @@ describe MergeRequest, models: true do
end end
end end
context 'without diffs' do
subject { create(:merge_request, :without_diffs) }
it 'returns the sha of the source branch last commit' do
expect(subject.source_sha).to eq(last_branch_commit.sha)
end
end
context 'when the merge request is being created' do context 'when the merge request is being created' do
subject { build(:merge_request, source_branch: nil, compare_commits: []) } subject { build(:merge_request, source_branch: nil, compare_commits: []) }
it 'returns nil' do it 'returns nil' do
......
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