Commit b6fe51b9 authored by Rubén Dávila's avatar Rubén Dávila Committed by Robert Speicher

Some updates from last code review.

parent 1708e1fc
......@@ -193,18 +193,18 @@ class Projects::MergeRequestsController < Projects::ApplicationController
end
def revert
target_branch_exists = @merge_request.target_branch_exists?
url_params = { merge_request: {
source_branch: @merge_request.revert_branch_name,
url_params = {
merge_request: { source_branch: @merge_request.revert_branch_name,
target_branch: @merge_request.target_branch,
source_project_id: @merge_request.target_project_id,
target_project_id: @merge_request.target_project_id,
description: @merge_request.revert_description
}}
description: @merge_request.revert_description }
}
if target_branch_exists
if @merge_request.target_branch_exists? && @merge_request.merge_commit_sha.present?
@repository.revert_merge(current_user, @merge_request.merge_commit_sha,
@merge_request.revert_branch_name, @merge_request.revert_title)
@merge_request.revert_branch_name, @merge_request.target_branch,
@merge_request.revert_title)
redirect_to new_namespace_project_merge_request_url(@project.namespace, @project, url_params)
else
......
......@@ -135,7 +135,7 @@ module ApplicationHelper
# Skip if user removed branch right after that
return false unless project.repository.branch_names.include?(event.branch_name)
return false if Gitlab::Git::REVERT_BRANCH_PATTERN === event.branch_name
return false if event.branch_name =~ Gitlab::Git::REVERT_BRANCH_PATTERN
true
end
......
......@@ -535,7 +535,7 @@ class MergeRequest < ActiveRecord::Base
end
def revert_branch_name
"revert-#{iid}-#{target_branch}"
"revert-#{iid}-#{source_branch}"
end
def revert_title
......
......@@ -622,13 +622,14 @@ class Repository
merge_commit_sha
end
def revert_merge(user, merge_commit_id, new_branch_name, commit_message)
def revert_merge(user, merge_commit_id, new_branch_name, target_branch, commit_message)
# branch exists and it's highly probable that it has the revert commit
return if find_branch(new_branch_name)
add_branch(user, new_branch_name, merge_commit_id)
target_sha = find_branch(target_branch).target
new_index = rugged.revert_commit(merge_commit_id, merge_commit_id, mainline: 1)
commit_with_hooks(user, new_branch_name) do |ref|
new_index = rugged.revert_commit(merge_commit_id, target_sha, mainline: 1)
committer = user_to_committer(user)
options = {
......@@ -636,12 +637,13 @@ class Repository
author: committer,
committer: committer,
tree: new_index.write_tree(rugged),
parents: [rugged.lookup(merge_commit_id)],
update_ref: "refs/heads/#{new_branch_name}"
parents: [rugged.lookup(target_sha)],
update_ref: ref
}
Rugged::Commit.create(rugged, options)
end
end
def merged_to_root_ref?(branch_name)
branch_commit = commit(branch_name)
......
......@@ -18,5 +18,5 @@
Edit
- if @merge_request.closed?
= link_to 'Reopen', merge_request_path(@merge_request, merge_request: {state_event: :reopen }), method: :put, class: 'btn btn-nr btn-grouped btn-reopen reopen-mr-link', title: "Reopen merge request"
- if @merge_request.merged?
- if @merge_request.merged? && @merge_request.merge_commit_sha.present?
= link_to 'Revert', revert_namespace_project_merge_request_path(@project.namespace, @project, @merge_request), method: :post, class: "btn btn-grouped btn-close", title: "Revert merge request"
......@@ -10,7 +10,6 @@ describe Repository, models: true do
{ message: 'Test message', committer: author, author: author }
end
let(:merge_commit_id) do
master_commit = repository.commit('master')
source_sha = repository.find_branch('feature').target
repository.merge(user, source_sha, 'master', commit_options)
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