commit: error out for missing commit message template
[git/dscho.git] / builtin / notes.c
blob6d07aac80cc2bfd042eee905abb5086a29e1d90a
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 [-n | -v]",
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 [<options>]",
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 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);;
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;
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;
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;
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);
377 if (rewrite_refs_env) {
378 c->refs_from_env = 1;
379 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
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;
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]);
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;
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;
458 strbuf_list_free(split);
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);
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()
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);
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()
545 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
548 if (1 < argc) {
549 error("too many parameters");
550 usage_with_options(git_notes_add_usage, options);
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;
568 fprintf(stderr, "Overwriting existing notes for object %s\n",
569 sha1_to_hex(object));
572 create_note(object, &msg, 0, note, new_note);
574 if (is_null_sha1(new_note))
575 remove_note(t, object);
576 else
577 add_note(t, object, new_note, combine_notes_overwrite);
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()
605 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
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);
617 if (argc < 2) {
618 error("too few parameters");
619 usage_with_options(git_notes_copy_usage, options);
621 if (2 < argc) {
622 error("too many parameters");
623 usage_with_options(git_notes_copy_usage, options);
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;
645 fprintf(stderr, "Overwriting existing notes for object %s\n",
646 sha1_to_hex(object));
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;
656 add_note(t, object, from_note, combine_notes_overwrite);
657 commit_notes(t, "Notes added by 'git notes copy'");
658 out:
659 free_notes(t);
660 return retval;
663 static int append_edit(int argc, const char **argv, const char *prefix)
665 const char *object_ref;
666 struct notes_tree *t;
667 unsigned char object[20], new_note[20];
668 const unsigned char *note;
669 char logmsg[100];
670 const char * const *usage;
671 struct msg_arg msg = { 0, 0, STRBUF_INIT };
672 struct option options[] = {
673 { OPTION_CALLBACK, 'm', "message", &msg, "MSG",
674 "note contents as a string", PARSE_OPT_NONEG,
675 parse_msg_arg},
676 { OPTION_CALLBACK, 'F', "file", &msg, "FILE",
677 "note contents in a file", PARSE_OPT_NONEG,
678 parse_file_arg},
679 { OPTION_CALLBACK, 'c', "reedit-message", &msg, "OBJECT",
680 "reuse and edit specified note object", PARSE_OPT_NONEG,
681 parse_reedit_arg},
682 { OPTION_CALLBACK, 'C', "reuse-message", &msg, "OBJECT",
683 "reuse specified note object", PARSE_OPT_NONEG,
684 parse_reuse_arg},
685 OPT_END()
687 int edit = !strcmp(argv[0], "edit");
689 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
690 argc = parse_options(argc, argv, prefix, options, usage,
691 PARSE_OPT_KEEP_ARGV0);
693 if (2 < argc) {
694 error("too many parameters");
695 usage_with_options(usage, options);
698 if (msg.given && edit)
699 fprintf(stderr, "The -m/-F/-c/-C options have been deprecated "
700 "for the 'edit' subcommand.\n"
701 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n");
703 object_ref = 1 < argc ? argv[1] : "HEAD";
705 if (get_sha1(object_ref, object))
706 die("Failed to resolve '%s' as a valid ref.", object_ref);
708 t = init_notes_check(argv[0]);
709 note = get_note(t, object);
711 create_note(object, &msg, !edit, note, new_note);
713 if (is_null_sha1(new_note))
714 remove_note(t, object);
715 else
716 add_note(t, object, new_note, combine_notes_overwrite);
718 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
719 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
720 commit_notes(t, logmsg);
721 free_notes(t);
722 strbuf_release(&(msg.buf));
723 return 0;
726 static int show(int argc, const char **argv, const char *prefix)
728 const char *object_ref;
729 struct notes_tree *t;
730 unsigned char object[20];
731 const unsigned char *note;
732 int retval;
733 struct option options[] = {
734 OPT_END()
737 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
740 if (1 < argc) {
741 error("too many parameters");
742 usage_with_options(git_notes_show_usage, options);
745 object_ref = argc ? argv[0] : "HEAD";
747 if (get_sha1(object_ref, object))
748 die("Failed to resolve '%s' as a valid ref.", object_ref);
750 t = init_notes_check("show");
751 note = get_note(t, object);
753 if (!note)
754 retval = error("No note found for object %s.",
755 sha1_to_hex(object));
756 else {
757 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
758 retval = execv_git_cmd(show_args);
760 free_notes(t);
761 return retval;
764 static int remove_cmd(int argc, const char **argv, const char *prefix)
766 struct option options[] = {
767 OPT_END()
769 const char *object_ref;
770 struct notes_tree *t;
771 unsigned char object[20];
772 int retval;
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);
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 retval = remove_note(t, object);
790 if (retval)
791 fprintf(stderr, "Object %s has no note\n", sha1_to_hex(object));
792 else {
793 fprintf(stderr, "Removing note for object %s\n",
794 sha1_to_hex(object));
796 commit_notes(t, "Notes removed by 'git notes remove'");
798 free_notes(t);
799 return retval;
802 static int prune(int argc, const char **argv, const char *prefix)
804 struct notes_tree *t;
805 int show_only = 0, verbose = 0;
806 struct option options[] = {
807 OPT_BOOLEAN('n', "dry-run", &show_only,
808 "do not remove, show only"),
809 OPT_BOOLEAN('v', "verbose", &verbose, "report pruned notes"),
810 OPT_END()
813 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
816 if (argc) {
817 error("too many parameters");
818 usage_with_options(git_notes_prune_usage, options);
821 t = init_notes_check("prune");
823 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
824 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
825 if (!show_only)
826 commit_notes(t, "Notes removed by 'git notes prune'");
827 free_notes(t);
828 return 0;
831 int cmd_notes(int argc, const char **argv, const char *prefix)
833 int result;
834 const char *override_notes_ref = NULL;
835 struct option options[] = {
836 OPT_STRING(0, "ref", &override_notes_ref, "notes_ref",
837 "use notes from <notes_ref>"),
838 OPT_END()
841 git_config(git_default_config, NULL);
842 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
843 PARSE_OPT_STOP_AT_NON_OPTION);
845 if (override_notes_ref) {
846 struct strbuf sb = STRBUF_INIT;
847 if (!prefixcmp(override_notes_ref, "refs/notes/"))
848 /* we're happy */;
849 else if (!prefixcmp(override_notes_ref, "notes/"))
850 strbuf_addstr(&sb, "refs/");
851 else
852 strbuf_addstr(&sb, "refs/notes/");
853 strbuf_addstr(&sb, override_notes_ref);
854 setenv("GIT_NOTES_REF", sb.buf, 1);
855 strbuf_release(&sb);
858 if (argc < 1 || !strcmp(argv[0], "list"))
859 result = list(argc, argv, prefix);
860 else if (!strcmp(argv[0], "add"))
861 result = add(argc, argv, prefix);
862 else if (!strcmp(argv[0], "copy"))
863 result = copy(argc, argv, prefix);
864 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
865 result = append_edit(argc, argv, prefix);
866 else if (!strcmp(argv[0], "show"))
867 result = show(argc, argv, prefix);
868 else if (!strcmp(argv[0], "remove"))
869 result = remove_cmd(argc, argv, prefix);
870 else if (!strcmp(argv[0], "prune"))
871 result = prune(argc, argv, prefix);
872 else {
873 result = error("Unknown subcommand: %s", argv[0]);
874 usage_with_options(git_notes_usage, options);
877 return result ? 1 : 0;