Skip to content

Commit 01ca73b

Browse files
author
Vicent Marti
committed
Merge pull request #545 from libgit2/vmg/revert
Revert commits!
2 parents 858f72e + 1f9dc2b commit 01ca73b

File tree

3 files changed

+122
-5
lines changed

3 files changed

+122
-5
lines changed

ext/rugged/rugged_repo.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,62 @@ static VALUE rb_git_repo_merge_analysis(int argc, VALUE *argv, VALUE self)
829829
return result;
830830
}
831831

832+
/*
833+
* call-seq:
834+
* repo.revert_commit(revert_commit, our_commit, options = {}) -> index
835+
*
836+
* Reverts the given commit against the given "our" commit, producing an
837+
* index that reflects the result of the revert.
838+
*/
839+
static VALUE rb_git_repo_revert_commit(int argc, VALUE *argv, VALUE self)
840+
{
841+
VALUE rb_revert_commit, rb_our_commit, rb_options;
842+
git_commit *revert_commit, *our_commit;
843+
git_index *index;
844+
git_repository *repo;
845+
git_merge_options opts = GIT_MERGE_OPTIONS_INIT;
846+
unsigned int mainline = 0;
847+
int error;
848+
849+
rb_scan_args(argc, argv, "20:", &rb_revert_commit, &rb_our_commit, &rb_options);
850+
851+
if (TYPE(rb_revert_commit) == T_STRING)
852+
rb_revert_commit = rugged_object_rev_parse(self, rb_revert_commit, 1);
853+
854+
if (TYPE(rb_our_commit) == T_STRING)
855+
rb_our_commit = rugged_object_rev_parse(self, rb_our_commit, 1);
856+
857+
if (!rb_obj_is_kind_of(rb_revert_commit, rb_cRuggedCommit) ||
858+
!rb_obj_is_kind_of(rb_our_commit, rb_cRuggedCommit)) {
859+
rb_raise(rb_eArgError, "Expected a Rugged::Commit.");
860+
}
861+
862+
if (!NIL_P(rb_options)) {
863+
VALUE rb_mainline;
864+
865+
Check_Type(rb_options, T_HASH);
866+
rugged_parse_merge_options(&opts, rb_options);
867+
868+
rb_mainline = rb_hash_aref(rb_options, CSTR2SYM("mainline"));
869+
if (!NIL_P(rb_mainline)) {
870+
Check_Type(rb_mainline, T_FIXNUM);
871+
mainline = FIX2UINT(rb_mainline);
872+
}
873+
}
874+
875+
Data_Get_Struct(self, git_repository, repo);
876+
Data_Get_Struct(rb_revert_commit, git_commit, revert_commit);
877+
Data_Get_Struct(rb_our_commit, git_commit, our_commit);
878+
879+
error = git_revert_commit(&index, repo, revert_commit, our_commit, mainline, &opts);
880+
if (error == GIT_EMERGECONFLICT)
881+
return Qnil;
882+
883+
rugged_exception_check(error);
884+
885+
return rugged_index_new(rb_cRuggedIndex, self, index);
886+
}
887+
832888
/*
833889
* call-seq:
834890
* repo.merge_commits(our_commit, their_commit, options = {}) -> index
@@ -2528,6 +2584,8 @@ void Init_rugged_repo(void)
25282584
rb_define_method(rb_cRuggedRepo, "merge_analysis", rb_git_repo_merge_analysis, -1);
25292585
rb_define_method(rb_cRuggedRepo, "merge_commits", rb_git_repo_merge_commits, -1);
25302586

2587+
rb_define_method(rb_cRuggedRepo, "revert_commit", rb_git_repo_revert_commit, -1);
2588+
25312589
rb_define_method(rb_cRuggedRepo, "path_ignored?", rb_git_repo_is_path_ignored, 1);
25322590

25332591
rb_define_method(rb_cRuggedRepo, "reset", rb_git_repo_reset, 2);

test/remote_test.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,6 @@ def test_fetch_prune_nil
202202
assert_equal "8496071c1b46c854b31185ea97743be6a8774479", @repo.ref("refs/remotes/origin/unit_test").target_id
203203
end
204204

205-
def test_fetch_prune_nil
206-
@remote.fetch(prune: nil)
207-
assert_equal "8496071c1b46c854b31185ea97743be6a8774479", @repo.ref("refs/remotes/origin/unit_test").target_id
208-
end
209-
210205
def test_fetch_prune_with_invalid_argument_raises
211206
assert_raises TypeError do
212207
@remote.fetch(prune: 'INVALID')

test/revert_test.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
require "test_helper"
2+
3+
class RevertTest < Rugged::TestCase
4+
def setup
5+
@repo = FixtureRepo.from_libgit2("revert")
6+
end
7+
8+
def verify_index(index, expected)
9+
assert index.is_a?(Rugged::Index)
10+
assert_equal expected.count, index.count
11+
expected.each_with_index do |(mode, oid, stage, path), i|
12+
entry = index[i]
13+
assert entry
14+
assert_equal path, entry[:path]
15+
assert_equal mode, entry[:mode]
16+
assert_equal oid, entry[:oid]
17+
assert_equal stage, entry[:stage]
18+
end
19+
end
20+
21+
def test_revert_automerge
22+
expected = [
23+
[0100644, "caf99de3a49827117bb66721010eac461b06a80c", 0, "file1.txt"],
24+
[0100644, "0ab09ea6d4c3634bdf6c221626d8b6f7dd890767", 0, "file2.txt"],
25+
[0100644, "f4e107c230d08a60fb419d19869f1f282b272d9c", 0, "file3.txt"],
26+
[0100644, "0f5bfcf58c558d865da6be0281d7795993646cee", 0, "file6.txt"]]
27+
28+
ours = Rugged::Commit.lookup(@repo, "72333f47d4e83616630ff3b0ffe4c0faebcc3c45")
29+
revert = Rugged::Commit.lookup(@repo, "d1d403d22cbe24592d725f442835cf46fe60c8ac")
30+
31+
index = @repo.revert_commit(revert, ours)
32+
verify_index(index, expected)
33+
end
34+
35+
def test_revert_with_conflicts
36+
expected = [
37+
[0100644, "7731926a337c4eaba1e2187d90ebfa0a93659382", 1, "file1.txt"],
38+
[0100644, "4b8fcff56437e60f58e9a6bc630dd242ebf6ea2c", 2, "file1.txt"],
39+
[0100644, "3a3ef367eaf3fe79effbfb0a56b269c04c2b59fe", 3, "file1.txt"],
40+
[0100644, "0ab09ea6d4c3634bdf6c221626d8b6f7dd890767", 0, "file2.txt"],
41+
[0100644, "f4e107c230d08a60fb419d19869f1f282b272d9c", 0, "file3.txt"],
42+
[0100644, "0f5bfcf58c558d865da6be0281d7795993646cee", 0, "file6.txt"]]
43+
44+
revert = Rugged::Commit.lookup(@repo, "72333f47d4e83616630ff3b0ffe4c0faebcc3c45")
45+
46+
index = @repo.revert_commit(revert, "HEAD")
47+
assert index.conflicts?
48+
verify_index(index, expected)
49+
50+
index = @repo.revert_commit(revert, "HEAD", :fail_on_conflict => true)
51+
refute index
52+
end
53+
54+
def test_revert_orphan
55+
expected = [
56+
[0100644, "296a6d3be1dff05c5d1f631d2459389fa7b619eb", 0, "file-mainline.txt"]]
57+
58+
head = Rugged::Commit.lookup(@repo,"39467716290f6df775a91cdb9a4eb39295018145")
59+
revert = Rugged::Commit.lookup(@repo, "ebb03002cee5d66c7732dd06241119fe72ab96a5")
60+
61+
index = @repo.revert_commit(revert, head)
62+
verify_index(index, expected)
63+
end
64+
end

0 commit comments

Comments
 (0)