From 6ae1a73cfdad4b98176bb99846042d4378119de2 Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin <godfat@godfat.org> Date: Wed, 7 Dec 2016 19:50:08 +0800 Subject: [PATCH] Pass source_branch properly for cherry-pick/revert Feedback: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/7237/diffs#note_19210818 --- app/models/repository.rb | 12 ++++++++---- app/services/commits/change_service.rb | 9 ++++++++- app/services/git_operation_service.rb | 16 +++++----------- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/app/models/repository.rb b/app/models/repository.rb index 491d2ab69b2..36cbb0d051e 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -962,14 +962,16 @@ class Repository 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) return false unless revert_tree_id GitOperationService.new(user, self).with_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 committer = user_to_committer(user) @@ -983,14 +985,16 @@ class Repository 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) return false unless cherry_pick_tree_id GitOperationService.new(user, self).with_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 committer = user_to_committer(user) diff --git a/app/services/commits/change_service.rb b/app/services/commits/change_service.rb index 04b28cfaed8..5458f7a6790 100644 --- a/app/services/commits/change_service.rb +++ b/app/services/commits/change_service.rb @@ -31,7 +31,14 @@ module Commits if tree_id 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 else error_msg = "Sorry, we cannot #{action.to_s.dasherize} this #{@commit.change_type_title} automatically. diff --git a/app/services/git_operation_service.rb b/app/services/git_operation_service.rb index c34d4bde150..c9e2c21737a 100644 --- a/app/services/git_operation_service.rb +++ b/app/services/git_operation_service.rb @@ -25,21 +25,15 @@ GitOperationService = Struct.new(:user, :repository) do end end - # Whenever `source_branch` or `source_commit` is passed, if `branch` - # doesn't exist, it would be created from `source_branch` or - # `source_commit`. Should only pass one of them, not both. + # Whenever `source_branch` is passed, if `branch` doesn't exist, + # it would be created from `source_branch`. # If `source_project` is passed, and the branch doesn't exist, # it would try to find the source from it instead of current repository. def with_branch( branch_name, source_branch: nil, - source_commit: nil, 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 oldrev = Gitlab::Git::BLANK_SHA @@ -62,13 +56,13 @@ GitOperationService = Struct.new(:user, :repository) do " #{source_project.path_with_namespace}") end - elsif source_commit || source_branch - newrev = (source_commit || repository.commit(source_branch)).try(:sha) + elsif source_branch + newrev = repository.commit(source_branch).try(:sha) unless newrev raise Repository::CommitError.new( "Cannot find branch #{branch_name} nor" \ - " #{source_commit.try(:sha) || source_branch} from" \ + " #{source_branch} from" \ " #{repository.project.path_with_namespace}") end -- GitLab