Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f7b1850

Browse files
Nico von GeysoNico von Geyso
authored andcommitted
fixed minor issues with new note iterator
* fixed style issues * use new iterator functions for git_note_foreach()
1 parent 1a90dcf commit f7b1850

File tree

4 files changed

+49
-62
lines changed

4 files changed

+49
-62
lines changed

include/git2/notes.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,17 @@ GIT_EXTERN(int) git_note_iterator_new(
5959
GIT_EXTERN(void) git_note_iterator_free(git_note_iterator *it);
6060

6161
/**
62-
* Next iteration step for note iteration
62+
* Returns the current item (note_id and annotated_id) and advance the iterator
63+
* internally to the next value
6364
*
6465
* The notes must not be freed manually by the user.
6566
*
6667
* @param it pointer to the iterator
6768
* @param note_id id of blob containing the message
6869
* @param annotated_id id of the git object being annotated
6970
*
70-
* @return 0, GIT_ITEROVER or an error code
71+
* @return 0 (no error), GIT_ITEROVER (iteration is done) or an error code
72+
* (negative value)
7173
*/
7274
GIT_EXTERN(int) git_note_next(
7375
git_oid* note_id,

src/notes.c

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,7 @@ void git_note_free(git_note *note)
531531

532532
static int process_entry_path(
533533
const char* entry_path,
534-
git_oid *annotated_object_id
535-
)
534+
git_oid *annotated_object_id)
536535
{
537536
int error = -1;
538537
size_t i = 0, j = 0, len;
@@ -569,49 +568,41 @@ static int process_entry_path(
569568
goto cleanup;
570569
}
571570

572-
if ((error = git_oid_fromstr(annotated_object_id, buf.ptr)) < 0)
573-
goto cleanup;
571+
error = git_oid_fromstr(annotated_object_id, buf.ptr);
574572

575573
cleanup:
576574
git_buf_free(&buf);
577575
return error;
578576
}
579577

580578
int git_note_foreach(
581-
git_repository *repo,
582-
const char *notes_ref,
583-
git_note_foreach_cb note_cb,
584-
void *payload)
579+
git_repository *repo,
580+
const char *notes_ref,
581+
git_note_foreach_cb note_cb,
582+
void *payload)
585583
{
586-
int error;
587-
git_note_iterator *iter = NULL;
588-
git_tree *tree = NULL;
589-
git_commit *commit = NULL;
590-
git_oid annotated_object_id;
591-
const git_index_entry *item;
592-
593-
if (!(error = retrieve_note_tree_and_commit(
594-
&tree, &commit, repo, &notes_ref)) &&
595-
!(error = git_iterator_for_tree(&iter, tree)))
596-
error = git_iterator_current(iter, &item);
597-
598-
while (!error && item) {
599-
error = process_entry_path(item->path, &annotated_object_id);
584+
int error;
585+
git_note_iterator *iter = NULL;
586+
git_oid note_id, annotated_id;
600587

601-
if (note_cb(&item->oid, &annotated_object_id, payload))
602-
error = GIT_EUSER;
588+
if ((error = git_note_iterator_new(&iter, repo, notes_ref)) < 0)
589+
return error;
603590

604-
if (!error)
605-
error = git_iterator_advance(iter, &item);
606-
}
591+
while (!(error = git_note_next(&note_id, &annotated_id, iter))) {
592+
if (note_cb(&note_id, &annotated_id, payload)) {
593+
error = GIT_EUSER;
594+
break;
595+
}
596+
}
607597

608-
git_iterator_free(iter);
609-
git_tree_free(tree);
610-
git_commit_free(commit);
598+
if (error == GIT_ITEROVER)
599+
error = 0;
611600

612-
return error;
601+
git_note_iterator_free(iter);
602+
return error;
613603
}
614604

605+
615606
void git_note_iterator_free(git_note_iterator *it)
616607
{
617608
if (it == NULL)
@@ -624,23 +615,20 @@ void git_note_iterator_free(git_note_iterator *it)
624615
int git_note_iterator_new(
625616
git_note_iterator **it,
626617
git_repository *repo,
627-
const char *notes_ref
628-
)
618+
const char *notes_ref)
629619
{
630620
int error;
631621
git_commit *commit = NULL;
632622
git_tree *tree = NULL;
633623

634624
error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref);
635-
if (!error) {
636-
*it = (git_note_iterator *)git__malloc(sizeof(git_iterator));
637-
GITERR_CHECK_ALLOC(*it);
625+
if (error < 0)
626+
goto cleanup;
638627

639-
error = git_iterator_for_tree(it, tree);
640-
if (error)
641-
git_iterator_free(*it);
642-
}
628+
if ((error = git_iterator_for_tree(it, tree)) < 0)
629+
git_iterator_free(*it);
643630

