Skip to content

Commit edcaca9

Browse files
committed
Spooky Scary Rb_Ensure Skeleton
1 parent dd859f6 commit edcaca9

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

ext/rugged/rugged_revwalk.c

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,6 @@ static VALUE do_walk(VALUE _payload)
302302
return Qnil;
303303
}
304304

305-
static VALUE do_walk_cleanup(VALUE _payload)
306-
{
307-
struct walk_options *w = (struct walk_options *)_payload;
308-
git_revwalk_free(w->walk);
309-
return Qnil;
310-
}
311-
312305
/*
313306
* call-seq:
314307
* Rugged::Walker.walk(repo, options={}) { |commit| block }
@@ -360,9 +353,16 @@ static VALUE rb_git_walk(int argc, VALUE *argv, VALUE self)
360353
{
361354
VALUE rb_repo, rb_options;
362355
struct walk_options w;
356+
int exception = 0;
363357

364358
rb_scan_args(argc, argv, "10:", &rb_repo, &rb_options);
365359

360+
if (!rb_block_given_p()) {
361+
ID iter_method = ID2SYM(rb_intern("walk"));
362+
return rb_funcall(self, rb_intern("to_enum"), 3,
363+
iter_method, rb_repo, rb_options);
364+
}
365+
366366
Data_Get_Struct(rb_repo, git_repository, w.repo);
367367
rugged_exception_check(git_revwalk_new(&w.walk, w.repo));
368368

@@ -373,22 +373,32 @@ static VALUE rb_git_walk(int argc, VALUE *argv, VALUE self)
373373
w.offset = 0;
374374
w.limit = UINT64_MAX;
375375

376-
if (!NIL_P(w.rb_options)) {
377-
rb_ensure(
378-
load_all_options, (VALUE)&w,
379-
do_walk_cleanup, (VALUE)&w);
380-
}
376+
if (!NIL_P(w.rb_options))
377+
rb_protect(load_all_options, (VALUE)&w, &exception);
378+
379+
if (!exception)
380+
rb_protect(do_walk, (VALUE)&w, &exception);
381+
382+
git_revwalk_free(w.walk);
381383

382-
return rb_ensure(
383-
do_walk, (VALUE)&w,
384-
do_walk_cleanup, (VALUE)&w);
384+
if (exception)
385+
rb_jump_tag(exception);
386+
387+
return Qnil;
385388
}
386389

387390
static VALUE rb_git_walk_with_opts(int argc, VALUE *argv, VALUE self, int oid_only)
388391
{
389392
VALUE rb_options;
390393
struct walk_options w;
391394

395+
rb_scan_args(argc, argv, "01", &rb_options);
396+
397+
if (!rb_block_given_p()) {
398+
ID iter_method = ID2SYM(rb_intern(oid_only ? "each_oid" : "each"));
399+
return rb_funcall(self, rb_intern("to_enum"), 2, iter_method, rb_options);
400+
}
401+
392402
Data_Get_Struct(self, git_revwalk, w.walk);
393403
w.repo = git_revwalk_repository(w.walk);
394404

@@ -399,13 +409,6 @@ static VALUE rb_git_walk_with_opts(int argc, VALUE *argv, VALUE self, int oid_on
399409
w.offset = 0;
400410
w.limit = UINT64_MAX;
401411

402-
rb_scan_args(argc, argv, "01", &rb_options);
403-
404-
if (!rb_block_given_p()) {
405-
ID iter_method = ID2SYM(rb_intern(oid_only ? "each_oid" : "each"));
406-
return rb_funcall(self, rb_intern("to_enum"), 2, iter_method, rb_options);
407-
}
408-
409412
if (!NIL_P(rb_options))
410413
load_walk_limits(&w, rb_options);
411414

test/walker_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ def test_sort_by_topo_reverse
138138
sort_list = do_sort(Rugged::SORT_TOPO | Rugged::SORT_REVERSE).reverse
139139
assert_equal is_toposorted(sort_list), true
140140
end
141+
142+
def test_walk_api
143+
sha = "9fd738e8f7967c078dceed8190330fc8648ee56a"
144+
data = Rugged::Walker.walk(@repo, show: sha).to_a
145+
oids = data.sort { |a, b| a.oid <=> b.oid }.map {|a| a.oid[0,5]}.join('.')
146+
assert_equal "4a202.5b5b0.84960.9fd73", oids
147+
end
141148
end
142149

143150
# testrepo (the non-bare repo) is the one with non-linear history,

0 commit comments

Comments
 (0)