summaryrefslogtreecommitdiff
path: root/src/notes.c
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2012-08-03 17:08:01 -0700
committerRussell Belfer <rb@github.com>2012-08-03 17:08:01 -0700
commit5dca201072724e4230141796d7c9f8836a277de8 (patch)
tree7eb6b8f3e08c8abc608ee43c0ddc32d4d043a2c6 /src/notes.c
parent2031760c626711cc69b4d63ac9798ff333583ca0 (diff)
downloadlibgit2-5dca201072724e4230141796d7c9f8836a277de8.tar.gz
Update iterators for consistency across library
This updates all the `foreach()` type functions across the library that take callbacks from the user to have a consistent behavior. The rules are: * A callback terminates the loop by returning any non-zero value * Once the callback returns non-zero, it will not be called again (i.e. the loop stops all iteration regardless of state) * If the callback returns non-zero, the parent fn returns GIT_EUSER * Although the parent returns GIT_EUSER, no error will be set in the library and `giterr_last()` will return NULL if called. This commit makes those changes across the library and adds tests for most of the iteration APIs to make sure that they follow the above rules.
Diffstat (limited to 'src/notes.c')
-rw-r--r--src/notes.c45
1 files changed, 19 insertions, 26 deletions
diff --git a/src/notes.c b/src/notes.c
index 7813e9985..212413a5a 100644
--- a/src/notes.c
+++ b/src/notes.c
@@ -522,13 +522,13 @@ static int process_entry_path(
int (*note_cb)(git_note_data *note_data, void *payload),
void *payload)
{
- int i = 0, j = 0, error = -1, len;
+ int i = 0, j = 0, error, len;
git_buf buf = GIT_BUF_INIT;
git_note_data note_data;
- if (git_buf_puts(&buf, entry_path) < 0)
+ if ((error = git_buf_puts(&buf, entry_path)) < 0)
goto cleanup;
-
+
len = git_buf_len(&buf);
while (i < len) {
@@ -536,10 +536,9 @@ static int process_entry_path(
i++;
continue;
}
-
+
if (git__fromhex(buf.ptr[i]) < 0) {
/* This is not a note entry */
- error = 0;
goto cleanup;
}
@@ -555,16 +554,17 @@ static int process_entry_path(
if (j != GIT_OID_HEXSZ) {
/* This is not a note entry */
- error = 0;
goto cleanup;
}
- if (git_oid_fromstr(&note_data.annotated_object_oid, buf.ptr) < 0)
- return -1;
+ if ((error = git_oid_fromstr(
+ &note_data.annotated_object_oid, buf.ptr)) < 0)
+ goto cleanup;
git_oid_cpy(&note_data.blob_oid, note_oid);
- error = note_cb(&note_data, payload);
+ if (note_cb(&note_data, payload))
+ error = GIT_EUSER;
cleanup:
git_buf_free(&buf);
@@ -577,34 +577,27 @@ int git_note_foreach(
int (*note_cb)(git_note_data *note_data, void *payload),
void *payload)
{
- int error = -1;
+ int error;
git_iterator *iter = NULL;
git_tree *tree = NULL;
git_commit *commit = NULL;
const git_index_entry *item;
- if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref)) < 0)
- goto cleanup;
-
- if (git_iterator_for_tree(&iter, repo, tree) < 0)
- goto cleanup;
+ if (!(error = retrieve_note_tree_and_commit(
+ &tree, &commit, repo, &notes_ref)) &&
+ !(error = git_iterator_for_tree(&iter, repo, tree)))
+ error = git_iterator_current(iter, &item);
- if (git_iterator_current(iter, &item) < 0)
- goto cleanup;
-
- while (item) {
- if (process_entry_path(item->path, &item->oid, note_cb, payload) < 0)
- goto cleanup;
+ while (!error && item) {
+ error = process_entry_path(item->path, &item->oid, note_cb, payload);
- if (git_iterator_advance(iter, &item) < 0)
- goto cleanup;
+ if (!error)
+ error = git_iterator_advance(iter, &item);
}
- error = 0;
-
-cleanup:
git_iterator_free(iter);
git_tree_free(tree);
git_commit_free(commit);
+
return error;
}