Skip to content

Commit 059bff4

Browse files
Merge pull request #576 from YorickPeterse/walker-count-enumerable-compatible
Make Walker#count compatible with Enumerable#count
2 parents d1ffcc8 + 5b409f3 commit 059bff4

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

ext/rugged/rugged_revwalk.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,19 @@ static VALUE rb_git_walker_simplify_first_parent(VALUE self)
194194
* call-seq:
195195
* walker.count -> Fixnum
196196
*
197-
* Returns the amount of objects a walker iterated over.
197+
* Returns the amount of objects a walker iterated over. If an argument or
198+
* block is given this method delegates to +Enumerable#count+.
198199
*/
199-
static VALUE rb_git_walker_count(VALUE self)
200+
static VALUE rb_git_walker_count(int argc, VALUE *argv, VALUE self)
200201
{
201202
git_revwalk *walk;
202203
git_oid commit_oid;
203204
int error = 0;
204205
uint64_t count = 0;
205206

207+
if (argc > 0 || rb_block_given_p())
208+
return rb_call_super(argc, argv);
209+
206210
Data_Get_Struct(self, git_revwalk, walk);
207211

208212
while (((error = git_revwalk_next(&commit_oid, walk)) == 0) && ++count != UINT64_MAX);
@@ -515,5 +519,5 @@ void Init_rugged_revwalk(void)
515519
rb_define_method(rb_cRuggedWalker, "reset", rb_git_walker_reset, 0);
516520
rb_define_method(rb_cRuggedWalker, "sorting", rb_git_walker_sorting, 1);
517521
rb_define_method(rb_cRuggedWalker, "simplify_first_parent", rb_git_walker_simplify_first_parent, 0);
518-
rb_define_method(rb_cRuggedWalker, "count", rb_git_walker_count, 0);
522+
rb_define_method(rb_cRuggedWalker, "count", rb_git_walker_count, -1);
519523
}

test/walker_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,24 @@ def test_walk_count
149149
@walker.hide("5b5b025afb0b4c913b4c338a42934a3863bf3644")
150150
assert_equal 2, @walker.count
151151
end
152+
153+
def test_walk_count_argument
154+
@walker.push("9fd738e8f7967c078dceed8190330fc8648ee56a")
155+
@walker.hide("5b5b025afb0b4c913b4c338a42934a3863bf3644")
156+
157+
assert_equal 0, @walker.count('foo')
158+
end
159+
160+
def test_walk_count_with_block
161+
@walker.push("9fd738e8f7967c078dceed8190330fc8648ee56a")
162+
@walker.hide("5b5b025afb0b4c913b4c338a42934a3863bf3644")
163+
164+
amount = @walker.count do |commit|
165+
commit.oid == "9fd738e8f7967c078dceed8190330fc8648ee56a"
166+
end
167+
168+
assert_equal 1, amount
169+
end
152170
end
153171

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

0 commit comments

Comments
 (0)