631+
cleanup:
644632
git_tree_free(tree);
645633
git_commit_free(commit);
646634

@@ -650,24 +638,24 @@ int git_note_iterator_new(
650638
int git_note_next(
651639
git_oid* note_id,
652640
git_oid* annotated_id,
653-
git_note_iterator *it
654-
)
641+
git_note_iterator *it)
655642
{
656643
int error;
657644
const git_index_entry *item;
658645

659-
error = git_iterator_current(it, &item);
660-
if (!error && item) {
661-
git_oid_cpy(note_id, &item->oid);
646+
if (error = git_iterator_current(it, &item) < 0)
647+
goto exit;
662648

649+
if (item != NULL) {
650+
git_oid_cpy(note_id, &item->oid);
663651
error = process_entry_path(item->path, annotated_id);
664652

665-
if (!error)
653+
if (error >= 0)
666654
error = git_iterator_advance(it, NULL);
667-
}
668-
669-
if (!error && !item)
655+
} else {
670656
error = GIT_ITEROVER;
657+
}
671658

659+
exit:
672660
return error;
673661
}

src/notes.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
#include "git2/oid.h"
1313
#include "git2/types.h"
1414

15-
#include "iterator.h"
16-
1715
#define GIT_NOTES_DEFAULT_REF "refs/notes/commits"
1816

1917
#define GIT_NOTES_DEFAULT_MSG_ADD \

tests-clar/notes/notes.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ static struct {
4141
const char *note_sha;
4242
const char *annotated_object_sha;
4343
}
44-
4544
list_expectations[] = {
4645
{ "1c73b1f51762155d357bcd1fd4f2c409ef80065b", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045" },
4746
{ "1c73b1f51762155d357bcd1fd4f2c409ef80065b", "9fd738e8f7967c078dceed8190330fc8648ee56a" },
@@ -330,7 +329,7 @@ void test_notes_notes__can_iterate_default_namespace(void)
330329
"I decorate a65f\n",
331330
"I decorate c478\n"
332331
};
333-
int i;
332+
int i, err;
334333

335334
create_note(&note_created[0], "refs/notes/commits",
336335
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750", note_message[0]);
@@ -339,13 +338,13 @@ void test_notes_notes__can_iterate_default_namespace(void)
339338

340339
cl_git_pass(git_note_iterator_new(&iter, _repo, NULL));
341340

342-
for (i = 0; git_note_next(&note_id, &annotated_id, iter) != GIT_ITEROVER; ++i)
343-
{
341+
for (i = 0; (err = git_note_next(&note_id, &annotated_id, iter)) >= 0; ++i) {
344342
cl_git_pass(git_note_read(&note, _repo, NULL, &annotated_id));
345343
cl_assert_equal_s(git_note_message(note), note_message[i]);
346344
}
347345

348-
cl_assert(i == 2);
346+
cl_assert_equal_i(GIT_ITEROVER, err);
347+
cl_assert_equal_i(2, i);
349348
git_note_iterator_free(iter);
350349
}
351350

@@ -359,7 +358,7 @@ void test_notes_notes__can_iterate_custom_namespace(void)
359358
"I decorate a65f\n",
360359
"I decorate c478\n"
361360
};
362-
int i;
361+
int i, err;
363362

364363
create_note(&note_created[0], "refs/notes/beer",
365364
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750", note_message[0]);
@@ -368,13 +367,13 @@ void test_notes_notes__can_iterate_custom_namespace(void)
368367

369368
cl_git_pass(git_note_iterator_new(&iter, _repo, "refs/notes/beer"));
370369

371-
for (i = 0; git_note_next(&note_id, &annotated_id, iter) != GIT_ITEROVER; ++i)
372-
{
370+
for (i = 0; (err = git_note_next(&note_id, &annotated_id, iter)) >= 0; ++i) {
373371
cl_git_pass(git_note_read(&note, _repo, "refs/notes/beer", &annotated_id));
374372
cl_assert_equal_s(git_note_message(note), note_message[i]);
375373
}
376374

377-
cl_assert(i == 2);
375+
cl_assert_equal_i(GIT_ITEROVER, err);
376+
cl_assert_equal_i(2, i);
378377
git_note_iterator_free(iter);
379378
}
380379

0 commit comments

Comments
 (0)