@matheusbsbs / matheus-bagliano / commits / 7aa4754e55
Fork 0

builtin-notes: Add "add" subcommand for adding notes to objects

"git notes add" is identical to "git notes edit" except that instead of editing existing notes for a given object, you can only add notes to an object that currently has none. If "git notes add" finds existing notes for the given object, the addition is aborted. However, if the new -f/--force option is used, "git notes add" will _overwrite_ the existing notes with the new notes contents. If there is no existing notes for the given object. "git notes add" is identical to "git notes edit" (i.e. it adds a new note). The patch includes tests verifying correct behaviour of the new subcommand. Suggested-by: Joey Hess <joey@kitenet.net> Improved-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 7aa4754e552eff22d70d496dea73a9c7639d66d3
3 files changed +74 -22
Documentation/git-notes.txt
+11
@@ -9,6 +9,7 @@ SYNOPSIS
9 --------
10 [verse]
11 'git notes' [list [<object>]]
12 +'git notes' add [-f] [-F <file> | -m <msg>] [<object>]
13 'git notes' edit [-F <file> | -m <msg>] [<object>]
14 'git notes' show [<object>]
15 'git notes' remove [<object>]
@@ -41,6 +42,11 @@ list::
42 annotate (in the format "<note object> <annotated object>").
43 This is the default subcommand if no subcommand is given.
44
45 +add::
46 + Add notes for a given object (defaults to HEAD). Abort if the
47 + object already has notes, abort. (use `-f` to overwrite an
48 + existing note).
49 +
50 edit::
51 Edit the notes for a given object (defaults to HEAD).
52
@@ -57,6 +63,11 @@ prune::
63
64 OPTIONS
65 -------
66 +-f::
67 +--force::
68 + When adding notes to an object that already has notes,
69 + overwrite the existing notes (instead of aborting).
70 +
71 -m <msg>::
72 --message=<msg>::
73 Use the given note message (instead of prompting).
builtin-notes.c
+26 -4
@@ -19,6 +19,7 @@
19
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 edit [-m <msg> | -F <file>] [<object>]",
24 "git notes show [<object>]",
25 "git notes remove [<object>]",
@@ -211,7 +212,8 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
212 const char *object_ref;
213 char logmsg[100];
214
214 - int list = 0, edit = 0, show = 0, remove = 0, prune = 0;
215 + int list = 0, add = 0, edit = 0, show = 0, remove = 0, prune = 0,
216 + force = 0;
217 int given_object;
218 const char *msgfile = NULL;
219 struct msg_arg msg = { 0, STRBUF_INIT };
@@ -220,6 +222,7 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
222 OPT_CALLBACK('m', "message", &msg, "msg",
223 "note contents as a string", parse_msg_arg),
224 OPT_FILENAME('F', "file", &msgfile, "note contents in a file"),
225 + OPT_BOOLEAN('f', "force", &force, "replace existing notes"),
226 OPT_END()
227 };
228
@@ -229,6 +232,8 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
232
233 if (argc && !strcmp(argv[0], "list"))
234 list = 1;
235 + else if (argc && !strcmp(argv[0], "add"))
236 + add = 1;
237 else if (argc && !strcmp(argv[0], "edit"))
238 edit = 1;
239 else if (argc && !strcmp(argv[0], "show"))
@@ -240,10 +245,10 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
245 else if (!argc)
246 list = 1; /* Default to 'list' if no other subcommand given */
247
243 - if (list + edit + show + remove + prune != 1)
248 + if (list + add + edit + show + remove + prune != 1)
249 usage_with_options(git_notes_usage, options);
250
246 - if ((msg.given || msgfile) && !edit) {
251 + if ((msg.given || msgfile) && !(add || edit)) {
252 error("cannot use -m/-F options with %s subcommand.", argv[0]);
253 usage_with_options(git_notes_usage, options);
254 }
@@ -253,6 +258,11 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
258 usage_with_options(git_notes_usage, options);
259 }
260
261 + if (force && !add) {
262 + error("cannot use -f option with %s subcommand.", argv[0]);
263 + usage_with_options(git_notes_usage, options);
264 + }
265 +
266 given_object = argc == 2;
267 object_ref = given_object ? argv[1] : "HEAD";
268 if (argc > 2 || (prune && argc > 1)) {
@@ -294,7 +304,19 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
304 return execv_git_cmd(show_args);
305 }
306
297 - /* edit/remove/prune command */
307 + /* add/edit/remove/prune command */
308 +
309 + if (add && note) {
310 + if (force)
311 + fprintf(stderr, "Overwriting existing notes for object %s\n",
312 + sha1_to_hex(object));
313 + else {
314 + error("Cannot add notes. Found existing notes for object %s. "
315 + "Use '-f' to overwrite existing notes",
316 + sha1_to_hex(object));
317 + return 1;
318 + }
319 + }
320
321 if (remove)
322 strbuf_reset(&buf);
t/t3301-notes.sh
+37 -18
@@ -16,7 +16,7 @@ GIT_EDITOR=./fake_editor.sh
16 export GIT_EDITOR
17
18 test_expect_success 'cannot annotate non-existing HEAD' '
19 - (MSG=3 && export MSG && test_must_fail git notes edit)
19 + (MSG=3 && export MSG && test_must_fail git notes add)
20 '
21
22 test_expect_success setup '
@@ -32,18 +32,18 @@ test_expect_success setup '
32
33 test_expect_success 'need valid notes ref' '
34 (MSG=1 GIT_NOTES_REF=/ && export MSG GIT_NOTES_REF &&
35 - test_must_fail git notes edit) &&
35 + test_must_fail git notes add) &&
36 (MSG=2 GIT_NOTES_REF=/ && export MSG GIT_NOTES_REF &&
37 test_must_fail git notes show)
38 '
39
40 -test_expect_success 'refusing to edit in refs/heads/' '
40 +test_expect_success 'refusing to add notes in refs/heads/' '
41 (MSG=1 GIT_NOTES_REF=refs/heads/bogus &&
42 export MSG GIT_NOTES_REF &&
43 - test_must_fail git notes edit)
43 + test_must_fail git notes add)
44 '
45
46 -test_expect_success 'refusing to edit in refs/remotes/' '
46 +test_expect_success 'refusing to edit notes in refs/remotes/' '
47 (MSG=1 GIT_NOTES_REF=refs/remotes/bogus &&
48 export MSG GIT_NOTES_REF &&
49 test_must_fail git notes edit)
@@ -56,16 +56,34 @@ test_expect_success 'handle empty notes gracefully' '
56
57 test_expect_success 'create notes' '
58 git config core.notesRef refs/notes/commits &&
59 - MSG=b0 git notes edit &&
59 + MSG=b4 git notes add &&
60 test ! -f .git/NOTES_EDITMSG &&
61 test 1 = $(git ls-tree refs/notes/commits | wc -l) &&
62 - test b0 = $(git notes show) &&
62 + test b4 = $(git notes show) &&
63 git show HEAD^ &&
64 test_must_fail git notes show HEAD^
65 '
66
67 test_expect_success 'edit existing notes' '
68 - MSG=b1 git notes edit &&
68 + MSG=b3 git notes edit &&
69 + test ! -f .git/NOTES_EDITMSG &&
70 + test 1 = $(git ls-tree refs/notes/commits | wc -l) &&
71 + test b3 = $(git notes show) &&
72 + git show HEAD^ &&
73 + test_must_fail git notes show HEAD^
74 +'
75 +
76 +test_expect_success 'cannot add note where one exists' '
77 + ! MSG=b2 git notes add &&
78 + test ! -f .git/NOTES_EDITMSG &&
79 + test 1 = $(git ls-tree refs/notes/commits | wc -l) &&
80 + test b3 = $(git notes show) &&
81 + git show HEAD^ &&
82 + test_must_fail git notes show HEAD^
83 +'
84 +
85 +test_expect_success 'can overwrite existing note with "git notes add -f"' '
86 + MSG=b1 git notes add -f &&
87 test ! -f .git/NOTES_EDITMSG &&
88 test 1 = $(git ls-tree refs/notes/commits | wc -l) &&
89 test b1 = $(git notes show) &&
@@ -89,6 +107,7 @@ test_expect_success 'show notes' '
107 git log -1 > output &&
108 test_cmp expect output
109 '
110 +
111 test_expect_success 'create multi-line notes (setup)' '
112 : > a3 &&
113 git add a3 &&
@@ -96,7 +115,7 @@ test_expect_success 'create multi-line notes (setup)' '
115 git commit -m 3rd &&
116 MSG="b3
117 c3c3c3c3
99 -d3d3d3" git notes edit
118 +d3d3d3" git notes add
119 '
120
121 cat > expect-multiline << EOF
@@ -125,7 +144,7 @@ test_expect_success 'create -F notes (setup)' '
144 test_tick &&
145 git commit -m 4th &&
146 echo "xyzzy" > note5 &&
128 - git notes edit -F note5
147 + git notes add -F note5
148 '
149
150 cat > expect-F << EOF
@@ -205,7 +224,7 @@ test_expect_success 'create -m notes (setup)' '
224 git add a5 &&
225 test_tick &&
226 git commit -m 5th &&
208 - git notes edit -m spam -m "foo
227 + git notes add -m spam -m "foo
228 bar
229 baz"
230 '
@@ -234,8 +253,8 @@ test_expect_success 'show -m notes' '
253 test_cmp expect-m output
254 '
255
237 -test_expect_success 'remove note with -F /dev/null (setup)' '
238 - git notes edit -F /dev/null
256 +test_expect_success 'remove note with add -f -F /dev/null (setup)' '
257 + git notes add -f -F /dev/null
258 '
259
260 cat > expect-rm-F << EOF
@@ -256,7 +275,7 @@ test_expect_success 'verify note removal with -F /dev/null' '
275 '
276
277 test_expect_success 'do not create empty note with -m "" (setup)' '
259 - git notes edit -m ""
278 + git notes add -m ""
279 '
280
281 test_expect_success 'verify non-creation of note with -m ""' '
@@ -329,7 +348,7 @@ test_expect_success 'create other note on a different notes ref (setup)' '
348 git add a6 &&
349 test_tick &&
350 git commit -m 6th &&
332 - GIT_NOTES_REF="refs/notes/other" git notes edit -m "other note"
351 + GIT_NOTES_REF="refs/notes/other" git notes add -m "other note"
352 '
353
354 cat > expect-other << EOF
@@ -374,17 +393,17 @@ test_expect_success 'Do not show note when core.notesRef is overridden' '
393
394 test_expect_success 'Allow notes on non-commits (trees, blobs, tags)' '
395 echo "Note on a tree" > expect
377 - git notes edit -m "Note on a tree" HEAD: &&
396 + git notes add -m "Note on a tree" HEAD: &&
397 git notes show HEAD: > actual &&
398 test_cmp expect actual &&
399 echo "Note on a blob" > expect
400 filename=$(git ls-tree --name-only HEAD | head -n1) &&
382 - git notes edit -m "Note on a blob" HEAD:$filename &&
401 + git notes add -m "Note on a blob" HEAD:$filename &&
402 git notes show HEAD:$filename > actual &&
403 test_cmp expect actual &&
404 echo "Note on a tag" > expect
405 git tag -a -m "This is an annotated tag" foobar HEAD^ &&
387 - git notes edit -m "Note on a tag" foobar &&
406 + git notes add -m "Note on a tag" foobar &&
407 git notes show foobar > actual &&
408 test_cmp expect actual
409 '