@matheusbsbs / matheus-bagliano / commits / 1ee1e43df3
Fork 0

notes: Don't create (empty) commit when removing non-existing notes

Extend remove_note() in the notes API to return whether or not a note was actually removed. Use this in 'git notes remove' to skip the creation of a notes commit when no notes were actually removed. Also add a test illustrating the change in behavior. Signed-off-by: Johan Herland <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johan Herland committed Aug 31, 2010 at 17:56 UTC 1ee1e43df37e53b0bc50a0eda57dd1772dc220f5
4 files changed +30 -9
builtin/notes.c
+10 -4
@@ -769,6 +769,7 @@ static int remove_cmd(int argc, const char **argv, const char *prefix)
769 const char *object_ref;
770 struct notes_tree *t;
771 unsigned char object[20];
772 + int retval;
773
774 argc = parse_options(argc, argv, prefix, options,
775 git_notes_remove_usage, 0);
@@ -785,12 +786,17 @@ static int remove_cmd(int argc, const char **argv, const char *prefix)
786
787 t = init_notes_check("remove");
788
788 - fprintf(stderr, "Removing note for object %s\n", sha1_to_hex(object));
789 - remove_note(t, object);
789 + retval = remove_note(t, object);
790 + if (retval)
791 + fprintf(stderr, "Object %s has no note\n", sha1_to_hex(object));
792 + else {
793 + fprintf(stderr, "Removing note for object %s\n",
794 + sha1_to_hex(object));
795
791 - commit_notes(t, "Notes removed by 'git notes remove'");
796 + commit_notes(t, "Notes removed by 'git notes remove'");
797 + }
798 free_notes(t);
793 - return 0;
799 + return retval;
800 }
801
802 static int prune(int argc, const char **argv, const char *prefix)
notes.c
+10 -4
@@ -263,11 +263,13 @@ static int note_tree_consolidate(struct int_node *tree,
263 * To remove a leaf_node:
264 * Search to the tree location appropriate for the given leaf_node's key:
265 * - If location does not hold a matching entry, abort and do nothing.
266 + * - Copy the matching entry's value into the given entry.
267 * - Replace the matching leaf_node with a NULL entry (and free the leaf_node).
268 * - Consolidate int_nodes repeatedly, while walking up the tree towards root.
269 */
269 -static void note_tree_remove(struct notes_tree *t, struct int_node *tree,
270 - unsigned char n, struct leaf_node *entry)
270 +static void note_tree_remove(struct notes_tree *t,
271 + struct int_node *tree, unsigned char n,
272 + struct leaf_node *entry)
273 {
274 struct leaf_node *l;
275 struct int_node *parent_stack[20];
@@ -282,6 +284,7 @@ static void note_tree_remove(struct notes_tree *t, struct int_node *tree,
284 return; /* key mismatch, nothing to remove */
285
286 /* we have found a matching entry */
287 + hashcpy(entry->val_sha1, l->val_sha1);
288 free(l);
289 *p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL);
290
@@ -1003,17 +1006,20 @@ void add_note(struct notes_tree *t, const unsigned char *object_sha1,
1006 note_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes);
1007 }
1008
1006 -void remove_note(struct notes_tree *t, const unsigned char *object_sha1)
1009 +int remove_note(struct notes_tree *t, const unsigned char *object_sha1)
1010 {
1011 struct leaf_node l;
1012
1013 if (!t)
1014 t = &default_notes_tree;
1015 assert(t->initialized);
1013 - t->dirty = 1;
1016 hashcpy(l.key_sha1, object_sha1);
1017 hashclr(l.val_sha1);
1018 note_tree_remove(t, t->root, 0, &l);
1019 + if (is_null_sha1(l.val_sha1)) // no note was removed
1020 + return 1;
1021 + t->dirty = 1;
1022 + return 0;
1023 }
1024
1025 const unsigned char *get_note(struct notes_tree *t,
notes.h
+3 -1
@@ -89,8 +89,10 @@ void add_note(struct notes_tree *t, const unsigned char *object_sha1,
89 * IMPORTANT: The changes made by remove_note() to the given notes_tree
90 * structure are not persistent until a subsequent call to write_notes_tree()
91 * returns zero.
92 + *
93 + * Return 0 if a note was removed; 1 if there was no note to remove.
94 */
93 -void remove_note(struct notes_tree *t, const unsigned char *object_sha1);
95 +int remove_note(struct notes_tree *t, const unsigned char *object_sha1);
96
97 /*
98 * Get the note object SHA1 containing the note data for the given object
t/t3301-notes.sh
+7
@@ -365,6 +365,13 @@ c18dc024e14f08d18d14eea0d747ff692d66d6a3 1584215f1d29c65e99c6c6848626553fdd07fd7
365 c9c6af7f78bc47490dbf3e822cf2f3c24d4b9061 268048bfb8a1fb38e703baceb8ab235421bf80c5
366 EOF
367
368 +test_expect_success 'removing non-existing note should not create new commit' '
369 + git rev-parse --verify refs/notes/commits > before_commit &&
370 + test_must_fail git notes remove HEAD^ &&
371 + git rev-parse --verify refs/notes/commits > after_commit &&
372 + test_cmp before_commit after_commit
373 +'
374 +
375 test_expect_success 'list notes with "git notes list"' '
376 git notes list > output &&
377 test_cmp expect output