Commit 6ae1a73c authored by Lin Jen-Shin's avatar Lin Jen-Shin

Pass source_branch properly for cherry-pick/revert

Feedback:
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/7237/diffs#note_19210818
parent 65806ec6
...@@ -962,14 +962,16 @@ class Repository ...@@ -962,14 +962,16 @@ class Repository
end end
end end
def revert(user, commit, base_branch, revert_tree_id = nil) def revert(
user, commit, base_branch, revert_tree_id = nil,
source_branch: nil, source_project: project)
revert_tree_id ||= check_revert_content(commit, base_branch) revert_tree_id ||= check_revert_content(commit, base_branch)
return false unless revert_tree_id return false unless revert_tree_id
GitOperationService.new(user, self).with_branch( GitOperationService.new(user, self).with_branch(
base_branch, base_branch,
source_commit: commit) do source_branch: source_branch, source_project: source_project) do
source_sha = find_branch(base_branch).dereferenced_target.sha source_sha = find_branch(base_branch).dereferenced_target.sha
committer = user_to_committer(user) committer = user_to_committer(user)
...@@ -983,14 +985,16 @@ class Repository ...@@ -983,14 +985,16 @@ class Repository
end end
end end
def cherry_pick(user, commit, base_branch, cherry_pick_tree_id = nil) def cherry_pick(
user, commit, base_branch, cherry_pick_tree_id = nil,
source_branch: nil, source_project: project)
cherry_pick_tree_id ||= check_cherry_pick_content(commit, base_branch) cherry_pick_tree_id ||= check_cherry_pick_content(commit, base_branch)
return false unless cherry_pick_tree_id return false unless cherry_pick_tree_id
GitOperationService.new(user, self).with_branch( GitOperationService.new(user, self).with_branch(
base_branch, base_branch,
source_commit: commit) do source_branch: source_branch, source_project: source_project) do
source_sha = find_branch(base_branch).dereferenced_target.sha source_sha = find_branch(base_branch).dereferenced_target.sha
committer = user_to_committer(user) committer = user_to_committer(user)
......
...@@ -31,7 +31,14 @@ module Commits ...@@ -31,7 +31,14 @@ module Commits
if tree_id if tree_id
validate_target_branch(into) if @create_merge_request validate_target_branch(into) if @create_merge_request
repository.public_send(action, current_user, @commit, into, tree_id) repository.public_send(
action,
current_user,
@commit,
into,
tree_id,
source_branch: @target_branch)
success success
else else
error_msg = "Sorry, we cannot #{action.to_s.dasherize} this #{@commit.change_type_title} automatically. error_msg = "Sorry, we cannot #{action.to_s.dasherize} this #{@commit.change_type_title} automatically.
......
...@@ -25,21 +25,15 @@ GitOperationService = Struct.new(:user, :repository) do ...@@ -25,21 +25,15 @@ GitOperationService = Struct.new(:user, :repository) do
end end
end end
# Whenever `source_branch` or `source_commit` is passed, if `branch` # Whenever `source_branch` is passed, if `branch` doesn't exist,
# doesn't exist, it would be created from `source_branch` or # it would be created from `source_branch`.
# `source_commit`. Should only pass one of them, not both.
# If `source_project` is passed, and the branch doesn't exist, # If `source_project` is passed, and the branch doesn't exist,
# it would try to find the source from it instead of current repository. # it would try to find the source from it instead of current repository.
def with_branch( def with_branch(
branch_name, branch_name,
source_branch: nil, source_branch: nil,
source_commit: nil,
source_project: repository.project) source_project: repository.project)
if source_commit && source_branch
raise ArgumentError, 'Should pass only :source_branch or :source_commit'
end
ref = Gitlab::Git::BRANCH_REF_PREFIX + branch_name ref = Gitlab::Git::BRANCH_REF_PREFIX + branch_name
oldrev = Gitlab::Git::BLANK_SHA oldrev = Gitlab::Git::BLANK_SHA
...@@ -62,13 +56,13 @@ GitOperationService = Struct.new(:user, :repository) do ...@@ -62,13 +56,13 @@ GitOperationService = Struct.new(:user, :repository) do
" #{source_project.path_with_namespace}") " #{source_project.path_with_namespace}")
end end
elsif source_commit || source_branch elsif source_branch
newrev = (source_commit || repository.commit(source_branch)).try(:sha) newrev = repository.commit(source_branch).try(:sha)
unless newrev unless newrev
raise Repository::CommitError.new( raise Repository::CommitError.new(
"Cannot find branch #{branch_name} nor" \ "Cannot find branch #{branch_name} nor" \
" #{source_commit.try(:sha) || source_branch} from" \ " #{source_branch} from" \
" #{repository.project.path_with_namespace}") " #{repository.project.path_with_namespace}")
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