[PATCH 2/2] notes.h: Extend remove_note to return the SHA1 of the removed note, if any
From: Johan Herland <hidden>
Date: 2016-06-15 22:49:25
Subsystem:
the rest · Maintainer:
Linus Torvalds
It might be useful to identify which note was removed by a call to remove_note(). Add an optional parameter to remove_note() for recording the SHA1 of the removed note object. If no note is removed by remove_note(), null_sha1 is stored in this parameter. Signed-off-by: Johan Herland <redacted> --- This patch is very much optional. It adds a small API extension that _might_ be useful in the future. builtin/notes.c | 6 +++--- notes.c | 7 +++++-- notes.h | 6 +++++- 3 files changed, 13 insertions(+), 6 deletions(-)
diff --git a/builtin/notes.c b/builtin/notes.c
index 6d07aac..e5c8208 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c@@ -572,7 +572,7 @@ static int add(int argc, const char **argv, const char *prefix) create_note(object, &msg, 0, note, new_note); if (is_null_sha1(new_note)) - remove_note(t, object); + remove_note(t, object, NULL); else add_note(t, object, new_note, combine_notes_overwrite);
@@ -711,7 +711,7 @@ static int append_edit(int argc, const char **argv, const char *prefix) create_note(object, &msg, !edit, note, new_note); if (is_null_sha1(new_note)) - remove_note(t, object); + remove_note(t, object, NULL); else add_note(t, object, new_note, combine_notes_overwrite);
@@ -786,7 +786,7 @@ static int remove_cmd(int argc, const char **argv, const char *prefix) t = init_notes_check("remove"); - retval = remove_note(t, object); + retval = remove_note(t, object, NULL); if (retval) fprintf(stderr, "Object %s has no note\n", sha1_to_hex(object)); else {
diff --git a/notes.c b/notes.c
index 70d0013..9fe857b 100644
--- a/notes.c
+++ b/notes.c@@ -1006,7 +1006,8 @@ void add_note(struct notes_tree *t, const unsigned char *object_sha1, note_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes); } -int remove_note(struct notes_tree *t, const unsigned char *object_sha1) +int remove_note(struct notes_tree *t, const unsigned char *object_sha1, + unsigned char *removed_note) { struct leaf_node l;
@@ -1016,6 +1017,8 @@ int remove_note(struct notes_tree *t, const unsigned char *object_sha1) hashcpy(l.key_sha1, object_sha1); hashclr(l.val_sha1); note_tree_remove(t, t->root, 0, &l); + if (removed_note) + hashcpy(removed_note, l.val_sha1); if (is_null_sha1(l.val_sha1)) // no note was removed return 1; t->dirty = 1;
@@ -1085,7 +1088,7 @@ void prune_notes(struct notes_tree *t, int flags) if (flags & NOTES_PRUNE_VERBOSE) printf("%s\n", sha1_to_hex(l->sha1)); if (!(flags & NOTES_PRUNE_DRYRUN)) - remove_note(t, l->sha1); + remove_note(t, l->sha1, NULL); l = l->next; } }
diff --git a/notes.h b/notes.h
index 5106761..c61e962 100644
--- a/notes.h
+++ b/notes.h@@ -90,9 +90,13 @@ void add_note(struct notes_tree *t, const unsigned char *object_sha1, * structure are not persistent until a subsequent call to write_notes_tree() * returns zero. * + * If removed_note is non-NULL, the SHA1 of the removed note will be written + * there. If there was no note to remove, then null_sha1 will be written there. + * * Return 0 if a note was removed; 1 if there was no note to remove. */ -int remove_note(struct notes_tree *t, const unsigned char *object_sha1); +int remove_note(struct notes_tree *t, const unsigned char *object_sha1, + unsigned char *removed_note); /* * Get the note object SHA1 containing the note data for the given object
--
1.7.2.220.gea1d3