Navigation Menu

Skip to content

Commit

Permalink
notes: make git_note_foreach() callback signature easier to cope with…
Browse files Browse the repository at this point in the history
… from a binding perspective
  • Loading branch information
nulltoken committed May 16, 2012
1 parent 9d0011f commit ee7680d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
13 changes: 12 additions & 1 deletion include/git2/notes.h
Expand Up @@ -102,6 +102,17 @@ GIT_EXTERN(void) git_note_free(git_note *note);
*/
GIT_EXTERN(int) git_note_default_ref(const char **out, git_repository *repo);

/**
* Basic components of a note
*
* - Oid of the blob containing the message
* - Oid of the git object being annotated
*/
typedef struct {
git_oid blob_oid;
git_oid annotated_object_oid;
} git_note_data;

/**
* Loop over all the notes within a specified namespace
* and issue a callback for each one.
Expand All @@ -119,7 +130,7 @@ GIT_EXTERN(int) git_note_default_ref(const char **out, git_repository *repo);
GIT_EXTERN(int) git_note_foreach(
git_repository *repo,
const char *notes_ref,
int (*note_cb)(const git_oid *note_oid, const git_oid *annotated_object_oid, void *payload),
int (*note_cb)(git_note_data *note_data, void *payload),
void *payload
);

Expand Down
16 changes: 9 additions & 7 deletions src/notes.c
Expand Up @@ -451,13 +451,13 @@ void git_note_free(git_note *note)

static int process_entry_path(
const char* entry_path,
git_oid note_oid,
int (*note_cb)(const git_oid *note_oid, const git_oid *annotated_object_oid, void *payload),
const git_oid *note_oid,
int (*note_cb)(git_note_data *note_data, void *payload),
void *payload)
{
int i = 0, j = 0, error = -1, len;
git_oid target_oid;
git_buf buf = GIT_BUF_INIT;
git_note_data note_data;

if (git_buf_puts(&buf, entry_path) < 0)
goto cleanup;
Expand Down Expand Up @@ -492,10 +492,12 @@ static int process_entry_path(
goto cleanup;
}

if (git_oid_fromstr(&target_oid, buf.ptr) < 0)
if (git_oid_fromstr(&note_data.annotated_object_oid, buf.ptr) < 0)
return -1;

error = note_cb(&note_oid, &target_oid, payload);
git_oid_cpy(&note_data.blob_oid, note_oid);

error = note_cb(&note_data, payload);

cleanup:
git_buf_free(&buf);
Expand All @@ -505,7 +507,7 @@ static int process_entry_path(
int git_note_foreach(
git_repository *repo,
const char *notes_ref,
int (*note_cb)(const git_oid *note_oid, const git_oid *annotated_object_oid, void *payload),
int (*note_cb)(git_note_data *note_data, void *payload),
void *payload)
{
int error = -1;
Expand All @@ -530,7 +532,7 @@ int git_note_foreach(
goto cleanup;

while (item) {
if (process_entry_path(item->path, item->oid, note_cb, payload) < 0)
if (process_entry_path(item->path, &item->oid, note_cb, payload) < 0)
goto cleanup;

if (git_iterator_advance(iter, &item) < 0)
Expand Down
6 changes: 3 additions & 3 deletions tests-clar/notes/notes.c
Expand Up @@ -68,7 +68,7 @@ static struct {

#define EXPECTATIONS_COUNT (sizeof(list_expectations)/sizeof(list_expectations[0])) - 1

static int note_list_cb(const git_oid *note_oid, const git_oid *annotated_object_oid, void *payload)
static int note_list_cb(git_note_data *note_data, void *payload)
{
git_oid expected_note_oid, expected_target_oid;

Expand All @@ -77,10 +77,10 @@ static int note_list_cb(const git_oid *note_oid, const git_oid *annotated_object
cl_assert(*count < EXPECTATIONS_COUNT);

cl_git_pass(git_oid_fromstr(&expected_note_oid, list_expectations[*count].note_sha));
cl_assert(git_oid_cmp(&expected_note_oid, note_oid) == 0);
cl_assert(git_oid_cmp(&expected_note_oid, &note_data->blob_oid) == 0);

cl_git_pass(git_oid_fromstr(&expected_target_oid, list_expectations[*count].annotated_object_sha));
cl_assert(git_oid_cmp(&expected_target_oid, annotated_object_oid) == 0);
cl_assert(git_oid_cmp(&expected_target_oid, &note_data->annotated_object_oid) == 0);

(*count)++;

Expand Down

0 comments on commit ee7680d

Please sign in to comment.