Skip to content

Commit 74fe8f6

Browse files
authored
Merge pull request #730 from libgit2/arthur/fix-enumerator-returns
Fix for returning enumerators.
2 parents 2ab8279 + c159552 commit 74fe8f6

17 files changed

+32
-88
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ sudo: required
2626
matrix:
2727
fast_finish: true
2828
allow_failures:
29-
- rvm: 2.5.0
3029
- rvm: rbx-2
3130
- rvm: ruby-head
3231

ext/rugged/rugged_blame.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ static VALUE rb_git_blame_count(VALUE self)
188188
return UINT2NUM(git_blame_get_hunk_count(blame));
189189
}
190190

191+
static VALUE rugged_blame_enum_size(VALUE rb_blame, VALUE rb_args, VALUE rb_eobj)
192+
{
193+
return rb_git_blame_count(rb_blame);
194+
}
195+
191196
/*
192197
* call-seq:
193198
* blame[index] -> hunk
@@ -242,9 +247,7 @@ static VALUE rb_git_blame_each(VALUE self)
242247
git_blame *blame;
243248
uint32_t i, blame_count;
244249

245-
if (!rb_block_given_p()) {
246-
return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each"), self);
247-
}
250+
RETURN_SIZED_ENUMERATOR(self, 0, 0, rugged_blame_enum_size);
248251

249252
Data_Get_Struct(self, git_blame, blame);
250253

ext/rugged/rugged_branch_collection.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,19 +178,16 @@ static VALUE rb_git_branch_collection_aref(VALUE self, VALUE rb_name) {
178178

179179
static VALUE each_branch(int argc, VALUE *argv, VALUE self, int branch_names_only)
180180
{
181-
VALUE rb_repo = rugged_owner(self), rb_filter;
181+
VALUE rb_repo, rb_filter;
182182
git_repository *repo;
183183
git_branch_iterator *iter;
184184
int error, exception = 0;
185185
git_branch_t filter = (GIT_BRANCH_LOCAL | GIT_BRANCH_REMOTE), branch_type;
186186

187+
RETURN_ENUMERATOR(self, argc, argv);
187188
rb_scan_args(argc, argv, "01", &rb_filter);
188189

189-
if (!rb_block_given_p()) {
190-
VALUE symbol = branch_names_only ? CSTR2SYM("each_name") : CSTR2SYM("each");
191-
return rb_funcall(self, rb_intern("to_enum"), 2, symbol, rb_filter);
192-
}
193-
190+
rb_repo = rugged_owner(self);
194191
rugged_check_repo(rb_repo);
195192

196193
if (!NIL_P(rb_filter))

ext/rugged/rugged_config.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ static int cb_config__to_hash(const git_config_entry *entry, void *opaque)
198198
/*
199199
* call-seq:
200200
* cfg.each_key { |key| block }
201-
* cfg.each_key -> enumarator
201+
* cfg.each_key -> enumerator
202202
*
203203
* Call the given block once for each key in the config file. If no block
204204
* is given, an enumerator is returned.
@@ -212,11 +212,9 @@ static VALUE rb_git_config_each_key(VALUE self)
212212
git_config *config;
213213
int error;
214214

215+
RETURN_ENUMERATOR(self, 0, 0);
215216
Data_Get_Struct(self, git_config, config);
216217

217-
if (!rb_block_given_p())
218-
return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_key"));
219-
220218
error = git_config_foreach(config, &cb_config__each_key, (void *)rb_block_proc());
221219
rugged_exception_check(error);
222220
return Qnil;
@@ -240,12 +238,10 @@ static VALUE rb_git_config_each_pair(VALUE self)
240238
{
241239
git_config *config;
242240
int error;
243-
241+
242+
RETURN_ENUMERATOR(self, 0, 0);
244243
Data_Get_Struct(self, git_config, config);
245244

246-
if (!rb_block_given_p())
247-
return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_pair"));
248-
249245
error = git_config_foreach(config, &cb_config__each_pair, (void *)rb_block_proc());
250246
rugged_exception_check(error);
251247
return Qnil;

ext/rugged/rugged_diff.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -447,10 +447,7 @@ static VALUE rb_git_diff_each_patch(VALUE self)
447447
int error = 0;
448448
size_t d, delta_count;
449449

450-
if (!rb_block_given_p()) {
451-
return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_patch"), self);
452-
}
453-
450+
RETURN_ENUMERATOR(self, 0, 0);
454451
Data_Get_Struct(self, git_diff, diff);
455452

456453
delta_count = git_diff_num_deltas(diff);
@@ -481,13 +478,9 @@ static VALUE rb_git_diff_each_delta(VALUE self)
481478
{
482479
git_diff *diff;
483480
const git_diff_delta *delta;
484-
int error = 0;
485481
size_t d, delta_count;
486482

487-
if (!rb_block_given_p()) {
488-
return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_delta"), self);
489-
}
490-
483+
RETURN_ENUMERATOR(self, 0, 0);
491484
Data_Get_Struct(self, git_diff, diff);
492485

493486
delta_count = git_diff_num_deltas(diff);
@@ -496,8 +489,6 @@ static VALUE rb_git_diff_each_delta(VALUE self)
496489
rb_yield(rugged_diff_delta_new(self, delta));
497490
}
498491

499-
rugged_exception_check(error);
500-
501492
return self;
502493
}
503494

@@ -524,6 +515,7 @@ static VALUE rb_git_diff_each_line(int argc, VALUE *argv, VALUE self)
524515
git_diff_format_t format;
525516
int exception = 0, error;
526517

518+
RETURN_ENUMERATOR(self, argc, argv);
527519
Data_Get_Struct(self, git_diff, diff);
528520

529521
if (rb_scan_args(argc, argv, "01", &rb_format) == 1) {
@@ -532,9 +524,6 @@ static VALUE rb_git_diff_each_line(int argc, VALUE *argv, VALUE self)
532524
rb_format = CSTR2SYM("patch");
533525
}
534526

535-
if (!rb_block_given_p())
536-
return rb_funcall(self, rb_intern("to_enum"), 2, CSTR2SYM("each_line"), rb_format);
537-
538527
if (SYM2ID(rb_format) == rb_intern("patch")) {
539528
format = GIT_DIFF_FORMAT_PATCH;
540529
} else if (SYM2ID(rb_format) == rb_intern("patch_header")) {

ext/rugged/rugged_diff_hunk.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ static VALUE rb_git_diff_hunk_each_line(VALUE self)
4242
git_patch *patch;
4343
int error = 0, l, lines_count, hunk_idx;
4444

45-
if (!rb_block_given_p()) {
46-
return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_line"), self);
47-
}
45+
RETURN_ENUMERATOR(self, 0, 0);
4846

4947
Data_Get_Struct(rugged_owner(self), git_patch, patch);
5048

ext/rugged/rugged_index.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,9 @@ static VALUE rb_git_index_each(VALUE self)
190190
git_index *index;
191191
unsigned int i, count;
192192

193+
RETURN_ENUMERATOR(self, 0, 0);
193194
Data_Get_Struct(self, git_index, index);
194195

195-
if (!rb_block_given_p())
196-
return rb_funcall(self, rb_intern("to_enum"), 0);
197-
198196
count = (unsigned int)git_index_entrycount(index);
199197
for (i = 0; i < count; ++i) {
200198
const git_index_entry *entry = git_index_get_byindex(index, i);

ext/rugged/rugged_note.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,9 @@ static VALUE rb_git_note_each(int argc, VALUE *argv, VALUE self)
297297
struct rugged_cb_payload payload = { self, 0 };
298298
VALUE rb_notes_ref;
299299

300+
RETURN_ENUMERATOR(self, argc, argv);
300301
rb_scan_args(argc, argv, "01", &rb_notes_ref);
301302

302-
if (!rb_block_given_p()) {
303-
return rb_funcall(self, rb_intern("to_enum"), 3, CSTR2SYM("each_note"), self, rb_notes_ref);
304-
}
305-
306303
if (!NIL_P(rb_notes_ref)) {
307304
Check_Type(rb_notes_ref, T_STRING);
308305
notes_ref = StringValueCStr(rb_notes_ref);

ext/rugged/rugged_patch.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,7 @@ static VALUE rb_git_diff_patch_each_hunk(VALUE self)
9393
int error = 0;
9494
size_t hunks_count, h;
9595

96-
if (!rb_block_given_p()) {
97-
return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_hunk"), self);
98-
}
99-
96+
RETURN_ENUMERATOR(self, 0, 0);
10097
Data_Get_Struct(self, git_patch, patch);
10198

10299
hunks_count = git_patch_num_hunks(patch);

ext/rugged/rugged_reference_collection.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,9 @@ static VALUE rb_git_reference_collection__each(int argc, VALUE *argv, VALUE self
114114
git_reference_iterator *iter;
115115
int error, exception = 0;
116116

117+
RETURN_ENUMERATOR(self, argc, argv);
117118
rb_scan_args(argc, argv, "01", &rb_glob);
118119

119-
if (!rb_block_given_p()) {
120-
return rb_funcall(self,
121-
rb_intern("to_enum"), 2,
122-
only_names ? CSTR2SYM("each_name") : CSTR2SYM("each"),
123-
rb_glob);
124-
}
125-
126120
rugged_check_repo(rb_repo);
127121

128122
Data_Get_Struct(rb_repo, git_repository, repo);

ext/rugged/rugged_remote.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,10 @@ static VALUE rb_git_remote_ls(int argc, VALUE *argv, VALUE self)
304304
int error;
305305
size_t heads_len, i;
306306

307+
RETURN_ENUMERATOR(self, argc, argv);
307308
Data_Get_Struct(self, git_remote, remote);
308-
309309
rb_scan_args(argc, argv, ":", &rb_options);
310310

311-
if (!rb_block_given_p())
312-
return rb_funcall(self, rb_intern("to_enum"), 2, CSTR2SYM("ls"), rb_options);
313-
314311
rugged_remote_init_callbacks_and_payload_from_options(rb_options, &callbacks, &payload);
315312
init_custom_headers(rb_options, &custom_headers);
316313

ext/rugged/rugged_remote_collection.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,7 @@ static VALUE rb_git_remote_collection__each(VALUE self, int only_names)
139139

140140
VALUE rb_repo;
141141

142-
if (!rb_block_given_p()) {
143-
if (only_names)
144-
return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_name"));
145-
else
146-
return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each"));
147-
}
142+
RETURN_ENUMERATOR(self, 0, 0);
148143

149144
rb_repo = rugged_owner(self);
150145
rugged_check_repo(rb_repo);

ext/rugged/rugged_repo.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,8 +1569,7 @@ static VALUE rb_git_repo_each_id(VALUE self)
15691569
git_odb *odb;
15701570
int error, exception = 0;
15711571

1572-
if (!rb_block_given_p())
1573-
return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_id"));
1572+
RETURN_ENUMERATOR(self, 0, 0);
15741573

15751574
Data_Get_Struct(self, git_repository, repo);
15761575

ext/rugged/rugged_revwalk.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -365,14 +365,9 @@ static VALUE rb_git_walk(int argc, VALUE *argv, VALUE self)
365365
struct walk_options w;
366366
int exception = 0;
367367

368+
RETURN_ENUMERATOR(self, argc, argv);
368369
rb_scan_args(argc, argv, "10:", &rb_repo, &rb_options);
369370

370-
if (!rb_block_given_p()) {
371-
ID iter_method = ID2SYM(rb_intern("walk"));
372-
return rb_funcall(self, rb_intern("to_enum"), 3,
373-
iter_method, rb_repo, rb_options);
374-
}
375-
376371
Data_Get_Struct(rb_repo, git_repository, w.repo);
377372
rugged_exception_check(git_revwalk_new(&w.walk, w.repo));
378373

@@ -402,13 +397,9 @@ static VALUE rb_git_walk_with_opts(int argc, VALUE *argv, VALUE self, int oid_on
402397
VALUE rb_options;
403398
struct walk_options w;
404399

400+
RETURN_ENUMERATOR(self, argc, argv);
405401
rb_scan_args(argc, argv, "01", &rb_options);
406402

407-
if (!rb_block_given_p()) {
408-
ID iter_method = ID2SYM(rb_intern(oid_only ? "each_oid" : "each"));
409-
return rb_funcall(self, rb_intern("to_enum"), 2, iter_method, rb_options);
410-
}
411-
412403
Data_Get_Struct(self, git_revwalk, w.walk);
413404
w.repo = git_revwalk_repository(w.walk);
414405

ext/rugged/rugged_submodule_collection.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,12 @@ static VALUE rb_git_submodule_collection_each(VALUE self)
124124
git_repository *repo;
125125
int error;
126126
struct rugged_cb_payload payload;
127+
VALUE rb_repo;
127128

128-
VALUE rb_repo = rugged_owner(self);
129+
RETURN_ENUMERATOR(self, 0, 0);
130+
rb_repo = rugged_owner(self);
129131
Data_Get_Struct(rb_repo, git_repository, repo);
130132

131-
if (!rb_block_given_p())
132-
return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each"));
133-
134133
payload.exception = 0;
135134
payload.rb_data = rb_repo;
136135

ext/rugged/rugged_tag_collection.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,9 @@ static VALUE each_tag(int argc, VALUE *argv, VALUE self, int tag_names_only)
236236
VALUE rb_repo = rugged_owner(self), rb_pattern;
237237
const char *pattern = NULL;
238238

239+
RETURN_ENUMERATOR(self, argc, argv);
239240
rb_scan_args(argc, argv, "01", &rb_pattern);
240241

241-
if (!rb_block_given_p()) {
242-
VALUE symbol = tag_names_only ? CSTR2SYM("each_name") : CSTR2SYM("each");
243-
return rb_funcall(self, rb_intern("to_enum"), 2, symbol, rb_pattern);
244-
}
245-
246242
if (!NIL_P(rb_pattern)) {
247243
Check_Type(rb_pattern, T_STRING);
248244
pattern = StringValueCStr(rb_pattern);

ext/rugged/rugged_tree.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,9 @@ static VALUE rb_git_tree_each(VALUE self)
220220
{
221221
git_tree *tree;
222222
size_t i, count;
223-
Data_Get_Struct(self, git_tree, tree);
224223

225-
if (!rb_block_given_p())
226-
return rb_funcall(self, rb_intern("to_enum"), 0);
224+
RETURN_ENUMERATOR(self, 0, 0);
225+
Data_Get_Struct(self, git_tree, tree);
227226

228227
count = git_tree_entrycount(tree);
229228

0 commit comments

Comments
 (0)