summaryrefslogtreecommitdiff
path: root/src/notes.c
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2013-03-06 13:58:21 -0800
committerRussell Belfer <rb@github.com>2013-03-06 13:58:21 -0800
commit1b405a232fc97b8d692e62e73d29c453e44a2970 (patch)
tree6ab7ad2cbafa2d794627c9f6a8a21102454d7516 /src/notes.c
parent69c28b75dfb0a3edefa4d4a8616624f856b15533 (diff)
parentaa518c709c8caa6577822e073e7f27733eb6aaa3 (diff)
downloadlibgit2-1b405a232fc97b8d692e62e73d29c453e44a2970.tar.gz
Merge pull request #1396 from cholin/features/note-iterator
[RFC] basic note iterator implementation
Diffstat (limited to 'src/notes.c')
-rw-r--r--src/notes.c99
1 files changed, 73 insertions, 26 deletions
diff --git a/src/notes.c b/src/notes.c
index f5537db3f..a1a47d989 100644
--- a/src/notes.c
+++ b/src/notes.c
@@ -531,14 +531,11 @@ void git_note_free(git_note *note)
static int process_entry_path(
const char* entry_path,
- const git_oid *note_oid,
- git_note_foreach_cb note_cb,
- void *payload)
+ git_oid *annotated_object_id)
{
int error = -1;
size_t i = 0, j = 0, len;
git_buf buf = GIT_BUF_INIT;
- git_oid annotated_object_id;
if ((error = git_buf_puts(&buf, entry_path)) < 0)
goto cleanup;
@@ -571,11 +568,7 @@ static int process_entry_path(
goto cleanup;
}
- if ((error = git_oid_fromstr(&annotated_object_id, buf.ptr)) < 0)
- goto cleanup;
-
- if (note_cb(note_oid, &annotated_object_id, payload))
- error = GIT_EUSER;
+ error = git_oid_fromstr(annotated_object_id, buf.ptr);
cleanup:
git_buf_free(&buf);
@@ -583,32 +576,86 @@ cleanup:
}
int git_note_foreach(
+ git_repository *repo,
+ const char *notes_ref,
+ git_note_foreach_cb note_cb,
+ void *payload)
+{
+ int error;
+ git_note_iterator *iter = NULL;
+ git_oid note_id, annotated_id;
+
+ if ((error = git_note_iterator_new(&iter, repo, notes_ref)) < 0)
+ return error;
+
+ while (!(error = git_note_next(&note_id, &annotated_id, iter))) {
+ if (note_cb(&note_id, &annotated_id, payload)) {
+ error = GIT_EUSER;
+ break;
+ }
+ }
+
+ if (error == GIT_ITEROVER)
+ error = 0;
+
+ git_note_iterator_free(iter);
+ return error;
+}
+
+
+void git_note_iterator_free(git_note_iterator *it)
+{
+ if (it == NULL)
+ return;
+
+ git_iterator_free(it);
+}
+
+
+int git_note_iterator_new(
+ git_note_iterator **it,
git_repository *repo,
- const char *notes_ref,
- git_note_foreach_cb note_cb,
- void *payload)
+ const char *notes_ref)
{
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)) &&
- !(error = git_iterator_for_tree(&iter, tree)))
- error = git_iterator_current(iter, &item);
+ git_tree *tree = NULL;
- while (!error && item) {
- error = process_entry_path(item->path, &item->oid, note_cb, payload);
+ error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref);
+ if (error < 0)
+ goto cleanup;
- if (!error)
- error = git_iterator_advance(iter, &item);
- }
+ if ((error = git_iterator_for_tree(it, tree)) < 0)
+ git_iterator_free(*it);
- git_iterator_free(iter);
+cleanup:
git_tree_free(tree);
git_commit_free(commit);
return error;
}
+
+int git_note_next(
+ git_oid* note_id,
+ git_oid* annotated_id,
+ git_note_iterator *it)
+{
+ int error;
+ const git_index_entry *item;
+
+ if ((error = git_iterator_current(it, &item)) < 0)
+ goto exit;
+
+ if (item != NULL) {
+ git_oid_cpy(note_id, &item->oid);
+ error = process_entry_path(item->path, annotated_id);
+
+ if (error >= 0)
+ error = git_iterator_advance(it, NULL);
+ } else {
+ error = GIT_ITEROVER;
+ }
+
+exit:
+ return error;
+}