From 6eeccfebb0af3084d963e8dd5d7fdb13b2907a04 Mon Sep 17 00:00:00 2001 From: Arthur Schreiber Date: Wed, 3 Jan 2018 14:09:50 +0100 Subject: [PATCH] Use `RETURN_ENUMERATOR` and `RETURN_SIZED_ENUMERATOR` macros. Patch-Source: https://github.com/libgit2/rugged/pull/730 --- ext/rugged/rugged_blame.c | 9 ++++++--- ext/rugged/rugged_branch_collection.c | 9 +++------ ext/rugged/rugged_config.c | 10 +++------- ext/rugged/rugged_diff.c | 14 +++----------- ext/rugged/rugged_diff_hunk.c | 4 +--- ext/rugged/rugged_index.c | 4 +--- ext/rugged/rugged_note.c | 5 +---- ext/rugged/rugged_patch.c | 5 +---- ext/rugged/rugged_reference_collection.c | 8 +------- ext/rugged/rugged_remote.c | 5 +---- ext/rugged/rugged_remote_collection.c | 7 +------ ext/rugged/rugged_repo.c | 3 +-- ext/rugged/rugged_revwalk.c | 13 ++----------- ext/rugged/rugged_submodule_collection.c | 7 +++---- ext/rugged/rugged_tag_collection.c | 6 +----- ext/rugged/rugged_tree.c | 5 ++--- 16 files changed, 31 insertions(+), 83 deletions(-) diff --git a/ext/rugged/rugged_blame.c b/ext/rugged/rugged_blame.c index 91716301f..97af0b022 100644 --- a/ext/rugged/rugged_blame.c +++ b/ext/rugged/rugged_blame.c @@ -188,6 +188,11 @@ static VALUE rb_git_blame_count(VALUE self) return UINT2NUM(git_blame_get_hunk_count(blame)); } +static VALUE rugged_blame_enum_size(VALUE rb_blame, VALUE rb_args, VALUE rb_eobj) +{ + return rb_git_blame_count(rb_blame); +} + /* * call-seq: * blame[index] -> hunk @@ -242,9 +247,7 @@ static VALUE rb_git_blame_each(VALUE self) git_blame *blame; uint32_t i, blame_count; - if (!rb_block_given_p()) { - return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each"), self); - } + RETURN_SIZED_ENUMERATOR(self, 0, 0, rugged_blame_enum_size); Data_Get_Struct(self, git_blame, blame); diff --git a/ext/rugged/rugged_branch_collection.c b/ext/rugged/rugged_branch_collection.c index b1975fbc7..21e0d5f21 100644 --- a/ext/rugged/rugged_branch_collection.c +++ b/ext/rugged/rugged_branch_collection.c @@ -178,19 +178,16 @@ static VALUE rb_git_branch_collection_aref(VALUE self, VALUE rb_name) { static VALUE each_branch(int argc, VALUE *argv, VALUE self, int branch_names_only) { - VALUE rb_repo = rugged_owner(self), rb_filter; + VALUE rb_repo, rb_filter; git_repository *repo; git_branch_iterator *iter; int error, exception = 0; git_branch_t filter = (GIT_BRANCH_LOCAL | GIT_BRANCH_REMOTE), branch_type; + RETURN_ENUMERATOR(self, argc, argv); rb_scan_args(argc, argv, "01", &rb_filter); - if (!rb_block_given_p()) { - VALUE symbol = branch_names_only ? CSTR2SYM("each_name") : CSTR2SYM("each"); - return rb_funcall(self, rb_intern("to_enum"), 2, symbol, rb_filter); - } - + rb_repo = rugged_owner(self); rugged_check_repo(rb_repo); if (!NIL_P(rb_filter)) diff --git a/ext/rugged/rugged_config.c b/ext/rugged/rugged_config.c index 634684261..861d09504 100644 --- a/ext/rugged/rugged_config.c +++ b/ext/rugged/rugged_config.c @@ -212,11 +212,9 @@ static VALUE rb_git_config_each_key(VALUE self) git_config *config; int error; + RETURN_ENUMERATOR(self, 0, 0); Data_Get_Struct(self, git_config, config); - if (!rb_block_given_p()) - return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_key")); - error = git_config_foreach(config, &cb_config__each_key, (void *)rb_block_proc()); rugged_exception_check(error); return Qnil; @@ -240,12 +238,10 @@ static VALUE rb_git_config_each_pair(VALUE self) { git_config *config; int error; - + + RETURN_ENUMERATOR(self, 0, 0); Data_Get_Struct(self, git_config, config); - if (!rb_block_given_p()) - return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_pair")); - error = git_config_foreach(config, &cb_config__each_pair, (void *)rb_block_proc()); rugged_exception_check(error); return Qnil; diff --git a/ext/rugged/rugged_diff.c b/ext/rugged/rugged_diff.c index 7810c5ab7..102bedcdf 100644 --- a/ext/rugged/rugged_diff.c +++ b/ext/rugged/rugged_diff.c @@ -447,10 +447,7 @@ static VALUE rb_git_diff_each_patch(VALUE self) int error = 0; size_t d, delta_count; - if (!rb_block_given_p()) { - return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_patch"), self); - } - + RETURN_ENUMERATOR(self, 0, 0); Data_Get_Struct(self, git_diff, diff); delta_count = git_diff_num_deltas(diff); @@ -484,10 +481,7 @@ static VALUE rb_git_diff_each_delta(VALUE self) int error = 0; size_t d, delta_count; - if (!rb_block_given_p()) { - return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_delta"), self); - } - + RETURN_ENUMERATOR(self, 0, 0); Data_Get_Struct(self, git_diff, diff); delta_count = git_diff_num_deltas(diff); @@ -524,6 +518,7 @@ static VALUE rb_git_diff_each_line(int argc, VALUE *argv, VALUE self) git_diff_format_t format; int exception = 0, error; + RETURN_ENUMERATOR(self, argc, argv); Data_Get_Struct(self, git_diff, diff); if (rb_scan_args(argc, argv, "01", &rb_format) == 1) { @@ -532,9 +527,6 @@ static VALUE rb_git_diff_each_line(int argc, VALUE *argv, VALUE self) rb_format = CSTR2SYM("patch"); } - if (!rb_block_given_p()) - return rb_funcall(self, rb_intern("to_enum"), 2, CSTR2SYM("each_line"), rb_format); - if (SYM2ID(rb_format) == rb_intern("patch")) { format = GIT_DIFF_FORMAT_PATCH; } else if (SYM2ID(rb_format) == rb_intern("patch_header")) { diff --git a/ext/rugged/rugged_diff_hunk.c b/ext/rugged/rugged_diff_hunk.c index bc7212a6e..37878e05d 100644 --- a/ext/rugged/rugged_diff_hunk.c +++ b/ext/rugged/rugged_diff_hunk.c @@ -42,9 +42,7 @@ static VALUE rb_git_diff_hunk_each_line(VALUE self) git_patch *patch; int error = 0, l, lines_count, hunk_idx; - if (!rb_block_given_p()) { - return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_line"), self); - } + RETURN_ENUMERATOR(self, 0, 0); Data_Get_Struct(rugged_owner(self), git_patch, patch); diff --git a/ext/rugged/rugged_index.c b/ext/rugged/rugged_index.c index dc52497ef..c21643d61 100644 --- a/ext/rugged/rugged_index.c +++ b/ext/rugged/rugged_index.c @@ -190,11 +190,9 @@ static VALUE rb_git_index_each(VALUE self) git_index *index; unsigned int i, count; + RETURN_ENUMERATOR(self, 0, 0); Data_Get_Struct(self, git_index, index); - if (!rb_block_given_p()) - return rb_funcall(self, rb_intern("to_enum"), 0); - count = (unsigned int)git_index_entrycount(index); for (i = 0; i < count; ++i) { const git_index_entry *entry = git_index_get_byindex(index, i); diff --git a/ext/rugged/rugged_note.c b/ext/rugged/rugged_note.c index ea2f0c5c2..70508d128 100644 --- a/ext/rugged/rugged_note.c +++ b/ext/rugged/rugged_note.c @@ -297,12 +297,9 @@ static VALUE rb_git_note_each(int argc, VALUE *argv, VALUE self) struct rugged_cb_payload payload = { self, 0 }; VALUE rb_notes_ref; + RETURN_ENUMERATOR(self, argc, argv); rb_scan_args(argc, argv, "01", &rb_notes_ref); - if (!rb_block_given_p()) { - return rb_funcall(self, rb_intern("to_enum"), 3, CSTR2SYM("each_note"), self, rb_notes_ref); - } - if (!NIL_P(rb_notes_ref)) { Check_Type(rb_notes_ref, T_STRING); notes_ref = StringValueCStr(rb_notes_ref); diff --git a/ext/rugged/rugged_patch.c b/ext/rugged/rugged_patch.c index 140d24d64..2a7c0b758 100644 --- a/ext/rugged/rugged_patch.c +++ b/ext/rugged/rugged_patch.c @@ -93,10 +93,7 @@ static VALUE rb_git_diff_patch_each_hunk(VALUE self) int error = 0; size_t hunks_count, h; - if (!rb_block_given_p()) { - return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_hunk"), self); - } - + RETURN_ENUMERATOR(self, 0, 0); Data_Get_Struct(self, git_patch, patch); hunks_count = git_patch_num_hunks(patch); diff --git a/ext/rugged/rugged_reference_collection.c b/ext/rugged/rugged_reference_collection.c index f335c2a69..907fa4c39 100644 --- a/ext/rugged/rugged_reference_collection.c +++ b/ext/rugged/rugged_reference_collection.c @@ -114,15 +114,9 @@ static VALUE rb_git_reference_collection__each(int argc, VALUE *argv, VALUE self git_reference_iterator *iter; int error, exception = 0; + RETURN_ENUMERATOR(self, argc, argv); rb_scan_args(argc, argv, "01", &rb_glob); - if (!rb_block_given_p()) { - return rb_funcall(self, - rb_intern("to_enum"), 2, - only_names ? CSTR2SYM("each_name") : CSTR2SYM("each"), - rb_glob); - } - rugged_check_repo(rb_repo); Data_Get_Struct(rb_repo, git_repository, repo); diff --git a/ext/rugged/rugged_remote.c b/ext/rugged/rugged_remote.c index 6c19397e8..b9e82f68f 100644 --- a/ext/rugged/rugged_remote.c +++ b/ext/rugged/rugged_remote.c @@ -304,13 +304,10 @@ static VALUE rb_git_remote_ls(int argc, VALUE *argv, VALUE self) int error; size_t heads_len, i; + RETURN_ENUMERATOR(self, argc, argv); Data_Get_Struct(self, git_remote, remote); - rb_scan_args(argc, argv, ":", &rb_options); - if (!rb_block_given_p()) - return rb_funcall(self, rb_intern("to_enum"), 2, CSTR2SYM("ls"), rb_options); - rugged_remote_init_callbacks_and_payload_from_options(rb_options, &callbacks, &payload); init_custom_headers(rb_options, &custom_headers); diff --git a/ext/rugged/rugged_remote_collection.c b/ext/rugged/rugged_remote_collection.c index ce9ca03f1..967f927d2 100644 --- a/ext/rugged/rugged_remote_collection.c +++ b/ext/rugged/rugged_remote_collection.c @@ -139,12 +139,7 @@ static VALUE rb_git_remote_collection__each(VALUE self, int only_names) VALUE rb_repo; - if (!rb_block_given_p()) { - if (only_names) - return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_name")); - else - return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each")); - } + RETURN_ENUMERATOR(self, 0, 0); rb_repo = rugged_owner(self); rugged_check_repo(rb_repo); diff --git a/ext/rugged/rugged_repo.c b/ext/rugged/rugged_repo.c index 1ac827a23..6baf591c3 100644 --- a/ext/rugged/rugged_repo.c +++ b/ext/rugged/rugged_repo.c @@ -1569,8 +1569,7 @@ static VALUE rb_git_repo_each_id(VALUE self) git_odb *odb; int error, exception = 0; - if (!rb_block_given_p()) - return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_id")); + RETURN_ENUMERATOR(self, 0, 0); Data_Get_Struct(self, git_repository, repo); diff --git a/ext/rugged/rugged_revwalk.c b/ext/rugged/rugged_revwalk.c index 582f32a3d..be5cdc4ad 100644 --- a/ext/rugged/rugged_revwalk.c +++ b/ext/rugged/rugged_revwalk.c @@ -365,14 +365,9 @@ static VALUE rb_git_walk(int argc, VALUE *argv, VALUE self) struct walk_options w; int exception = 0; + RETURN_ENUMERATOR(self, argc, argv); rb_scan_args(argc, argv, "10:", &rb_repo, &rb_options); - if (!rb_block_given_p()) { - ID iter_method = ID2SYM(rb_intern("walk")); - return rb_funcall(self, rb_intern("to_enum"), 3, - iter_method, rb_repo, rb_options); - } - Data_Get_Struct(rb_repo, git_repository, w.repo); rugged_exception_check(git_revwalk_new(&w.walk, w.repo)); @@ -402,13 +397,9 @@ static VALUE rb_git_walk_with_opts(int argc, VALUE *argv, VALUE self, int oid_on VALUE rb_options; struct walk_options w; + RETURN_ENUMERATOR(self, argc, argv); rb_scan_args(argc, argv, "01", &rb_options); - if (!rb_block_given_p()) { - ID iter_method = ID2SYM(rb_intern(oid_only ? "each_oid" : "each")); - return rb_funcall(self, rb_intern("to_enum"), 2, iter_method, rb_options); - } - Data_Get_Struct(self, git_revwalk, w.walk); w.repo = git_revwalk_repository(w.walk); diff --git a/ext/rugged/rugged_submodule_collection.c b/ext/rugged/rugged_submodule_collection.c index 23799793a..ced2c01a2 100644 --- a/ext/rugged/rugged_submodule_collection.c +++ b/ext/rugged/rugged_submodule_collection.c @@ -124,13 +124,12 @@ static VALUE rb_git_submodule_collection_each(VALUE self) git_repository *repo; int error; struct rugged_cb_payload payload; + VALUE rb_repo; - VALUE rb_repo = rugged_owner(self); + RETURN_ENUMERATOR(self, 0, 0); + rb_repo = rugged_owner(self); Data_Get_Struct(rb_repo, git_repository, repo); - if (!rb_block_given_p()) - return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each")); - payload.exception = 0; payload.rb_data = rb_repo; diff --git a/ext/rugged/rugged_tag_collection.c b/ext/rugged/rugged_tag_collection.c index 11942c159..8f5fd4bd5 100644 --- a/ext/rugged/rugged_tag_collection.c +++ b/ext/rugged/rugged_tag_collection.c @@ -236,13 +236,9 @@ static VALUE each_tag(int argc, VALUE *argv, VALUE self, int tag_names_only) VALUE rb_repo = rugged_owner(self), rb_pattern; const char *pattern = NULL; + RETURN_ENUMERATOR(self, argc, argv); rb_scan_args(argc, argv, "01", &rb_pattern); - if (!rb_block_given_p()) { - VALUE symbol = tag_names_only ? CSTR2SYM("each_name") : CSTR2SYM("each"); - return rb_funcall(self, rb_intern("to_enum"), 2, symbol, rb_pattern); - } - if (!NIL_P(rb_pattern)) { Check_Type(rb_pattern, T_STRING); pattern = StringValueCStr(rb_pattern); diff --git a/ext/rugged/rugged_tree.c b/ext/rugged/rugged_tree.c index 3fab8f367..73423a3cf 100644 --- a/ext/rugged/rugged_tree.c +++ b/ext/rugged/rugged_tree.c @@ -220,10 +220,9 @@ static VALUE rb_git_tree_each(VALUE self) { git_tree *tree; size_t i, count; - Data_Get_Struct(self, git_tree, tree); - if (!rb_block_given_p()) - return rb_funcall(self, rb_intern("to_enum"), 0); + RETURN_ENUMERATOR(self, 0, 0); + Data_Get_Struct(self, git_tree, tree); count = git_tree_entrycount(tree);