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

Make it work for merge commits.

parent b36319a1
...@@ -223,6 +223,10 @@ class Commit ...@@ -223,6 +223,10 @@ class Commit
"Revert \"#{safe_message.lines.first}\"".truncate(80) + "\n\nReverts #{to_reference}" "Revert \"#{safe_message.lines.first}\"".truncate(80) + "\n\nReverts #{to_reference}"
end end
def is_a_merge_commit?
parents.size > 1
end
private private
def repo_changes def repo_changes
......
...@@ -622,12 +622,13 @@ class Repository ...@@ -622,12 +622,13 @@ class Repository
merge_commit_sha merge_commit_sha
end end
def revert(user, commit_id, target_branch, base_branch, commit_message, 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_sha = find_branch(target_branch).try(:target) target_sha = find_branch(target_branch).try(:target)
# First make revert in temp branch # First make revert in temp branch
status = target_sha ? true : revert_commit(user, commit_id, target_branch, base_branch, commit_message) status = target_sha ? true : revert_commit(user, commit, target_branch, base_branch)
# Make the revert happen in the target branch # Make the revert happen in the target branch
source_sha = find_branch(target_branch).target source_sha = find_branch(target_branch).target
...@@ -635,24 +636,26 @@ class Repository ...@@ -635,24 +636,26 @@ class Repository
has_changes = is_there_something_to_merge?(source_sha, target_sha) has_changes = is_there_something_to_merge?(source_sha, target_sha)
if has_changes && !create_mr if has_changes && !create_mr
status = revert_commit(user, commit_id, base_branch, base_branch, commit_message) status = revert_commit(user, commit, base_branch, base_branch)
end end
::File.open('/Users/ruben/Desktop/log.txt', 'w') { |f| f.puts "HAS CHANGES: #{has_changes && status}" }
has_changes && status has_changes && status
end end
def revert_commit(user, commit_id, target_branch, base_branch, commit_message) def revert_commit(user, commit, target_branch, base_branch)
base_sha = find_branch(base_branch).target base_sha = find_branch(base_branch).target
commit_with_hooks(user, target_branch) do |ref| commit_with_hooks(user, target_branch) do |ref|
new_index = rugged.revert_commit(commit_id, base_sha)#, mainline: 1) 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? 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_message, message: commit.revert_message,
author: committer, author: committer,
committer: committer, committer: committer,
tree: new_index.write_tree(rugged), tree: new_index.write_tree(rugged),
......
...@@ -24,9 +24,7 @@ module Commits ...@@ -24,9 +24,7 @@ module Commits
raw_repo = repository.rugged raw_repo = repository.rugged
# Create branch with revert commit # Create branch with revert commit
reverted = repository.revert(current_user, @commit.id, reverted = repository.revert(current_user, @commit, @target_branch, @create_merge_request)
@commit.revert_branch_name, @target_branch,
@commit.revert_message, @create_merge_request)
unless @create_merge_request unless @create_merge_request
repository.rm_branch(current_user, @commit.revert_branch_name) repository.rm_branch(current_user, @commit.revert_branch_name)
......
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