builtin-notes: Add "list" subcommand for listing note objects
"git notes list" will list all note objects in the current notes ref (in the format "<note object> <annotated object>"). "git notes list <object>" will list the note object associated with the given <object>, or fail loudly if the given <object> has no associated notes. If no arguments are given to "git notes", it defaults to the "list" subcommand. This is for pseudo-compatibility with "git tag" and "git branch". 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
e397421abf1003c4da824441f91d8c990b70fa98
3 files changed
+76
-6
Documentation/git-notes.txt
+12
-1
@@ -8,7 +8,12 @@ git-notes - Add/inspect object notes
8
SYNOPSIS
9
--------
10
[verse]
11
-'git notes' (edit [-F <file> | -m <msg>] | show | remove | prune) [object]
11
+'git notes' [list [<object>]]
12
+'git notes' edit [-F <file> | -m <msg>] [<object>]
13
+'git notes' show [<object>]
14
+'git notes' remove [<object>]
15
+'git notes' prune
16
+
17
18
DESCRIPTION
19
-----------
@@ -30,6 +35,12 @@ by the environment variable "GIT_NOTES_REF".
35
SUBCOMMANDS
36
-----------
37
38
+list::
39
+ List the notes object for a given object. If no object is
40
+ given, show a list of all note objects and the objects they
41
+ annotate (in the format "<note object> <annotated object>").
42
+ This is the default subcommand if no subcommand is given.
43
+
44
edit::
45
Edit the notes for a given object (defaults to HEAD).
46
builtin-notes.c
+32
-5
@@ -18,6 +18,7 @@
18
#include "parse-options.h"
19
20
static const char * const git_notes_usage[] = {
21
+ "git notes [list [<object>]]",
22
"git notes edit [-m <msg> | -F <file>] [<object>]",
23
"git notes show [<object>]",
24
"git notes remove [<object>]",
@@ -31,6 +32,14 @@ static const char note_template[] =
32
"# Write/edit the notes for the following object:\n"
33
"#\n";
34
35
+static int list_each_note(const unsigned char *object_sha1,
36
+ const unsigned char *note_sha1, char *note_path,
37
+ void *cb_data)
38
+{
39
+ printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
40
+ return 0;
41
+}
42
+
43
static void write_note_data(int fd, const unsigned char *sha1)
44
{
45
unsigned long size;
@@ -202,7 +211,8 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
211
const char *object_ref;
212
char logmsg[100];
213
205
- int edit = 0, show = 0, remove = 0, prune = 0;
214
+ int list = 0, edit = 0, show = 0, remove = 0, prune = 0;
215
+ int given_object;
216
const char *msgfile = NULL;
217
struct msg_arg msg = { 0, STRBUF_INIT };
218
struct option options[] = {
@@ -217,7 +227,9 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
227
228
argc = parse_options(argc, argv, prefix, options, git_notes_usage, 0);
229
220
- if (argc && !strcmp(argv[0], "edit"))
230
+ if (argc && !strcmp(argv[0], "list"))
231
+ list = 1;
232
+ else if (argc && !strcmp(argv[0], "edit"))
233
edit = 1;
234
else if (argc && !strcmp(argv[0], "show"))
235
show = 1;
@@ -225,8 +237,10 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
237
remove = 1;
238
else if (argc && !strcmp(argv[0], "prune"))
239
prune = 1;
240
+ else if (!argc)
241
+ list = 1; /* Default to 'list' if no other subcommand given */
242
229
- if (edit + show + remove + prune != 1)
243
+ if (list + edit + show + remove + prune != 1)
244
usage_with_options(git_notes_usage, options);
245
246
if ((msg.given || msgfile) && !edit) {
@@ -239,7 +253,8 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
253
usage_with_options(git_notes_usage, options);
254
}
255
242
- object_ref = argc == 2 ? argv[1] : "HEAD";
256
+ given_object = argc == 2;
257
+ object_ref = given_object ? argv[1] : "HEAD";
258
if (argc > 2 || (prune && argc > 1)) {
259
error("too many parameters");
260
usage_with_options(git_notes_usage, options);
@@ -257,9 +272,21 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
272
273
note = get_note(t, object);
274
275
+ /* list command */
276
+
277
+ if (list) {
278
+ if (given_object) {
279
+ if (note) {
280
+ puts(sha1_to_hex(note));
281
+ return 0;
282
+ }
283
+ } else
284
+ return for_each_note(t, 0, list_each_note, NULL);
285
+ }
286
+
287
/* show command */
288
262
- if (show && !note) {
289
+ if ((list || show) && !note) {
290
error("No note found for object %s.", sha1_to_hex(object));
291
return 1;
292
} else if (show) {
t/t3301-notes.sh
+32
@@ -292,6 +292,38 @@ test_expect_success 'verify note removal with "git notes remove"' '
292
! git notes show HEAD^
293
'
294
295
+cat > expect << EOF
296
+c18dc024e14f08d18d14eea0d747ff692d66d6a3 1584215f1d29c65e99c6c6848626553fdd07fd75
297
+c9c6af7f78bc47490dbf3e822cf2f3c24d4b9061 268048bfb8a1fb38e703baceb8ab235421bf80c5
298
+EOF
299
+
300
+test_expect_success 'list notes with "git notes list"' '
301
+ git notes list > output &&
302
+ test_cmp expect output
303
+'
304
+
305
+test_expect_success 'list notes with "git notes"' '
306
+ git notes > output &&
307
+ test_cmp expect output
308
+'
309
+
310
+cat > expect << EOF
311
+c18dc024e14f08d18d14eea0d747ff692d66d6a3
312
+EOF
313
+
314
+test_expect_success 'list specific note with "git notes list <object>"' '
315
+ git notes list HEAD^^ > output &&
316
+ test_cmp expect output
317
+'
318
+
319
+cat > expect << EOF
320
+EOF
321
+
322
+test_expect_success 'listing non-existing notes fails' '
323
+ test_must_fail git notes list HEAD > output &&
324
+ test_cmp expect output
325
+'
326
+
327
test_expect_success 'create other note on a different notes ref (setup)' '
328
: > a6 &&
329
git add a6 &&