Skip to content

Commit 7096bf1

Browse files
committed
notes: Add git_note_commit_read
This also adds tests for this function.
1 parent a46e743 commit 7096bf1

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

include/git2/notes.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,25 @@ GIT_EXTERN(int) git_note_read(
9494
const char *notes_ref,
9595
const git_oid *oid);
9696

97+
98+
/**
99+
* Read the note for an object from a note commit
100+
*
101+
* The note must be freed manually by the user.
102+
*
103+
* @param out pointer to the read note; NULL in case of error
104+
* @param repo repository where to look up the note
105+
* @param notes_commit a pointer to the notes commit object
106+
* @param oid OID of the git object to read the note from
107+
*
108+
* @return 0 or an error code
109+
*/
110+
GIT_EXTERN(int) git_note_commit_read(
111+
git_note **out,
112+
git_repository *repo,
113+
git_commit *notes_commit,
114+
const git_oid *oid);
115+
97116
/**
98117
* Get the note author
99118
*

src/notes.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,28 @@ int git_note_read(git_note **out, git_repository *repo,
463463
return error;
464464
}
465465

466+
int git_note_commit_read(
467+
git_note **out,
468+
git_repository *repo,
469+
git_commit *notes_commit,
470+
const git_oid *oid)
471+
{
472+
int error;
473+
git_tree *tree = NULL;
474+
char target[GIT_OID_HEXSZ + 1];
475+
476+
git_oid_tostr(target, sizeof(target), oid);
477+
478+
if ((error = git_commit_tree(&tree, notes_commit)) < 0)
479+
goto cleanup;
480+
481+
error = note_lookup(out, repo, notes_commit, tree, target);
482+
483+
cleanup:
484+
git_tree_free(tree);
485+
return error;
486+
}
487+
466488
int git_note_create(
467489
git_oid *out,
468490
git_repository *repo,

tests/notes/notes.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,46 @@ static char *messages[] = {
375375

376376
#define MESSAGES_COUNT (sizeof(messages)/sizeof(messages[0])) - 1
377377

378+
/* Test that we can read a note */
379+
void test_notes_notes__can_read_a_note(void)
380+
{
381+
git_oid note_oid, target_oid;
382+
git_note *note;
383+
384+
create_note(&note_oid, "refs/notes/i-can-see-dead-notes", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", "I decorate 4a20\n");
385+
386+
cl_git_pass(git_oid_fromstr(&target_oid, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"));
387+
388+
cl_git_pass(git_note_read(&note, _repo, "refs/notes/i-can-see-dead-notes", &target_oid));
389+
390+
cl_assert_equal_s(git_note_message(note), "I decorate 4a20\n");
391+
392+
git_note_free(note);
393+
}
394+
395+
/* Test that we can read a note with from commit api */
396+
void test_notes_notes__can_read_a_note_from_a_commit(void)
397+
{
398+
git_oid oid, notes_commit_oid;
399+
git_commit *notes_commit;
400+
git_note *note;
401+
402+
cl_git_pass(git_oid_fromstr(&oid, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"));
403+
404+
cl_git_pass(git_note_commit_create(&notes_commit_oid, NULL, _repo, NULL, _sig, _sig, &oid, "I decorate 4a20\n", 1));
405+
406+
git_commit_lookup(&notes_commit, _repo, &notes_commit_oid);
407+
408+
cl_assert(notes_commit);
409+
410+
cl_git_pass(git_note_commit_read(&note, _repo, notes_commit, &oid));
411+
412+
cl_assert_equal_s(git_note_message(note), "I decorate 4a20\n");
413+
414+
git_commit_free(notes_commit);
415+
git_note_free(note);
416+
}
417+
378418
/*
379419
* $ git ls-tree refs/notes/fanout
380420
* 040000 tree 4b22b35d44b5a4f589edf3dc89196399771796ea 84

0 commit comments

Comments
 (0)