@matheusbsbs / matheus-bagliano / commits / a0b4dfa9b3
Fork 0

Teach builtin-notes to remove empty notes

When the result of editing a note is an empty string, the associated note entry should be deleted from the notes tree. This allows deleting notes by invoking either "git notes -m ''" or "git notes -F /dev/null". Signed-off-by: Johan Herland <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johan Herland committed Feb 13, 2010 at 22:28 UTC a0b4dfa9b35a2ebac578ea5547b041bb78557238
2 files changed +42 -4
builtin-notes.c
+11 -4
@@ -113,7 +113,7 @@ static void create_note(const unsigned char *object,
113
114 stripspace(buf, 1);
115
116 - if (!skip_editor && !buf->len) {
116 + if (!buf->len) {
117 fprintf(stderr, "Removing note for object %s\n",
118 sha1_to_hex(object));
119 hashclr(result);
@@ -197,7 +197,8 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
197 struct notes_tree *t;
198 unsigned char object[20], new_note[20];
199 const unsigned char *note;
200 - const char *object_ref;
200 + const char *object_ref, *logmsg;
201 +
202 int edit = 0, show = 0;
203 const char *msgfile = NULL;
204 struct msg_arg msg = { 0, STRBUF_INIT };
@@ -271,8 +272,14 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
272 }
273
274 create_note(object, &buf, msg.given || msgfile, note, new_note);
274 - add_note(t, object, new_note, combine_notes_overwrite);
275 - commit_notes(t, "Note added by 'git notes edit'");
275 + if (is_null_sha1(new_note)) {
276 + remove_note(t, object);
277 + logmsg = "Note removed by 'git notes edit'";
278 + } else {
279 + add_note(t, object, new_note, combine_notes_overwrite);
280 + logmsg = "Note added by 'git notes edit'";
281 + }
282 + commit_notes(t, logmsg);
283
284 free_notes(t);
285 strbuf_release(&buf);
t/t3301-notes.sh
+31
@@ -234,6 +234,37 @@ test_expect_success 'show -m notes' '
234 test_cmp expect-m output
235 '
236
237 +test_expect_success 'remove note with -F /dev/null (setup)' '
238 + git notes edit -F /dev/null
239 +'
240 +
241 +cat > expect-rm-F << EOF
242 +commit bd1753200303d0a0344be813e504253b3d98e74d
243 +Author: A U Thor <author@example.com>
244 +Date: Thu Apr 7 15:17:13 2005 -0700
245 +
246 + 5th
247 +EOF
248 +
249 +printf "\n" >> expect-rm-F
250 +cat expect-F >> expect-rm-F
251 +
252 +test_expect_success 'verify note removal with -F /dev/null' '
253 + git log -4 > output &&
254 + test_cmp expect-rm-F output &&
255 + ! git notes show
256 +'
257 +
258 +test_expect_success 'do not create empty note with -m "" (setup)' '
259 + git notes edit -m ""
260 +'
261 +
262 +test_expect_success 'verify non-creation of note with -m ""' '
263 + git log -4 > output &&
264 + test_cmp expect-rm-F output &&
265 + ! git notes show
266 +'
267 +
268 test_expect_success 'create other note on a different notes ref (setup)' '
269 : > a6 &&
270 git add a6 &&