Skip to content

Commit 482c238

Browse files
committed
repo: Implement Repository#revert_commit
1 parent 858f72e commit 482c238

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
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);

0 commit comments

Comments
 (0)