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

Big refactor for #revert_commit.

* Now checking if a commit is already reverted or there is a conflict is
  much more faster.
* No longer need to create a new branch.
parent 4dc83505
...@@ -623,50 +623,32 @@ class Repository ...@@ -623,50 +623,32 @@ class Repository
end end
def revert(user, commit, base_branch, create_mr = false) def revert(user, commit, base_branch, create_mr = false)
target_branch = commit.revert_branch_name source_sha = find_branch(base_branch).target
source_sha = find_branch(base_branch).target target_branch = create_mr ? commit.revert_branch_name : base_branch
target_sha = find_branch(target_branch).try(:target) args = [commit.id, source_sha]
args << { mainline: 1 } if commit.is_a_merge_commit?
# First make revert in temp branch return false unless diff_exists?(source_sha, commit.id)
success = target_sha ? true : revert_commit(user, commit, target_branch, base_branch)
# Make the revert happen in the target branch revert_index = rugged.revert_commit(*args)
source_sha = find_branch(target_branch).target
target_sha = find_branch(base_branch).target
has_changes = is_there_something_to_merge?(source_sha, target_sha)
if has_changes && !create_mr return false if revert_index.conflicts?
success = revert_commit(user, commit, base_branch, base_branch)
end
has_changes && success
end
def revert_commit(user, commit, target_branch, base_branch)
base_sha = find_branch(base_branch).target
commit_with_hooks(user, target_branch) do |ref| commit_with_hooks(user, target_branch) do |ref|
args = [commit.id, base_sha]
args << { mainline: 1 } if commit.is_a_merge_commit?
new_index = rugged.revert_commit(*args)
return false if new_index.conflicts?
committer = user_to_committer(user) committer = user_to_committer(user)
source_sha = Rugged::Commit.create(rugged, { source_sha = Rugged::Commit.create(rugged, {
message: commit.revert_message, message: commit.revert_message,
author: committer, author: committer,
committer: committer, committer: committer,
tree: new_index.write_tree(rugged), tree: revert_index.write_tree(rugged),
parents: [rugged.lookup(base_sha)], parents: [rugged.lookup(source_sha)],
update_ref: ref update_ref: ref
}) })
end end
end end
def is_there_something_to_merge?(source_branch_sha, target_branch_sha) def diff_exists?(source_sha, target_sha)
CompareService.new.execute(project, source_branch_sha, project, target_branch_sha).diffs.present? rugged.diff(source_sha, target_sha).size.zero?
end end
def merged_to_root_ref?(branch_name) def merged_to_root_ref?(branch_name)
......
...@@ -23,16 +23,7 @@ module Commits ...@@ -23,16 +23,7 @@ module Commits
end end
def commit def commit
raw_repo = repository.rugged repository.revert(current_user, @commit, @target_branch, @create_merge_request)
# Create branch with revert commit
reverted = repository.revert(current_user, @commit, @target_branch, @create_merge_request)
if reverted && !@create_merge_request
repository.rm_branch(current_user, @commit.revert_branch_name)
end
reverted
end end
private private
......
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