Merge pull request #4159 from richardipsum/notes-commit Support using notes via a commit rather than a ref
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
diff --git a/include/git2/notes.h b/include/git2/notes.h
index 3a626ca..853e5de 100644
--- a/include/git2/notes.h
+++ b/include/git2/notes.h
@@ -52,6 +52,20 @@ GIT_EXTERN(int) git_note_iterator_new(
const char *notes_ref);
/**
+ * Creates a new iterator for notes from a commit
+ *
+ * The iterator must be freed manually by the user.
+ *
+ * @param out pointer to the iterator
+ * @param notes_commit a pointer to the notes commit object
+ *
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_note_commit_iterator_new(
+ git_note_iterator **out,
+ git_commit *notes_commit);
+
+/**
* Frees an git_note_iterator
*
* @param it pointer to the iterator
@@ -94,6 +108,25 @@ GIT_EXTERN(int) git_note_read(
const char *notes_ref,
const git_oid *oid);
+
+/**
+ * Read the note for an object from a note commit
+ *
+ * The note must be freed manually by the user.
+ *
+ * @param out pointer to the read note; NULL in case of error
+ * @param repo repository where to look up the note
+ * @param notes_commit a pointer to the notes commit object
+ * @param oid OID of the git object to read the note from
+ *
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_note_commit_read(
+ git_note **out,
+ git_repository *repo,
+ git_commit *notes_commit,
+ const git_oid *oid);
+
/**
* Get the note author
*
@@ -153,6 +186,36 @@ GIT_EXTERN(int) git_note_create(
const char *note,
int force);
+/**
+ * Add a note for an object from a commit
+ *
+ * This function will create a notes commit for a given object,
+ * the commit is a dangling commit, no reference is created.
+ *
+ * @param notes_commit_out pointer to store the commit (optional);
+ * NULL in case of error
+ * @param notes_blob_out a point to the id of a note blob (optional)
+ * @param repo repository where the note will live
+ * @param parent Pointer to parent note
+ * or NULL if this shall start a new notes tree
+ * @param author signature of the notes commit author
+ * @param committer signature of the notes commit committer
+ * @param oid OID of the git object to decorate
+ * @param note Content of the note to add for object oid
+ * @param allow_note_overwrite Overwrite existing note
+ *
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_note_commit_create(
+ git_oid *notes_commit_out,
+ git_oid *notes_blob_out,
+ git_repository *repo,
+ git_commit *parent,
+ const git_signature *author,
+ const git_signature *committer,
+ const git_oid *oid,
+ const char *note,
+ int allow_note_overwrite);
/**
* Remove the note for an object
@@ -174,6 +237,32 @@ GIT_EXTERN(int) git_note_remove(
const git_oid *oid);
/**
+ * Remove the note for an object
+ *
+ * @param notes_commit_out pointer to store the new notes commit (optional);
+ * NULL in case of error.
+ * When removing a note a new tree containing all notes
+ * sans the note to be removed is created and a new commit
+ * pointing to that tree is also created.
+ * In the case where the resulting tree is an empty tree
+ * a new commit pointing to this empty tree will be returned.
+ * @param repo repository where the note lives
+ * @param notes_commit a pointer to the notes commit object
+ * @param author signature of the notes commit author
+ * @param committer signature of the notes commit committer
+ * @param oid OID of the git object to remove the note from
+ *
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_note_commit_remove(
+ git_oid *notes_commit_out,
+ git_repository *repo,
+ git_commit *notes_commit,
+ const git_signature *author,
+ const git_signature *committer,
+ const git_oid *oid);
+
+/**
* Free a git_note object
*
* @param note git_note object
diff --git a/src/notes.c b/src/notes.c
index 75108b9..f63ef36 100644
--- a/src/notes.c
+++ b/src/notes.c
@@ -268,7 +268,9 @@ static int insert_note_in_tree_enotfound_cb(git_tree **out,
GIT_FILEMODE_BLOB);
}
-static int note_write(git_oid *out,
+static int note_write(
+ git_oid *notes_commit_out,
+ git_oid *notes_blob_out,
git_repository *repo,
const git_signature *author,
const git_signature *committer,
@@ -294,13 +296,17 @@ static int note_write(git_oid *out,
insert_note_in_tree_enotfound_cb)) < 0)
goto cleanup;
- if (out)
- git_oid_cpy(out, &oid);
+ if (notes_blob_out)
+ git_oid_cpy(notes_blob_out, &oid);
+
error = git_commit_create(&oid, repo, notes_ref, author, committer,
NULL, GIT_NOTES_DEFAULT_MSG_ADD,
tree, *parents == NULL ? 0 : 1, (const git_commit **) parents);
+ if (notes_commit_out)
+ git_oid_cpy(notes_commit_out, &oid);
+
cleanup:
git_tree_free(tree);
return error;
@@ -363,7 +369,9 @@ cleanup:
return error;
}
-static int note_remove(git_repository *repo,
+static int note_remove(
+ git_oid *notes_commit_out,
+ git_repository *repo,
const git_signature *author, const git_signature *committer,
const char *notes_ref, git_tree *tree,
const char *target, git_commit **parents)
@@ -383,6 +391,12 @@ static int note_remove(git_repository *repo,
*parents == NULL ? 0 : 1,
(const git_commit **) parents);
+ if (error < 0)
+ goto cleanup;
+
+ if (notes_commit_out)
+ git_oid_cpy(notes_commit_out, &oid);
+
cleanup:
git_tree_free(tree_after_removal);
return error;
@@ -410,8 +424,7 @@ static int normalize_namespace(char **out, git_repository *repo, const char *not
return note_get_default_ref(out, repo);
}
-static int retrieve_note_tree_and_commit(
- git_tree **tree_out,
+static int retrieve_note_commit(
git_commit **commit_out,
char **notes_ref_out,
git_repository *repo,
@@ -429,34 +442,82 @@ static int retrieve_note_tree_and_commit(
if (git_commit_lookup(commit_out, repo, &oid) < 0)
return error;
- if ((error = git_commit_tree(tree_out, *commit_out)) < 0)
- return error;
-
return 0;
}
+int git_note_commit_read(
+ git_note **out,
+ git_repository *repo,
+ git_commit *notes_commit,
+ const git_oid *oid)
+{
+ int error;
+ git_tree *tree = NULL;
+ char target[GIT_OID_HEXSZ + 1];
+
+ git_oid_tostr(target, sizeof(target), oid);
+
+ if ((error = git_commit_tree(&tree, notes_commit)) < 0)
+ goto cleanup;
+
+ error = note_lookup(out, repo, notes_commit, tree, target);
+
+cleanup:
+ git_tree_free(tree);
+ return error;
+}
+
int git_note_read(git_note **out, git_repository *repo,
const char *notes_ref_in, const git_oid *oid)
{
int error;
- char *target = NULL, *notes_ref = NULL;
- git_tree *tree = NULL;
+ char *notes_ref = NULL;
git_commit *commit = NULL;
- target = git_oid_allocfmt(oid);
- GITERR_CHECK_ALLOC(target);
+ error = retrieve_note_commit(&commit, ¬es_ref, repo, notes_ref_in);
- if (!(error = retrieve_note_tree_and_commit(
- &tree, &commit, ¬es_ref, repo, notes_ref_in)))
- error = note_lookup(out, repo, commit, tree, target);
+ if (error < 0)
+ goto cleanup;
+ error = git_note_commit_read(out, repo, commit, oid);
+
+cleanup:
git__free(notes_ref);
- git__free(target);
- git_tree_free(tree);
git_commit_free(commit);
return error;
}
+int git_note_commit_create(
+ git_oid *notes_commit_out,
+ git_oid *notes_blob_out,
+ git_repository *repo,
+ git_commit *parent,
+ const git_signature *author,
+ const git_signature *committer,
+ const git_oid *oid,
+ const char *note,
+ int allow_note_overwrite)
+{
+ int error;
+ git_tree *tree = NULL;
+ char target[GIT_OID_HEXSZ + 1];
+
+ git_oid_tostr(target, sizeof(target), oid);
+
+ if (parent != NULL && (error = git_commit_tree(&tree, parent)) < 0)
+ goto cleanup;
+
+ error = note_write(notes_commit_out, notes_blob_out, repo, author,
+ committer, NULL, note, tree, target, &parent, allow_note_overwrite);
+
+ if (error < 0)
+ goto cleanup;
+
+cleanup:
+ git_tree_free(tree);
+ return error;
+}
+
int git_note_create(
git_oid *out,
git_repository *repo,
@@ -468,25 +529,59 @@ int git_note_create(
int allow_note_overwrite)
{
int error;
- char *target = NULL, *notes_ref = NULL;
- git_commit *commit = NULL;
- git_tree *tree = NULL;
-
- target = git_oid_allocfmt(oid);
- GITERR_CHECK_ALLOC(target);
+ char *notes_ref = NULL;
+ git_commit *existing_notes_commit = NULL;
+ git_reference *ref = NULL;
+ git_oid notes_blob_oid, notes_commit_oid;
- error = retrieve_note_tree_and_commit(&tree, &commit, ¬es_ref, repo, notes_ref_in);
+ error = retrieve_note_commit(&existing_notes_commit, ¬es_ref,
+ repo, notes_ref_in);
if (error < 0 && error != GIT_ENOTFOUND)
goto cleanup;
- error = note_write(out, repo, author, committer, notes_ref,
- note, tree, target, &commit, allow_note_overwrite);
+ error = git_note_commit_create(¬es_commit_oid,
+ ¬es_blob_oid,
+ repo, existing_notes_commit, author,
+ committer, oid, note,
+ allow_note_overwrite);
+ if (error < 0)
+ goto cleanup;
+
+ error = git_reference_create(&ref, repo, notes_ref,
+ ¬es_commit_oid, 1, NULL);
+
+ if (out != NULL)
+ git_oid_cpy(out, ¬es_blob_oid);
cleanup:
git__free(notes_ref);
- git__free(target);
- git_commit_free(commit);
+ git_commit_free(existing_notes_commit);
+ git_reference_free(ref);
+ return error;
+}
+
+int git_note_commit_remove(
+ git_oid *notes_commit_out,
+ git_repository *repo,
+ git_commit *notes_commit,
+ const git_signature *author,
+ const git_signature *committer,
+ const git_oid *oid)
+{
+ int error;
+ git_tree *tree = NULL;
+ char target[GIT_OID_HEXSZ + 1];
+
+ git_oid_tostr(target, sizeof(target), oid);
+
+ if ((error = git_commit_tree(&tree, notes_commit)) < 0)
+ goto cleanup;
+
+ error = note_remove(notes_commit_out,
+ repo, author, committer, NULL, tree, target, ¬es_commit);
+
+cleanup:
git_tree_free(tree);
return error;
}
@@ -496,22 +591,29 @@ int git_note_remove(git_repository *repo, const char *notes_ref_in,
const git_oid *oid)
{
int error;
- char *target = NULL, *notes_ref;
- git_commit *commit = NULL;
- git_tree *tree = NULL;
+ char *notes_ref_target = NULL;
+ git_commit *existing_notes_commit = NULL;
+ git_oid new_notes_commit;
+ git_reference *notes_ref = NULL;
- target = git_oid_allocfmt(oid);
- GITERR_CHECK_ALLOC(target);
+ error = retrieve_note_commit(&existing_notes_commit, ¬es_ref_target,
+ repo, notes_ref_in);
- if (!(error = retrieve_note_tree_and_commit(
- &tree, &commit, ¬es_ref, repo, notes_ref_in)))
- error = note_remove(
- repo, author, committer, notes_ref, tree, target, &commit);
+ if (error < 0)
+ goto cleanup;
- git__free(notes_ref);
- git__free(target);
- git_commit_free(commit);
- git_tree_free(tree);
+ error = git_note_commit_remove(&new_notes_commit, repo,
+ existing_notes_commit, author, committer, oid);
+ if (error < 0)
+ goto cleanup;
+
+ error = git_reference_create(¬es_ref, repo, notes_ref_target,
+ &new_notes_commit, 1, NULL);
+
+cleanup:
+ git__free(notes_ref_target);
+ git_reference_free(notes_ref);
+ git_commit_free(existing_notes_commit);
return error;
}
@@ -639,7 +741,6 @@ int git_note_foreach(
return error;
}
-
void git_note_iterator_free(git_note_iterator *it)
{
if (it == NULL)
@@ -648,6 +749,24 @@ void git_note_iterator_free(git_note_iterator *it)
git_iterator_free(it);
}
+int git_note_commit_iterator_new(
+ git_note_iterator **it,
+ git_commit *notes_commit)
+{
+ int error;
+ git_tree *tree;
+
+ if ((error = git_commit_tree(&tree, notes_commit)) < 0)
+ goto cleanup;
+
+ if ((error = git_iterator_for_tree(it, tree, NULL)) < 0)
+ git_iterator_free(*it);
+
+cleanup:
+ git_tree_free(tree);
+
+ return error;
+}
int git_note_iterator_new(
git_note_iterator **it,
@@ -656,19 +775,16 @@ int git_note_iterator_new(
{
int error;
git_commit *commit = NULL;
- git_tree *tree = NULL;
char *notes_ref;
- error = retrieve_note_tree_and_commit(&tree, &commit, ¬es_ref, repo, notes_ref_in);
+ error = retrieve_note_commit(&commit, ¬es_ref, repo, notes_ref_in);
if (error < 0)
goto cleanup;
- if ((error = git_iterator_for_tree(it, tree, NULL)) < 0)
- git_iterator_free(*it);
+ error = git_note_commit_iterator_new(it, commit);
cleanup:
git__free(notes_ref);
- git_tree_free(tree);
git_commit_free(commit);
return error;
diff --git a/tests/notes/notes.c b/tests/notes/notes.c
index a91bf5b..9626f53 100644
--- a/tests/notes/notes.c
+++ b/tests/notes/notes.c
@@ -73,6 +73,128 @@ static int note_list_cb(
return 0;
}
+struct note_create_payload {
+ const char *note_oid;
+ const char *object_oid;
+ unsigned seen;
+};
+
+static int note_list_create_cb(
+ const git_oid *blob_oid, const git_oid *annotated_obj_id, void *payload)
+{
+ git_oid expected_note_oid, expected_target_oid;
+ struct note_create_payload *notes = payload;
+ size_t i;
+
+ for (i = 0; notes[i].note_oid != NULL; i++) {
+ cl_git_pass(git_oid_fromstr(&expected_note_oid, notes[i].note_oid));
+
+ if (git_oid_cmp(&expected_note_oid, blob_oid) != 0)
+ continue;
+
+ cl_git_pass(git_oid_fromstr(&expected_target_oid, notes[i].object_oid));
+
+ if (git_oid_cmp(&expected_target_oid, annotated_obj_id) != 0)
+ continue;
+
+ notes[i].seen = 1;
+ return 0;
+ }
+
+ cl_fail("Did not see expected note");
+ return 0;
+}
+
+void assert_notes_seen(struct note_create_payload payload[], size_t n)
+{
+ size_t seen = 0, i;
+
+ for (i = 0; payload[i].note_oid != NULL; i++) {
+ if (payload[i].seen)
+ seen++;
+ }
+
+ cl_assert_equal_i(seen, n);
+}
+
+void test_notes_notes__can_create_a_note(void)
+{
+ git_oid note_oid;
+ static struct note_create_payload can_create_a_note[] = {
+ { "1c9b1bc36730582a42d56eeee0dc58673d7ae869", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", 0 },
+ { NULL, NULL, 0 }
+ };
+
+ create_note(¬e_oid, "refs/notes/i-can-see-dead-notes", can_create_a_note[0].object_oid, "I decorate 4a20\n");
+
+ cl_git_pass(git_note_foreach(_repo, "refs/notes/i-can-see-dead-notes", note_list_create_cb, &can_create_a_note));
+
+ assert_notes_seen(can_create_a_note, 1);
+}
+
+void test_notes_notes__can_create_a_note_from_commit(void)
+{
+ git_oid oid;
+ git_oid notes_commit_out;
+ git_reference *ref;
+ static struct note_create_payload can_create_a_note_from_commit[] = {
+ { "1c9b1bc36730582a42d56eeee0dc58673d7ae869", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", 0 },
+ { NULL, NULL, 0 }
+ };
+
+ cl_git_pass(git_oid_fromstr(&oid, can_create_a_note_from_commit[0].object_oid));
+
+ cl_git_pass(git_note_commit_create(¬es_commit_out, NULL, _repo, NULL, _sig, _sig, &oid, "I decorate 4a20\n", 1));
+
+ /* create_from_commit will not update any ref,
+ * so we must manually create the ref, that points to the commit */
+ cl_git_pass(git_reference_create(&ref, _repo, "refs/notes/i-can-see-dead-notes", ¬es_commit_out, 0, NULL));
+
+ cl_git_pass(git_note_foreach(_repo, "refs/notes/i-can-see-dead-notes", note_list_create_cb, &can_create_a_note_from_commit));
+
+ assert_notes_seen(can_create_a_note_from_commit, 1);
+
+ git_reference_free(ref);
+}
+
+
+/* Test that we can create a note from a commit, given an existing commit */
+void test_notes_notes__can_create_a_note_from_commit_given_an_existing_commit(void)
+{
+ git_oid oid;
+ git_oid notes_commit_out;
+ git_commit *existing_notes_commit = NULL;
+ git_reference *ref;
+ static struct note_create_payload can_create_a_note_from_commit_given_an_existing_commit[] = {
+ { "1c9b1bc36730582a42d56eeee0dc58673d7ae869", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", 0 },
+ { "1aaf94147c21f981e0a20bf57b89137c5a6aae52", "9fd738e8f7967c078dceed8190330fc8648ee56a", 0 },
+ { NULL, NULL, 0 }
+ };
+
+ cl_git_pass(git_oid_fromstr(&oid, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"));
+
+ cl_git_pass(git_note_commit_create(¬es_commit_out, NULL, _repo, NULL, _sig, _sig, &oid, "I decorate 4a20\n", 0));
+
+ cl_git_pass(git_oid_fromstr(&oid, "9fd738e8f7967c078dceed8190330fc8648ee56a"));
+
+ git_commit_lookup(&existing_notes_commit, _repo, ¬es_commit_out);
+
+ cl_assert(existing_notes_commit);
+
+ cl_git_pass(git_note_commit_create(¬es_commit_out, NULL, _repo, existing_notes_commit, _sig, _sig, &oid, "I decorate 9fd7\n", 0));
+
+ /* create_from_commit will not update any ref,
+ * so we must manually create the ref, that points to the commit */
+ cl_git_pass(git_reference_create(&ref, _repo, "refs/notes/i-can-see-dead-notes", ¬es_commit_out, 0, NULL));
+
+ cl_git_pass(git_note_foreach(_repo, "refs/notes/i-can-see-dead-notes", note_list_create_cb, &can_create_a_note_from_commit_given_an_existing_commit));
+
+ assert_notes_seen(can_create_a_note_from_commit_given_an_existing_commit, 2);
+
+ git_commit_free(existing_notes_commit);
+ git_reference_free(ref);
+}
+
/*
* $ git notes --ref i-can-see-dead-notes add -m "I decorate a65f" a65fedf39aefe402d3bb6e24df4d4f5fe4547750
* $ git notes --ref i-can-see-dead-notes add -m "I decorate c478" c47800c7266a2be04c571c04d5a6614691ea99bd
@@ -253,6 +375,71 @@ static char *messages[] = {
#define MESSAGES_COUNT (sizeof(messages)/sizeof(messages[0])) - 1
+/* Test that we can read a note */
+void test_notes_notes__can_read_a_note(void)
+{
+ git_oid note_oid, target_oid;
+ git_note *note;
+
+ create_note(¬e_oid, "refs/notes/i-can-see-dead-notes", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", "I decorate 4a20\n");
+
+ cl_git_pass(git_oid_fromstr(&target_oid, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"));
+
+ cl_git_pass(git_note_read(¬e, _repo, "refs/notes/i-can-see-dead-notes", &target_oid));
+
+ cl_assert_equal_s(git_note_message(note), "I decorate 4a20\n");
+
+ git_note_free(note);
+}
+
+/* Test that we can read a note with from commit api */
+void test_notes_notes__can_read_a_note_from_a_commit(void)
+{
+ git_oid oid, notes_commit_oid;
+ git_commit *notes_commit;
+ git_note *note;
+
+ cl_git_pass(git_oid_fromstr(&oid, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"));
+
+ cl_git_pass(git_note_commit_create(¬es_commit_oid, NULL, _repo, NULL, _sig, _sig, &oid, "I decorate 4a20\n", 1));
+
+ git_commit_lookup(¬es_commit, _repo, ¬es_commit_oid);
+
+ cl_assert(notes_commit);
+
+ cl_git_pass(git_note_commit_read(¬e, _repo, notes_commit, &oid));
+
+ cl_assert_equal_s(git_note_message(note), "I decorate 4a20\n");
+
+ git_commit_free(notes_commit);
+ git_note_free(note);
+}
+
+/* Test that we can read a commit with no note fails */
+void test_notes_notes__attempt_to_read_a_note_from_a_commit_with_no_note_fails(void)
+{
+ git_oid oid, notes_commit_oid;
+ git_commit *notes_commit;
+ git_note *note;
+
+ cl_git_pass(git_oid_fromstr(&oid, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"));
+
+ cl_git_pass(git_note_commit_create(¬es_commit_oid, NULL, _repo, NULL, _sig, _sig, &oid, "I decorate 4a20\n", 1));
+
+ git_commit_lookup(¬es_commit, _repo, ¬es_commit_oid);
+
+ cl_git_pass(git_note_commit_remove(¬es_commit_oid, _repo, notes_commit, _sig, _sig, &oid));
+ git_commit_free(notes_commit);
+
+ git_commit_lookup(¬es_commit, _repo, ¬es_commit_oid);
+
+ cl_assert(notes_commit);
+
+ cl_git_fail_with(GIT_ENOTFOUND, git_note_commit_read(¬e, _repo, notes_commit, &oid));
+
+ git_commit_free(notes_commit);
+}
+
/*
* $ git ls-tree refs/notes/fanout
* 040000 tree 4b22b35d44b5a4f589edf3dc89196399771796ea 84
@@ -298,6 +485,50 @@ void test_notes_notes__can_read_a_note_in_an_existing_fanout(void)
git_note_free(note);
}
+/* Can remove a note */
+void test_notes_notes__can_remove_a_note(void)
+{
+ git_oid note_oid, target_oid;
+ git_note *note;
+
+ create_note(¬e_oid, "refs/notes/i-can-see-dead-notes", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", "I decorate 4a20\n");
+
+ cl_git_pass(git_oid_fromstr(&target_oid, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"));
+ cl_git_pass(git_note_remove(_repo, "refs/notes/i-can-see-dead-notes", _sig, _sig, &target_oid));
+
+ cl_git_fail(git_note_read(¬e, _repo, "refs/notes/i-can-see-dead-notes", &target_oid));
+}
+
+/* Can remove a note from a commit */
+void test_notes_notes__can_remove_a_note_from_commit(void)
+{
+ git_oid oid, notes_commit_oid;
+ git_note *note = NULL;
+ git_commit *existing_notes_commit;
+ git_reference *ref;
+
+ cl_git_pass(git_oid_fromstr(&oid, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"));
+
+ cl_git_pass(git_note_commit_create(¬es_commit_oid, NULL, _repo, NULL, _sig, _sig, &oid, "I decorate 4a20\n", 0));
+
+ git_commit_lookup(&existing_notes_commit, _repo, ¬es_commit_oid);
+
+ cl_assert(existing_notes_commit);
+
+ cl_git_pass(git_note_commit_remove(¬es_commit_oid, _repo, existing_notes_commit, _sig, _sig, &oid));
+
+ /* remove_from_commit will not update any ref,
+ * so we must manually create the ref, that points to the commit */
+ cl_git_pass(git_reference_create(&ref, _repo, "refs/notes/i-can-see-dead-notes", ¬es_commit_oid, 0, NULL));
+
+ cl_git_fail(git_note_read(¬e, _repo, "refs/notes/i-can-see-dead-notes", &oid));
+
+ git_commit_free(existing_notes_commit);
+ git_reference_free(ref);
+ git_note_free(note);
+}
+
+
void test_notes_notes__can_remove_a_note_in_an_existing_fanout(void)
{
git_oid target_oid;
@@ -388,3 +619,46 @@ void test_notes_notes__empty_iterate(void)
cl_git_fail(git_note_iterator_new(&iter, _repo, "refs/notes/commits"));
}
+
+void test_notes_notes__iterate_from_commit(void)
+{
+ git_note_iterator *iter;
+ git_note *note;
+ git_oid note_id, annotated_id;
+ git_oid oids[2];
+ git_oid notes_commit_oids[2];
+ git_commit *notes_commits[2];
+ const char* note_message[] = {
+ "I decorate a65f\n",
+ "I decorate c478\n"
+ };
+ int i, err;
+
+ cl_git_pass(git_oid_fromstr(&(oids[0]), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"));
+ cl_git_pass(git_oid_fromstr(&(oids[1]), "c47800c7266a2be04c571c04d5a6614691ea99bd"));
+
+ cl_git_pass(git_note_commit_create(¬es_commit_oids[0], NULL, _repo, NULL, _sig, _sig, &(oids[0]), note_message[0], 0));
+
+ git_commit_lookup(¬es_commits[0], _repo, ¬es_commit_oids[0]);
+ cl_assert(notes_commits[0]);
+
+ cl_git_pass(git_note_commit_create(¬es_commit_oids[1], NULL, _repo, notes_commits[0], _sig, _sig, &(oids[1]), note_message[1], 0));
+
+ git_commit_lookup(¬es_commits[1], _repo, ¬es_commit_oids[1]);
+ cl_assert(notes_commits[1]);
+
+ cl_git_pass(git_note_commit_iterator_new(&iter, notes_commits[1]));
+
+ for (i = 0; (err = git_note_next(¬e_id, &annotated_id, iter)) >= 0; ++i) {
+ cl_git_pass(git_note_commit_read(¬e, _repo, notes_commits[1], &annotated_id));
+ cl_assert_equal_s(git_note_message(note), note_message[i]);
+ git_note_free(note);
+ }
+
+ cl_assert_equal_i(GIT_ITEROVER, err);
+ cl_assert_equal_i(2, i);
+
+ git_note_iterator_free(iter);
+ git_commit_free(notes_commits[0]);
+ git_commit_free(notes_commits[1]);
+}