Correct the cherry-pick message for merge commits

The list of commits must be generated from the merge request,
not from a diff of the branches.
This commit is contained in:
Saverio Miroddi 2017-08-17 20:26:06 +02:00
parent 919c0c7ba7
commit c0f921606c
5 changed files with 45 additions and 23 deletions

View file

@ -251,19 +251,16 @@ class Commit
project.repository.next_branch("cherry-pick-#{short_id}", mild: true) project.repository.next_branch("cherry-pick-#{short_id}", mild: true)
end end
def cherry_pick_description(start_project, start_branch_name) def cherry_pick_description(user)
message_buffer = "(cherry picked from commit #{sha})" message_buffer = "(cherry picked from commit #{sha})"
if merge_commit? if merged_merge_request?(user)
compare = CompareService.new(project, sha).execute(start_project, start_branch_name) commits_in_merge_request = merged_merge_request(user).commits
# Ignore the merge commit. if commits_in_merge_request.present?
commits_in_merge = compare.commits[0..-2]
if commits_in_merge.present?
message_buffer << "\n" message_buffer << "\n"
commits_in_merge.each do |commit_in_merge| commits_in_merge_request.each do |commit_in_merge|
message_buffer << "\n#{commit_in_merge.short_id} #{commit_in_merge.title}" message_buffer << "\n#{commit_in_merge.short_id} #{commit_in_merge.title}"
end end
end end
@ -272,8 +269,8 @@ class Commit
message_buffer message_buffer
end end
def cherry_pick_message(start_project, start_branch_name) def cherry_pick_message(user)
%Q{#{message}\n\n#{cherry_pick_description(start_project, start_branch_name)}} %Q{#{message}\n\n#{cherry_pick_description(user)}}
end end
def revert_description(user) def revert_description(user)

View file

@ -879,9 +879,7 @@ class Repository
committer = user_to_committer(user) committer = user_to_committer(user)
commit_message = commit.cherry_pick_message(start_project, start_branch_name) create_commit(message: commit.cherry_pick_message(user),
create_commit(message: commit_message,
author: { author: {
email: commit.author_email, email: commit.author_email,
name: commit.author_name, name: commit.author_name,

View file

@ -196,25 +196,52 @@ eos
end end
describe '#cherry_pick_message' do describe '#cherry_pick_message' do
let(:regular_commit) { project.commit('video') } let(:user) { create(:user) }
let(:merge_commit) { project.commit('wip') }
context 'of a regular commit' do context 'of a regular commit' do
it { expect(regular_commit.cherry_pick_message(project, 'master')).to include("\n\n(cherry picked from commit 88790590ed1337ab189bccaa355f068481c90bec)") } let(:commit) { project.commit('video') }
it { expect(commit.cherry_pick_message(user)).to include("\n\n(cherry picked from commit 88790590ed1337ab189bccaa355f068481c90bec)") }
end end
context 'of a merge commit' do context 'of a merge commit' do
let(:repository) { project.repository }
let(:commit_options) do
author = repository.user_to_committer(user)
commit_options = { message: 'Test message', committer: author, author: author }
end
let(:merge_commit) do
merge_request = create(:merge_request,
source_branch: 'feature',
target_branch: 'master',
source_project: project,
author: user)
merge_commit_id = repository.merge(user,
merge_request.diff_head_sha,
merge_request,
commit_options)
merge_commit = repository.commit(merge_commit_id)
# Manually mark as completed.
#
merge_request.update(merge_commit_sha: merge_commit_id)
merge_commit
end
it do it do
expected_appended_text = <<~STR.rstrip expected_appended_text = <<~STR.rstrip
(cherry picked from commit #{merge_commit.sha})
(cherry picked from commit b9238ee5bf1d7359dd3b8c89fd76c1c7f8b75aba) 0b4bc9a4 Feature added
6d664995 This commit will be fixupped against
64117577 fixup! This commit will be fixupped against
STR STR
expect(merge_commit.cherry_pick_message(project, 'master')).to include(expected_appended_text) expect(merge_commit.cherry_pick_message(user)).to include(expected_appended_text)
end end
end end
end end

View file

@ -791,7 +791,7 @@ describe API::Commits do
expect(response).to have_gitlab_http_status(201) expect(response).to have_gitlab_http_status(201)
expect(response).to match_response_schema('public_api/v4/commit/basic') expect(response).to match_response_schema('public_api/v4/commit/basic')
expect(json_response['title']).to eq(commit.title) expect(json_response['title']).to eq(commit.title)
expect(json_response['message']).to eq(commit.cherry_pick_message(project, branch)) expect(json_response['message']).to eq(commit.cherry_pick_message(user))
expect(json_response['author_name']).to eq(commit.author_name) expect(json_response['author_name']).to eq(commit.author_name)
expect(json_response['committer_name']).to eq(user.name) expect(json_response['committer_name']).to eq(user.name)
end end

View file

@ -474,7 +474,7 @@ describe API::V3::Commits do
expect(response).to have_http_status(201) expect(response).to have_http_status(201)
expect(json_response['title']).to eq(master_pickable_commit.title) expect(json_response['title']).to eq(master_pickable_commit.title)
expect(json_response['message']).to eq(master_pickable_commit.cherry_pick_message(project, 'master')) expect(json_response['message']).to eq(master_pickable_commit.cherry_pick_message(user))
expect(json_response['author_name']).to eq(master_pickable_commit.author_name) expect(json_response['author_name']).to eq(master_pickable_commit.author_name)
expect(json_response['committer_name']).to eq(user.name) expect(json_response['committer_name']).to eq(user.name)
end end