diff --git a/app/models/repository.rb b/app/models/repository.rb index 491d2ab69b280abe18fd62d32e6963587e00ab9f..36cbb0d051e96475ac10a0f9c51115f7ea7a9c3d 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 04b28cfaed8d7daed6a0109bb121f3c5bc886e6d..5458f7a6790c6c0008090241cf18f995e0e76a26 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 c34d4bde1502e454b43c63038fc4ccfec66d38f9..c9e2c21737a3b8bff0f36a8dbe731a9ebf182204 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