Commit d99d5198 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Add MergeRequest#branch_merge_base_commit method

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 08f5f897
...@@ -177,8 +177,8 @@ class MergeRequest < ActiveRecord::Base ...@@ -177,8 +177,8 @@ class MergeRequest < ActiveRecord::Base
def diff_base_commit def diff_base_commit
if persisted? if persisted?
merge_request_diff.base_commit merge_request_diff.base_commit
elsif diff_start_commit && diff_head_commit else
self.target_project.merge_base_commit(diff_start_sha, diff_head_sha) branch_merge_base_commit
end end
end end
...@@ -239,6 +239,15 @@ class MergeRequest < ActiveRecord::Base ...@@ -239,6 +239,15 @@ class MergeRequest < ActiveRecord::Base
target_project.repository.commit(target_branch) if target_branch_ref target_project.repository.commit(target_branch) if target_branch_ref
end end
def branch_merge_base_commit
start_sha = target_branch_sha
head_sha = source_branch_sha
if start_sha && head_sha
target_project.merge_base_commit(start_sha, head_sha)
end
end
def target_branch_sha def target_branch_sha
@target_branch_sha || target_branch_head.try(:sha) @target_branch_sha || target_branch_head.try(:sha)
end end
...@@ -247,15 +256,25 @@ class MergeRequest < ActiveRecord::Base ...@@ -247,15 +256,25 @@ class MergeRequest < ActiveRecord::Base
@source_branch_sha || source_branch_head.try(:sha) @source_branch_sha || source_branch_head.try(:sha)
end end
def branch_merge_base_sha
branch_merge_base_commit.try(:sha)
end
def diff_refs def diff_refs
if merge_request_diff if merge_request_diff
merge_request_diff.diff_refs merge_request_diff.diff_refs
elsif diff_start_commit || diff_base_commit else
Gitlab::Diff::DiffRefs.new( start_sha = target_branch_sha
base_sha: diff_base_sha, head_sha = source_branch_sha
start_sha: diff_start_sha, base_sha = branch_merge_base_sha
head_sha: diff_head_sha
) if start_sha || base_sha
Gitlab::Diff::DiffRefs.new(
base_sha: base_sha,
start_sha: start_sha,
head_sha: head_sha
)
end
end end
end end
......
...@@ -691,4 +691,21 @@ describe MergeRequest, models: true do ...@@ -691,4 +691,21 @@ describe MergeRequest, models: true do
subject.reload_diff subject.reload_diff
end end
end end
describe '#branch_merge_base_commit' do
context 'source and target branch exist' do
it { expect(subject.branch_merge_base_commit.sha).to eq('ae73cb07c9eeaf35924a10f713b364d32b2dd34f') }
it { expect(subject.branch_merge_base_commit).to be_a(Commit) }
end
context 'when the target branch does not exist' do
before do
subject.project.repository.raw_repository.delete_branch(subject.target_branch)
end
it 'returns nil' do
expect(subject.branch_merge_base_commit).to be_nil
end
end
end
end end
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