@matheusbsbs / matheus-bagliano / commits / 2347fae50b
Fork 0

builtin-notes: Add "append" subcommand for appending to note objects

"git notes append" is equivalent to "git notes edit" except that instead of editing existing notes contents, you can only append to it. This is useful for quickly adding annotations like e.g.: git notes append -m "Acked-by: A U Thor <author@example.com>" "git notes append" takes the same -m/-F options as "git notes add". If there is no existing note to append to, "git notes append" is identical to "git notes add" (i.e. it adds a new note). The patch includes tests verifying correct behaviour of the new subcommand. Suggested-by: Junio C Hamano <gitster@pobox.com> 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 2347fae50b2f75c6c0b362bd9ef24249419ed2b1
3 files changed +67 -9
Documentation/git-notes.txt
+5
@@ -10,6 +10,7 @@ SYNOPSIS
10 [verse]
11 'git notes' [list [<object>]]
12 'git notes' add [-f] [-F <file> | -m <msg>] [<object>]
13 +'git notes' append [-F <file> | -m <msg>] [<object>]
14 'git notes' edit [-F <file> | -m <msg>] [<object>]
15 'git notes' show [<object>]
16 'git notes' remove [<object>]
@@ -47,6 +48,10 @@ add::
48 object already has notes, abort. (use `-f` to overwrite an
49 existing note).
50
51 +append::
52 + Append to the notes of an existing object (defaults to HEAD).
53 + Creates a new notes object if needed.
54 +
55 edit::
56 Edit the notes for a given object (defaults to HEAD).
57
builtin-notes.c
+26 -9
@@ -20,6 +20,7 @@
20 static const char * const git_notes_usage[] = {
21 "git notes [list [<object>]]",
22 "git notes add [-f] [-m <msg> | -F <file>] [<object>]",
23 + "git notes append [-m <msg> | -F <file>] [<object>]",
24 "git notes edit [-m <msg> | -F <file>] [<object>]",
25 "git notes show [<object>]",
26 "git notes remove [<object>]",
@@ -94,7 +95,7 @@ static void write_commented_object(int fd, const unsigned char *object)
95
96 static void create_note(const unsigned char *object,
97 struct strbuf *buf,
97 - int skip_editor,
98 + int skip_editor, int append_only,
99 const unsigned char *prev,
100 unsigned char *result)
101 {
@@ -109,7 +110,7 @@ static void create_note(const unsigned char *object,
110 if (fd < 0)
111 die_errno("could not create file '%s'", path);
112
112 - if (prev)
113 + if (prev && !append_only)
114 write_note_data(fd, prev);
115 write_or_die(fd, note_template, strlen(note_template));
116
@@ -125,6 +126,20 @@ static void create_note(const unsigned char *object,
126
127 stripspace(buf, 1);
128
129 + if (prev && append_only) {
130 + /* Append buf to previous note contents */
131 + unsigned long size;
132 + enum object_type type;
133 + char *prev_buf = read_sha1_file(prev, &type, &size);
134 +
135 + strbuf_grow(buf, size + 1);
136 + if (buf->len && prev_buf && size)
137 + strbuf_insert(buf, 0, "\n", 1);
138 + if (prev_buf && size)
139 + strbuf_insert(buf, 0, prev_buf, size);
140 + free(prev_buf);
141 + }
142 +
143 if (!buf->len) {
144 fprintf(stderr, "Removing note for object %s\n",
145 sha1_to_hex(object));
@@ -212,8 +227,8 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
227 const char *object_ref;
228 char logmsg[100];
229
215 - int list = 0, add = 0, edit = 0, show = 0, remove = 0, prune = 0,
216 - force = 0;
230 + int list = 0, add = 0, append = 0, edit = 0, show = 0, remove = 0,
231 + prune = 0, force = 0;
232 int given_object;
233 const char *msgfile = NULL;
234 struct msg_arg msg = { 0, STRBUF_INIT };
@@ -234,6 +249,8 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
249 list = 1;
250 else if (argc && !strcmp(argv[0], "add"))
251 add = 1;
252 + else if (argc && !strcmp(argv[0], "append"))
253 + append = 1;
254 else if (argc && !strcmp(argv[0], "edit"))
255 edit = 1;
256 else if (argc && !strcmp(argv[0], "show"))
@@ -245,10 +262,10 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
262 else if (!argc)
263 list = 1; /* Default to 'list' if no other subcommand given */
264
248 - if (list + add + edit + show + remove + prune != 1)
265 + if (list + add + append + edit + show + remove + prune != 1)
266 usage_with_options(git_notes_usage, options);
267
251 - if ((msg.given || msgfile) && !(add || edit)) {
268 + if ((msg.given || msgfile) && !(add || append || edit)) {
269 error("cannot use -m/-F options with %s subcommand.", argv[0]);
270 usage_with_options(git_notes_usage, options);
271 }
@@ -304,7 +321,7 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
321 return execv_git_cmd(show_args);
322 }
323
307 - /* add/edit/remove/prune command */
324 + /* add/append/edit/remove/prune command */
325
326 if (add && note) {
327 if (force)
@@ -334,8 +351,8 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
351 hashclr(new_note);
352 prune_notes(t);
353 } else {
337 - create_note(object, &buf, msg.given || msgfile || remove, note,
338 - new_note);
354 + create_note(object, &buf, msg.given || msgfile || remove,
355 + append, note, new_note);
356 if (is_null_sha1(new_note))
357 remove_note(t, object);
358 else
t/t3301-notes.sh
+36
@@ -343,6 +343,42 @@ test_expect_success 'listing non-existing notes fails' '
343 test_cmp expect output
344 '
345
346 +cat > expect << EOF
347 +Initial set of notes
348 +
349 +More notes appended with git notes append
350 +EOF
351 +
352 +test_expect_success 'append to existing note with "git notes append"' '
353 + git notes add -m "Initial set of notes" &&
354 + git notes append -m "More notes appended with git notes append" &&
355 + git notes show > output &&
356 + test_cmp expect output
357 +'
358 +
359 +test_expect_success 'appending empty string does not change existing note' '
360 + git notes append -m "" &&
361 + git notes show > output &&
362 + test_cmp expect output
363 +'
364 +
365 +test_expect_success 'git notes append == add when there is no existing note' '
366 + git notes remove HEAD &&
367 + test_must_fail git notes list HEAD &&
368 + git notes append -m "Initial set of notes
369 +
370 +More notes appended with git notes append" &&
371 + git notes show > output &&
372 + test_cmp expect output
373 +'
374 +
375 +test_expect_success 'appending empty string to non-existing note does not create note' '
376 + git notes remove HEAD &&
377 + test_must_fail git notes list HEAD &&
378 + git notes append -m "" &&
379 + test_must_fail git notes list HEAD
380 +'
381 +
382 test_expect_success 'create other note on a different notes ref (setup)' '
383 : > a6 &&
384 git add a6 &&