Skip to content

Commit 1ee1e43

Browse files
jherlandgitster
authored andcommitted
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>
1 parent d8a9480 commit 1ee1e43

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

builtin/notes.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,7 @@ static int remove_cmd(int argc, const char **argv, const char *prefix)
769769
const char *object_ref;
770770
struct notes_tree *t;
771771
unsigned char object[20];
772+
int retval;
772773

773774
argc = parse_options(argc, argv, prefix, options,
774775
git_notes_remove_usage, 0);
@@ -785,12 +786,17 @@ static int remove_cmd(int argc, const char **argv, const char *prefix)
785786

786787
t = init_notes_check("remove");
787788

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));
790795

791-
commit_notes(t, "Notes removed by 'git notes remove'");
796+
commit_notes(t, "Notes removed by 'git notes remove'");
797+
}
792798
free_notes(t);
793-
return 0;
799+
return retval;
794800
}
795801

796802
static int prune(int argc, const char **argv, const char *prefix)

notes.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,13 @@ static int note_tree_consolidate(struct int_node *tree,
263263
* To remove a leaf_node:
264264
* Search to the tree location appropriate for the given leaf_node's key:
265265
* - If location does not hold a matching entry, abort and do nothing.
266+
* - Copy the matching entry's value into the given entry.
266267
* - Replace the matching leaf_node with a NULL entry (and free the leaf_node).
267268
* - Consolidate int_nodes repeatedly, while walking up the tree towards root.
268269
*/
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)
271273
{
272274
struct leaf_node *l;
273275
struct int_node *parent_stack[20];
@@ -282,6 +284,7 @@ static void note_tree_remove(struct notes_tree *t, struct int_node *tree,
282284
return; /* key mismatch, nothing to remove */
283285

284286
/* we have found a matching entry */
287+
hashcpy(entry->val_sha1, l->val_sha1);
285288
free(l);
286289
*p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL);
287290

@@ -1003,17 +1006,20 @@ void add_note(struct notes_tree *t, const unsigned char *object_sha1,
10031006
note_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes);
10041007
}
10051008

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)
10071010
{
10081011
struct leaf_node l;
10091012

10101013
if (!t)
10111014
t = &default_notes_tree;
10121015
assert(t->initialized);
1013-
t->dirty = 1;
10141016
hashcpy(l.key_sha1, object_sha1);
10151017
hashclr(l.val_sha1);
10161018
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;
10171023
}
10181024

10191025
const unsigned char *get_note(struct notes_tree *t,

notes.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ void add_note(struct notes_tree *t, const unsigned char *object_sha1,
8989
* IMPORTANT: The changes made by remove_note() to the given notes_tree
9090
* structure are not persistent until a subsequent call to write_notes_tree()
9191
* returns zero.
92+
*
93+
* Return 0 if a note was removed; 1 if there was no note to remove.
9294
*/
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);
9496

9597
/*
9698
* Get the note object SHA1 containing the note data for the given object

t/t3301-notes.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,13 @@ c18dc024e14f08d18d14eea0d747ff692d66d6a3 1584215f1d29c65e99c6c6848626553fdd07fd7
365365
c9c6af7f78bc47490dbf3e822cf2f3c24d4b9061 268048bfb8a1fb38e703baceb8ab235421bf80c5
366366
EOF
367367

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+
368375
test_expect_success 'list notes with "git notes list"' '
369376
git notes list > output &&
370377
test_cmp expect output

0 commit comments

Comments
 (0)