Move a couple more functions to use iterators
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
diff --git a/src/branch.c b/src/branch.c
index 74e8d84..5f4e338 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -124,40 +124,43 @@ on_error:
return error;
}
-typedef struct {
- git_branch_foreach_cb branch_cb;
- void *callback_payload;
- unsigned int branch_type;
-} branch_foreach_filter;
-
-static int branch_foreach_cb(const char *branch_name, void *payload)
-{
- branch_foreach_filter *filter = (branch_foreach_filter *)payload;
-
- if (filter->branch_type & GIT_BRANCH_LOCAL &&
- git__prefixcmp(branch_name, GIT_REFS_HEADS_DIR) == 0)
- return filter->branch_cb(branch_name + strlen(GIT_REFS_HEADS_DIR), GIT_BRANCH_LOCAL, filter->callback_payload);
-
- if (filter->branch_type & GIT_BRANCH_REMOTE &&
- git__prefixcmp(branch_name, GIT_REFS_REMOTES_DIR) == 0)
- return filter->branch_cb(branch_name + strlen(GIT_REFS_REMOTES_DIR), GIT_BRANCH_REMOTE, filter->callback_payload);
-
- return 0;
-}
-
int git_branch_foreach(
git_repository *repo,
unsigned int list_flags,
- git_branch_foreach_cb branch_cb,
+ git_branch_foreach_cb callback,
void *payload)
{
- branch_foreach_filter filter;
+ git_reference_iterator *iter;
+ const char *name;
+ int error;
+
+ if (git_reference_iterator_new(&iter, repo) < 0)
+ return -1;
+
+ while ((error = git_reference_next(&name, iter)) == 0) {
+ if (list_flags & GIT_BRANCH_LOCAL &&
+ git__prefixcmp(name, GIT_REFS_HEADS_DIR) == 0) {
+ if (callback(name + strlen(GIT_REFS_HEADS_DIR), GIT_BRANCH_LOCAL, payload)) {
+ error = GIT_EUSER;
+ break;
+ }
+ }
+
+ if (list_flags & GIT_BRANCH_REMOTE &&
+ git__prefixcmp(name, GIT_REFS_REMOTES_DIR) == 0) {
+ if (callback(name + strlen(GIT_REFS_REMOTES_DIR), GIT_BRANCH_REMOTE, payload)) {
+ error = GIT_EUSER;
+ break;
+ }
+ }
+ }
- filter.branch_cb = branch_cb;
- filter.branch_type = list_flags;
- filter.callback_payload = payload;
+ if (error == GIT_ITEROVER)
+ error = 0;
+
+ git_reference_iterator_free(iter);
+ return error;
- return git_reference_foreach(repo, &branch_foreach_cb, (void *)&filter);
}
int git_branch_move(
diff --git a/src/remote.c b/src/remote.c
index 10bffec..2f25c0f 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -1244,14 +1244,6 @@ static int update_branch_remote_config_entry(
update_config_entries_cb, &data);
}
-static int rename_cb(const char *ref, void *data)
-{
- if (git__prefixcmp(ref, GIT_REFS_REMOTES_DIR))
- return 0;
-
- return git_vector_insert((git_vector *)data, git__strdup(ref));
-}
-
static int rename_one_remote_reference(
git_repository *repo,
const char *reference_name,
@@ -1291,15 +1283,29 @@ static int rename_remote_references(
int error = -1;
unsigned int i;
char *name;
+ const char *refname;
+ git_reference_iterator *iter;
if (git_vector_init(&refnames, 8, NULL) < 0)
+ return -1;
+
+ if (git_reference_iterator_new(&iter, repo) < 0)
goto cleanup;
- if (git_reference_foreach(
- repo,
- rename_cb,
- &refnames) < 0)
- goto cleanup;
+ while ((error = git_reference_next(&refname, iter)) == 0) {
+ if (git__prefixcmp(refname, GIT_REFS_REMOTES_DIR))
+ continue;
+
+ if ((error = git_vector_insert(&refnames, git__strdup(refname))) < 0)
+ break;
+
+ }
+
+ git_reference_iterator_free(iter);
+ if (error == GIT_ITEROVER)
+ error = 0;
+ else
+ goto cleanup;
git_vector_foreach(&refnames, i, name) {
if ((error = rename_one_remote_reference(repo, name, old_name, new_name)) < 0)