Code

notes.h/c: Propagate combine_notes_fn return value to add_note() and beyond
[git.git] / builtin / notes.c
1 /*
2  * Builtin "git notes"
3  *
4  * Copyright (c) 2010 Johan Herland <johan@herland.net>
5  *
6  * Based on git-notes.sh by Johannes Schindelin,
7  * and builtin-tag.c by Kristian Høgsberg and Carlos Rica.
8  */
10 #include "cache.h"
11 #include "builtin.h"
12 #include "notes.h"
13 #include "blob.h"
14 #include "commit.h"
15 #include "refs.h"
16 #include "exec_cmd.h"
17 #include "run-command.h"
18 #include "parse-options.h"
19 #include "string-list.h"
21 static const char * const git_notes_usage[] = {
22         "git notes [--ref <notes_ref>] [list [<object>]]",
23         "git notes [--ref <notes_ref>] add [-f] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
24         "git notes [--ref <notes_ref>] copy [-f] <from-object> <to-object>",
25         "git notes [--ref <notes_ref>] append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
26         "git notes [--ref <notes_ref>] edit [<object>]",
27         "git notes [--ref <notes_ref>] show [<object>]",
28         "git notes [--ref <notes_ref>] remove [<object>]",
29         "git notes [--ref <notes_ref>] prune [-n | -v]",
30         NULL
31 };
33 static const char * const git_notes_list_usage[] = {
34         "git notes [list [<object>]]",
35         NULL
36 };
38 static const char * const git_notes_add_usage[] = {
39         "git notes add [<options>] [<object>]",
40         NULL
41 };
43 static const char * const git_notes_copy_usage[] = {
44         "git notes copy [<options>] <from-object> <to-object>",
45         "git notes copy --stdin [<from-object> <to-object>]...",
46         NULL
47 };
49 static const char * const git_notes_append_usage[] = {
50         "git notes append [<options>] [<object>]",
51         NULL
52 };
54 static const char * const git_notes_edit_usage[] = {
55         "git notes edit [<object>]",
56         NULL
57 };
59 static const char * const git_notes_show_usage[] = {
60         "git notes show [<object>]",
61         NULL
62 };
64 static const char * const git_notes_remove_usage[] = {
65         "git notes remove [<object>]",
66         NULL
67 };
69 static const char * const git_notes_prune_usage[] = {
70         "git notes prune [<options>]",
71         NULL
72 };
74 static const char note_template[] =
75         "\n"
76         "#\n"
77         "# Write/edit the notes for the following object:\n"
78         "#\n";
80 struct msg_arg {
81         int given;
82         int use_editor;
83         struct strbuf buf;
84 };
86 static int list_each_note(const unsigned char *object_sha1,
87                 const unsigned char *note_sha1, char *note_path,
88                 void *cb_data)
89 {
90         printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
91         return 0;
92 }
94 static void write_note_data(int fd, const unsigned char *sha1)
95 {
96         unsigned long size;
97         enum object_type type;
98         char *buf = read_sha1_file(sha1, &type, &size);
99         if (buf) {
100                 if (size)
101                         write_or_die(fd, buf, size);
102                 free(buf);
103         }
106 static void write_commented_object(int fd, const unsigned char *object)
108         const char *show_args[5] =
109                 {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
110         struct child_process show;
111         struct strbuf buf = STRBUF_INIT;
112         FILE *show_out;
114         /* Invoke "git show --stat --no-notes $object" */
115         memset(&show, 0, sizeof(show));
116         show.argv = show_args;
117         show.no_stdin = 1;
118         show.out = -1;
119         show.err = 0;
120         show.git_cmd = 1;
121         if (start_command(&show))
122                 die("unable to start 'show' for object '%s'",
123                     sha1_to_hex(object));
125         /* Open the output as FILE* so strbuf_getline() can be used. */
126         show_out = xfdopen(show.out, "r");
127         if (show_out == NULL)
128                 die_errno("can't fdopen 'show' output fd");
130         /* Prepend "# " to each output line and write result to 'fd' */
131         while (strbuf_getline(&buf, show_out, '\n') != EOF) {
132                 write_or_die(fd, "# ", 2);
133                 write_or_die(fd, buf.buf, buf.len);
134                 write_or_die(fd, "\n", 1);
135         }
136         strbuf_release(&buf);
137         if (fclose(show_out))
138                 die_errno("failed to close pipe to 'show' for object '%s'",
139                           sha1_to_hex(object));
140         if (finish_command(&show))
141                 die("failed to finish 'show' for object '%s'",
142                     sha1_to_hex(object));
145 static void create_note(const unsigned char *object, struct msg_arg *msg,
146                         int append_only, const unsigned char *prev,
147                         unsigned char *result)
149         char *path = NULL;
151         if (msg->use_editor || !msg->given) {
152                 int fd;
154                 /* write the template message before editing: */
155                 path = git_pathdup("NOTES_EDITMSG");
156                 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
157                 if (fd < 0)
158                         die_errno("could not create file '%s'", path);
160                 if (msg->given)
161                         write_or_die(fd, msg->buf.buf, msg->buf.len);
162                 else if (prev && !append_only)
163                         write_note_data(fd, prev);
164                 write_or_die(fd, note_template, strlen(note_template));
166                 write_commented_object(fd, object);
168                 close(fd);
169                 strbuf_reset(&(msg->buf));
171                 if (launch_editor(path, &(msg->buf), NULL)) {
172                         die("Please supply the note contents using either -m" \
173                             " or -F option");
174                 }
175                 stripspace(&(msg->buf), 1);
176         }
178         if (prev && append_only) {
179                 /* Append buf to previous note contents */
180                 unsigned long size;
181                 enum object_type type;
182                 char *prev_buf = read_sha1_file(prev, &type, &size);
184                 strbuf_grow(&(msg->buf), size + 1);
185                 if (msg->buf.len && prev_buf && size)
186                         strbuf_insert(&(msg->buf), 0, "\n", 1);
187                 if (prev_buf && size)
188                         strbuf_insert(&(msg->buf), 0, prev_buf, size);
189                 free(prev_buf);
190         }
192         if (!msg->buf.len) {
193                 fprintf(stderr, "Removing note for object %s\n",
194                         sha1_to_hex(object));
195                 hashclr(result);
196         } else {
197                 if (write_sha1_file(msg->buf.buf, msg->buf.len, blob_type, result)) {
198                         error("unable to write note object");
199                         if (path)
200                                 error("The note contents has been left in %s",
201                                       path);
202                         exit(128);
203                 }
204         }
206         if (path) {
207                 unlink_or_warn(path);
208                 free(path);
209         }
212 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
214         struct msg_arg *msg = opt->value;
216         strbuf_grow(&(msg->buf), strlen(arg) + 2);
217         if (msg->buf.len)
218                 strbuf_addch(&(msg->buf), '\n');
219         strbuf_addstr(&(msg->buf), arg);
220         stripspace(&(msg->buf), 0);
222         msg->given = 1;
223         return 0;
226 static int parse_file_arg(const struct option *opt, const char *arg, int unset)
228         struct msg_arg *msg = opt->value;
230         if (msg->buf.len)
231                 strbuf_addch(&(msg->buf), '\n');
232         if (!strcmp(arg, "-")) {
233                 if (strbuf_read(&(msg->buf), 0, 1024) < 0)
234                         die_errno("cannot read '%s'", arg);
235         } else if (strbuf_read_file(&(msg->buf), arg, 1024) < 0)
236                 die_errno("could not open or read '%s'", arg);
237         stripspace(&(msg->buf), 0);
239         msg->given = 1;
240         return 0;
243 static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
245         struct msg_arg *msg = opt->value;
246         char *buf;
247         unsigned char object[20];
248         enum object_type type;
249         unsigned long len;
251         if (msg->buf.len)
252                 strbuf_addch(&(msg->buf), '\n');
254         if (get_sha1(arg, object))
255                 die("Failed to resolve '%s' as a valid ref.", arg);
256         if (!(buf = read_sha1_file(object, &type, &len)) || !len) {
257                 free(buf);
258                 die("Failed to read object '%s'.", arg);;
259         }
260         strbuf_add(&(msg->buf), buf, len);
261         free(buf);
263         msg->given = 1;
264         return 0;
267 static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
269         struct msg_arg *msg = opt->value;
270         msg->use_editor = 1;
271         return parse_reuse_arg(opt, arg, unset);
274 int commit_notes(struct notes_tree *t, const char *msg)
276         struct commit_list *parent;
277         unsigned char tree_sha1[20], prev_commit[20], new_commit[20];
278         struct strbuf buf = STRBUF_INIT;
280         if (!t)
281                 t = &default_notes_tree;
282         if (!t->initialized || !t->ref || !*t->ref)
283                 die("Cannot commit uninitialized/unreferenced notes tree");
284         if (!t->dirty)
285                 return 0; /* don't have to commit an unchanged tree */
287         /* Prepare commit message and reflog message */
288         strbuf_addstr(&buf, "notes: "); /* commit message starts at index 7 */
289         strbuf_addstr(&buf, msg);
290         if (buf.buf[buf.len - 1] != '\n')
291                 strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
293         /* Convert notes tree to tree object */
294         if (write_notes_tree(t, tree_sha1))
295                 die("Failed to write current notes tree to database");
297         /* Create new commit for the tree object */
298         if (!read_ref(t->ref, prev_commit)) { /* retrieve parent commit */
299                 parent = xmalloc(sizeof(*parent));
300                 parent->item = lookup_commit(prev_commit);
301                 parent->next = NULL;
302         } else {
303                 hashclr(prev_commit);
304                 parent = NULL;
305         }
306         if (commit_tree(buf.buf + 7, tree_sha1, parent, new_commit, NULL))
307                 die("Failed to commit notes tree to database");
309         /* Update notes ref with new commit */
310         update_ref(buf.buf, t->ref, new_commit, prev_commit, 0, DIE_ON_ERR);
312         strbuf_release(&buf);
313         return 0;
316 combine_notes_fn parse_combine_notes_fn(const char *v)
318         if (!strcasecmp(v, "overwrite"))
319                 return combine_notes_overwrite;
320         else if (!strcasecmp(v, "ignore"))
321                 return combine_notes_ignore;
322         else if (!strcasecmp(v, "concatenate"))
323                 return combine_notes_concatenate;
324         else
325                 return NULL;
328 static int notes_rewrite_config(const char *k, const char *v, void *cb)
330         struct notes_rewrite_cfg *c = cb;
331         if (!prefixcmp(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
332                 c->enabled = git_config_bool(k, v);
333                 return 0;
334         } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
335                 if (!v)
336                         config_error_nonbool(k);
337                 c->combine = parse_combine_notes_fn(v);
338                 if (!c->combine) {
339                         error("Bad notes.rewriteMode value: '%s'", v);
340                         return 1;
341                 }
342                 return 0;
343         } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
344                 /* note that a refs/ prefix is implied in the
345                  * underlying for_each_glob_ref */
346                 if (!prefixcmp(v, "refs/notes/"))
347                         string_list_add_refs_by_glob(c->refs, v);
348                 else
349                         warning("Refusing to rewrite notes in %s"
350                                 " (outside of refs/notes/)", v);
351                 return 0;
352         }
354         return 0;
358 struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
360         struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
361         const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
362         const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
363         c->cmd = cmd;
364         c->enabled = 1;
365         c->combine = combine_notes_concatenate;
366         c->refs = xcalloc(1, sizeof(struct string_list));
367         c->refs->strdup_strings = 1;
368         c->refs_from_env = 0;
369         c->mode_from_env = 0;
370         if (rewrite_mode_env) {
371                 c->mode_from_env = 1;
372                 c->combine = parse_combine_notes_fn(rewrite_mode_env);
373                 if (!c->combine)
374                         error("Bad " GIT_NOTES_REWRITE_MODE_ENVIRONMENT
375                               " value: '%s'", rewrite_mode_env);
376         }
377         if (rewrite_refs_env) {
378                 c->refs_from_env = 1;
379                 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
380         }
381         git_config(notes_rewrite_config, c);
382         if (!c->enabled || !c->refs->nr) {
383                 string_list_clear(c->refs, 0);
384                 free(c->refs);
385                 free(c);
386                 return NULL;
387         }
388         c->trees = load_notes_trees(c->refs);
389         string_list_clear(c->refs, 0);
390         free(c->refs);
391         return c;
394 int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
395                           const unsigned char *from_obj, const unsigned char *to_obj)
397         int ret = 0;
398         int i;
399         for (i = 0; c->trees[i]; i++)
400                 ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
401         return ret;
404 void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c)
406         int i;
407         for (i = 0; c->trees[i]; i++) {
408                 commit_notes(c->trees[i], "Notes added by 'git notes copy'");
409                 free_notes(c->trees[i]);
410         }
411         free(c->trees);
412         free(c);
415 int notes_copy_from_stdin(int force, const char *rewrite_cmd)
417         struct strbuf buf = STRBUF_INIT;
418         struct notes_rewrite_cfg *c = NULL;
419         struct notes_tree *t = NULL;
420         int ret = 0;
422         if (rewrite_cmd) {
423                 c = init_copy_notes_for_rewrite(rewrite_cmd);
424                 if (!c)
425                         return 0;
426         } else {
427                 init_notes(NULL, NULL, NULL, 0);
428                 t = &default_notes_tree;
429         }
431         while (strbuf_getline(&buf, stdin, '\n') != EOF) {
432                 unsigned char from_obj[20], to_obj[20];
433                 struct strbuf **split;
434                 int err;
436                 split = strbuf_split(&buf, ' ');
437                 if (!split[0] || !split[1])
438                         die("Malformed input line: '%s'.", buf.buf);
439                 strbuf_rtrim(split[0]);
440                 strbuf_rtrim(split[1]);
441                 if (get_sha1(split[0]->buf, from_obj))
442                         die("Failed to resolve '%s' as a valid ref.", split[0]->buf);
443                 if (get_sha1(split[1]->buf, to_obj))
444                         die("Failed to resolve '%s' as a valid ref.", split[1]->buf);
446                 if (rewrite_cmd)
447                         err = copy_note_for_rewrite(c, from_obj, to_obj);
448                 else
449                         err = copy_note(t, from_obj, to_obj, force,
450                                         combine_notes_overwrite);
452                 if (err) {
453                         error("Failed to copy notes from '%s' to '%s'",
454                               split[0]->buf, split[1]->buf);
455                         ret = 1;
456                 }
458                 strbuf_list_free(split);
459         }
461         if (!rewrite_cmd) {
462                 commit_notes(t, "Notes added by 'git notes copy'");
463                 free_notes(t);
464         } else {
465                 finish_copy_notes_for_rewrite(c);
466         }
467         return ret;
470 static struct notes_tree *init_notes_check(const char *subcommand)
472         struct notes_tree *t;
473         init_notes(NULL, NULL, NULL, 0);
474         t = &default_notes_tree;
476         if (prefixcmp(t->ref, "refs/notes/"))
477                 die("Refusing to %s notes in %s (outside of refs/notes/)",
478                     subcommand, t->ref);
479         return t;
482 static int list(int argc, const char **argv, const char *prefix)
484         struct notes_tree *t;
485         unsigned char object[20];
486         const unsigned char *note;
487         int retval = -1;
488         struct option options[] = {
489                 OPT_END()
490         };
492         if (argc)
493                 argc = parse_options(argc, argv, prefix, options,
494                                      git_notes_list_usage, 0);
496         if (1 < argc) {
497                 error("too many parameters");
498                 usage_with_options(git_notes_list_usage, options);
499         }
501         t = init_notes_check("list");
502         if (argc) {
503                 if (get_sha1(argv[0], object))
504                         die("Failed to resolve '%s' as a valid ref.", argv[0]);
505                 note = get_note(t, object);
506                 if (note) {
507                         puts(sha1_to_hex(note));
508                         retval = 0;
509                 } else
510                         retval = error("No note found for object %s.",
511                                        sha1_to_hex(object));
512         } else
513                 retval = for_each_note(t, 0, list_each_note, NULL);
515         free_notes(t);
516         return retval;
519 static int add(int argc, const char **argv, const char *prefix)
521         int retval = 0, force = 0;
522         const char *object_ref;
523         struct notes_tree *t;
524         unsigned char object[20], new_note[20];
525         char logmsg[100];
526         const unsigned char *note;
527         struct msg_arg msg = { 0, 0, STRBUF_INIT };
528         struct option options[] = {
529                 { OPTION_CALLBACK, 'm', "message", &msg, "MSG",
530                         "note contents as a string", PARSE_OPT_NONEG,
531                         parse_msg_arg},
532                 { OPTION_CALLBACK, 'F', "file", &msg, "FILE",
533                         "note contents in a file", PARSE_OPT_NONEG,
534                         parse_file_arg},
535                 { OPTION_CALLBACK, 'c', "reedit-message", &msg, "OBJECT",
536                         "reuse and edit specified note object", PARSE_OPT_NONEG,
537                         parse_reedit_arg},
538                 { OPTION_CALLBACK, 'C', "reuse-message", &msg, "OBJECT",
539                         "reuse specified note object", PARSE_OPT_NONEG,
540                         parse_reuse_arg},
541                 OPT_BOOLEAN('f', "force", &force, "replace existing notes"),
542                 OPT_END()
543         };
545         argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
546                              0);
548         if (1 < argc) {
549                 error("too many parameters");
550                 usage_with_options(git_notes_add_usage, options);
551         }
553         object_ref = argc ? argv[0] : "HEAD";
555         if (get_sha1(object_ref, object))
556                 die("Failed to resolve '%s' as a valid ref.", object_ref);
558         t = init_notes_check("add");
559         note = get_note(t, object);
561         if (note) {
562                 if (!force) {
563                         retval = error("Cannot add notes. Found existing notes "
564                                        "for object %s. Use '-f' to overwrite "
565                                        "existing notes", sha1_to_hex(object));
566                         goto out;
567                 }
568                 fprintf(stderr, "Overwriting existing notes for object %s\n",
569                         sha1_to_hex(object));
570         }
572         create_note(object, &msg, 0, note, new_note);
574         if (is_null_sha1(new_note))
575                 remove_note(t, object);
576         else if (add_note(t, object, new_note, combine_notes_overwrite))
577                 die("BUG: combine_notes_overwrite failed");
579         snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
580                  is_null_sha1(new_note) ? "removed" : "added", "add");
581         commit_notes(t, logmsg);
582 out:
583         free_notes(t);
584         strbuf_release(&(msg.buf));
585         return retval;
588 static int copy(int argc, const char **argv, const char *prefix)
590         int retval = 0, force = 0, from_stdin = 0;
591         const unsigned char *from_note, *note;
592         const char *object_ref;
593         unsigned char object[20], from_obj[20];
594         struct notes_tree *t;
595         const char *rewrite_cmd = NULL;
596         struct option options[] = {
597                 OPT_BOOLEAN('f', "force", &force, "replace existing notes"),
598                 OPT_BOOLEAN(0, "stdin", &from_stdin, "read objects from stdin"),
599                 OPT_STRING(0, "for-rewrite", &rewrite_cmd, "command",
600                            "load rewriting config for <command> (implies "
601                            "--stdin)"),
602                 OPT_END()
603         };
605         argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
606                              0);
608         if (from_stdin || rewrite_cmd) {
609                 if (argc) {
610                         error("too many parameters");
611                         usage_with_options(git_notes_copy_usage, options);
612                 } else {
613                         return notes_copy_from_stdin(force, rewrite_cmd);
614                 }
615         }
617         if (argc < 2) {
618                 error("too few parameters");
619                 usage_with_options(git_notes_copy_usage, options);
620         }
621         if (2 < argc) {
622                 error("too many parameters");
623                 usage_with_options(git_notes_copy_usage, options);
624         }
626         if (get_sha1(argv[0], from_obj))
627                 die("Failed to resolve '%s' as a valid ref.", argv[0]);
629         object_ref = 1 < argc ? argv[1] : "HEAD";
631         if (get_sha1(object_ref, object))
632                 die("Failed to resolve '%s' as a valid ref.", object_ref);
634         t = init_notes_check("copy");
635         note = get_note(t, object);
637         if (note) {
638                 if (!force) {
639                         retval = error("Cannot copy notes. Found existing "
640                                        "notes for object %s. Use '-f' to "
641                                        "overwrite existing notes",
642                                        sha1_to_hex(object));
643                         goto out;
644                 }
645                 fprintf(stderr, "Overwriting existing notes for object %s\n",
646                         sha1_to_hex(object));
647         }
649         from_note = get_note(t, from_obj);
650         if (!from_note) {
651                 retval = error("Missing notes on source object %s. Cannot "
652                                "copy.", sha1_to_hex(from_obj));
653                 goto out;
654         }
656         if (add_note(t, object, from_note, combine_notes_overwrite))
657                 die("BUG: combine_notes_overwrite failed");
658         commit_notes(t, "Notes added by 'git notes copy'");
659 out:
660         free_notes(t);
661         return retval;
664 static int append_edit(int argc, const char **argv, const char *prefix)
666         const char *object_ref;
667         struct notes_tree *t;
668         unsigned char object[20], new_note[20];
669         const unsigned char *note;
670         char logmsg[100];
671         const char * const *usage;
672         struct msg_arg msg = { 0, 0, STRBUF_INIT };
673         struct option options[] = {
674                 { OPTION_CALLBACK, 'm', "message", &msg, "MSG",
675                         "note contents as a string", PARSE_OPT_NONEG,
676                         parse_msg_arg},
677                 { OPTION_CALLBACK, 'F', "file", &msg, "FILE",
678                         "note contents in a file", PARSE_OPT_NONEG,
679                         parse_file_arg},
680                 { OPTION_CALLBACK, 'c', "reedit-message", &msg, "OBJECT",
681                         "reuse and edit specified note object", PARSE_OPT_NONEG,
682                         parse_reedit_arg},
683                 { OPTION_CALLBACK, 'C', "reuse-message", &msg, "OBJECT",
684                         "reuse specified note object", PARSE_OPT_NONEG,
685                         parse_reuse_arg},
686                 OPT_END()
687         };
688         int edit = !strcmp(argv[0], "edit");
690         usage = edit ? git_notes_edit_usage : git_notes_append_usage;
691         argc = parse_options(argc, argv, prefix, options, usage,
692                              PARSE_OPT_KEEP_ARGV0);
694         if (2 < argc) {
695                 error("too many parameters");
696                 usage_with_options(usage, options);
697         }
699         if (msg.given && edit)
700                 fprintf(stderr, "The -m/-F/-c/-C options have been deprecated "
701                         "for the 'edit' subcommand.\n"
702                         "Please use 'git notes add -f -m/-F/-c/-C' instead.\n");
704         object_ref = 1 < argc ? argv[1] : "HEAD";
706         if (get_sha1(object_ref, object))
707                 die("Failed to resolve '%s' as a valid ref.", object_ref);
709         t = init_notes_check(argv[0]);
710         note = get_note(t, object);
712         create_note(object, &msg, !edit, note, new_note);
714         if (is_null_sha1(new_note))
715                 remove_note(t, object);
716         else if (add_note(t, object, new_note, combine_notes_overwrite))
717                 die("BUG: combine_notes_overwrite failed");
719         snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
720                  is_null_sha1(new_note) ? "removed" : "added", argv[0]);
721         commit_notes(t, logmsg);
722         free_notes(t);
723         strbuf_release(&(msg.buf));
724         return 0;
727 static int show(int argc, const char **argv, const char *prefix)
729         const char *object_ref;
730         struct notes_tree *t;
731         unsigned char object[20];
732         const unsigned char *note;
733         int retval;
734         struct option options[] = {
735                 OPT_END()
736         };
738         argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
739                              0);
741         if (1 < argc) {
742                 error("too many parameters");
743                 usage_with_options(git_notes_show_usage, options);
744         }
746         object_ref = argc ? argv[0] : "HEAD";
748         if (get_sha1(object_ref, object))
749                 die("Failed to resolve '%s' as a valid ref.", object_ref);
751         t = init_notes_check("show");
752         note = get_note(t, object);
754         if (!note)
755                 retval = error("No note found for object %s.",
756                                sha1_to_hex(object));
757         else {
758                 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
759                 retval = execv_git_cmd(show_args);
760         }
761         free_notes(t);
762         return retval;
765 static int remove_cmd(int argc, const char **argv, const char *prefix)
767         struct option options[] = {
768                 OPT_END()
769         };
770         const char *object_ref;
771         struct notes_tree *t;
772         unsigned char object[20];
774         argc = parse_options(argc, argv, prefix, options,
775                              git_notes_remove_usage, 0);
777         if (1 < argc) {
778                 error("too many parameters");
779                 usage_with_options(git_notes_remove_usage, options);
780         }
782         object_ref = argc ? argv[0] : "HEAD";
784         if (get_sha1(object_ref, object))
785                 die("Failed to resolve '%s' as a valid ref.", object_ref);
787         t = init_notes_check("remove");
789         fprintf(stderr, "Removing note for object %s\n", sha1_to_hex(object));
790         remove_note(t, object);
792         commit_notes(t, "Notes removed by 'git notes remove'");
793         free_notes(t);
794         return 0;
797 static int prune(int argc, const char **argv, const char *prefix)
799         struct notes_tree *t;
800         int show_only = 0, verbose = 0;
801         struct option options[] = {
802                 OPT_BOOLEAN('n', "dry-run", &show_only,
803                             "do not remove, show only"),
804                 OPT_BOOLEAN('v', "verbose", &verbose, "report pruned notes"),
805                 OPT_END()
806         };
808         argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
809                              0);
811         if (argc) {
812                 error("too many parameters");
813                 usage_with_options(git_notes_prune_usage, options);
814         }
816         t = init_notes_check("prune");
818         prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
819                 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
820         if (!show_only)
821                 commit_notes(t, "Notes removed by 'git notes prune'");
822         free_notes(t);
823         return 0;
826 int cmd_notes(int argc, const char **argv, const char *prefix)
828         int result;
829         const char *override_notes_ref = NULL;
830         struct option options[] = {
831                 OPT_STRING(0, "ref", &override_notes_ref, "notes_ref",
832                            "use notes from <notes_ref>"),
833                 OPT_END()
834         };
836         git_config(git_default_config, NULL);
837         argc = parse_options(argc, argv, prefix, options, git_notes_usage,
838                              PARSE_OPT_STOP_AT_NON_OPTION);
840         if (override_notes_ref) {
841                 struct strbuf sb = STRBUF_INIT;
842                 if (!prefixcmp(override_notes_ref, "refs/notes/"))
843                         /* we're happy */;
844                 else if (!prefixcmp(override_notes_ref, "notes/"))
845                         strbuf_addstr(&sb, "refs/");
846                 else
847                         strbuf_addstr(&sb, "refs/notes/");
848                 strbuf_addstr(&sb, override_notes_ref);
849                 setenv("GIT_NOTES_REF", sb.buf, 1);
850                 strbuf_release(&sb);
851         }
853         if (argc < 1 || !strcmp(argv[0], "list"))
854                 result = list(argc, argv, prefix);
855         else if (!strcmp(argv[0], "add"))
856                 result = add(argc, argv, prefix);
857         else if (!strcmp(argv[0], "copy"))
858                 result = copy(argc, argv, prefix);
859         else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
860                 result = append_edit(argc, argv, prefix);
861         else if (!strcmp(argv[0], "show"))
862                 result = show(argc, argv, prefix);
863         else if (!strcmp(argv[0], "remove"))
864                 result = remove_cmd(argc, argv, prefix);
865         else if (!strcmp(argv[0], "prune"))
866                 result = prune(argc, argv, prefix);
867         else {
868                 result = error("Unknown subcommand: %s", argv[0]);
869                 usage_with_options(git_notes_usage, options);
870         }
872         return result ? 1 : 0;