notes: rework subcommands and parse options
[git/ericb.git] / builtin-notes.c
blob2c7351e1e53de98efa3bb444bb873c86ce342d85
1 /*
2 * Builtin "git notes"
4 * Copyright (c) 2010 Johan Herland <johan@herland.net>
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",
30 NULL
33 static const char * const git_notes_list_usage[] = {
34 "git notes [list [<object>]]",
35 NULL
38 static const char * const git_notes_add_usage[] = {
39 "git notes add [<options>] [<object>]",
40 NULL
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
49 static const char * const git_notes_append_usage[] = {
50 "git notes append [<options>] [<object>]",
51 NULL
54 static const char * const git_notes_edit_usage[] = {
55 "git notes edit [<object>]",
56 NULL
59 static const char * const git_notes_show_usage[] = {
60 "git notes show [<object>]",
61 NULL
64 static const char * const git_notes_remove_usage[] = {
65 "git notes remove [<object>]",
66 NULL
69 static const char * const git_notes_prune_usage[] = {
70 "git notes prune",
71 NULL
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;
86 static int list_each_note(const unsigned char *object_sha1,
87 const unsigned char *note_sha1, char *note_path,
88 void *cb_data)
90 printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
91 return 0;
94 static void write_note_data(int fd, const unsigned char *sha1)
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);
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);
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");
175 stripspace(&(msg->buf), 1);
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);
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);
206 if (path) {
207 unlink_or_warn(path);
208 free(path);
212 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
214 struct msg_arg *msg = opt->value;
216 if (!arg)
217 return -1;
219 strbuf_grow(&(msg->buf), strlen(arg) + 2);
220 if (msg->buf.len)
221 strbuf_addstr(&(msg->buf), "\n");
222 strbuf_addstr(&(msg->buf), arg);
223 stripspace(&(msg->buf), 0);
225 msg->given = 1;
226 return 0;
229 static int parse_file_arg(const struct option *opt, const char *arg, int unset)
231 struct msg_arg *msg = opt->value;
233 if (!arg)
234 return -1;
236 if (msg->buf.len)
237 strbuf_addstr(&(msg->buf), "\n");
238 if (!strcmp(arg, "-")) {
239 if (strbuf_read(&(msg->buf), 0, 1024) < 0)
240 die_errno("cannot read '%s'", arg);
241 } else if (strbuf_read_file(&(msg->buf), arg, 1024) < 0)
242 die_errno("could not open or read '%s'", arg);
243 stripspace(&(msg->buf), 0);
245 msg->given = 1;
246 return 0;
249 static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
251 struct msg_arg *msg = opt->value;
252 char *buf;
253 unsigned char object[20];
254 enum object_type type;
255 unsigned long len;
257 if (!arg)
258 return -1;
260 if (msg->buf.len)
261 strbuf_addstr(&(msg->buf), "\n");
263 if (get_sha1(arg, object))
264 die("Failed to resolve '%s' as a valid ref.", arg);
265 if (!(buf = read_sha1_file(object, &type, &len)) || !len) {
266 free(buf);
267 die("Failed to read object '%s'.", arg);;
269 strbuf_add(&(msg->buf), buf, len);
270 free(buf);
272 msg->given = 1;
273 return 0;
276 static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
278 struct msg_arg *msg = opt->value;
279 msg->use_editor = 1;
280 return parse_reuse_arg(opt, arg, unset);
283 int commit_notes(struct notes_tree *t, const char *msg)
285 struct commit_list *parent;
286 unsigned char tree_sha1[20], prev_commit[20], new_commit[20];
287 struct strbuf buf = STRBUF_INIT;
289 if (!t)
290 t = &default_notes_tree;
291 if (!t->initialized || !t->ref || !*t->ref)
292 die("Cannot commit uninitialized/unreferenced notes tree");
293 if (!t->dirty)
294 return 0; /* don't have to commit an unchanged tree */
296 /* Prepare commit message and reflog message */
297 strbuf_addstr(&buf, "notes: "); /* commit message starts at index 7 */
298 strbuf_addstr(&buf, msg);
299 if (buf.buf[buf.len - 1] != '\n')
300 strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
302 /* Convert notes tree to tree object */
303 if (write_notes_tree(t, tree_sha1))
304 die("Failed to write current notes tree to database");
306 /* Create new commit for the tree object */
307 if (!read_ref(t->ref, prev_commit)) { /* retrieve parent commit */
308 parent = xmalloc(sizeof(*parent));
309 parent->item = lookup_commit(prev_commit);
310 parent->next = NULL;
311 } else {
312 hashclr(prev_commit);
313 parent = NULL;
315 if (commit_tree(buf.buf + 7, tree_sha1, parent, new_commit, NULL))
316 die("Failed to commit notes tree to database");
318 /* Update notes ref with new commit */
319 update_ref(buf.buf, t->ref, new_commit, prev_commit, 0, DIE_ON_ERR);
321 strbuf_release(&buf);
322 return 0;
325 combine_notes_fn *parse_combine_notes_fn(const char *v)
327 if (!strcasecmp(v, "overwrite"))
328 return combine_notes_overwrite;
329 else if (!strcasecmp(v, "ignore"))
330 return combine_notes_ignore;
331 else if (!strcasecmp(v, "concatenate"))
332 return combine_notes_concatenate;
333 else
334 return NULL;
337 static int notes_rewrite_config(const char *k, const char *v, void *cb)
339 struct notes_rewrite_cfg *c = cb;
340 if (!prefixcmp(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
341 c->enabled = git_config_bool(k, v);
342 return 0;
343 } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
344 if (!v)
345 config_error_nonbool(k);
346 c->combine = parse_combine_notes_fn(v);
347 if (!c->combine) {
348 error("Bad notes.rewriteMode value: '%s'", v);
349 return 1;
351 return 0;
352 } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
353 /* note that a refs/ prefix is implied in the
354 * underlying for_each_glob_ref */
355 if (!prefixcmp(v, "refs/notes/"))
356 string_list_add_refs_by_glob(c->refs, v);
357 else
358 warning("Refusing to rewrite notes in %s"
359 " (outside of refs/notes/)", v);
360 return 0;
363 return 0;
367 struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
369 struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
370 const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
371 const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
372 c->cmd = cmd;
373 c->enabled = 1;
374 c->combine = combine_notes_concatenate;
375 c->refs = xcalloc(1, sizeof(struct string_list));
376 c->refs->strdup_strings = 1;
377 c->refs_from_env = 0;
378 c->mode_from_env = 0;
379 if (rewrite_mode_env) {
380 c->mode_from_env = 1;
381 c->combine = parse_combine_notes_fn(rewrite_mode_env);
382 if (!c->combine)
383 error("Bad " GIT_NOTES_REWRITE_MODE_ENVIRONMENT
384 " value: '%s'", rewrite_mode_env);
386 if (rewrite_refs_env) {
387 c->refs_from_env = 1;
388 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
390 git_config(notes_rewrite_config, c);
391 if (!c->enabled || !c->refs->nr) {
392 string_list_clear(c->refs, 0);
393 free(c->refs);
394 free(c);
395 return NULL;
397 c->trees = load_notes_trees(c->refs);
398 string_list_clear(c->refs, 0);
399 free(c->refs);
400 return c;
403 int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
404 const unsigned char *from_obj, const unsigned char *to_obj)
406 int ret = 0;
407 int i;
408 for (i = 0; c->trees[i]; i++)
409 ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
410 return ret;
413 void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c)
415 int i;
416 for (i = 0; c->trees[i]; i++) {
417 commit_notes(c->trees[i], "Notes added by 'git notes copy'");
418 free_notes(c->trees[i]);
420 free(c->trees);
421 free(c);
424 int notes_copy_from_stdin(int force, const char *rewrite_cmd)
426 struct strbuf buf = STRBUF_INIT;
427 struct notes_rewrite_cfg *c = NULL;
428 struct notes_tree *t;
429 int ret = 0;
431 if (rewrite_cmd) {
432 c = init_copy_notes_for_rewrite(rewrite_cmd);
433 if (!c)
434 return 0;
435 } else {
436 init_notes(NULL, NULL, NULL, 0);
437 t = &default_notes_tree;
440 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
441 unsigned char from_obj[20], to_obj[20];
442 struct strbuf **split;
443 int err;
445 split = strbuf_split(&buf, ' ');
446 if (!split[0] || !split[1])
447 die("Malformed input line: '%s'.", buf.buf);
448 strbuf_rtrim(split[0]);
449 strbuf_rtrim(split[1]);
450 if (get_sha1(split[0]->buf, from_obj))
451 die("Failed to resolve '%s' as a valid ref.", split[0]->buf);
452 if (get_sha1(split[1]->buf, to_obj))
453 die("Failed to resolve '%s' as a valid ref.", split[1]->buf);
455 if (rewrite_cmd)
456 err = copy_note_for_rewrite(c, from_obj, to_obj);
457 else
458 err = copy_note(t, from_obj, to_obj, force,
459 combine_notes_overwrite);
461 if (err) {
462 error("Failed to copy notes from '%s' to '%s'",
463 split[0]->buf, split[1]->buf);
464 ret = 1;
467 strbuf_list_free(split);
470 if (!rewrite_cmd) {
471 commit_notes(t, "Notes added by 'git notes copy'");
472 free_notes(t);
473 } else {
474 finish_copy_notes_for_rewrite(c);
476 return ret;
479 static struct notes_tree *init_notes_check(const char *subcommand)
481 struct notes_tree *t;
482 init_notes(NULL, NULL, NULL, 0);
483 t = &default_notes_tree;
485 if (prefixcmp(t->ref, "refs/notes/"))
486 die("Refusing to %s notes in %s (outside of refs/notes/)",
487 subcommand, t->ref);
488 return t;
491 static int list(int argc, const char **argv, const char *prefix)
493 struct notes_tree *t;
494 unsigned char object[20];
495 const unsigned char *note;
496 int retval = -1;
497 struct option options[] = {
498 OPT_END()
501 if (argc)
502 argc = parse_options(argc, argv, prefix, options,
503 git_notes_list_usage, 0);
505 if (1 < argc) {
506 error("too many parameters");
507 usage_with_options(git_notes_list_usage, options);
510 t = init_notes_check("list");
511 if (argc) {
512 if (get_sha1(argv[0], object))
513 die("Failed to resolve '%s' as a valid ref.", argv[0]);
514 note = get_note(t, object);
515 if (note) {
516 puts(sha1_to_hex(note));
517 retval = 0;
518 } else
519 retval = error("No note found for object %s.",
520 sha1_to_hex(object));
521 } else
522 retval = for_each_note(t, 0, list_each_note, NULL);
524 free_notes(t);
525 return retval;
528 static int add(int argc, const char **argv, const char *prefix)
530 int retval = 0, force = 0;
531 const char *object_ref;
532 struct notes_tree *t;
533 unsigned char object[20], new_note[20];
534 char logmsg[100];
535 const unsigned char *note;
536 struct msg_arg msg = { 0, 0, STRBUF_INIT };
537 struct option options[] = {
538 { OPTION_CALLBACK, 'm', "message", &msg, "MSG",
539 "note contents as a string", PARSE_OPT_NONEG,
540 parse_msg_arg},
541 { OPTION_CALLBACK, 'F', "file", &msg, "FILE",
542 "note contents in a file", PARSE_OPT_NONEG,
543 parse_file_arg},
544 { OPTION_CALLBACK, 'c', "reedit-message", &msg, "OBJECT",
545 "reuse and edit specified note object", PARSE_OPT_NONEG,
546 parse_reedit_arg},
547 { OPTION_CALLBACK, 'C', "reuse-message", &msg, "OBJECT",
548 "reuse specified note object", PARSE_OPT_NONEG,
549 parse_reuse_arg},
550 OPT_BOOLEAN('f', "force", &force, "replace existing notes"),
551 OPT_END()
554 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
557 if (1 < argc) {
558 error("too many parameters");
559 usage_with_options(git_notes_add_usage, options);
562 object_ref = argc ? argv[0] : "HEAD";
564 if (get_sha1(object_ref, object))
565 die("Failed to resolve '%s' as a valid ref.", object_ref);
567 t = init_notes_check("add");
568 note = get_note(t, object);
570 if (note) {
571 if (!force) {
572 retval = error("Cannot add notes. Found existing notes "
573 "for object %s. Use '-f' to overwrite "
574 "existing notes", sha1_to_hex(object));
575 goto out;
577 fprintf(stderr, "Overwriting existing notes for object %s\n",
578 sha1_to_hex(object));
581 create_note(object, &msg, 0, note, new_note);
583 if (is_null_sha1(new_note))
584 remove_note(t, object);
585 else
586 add_note(t, object, new_note, combine_notes_overwrite);
588 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
589 is_null_sha1(new_note) ? "removed" : "added", "add");
590 commit_notes(t, logmsg);
591 out:
592 free_notes(t);
593 strbuf_release(&(msg.buf));
594 return retval;
597 static int copy(int argc, const char **argv, const char *prefix)
599 int retval = 0, force = 0, from_stdin = 0;
600 const unsigned char *from_note, *note;
601 const char *object_ref;
602 unsigned char object[20], from_obj[20];
603 struct notes_tree *t;
604 const char *rewrite_cmd = NULL;
605 struct option options[] = {
606 OPT_BOOLEAN('f', "force", &force, "replace existing notes"),
607 OPT_BOOLEAN(0, "stdin", &from_stdin, "read objects from stdin"),
608 OPT_STRING(0, "for-rewrite", &rewrite_cmd, "command",
609 "load rewriting config for <command> (implies "
610 "--stdin)"),
611 OPT_END()
614 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
617 if (from_stdin || rewrite_cmd) {
618 if (argc) {
619 error("too many parameters");
620 usage_with_options(git_notes_copy_usage, options);
621 } else {
622 return notes_copy_from_stdin(force, rewrite_cmd);
626 if (2 < argc) {
627 error("too many parameters");
628 usage_with_options(git_notes_copy_usage, options);
631 if (get_sha1(argv[0], from_obj))
632 die("Failed to resolve '%s' as a valid ref.", argv[0]);
634 object_ref = 1 < argc ? argv[1] : "HEAD";
636 if (get_sha1(object_ref, object))
637 die("Failed to resolve '%s' as a valid ref.", object_ref);
639 t = init_notes_check("copy");
640 note = get_note(t, object);
642 if (note) {
643 if (!force) {
644 retval = error("Cannot copy notes. Found existing "
645 "notes for object %s. Use '-f' to "
646 "overwrite existing notes",
647 sha1_to_hex(object));
648 goto out;
650 fprintf(stderr, "Overwriting existing notes for object %s\n",
651 sha1_to_hex(object));
654 from_note = get_note(t, from_obj);
655 if (!from_note) {
656 retval = error("Missing notes on source object %s. Cannot "
657 "copy.", sha1_to_hex(from_obj));
658 goto out;
661 add_note(t, object, from_note, combine_notes_overwrite);
662 commit_notes(t, "Notes added by 'git notes copy'");
663 out:
664 free_notes(t);
665 return retval;
668 static int append_edit(int argc, const char **argv, const char *prefix)
670 const char *object_ref;
671 struct notes_tree *t;
672 unsigned char object[20], new_note[20];
673 const unsigned char *note;
674 char logmsg[100];
675 const char * const *usage;
676 struct msg_arg msg = { 0, 0, STRBUF_INIT };
677 struct option options[] = {
678 { OPTION_CALLBACK, 'm', "message", &msg, "MSG",
679 "note contents as a string", PARSE_OPT_NONEG,
680 parse_msg_arg},
681 { OPTION_CALLBACK, 'F', "file", &msg, "FILE",
682 "note contents in a file", PARSE_OPT_NONEG,
683 parse_file_arg},
684 { OPTION_CALLBACK, 'c', "reedit-message", &msg, "OBJECT",
685 "reuse and edit specified note object", PARSE_OPT_NONEG,
686 parse_reedit_arg},
687 { OPTION_CALLBACK, 'C', "reuse-message", &msg, "OBJECT",
688 "reuse specified note object", PARSE_OPT_NONEG,
689 parse_reuse_arg},
690 OPT_END()
692 int edit = !strcmp(argv[0], "edit");
694 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
695 argc = parse_options(argc, argv, prefix, options, usage,
696 PARSE_OPT_KEEP_ARGV0);
698 if (2 < argc) {
699 error("too many parameters");
700 usage_with_options(usage, options);
703 if (msg.given && edit)
704 fprintf(stderr, "The -m/-F/-c/-C options have been deprecated "
705 "for the 'edit' subcommand.\n"
706 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n");
708 object_ref = 1 < argc ? argv[1] : "HEAD";
710 if (get_sha1(object_ref, object))
711 die("Failed to resolve '%s' as a valid ref.", object_ref);
713 t = init_notes_check(argv[0]);
714 note = get_note(t, object);
716 create_note(object, &msg, !edit, note, new_note);
718 if (is_null_sha1(new_note))
719 remove_note(t, object);
720 else
721 add_note(t, object, new_note, combine_notes_overwrite);
723 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
724 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
725 commit_notes(t, logmsg);
726 free_notes(t);
727 strbuf_release(&(msg.buf));
728 return 0;
731 static int show(int argc, const char **argv, const char *prefix)
733 const char *object_ref;
734 struct notes_tree *t;
735 unsigned char object[20];
736 const unsigned char *note;
737 int retval;
738 struct option options[] = {
739 OPT_END()
742 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
745 if (1 < argc) {
746 error("too many parameters");
747 usage_with_options(git_notes_show_usage, options);
750 object_ref = argc ? argv[0] : "HEAD";
752 if (get_sha1(object_ref, object))
753 die("Failed to resolve '%s' as a valid ref.", object_ref);
755 t = init_notes_check("show");
756 note = get_note(t, object);
758 if (!note)
759 retval = error("No note found for object %s.",
760 sha1_to_hex(object));
761 else {
762 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
763 retval = execv_git_cmd(show_args);
765 free_notes(t);
766 return retval;
769 static int remove_cmd(int argc, const char **argv, const char *prefix)
771 struct option options[] = {
772 OPT_END()
774 const char *object_ref;
775 struct notes_tree *t;
776 unsigned char object[20];
778 argc = parse_options(argc, argv, prefix, options,
779 git_notes_remove_usage, 0);
781 if (1 < argc) {
782 error("too many parameters");
783 usage_with_options(git_notes_remove_usage, options);
786 object_ref = argc ? argv[0] : "HEAD";
788 if (get_sha1(object_ref, object))
789 die("Failed to resolve '%s' as a valid ref.", object_ref);
791 t = init_notes_check("remove");
793 fprintf(stderr, "Removing note for object %s\n", sha1_to_hex(object));
794 remove_note(t, object);
796 commit_notes(t, "Notes removed by 'git notes remove'");
797 free_notes(t);
798 return 0;
801 static int prune(int argc, const char **argv, const char *prefix)
803 struct notes_tree *t;
804 struct option options[] = {
805 OPT_END()
808 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
811 if (argc) {
812 error("too many parameters");
813 usage_with_options(git_notes_prune_usage, options);
816 t = init_notes_check("prune");
818 prune_notes(t);
819 commit_notes(t, "Notes removed by 'git notes prune'");
820 free_notes(t);
821 return 0;
824 int cmd_notes(int argc, const char **argv, const char *prefix)
826 int result;
827 const char *override_notes_ref = NULL;
828 struct option options[] = {
829 OPT_STRING(0, "ref", &override_notes_ref, "notes_ref",
830 "use notes from <notes_ref>"),
831 OPT_END()
834 git_config(git_default_config, NULL);
835 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
836 PARSE_OPT_STOP_AT_NON_OPTION);
838 if (override_notes_ref) {
839 struct strbuf sb = STRBUF_INIT;
840 if (!prefixcmp(override_notes_ref, "refs/notes/"))
841 /* we're happy */;
842 else if (!prefixcmp(override_notes_ref, "notes/"))
843 strbuf_addstr(&sb, "refs/");
844 else
845 strbuf_addstr(&sb, "refs/notes/");
846 strbuf_addstr(&sb, override_notes_ref);
847 setenv("GIT_NOTES_REF", sb.buf, 1);
848 strbuf_release(&sb);
851 if (argc < 1 || !strcmp(argv[0], "list"))
852 result = list(argc, argv, prefix);
853 else if (!strcmp(argv[0], "add"))
854 result = add(argc, argv, prefix);
855 else if (!strcmp(argv[0], "copy"))
856 result = copy(argc, argv, prefix);
857 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
858 result = append_edit(argc, argv, prefix);
859 else if (!strcmp(argv[0], "show"))
860 result = show(argc, argv, prefix);
861 else if (!strcmp(argv[0], "remove"))
862 result = remove_cmd(argc, argv, prefix);
863 else if (!strcmp(argv[0], "prune"))
864 result = prune(argc, argv, prefix);
865 else {
866 result = error("Unknown subcommand: %s", argv[0]);
867 usage_with_options(git_notes_usage, options);
870 return result ? 1 : 0;