summaryrefslogtreecommitdiff
path: root/app/models/repository.rb
diff options
context:
space:
mode:
authorRubén Dávila <rdavila84@gmail.com>2016-02-03 18:28:40 -0500
committerRobert Speicher <rspeicher@gmail.com>2016-02-19 13:14:50 -0500
commit806139936898726b32c4fe216ac3a9f4419ce91e (patch)
tree7329f75325b00fe74999ed1d5d384c9ff01038d3 /app/models/repository.rb
parent34e26b8212954dba32165c39b63858658b82c0f0 (diff)
downloadgitlab-ce-806139936898726b32c4fe216ac3a9f4419ce91e.tar.gz
Add RevertService class with basic logic to revert commit
Diffstat (limited to 'app/models/repository.rb')
-rw-r--r--app/models/repository.rb43
1 files changed, 31 insertions, 12 deletions
diff --git a/app/models/repository.rb b/app/models/repository.rb
index cca7afadbec..19d898c85ea 100644
--- a/app/models/repository.rb
+++ b/app/models/repository.rb
@@ -622,29 +622,48 @@ class Repository
merge_commit_sha
end
- 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)
+ def revert(user, commit_id, target_branch, base_branch, commit_message)
+ source_sha = find_branch(base_branch).target
+ target_sha = find_branch(target_branch).try(:target)
- target_sha = find_branch(target_branch).target
+ # First make revert in temp branch
+ unless target_sha
+ revert_commit(user, commit_id, target_branch, base_branch, commit_message)
+ end
- 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)
+ # Make the revert happen in the target branch
+ source_sha = find_branch(target_branch).target
+ target_sha = find_branch(base_branch).target
+
+ if is_there_something_to_merge?(source_sha, target_sha)
+ revert_commit(user, commit_id, base_branch, base_branch, commit_message)
+ end
+ end
+
+ def revert_commit(user, commit_id, target_branch, base_branch, commit_message)
+ base_sha = find_branch(base_branch).target
+
+ commit_with_hooks(user, target_branch) do |ref|
+ new_index = rugged.revert_commit(commit_id, base_sha)#, mainline: 1)
+
+ return false if new_index.conflicts?
- options = {
+ committer = user_to_committer(user)
+ source_sha = Rugged::Commit.create(rugged, {
message: commit_message,
author: committer,
committer: committer,
tree: new_index.write_tree(rugged),
- parents: [rugged.lookup(target_sha)],
+ parents: [rugged.lookup(base_sha)],
update_ref: ref
- }
-
- Rugged::Commit.create(rugged, options)
+ })
end
end
+ def is_there_something_to_merge?(source_branch_sha, target_branch_sha)
+ CompareService.new.execute(project, source_branch_sha, project, target_branch_sha).diffs.present?
+ end
+
def merged_to_root_ref?(branch_name)
branch_commit = commit(branch_name)
root_ref_commit = commit(root_ref)