Skip to content

Commit c654c2b

Browse files
Merge pull request #496 from niamster/master
repo: support for checkout_index
2 parents eb98469 + 585f094 commit c654c2b

File tree

3 files changed

+78
-13
lines changed

3 files changed

+78
-13
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
* Add `Rugged::Repository#checkout_index`.
2+
3+
This allows to perform checkout from a given GIT index.
4+
It might be handy in case of manual merge conflicts resolution with user intervention.
5+
6+
*Dmytro Milinevskyy*
7+
18
* Add accessors for the Repository ident.
29

310
Added `Repository#ident` and `Repository#ident=` to read and set the

ext/rugged/rugged_repo.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,6 +2181,54 @@ static VALUE rb_git_checkout_tree(int argc, VALUE *argv, VALUE self)
21812181
return Qnil;
21822182
}
21832183

2184+
/**
2185+
* call-seq: repo.checkout_index(index[,options]) -> nil
2186+
*
2187+
* Updates files in the index and the working tree to match the content of the
2188+
* commit pointed at by +index+.
2189+
*
2190+
* See Repository#checkout_tree for a list of supported +options+.
2191+
*/
2192+
static VALUE rb_git_checkout_index(int argc, VALUE *argv, VALUE self)
2193+
{
2194+
VALUE rb_index, rb_options;
2195+
git_repository *repo;
2196+
git_index *index;
2197+
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
2198+
struct rugged_cb_payload *payload;
2199+
int error, exception = 0;
2200+
2201+
rb_scan_args(argc, argv, "10:", &rb_index, &rb_options);
2202+
2203+
if (!rb_obj_is_kind_of(rb_index, rb_cRuggedIndex))
2204+
rb_raise(rb_eTypeError, "Expected Rugged::Index");
2205+
2206+
Data_Get_Struct(self, git_repository, repo);
2207+
Data_Get_Struct(rb_index, git_index, index);
2208+
2209+
rugged_parse_checkout_options(&opts, rb_options);
2210+
2211+
error = git_checkout_index(repo, index, &opts);
2212+
xfree(opts.paths.strings);
2213+
2214+
if ((payload = opts.notify_payload) != NULL) {
2215+
exception = payload->exception;
2216+
xfree(opts.notify_payload);
2217+
}
2218+
2219+
if ((payload = opts.progress_payload) != NULL) {
2220+
exception = payload->exception;
2221+
xfree(opts.progress_payload);
2222+
}
2223+
2224+
if (exception)
2225+
rb_jump_tag(exception);
2226+
2227+
rugged_exception_check(error);
2228+
2229+
return Qnil;
2230+
}
2231+
21842232
/**
21852233
* call-seq: repo.checkout_head([options]) -> nil
21862234
*
@@ -2483,6 +2531,7 @@ void Init_rugged_repo(void)
24832531
rb_define_method(rb_cRuggedRepo, "default_signature", rb_git_repo_default_signature, 0);
24842532

24852533
rb_define_method(rb_cRuggedRepo, "checkout_tree", rb_git_checkout_tree, -1);
2534+
rb_define_method(rb_cRuggedRepo, "checkout_index", rb_git_checkout_index, -1);
24862535
rb_define_method(rb_cRuggedRepo, "checkout_head", rb_git_checkout_head, -1);
24872536

24882537
rb_define_method(rb_cRuggedRepo, "cherrypick", rb_git_repo_cherrypick, -1);

test/repo_test.rb

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -811,20 +811,16 @@ def teardown
811811
super
812812
end
813813

814-
def test_checkout_tree_with_commit
815-
@repo.checkout_tree(@repo.rev_parse("refs/heads/dir"), :strategy => :force)
816-
@repo.head = "refs/heads/dir"
817-
814+
def verify_dir
818815
assert File.exist?(File.join(@repo.workdir, "README"))
819816
assert File.exist?(File.join(@repo.workdir, "branch_file.txt"))
820817
assert File.exist?(File.join(@repo.workdir, "new.txt"))
821818
assert File.exist?(File.join(@repo.workdir, "a/b.txt"))
822819

823820
refute File.exist?(File.join(@repo.workdir, "ab"))
821+
end
824822

825-
@repo.checkout_tree(@repo.rev_parse("refs/heads/subtrees"), :strategy => :safe)
826-
@repo.head = "refs/heads/subtrees"
827-
823+
def verify_subtrees
828824
assert File.exist?(File.join(@repo.workdir, "README"))
829825
assert File.exist?(File.join(@repo.workdir, "branch_file.txt"))
830826
assert File.exist?(File.join(@repo.workdir, "new.txt"))
@@ -836,16 +832,29 @@ def test_checkout_tree_with_commit
836832
refute File.exist?(File.join(@repo.workdir, "a"))
837833
end
838834

835+
def test_checkout_tree_with_commit
836+
@repo.checkout_tree(@repo.rev_parse("refs/heads/dir"), :strategy => :force)
837+
@repo.head = "refs/heads/dir"
838+
verify_dir
839+
840+
@repo.checkout_tree(@repo.rev_parse("refs/heads/subtrees"), :strategy => :safe)
841+
@repo.head = "refs/heads/subtrees"
842+
verify_subtrees
843+
end
844+
839845
def test_checkout_with_revspec_string
840846
@repo.checkout_tree("refs/heads/dir", :strategy => :force)
841847
@repo.head = "refs/heads/dir"
848+
verify_dir
849+
end
842850

843-
assert File.exist?(File.join(@repo.workdir, "README"))
844-
assert File.exist?(File.join(@repo.workdir, "branch_file.txt"))
845-
assert File.exist?(File.join(@repo.workdir, "new.txt"))
846-
assert File.exist?(File.join(@repo.workdir, "a/b.txt"))
847-
848-
refute File.exist?(File.join(@repo.workdir, "ab"))
851+
def test_checkout_tree_with_index
852+
index = @repo.index
853+
head = @repo.references["refs/heads/dir"]
854+
index.read_tree head.target.tree
855+
@repo.checkout_index(index, :strategy => :force)
856+
@repo.head = "refs/heads/dir"
857+
verify_dir
849858
end
850859

851860
def test_checkout_tree_raises_errors_in_notify_cb

0 commit comments

Comments
 (0)