Skip to content

Commit 60bee89

Browse files
committed
notes: Add git_note_commit_iterator_new
This also adds tests for this function.
1 parent 9a02725 commit 60bee89

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

include/git2/notes.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ GIT_EXTERN(int) git_note_iterator_new(
5151
git_repository *repo,
5252
const char *notes_ref);
5353

54+
/**
55+
* Creates a new iterator for notes from a commit
56+
*
57+
* The iterator must be freed manually by the user.
58+
*
59+
* @param out pointer to the iterator
60+
* @param notes_commit a pointer to the notes commit object
61+
*
62+
* @return 0 or an error code
63+
*/
64+
GIT_EXTERN(int) git_note_commit_iterator_new(
65+
git_note_iterator **out,
66+
git_commit *notes_commit);
67+
5468
/**
5569
* Frees an git_note_iterator
5670
*

src/notes.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,25 @@ int git_note_iterator_new(
766766
return error;
767767
}
768768

769+
int git_note_commit_iterator_new(
770+
git_note_iterator **it,
771+
git_commit *notes_commit)
772+
{
773+
int error;
774+
git_tree *tree;
775+
776+
if ((error = git_commit_tree(&tree, notes_commit)) < 0)
777+
goto cleanup;
778+
779+
if ((error = git_iterator_for_tree(it, tree, NULL)) < 0)
780+
git_iterator_free(*it);
781+
782+
cleanup:
783+
git_tree_free(tree);
784+
785+
return error;
786+
}
787+
769788
int git_note_next(
770789
git_oid* note_id,
771790
git_oid* annotated_id,

tests/notes/notes.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,3 +594,46 @@ void test_notes_notes__empty_iterate(void)
594594

595595
cl_git_fail(git_note_iterator_new(&iter, _repo, "refs/notes/commits"));
596596
}
597+
598+
void test_notes_notes__iterate_from_commit(void)
599+
{
600+
git_note_iterator *iter;
601+
git_note *note;
602+
git_oid note_id, annotated_id;
603+
git_oid oids[2];
604+
git_oid notes_commit_oids[2];
605+
git_commit *notes_commits[2];
606+
const char* note_message[] = {
607+
"I decorate a65f\n",
608+
"I decorate c478\n"
609+
};
610+
int i, err;
611+
612+
cl_git_pass(git_oid_fromstr(&(oids[0]), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"));
613+
cl_git_pass(git_oid_fromstr(&(oids[1]), "c47800c7266a2be04c571c04d5a6614691ea99bd"));
614+
615+
cl_git_pass(git_note_commit_create(&notes_commit_oids[0], NULL, _repo, NULL, _sig, _sig, &(oids[0]), note_message[0], 0));
616+
617+
git_commit_lookup(&notes_commits[0], _repo, &notes_commit_oids[0]);
618+
cl_assert(notes_commits[0]);
619+
620+
cl_git_pass(git_note_commit_create(&notes_commit_oids[1], NULL, _repo, notes_commits[0], _sig, _sig, &(oids[1]), note_message[1], 0));
621+
622+
git_commit_lookup(&notes_commits[1], _repo, &notes_commit_oids[1]);
623+
cl_assert(notes_commits[1]);
624+
625+
cl_git_pass(git_note_commit_iterator_new(&iter, notes_commits[1]));
626+
627+
for (i = 0; (err = git_note_next(&note_id, &annotated_id, iter)) >= 0; ++i) {
628+
cl_git_pass(git_note_commit_read(&note, _repo, notes_commits[1], &annotated_id));
629+
cl_assert_equal_s(git_note_message(note), note_message[i]);
630+
git_note_free(note);
631+
}
632+
633+
cl_assert_equal_i(GIT_ITEROVER, err);
634+
cl_assert_equal_i(2, i);
635+
636+
git_note_iterator_free(iter);
637+
git_commit_free(notes_commits[0]);
638+
git_commit_free(notes_commits[1]);
639+
}

0 commit comments

Comments
 (0)