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.
17 #include "run-command.h"
18 #include "parse-options.h"
19 #include "string-list.h"
20 #include "notes-merge.h"
21 #include "notes-utils.h"
23 static const char * const git_notes_usage[] = {
24 N_("git notes [--ref <notes_ref>] [list [<object>]]"),
25 N_("git notes [--ref <notes_ref>] add [-f] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
26 N_("git notes [--ref <notes_ref>] copy [-f] <from-object> <to-object>"),
27 N_("git notes [--ref <notes_ref>] append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
28 N_("git notes [--ref <notes_ref>] edit [<object>]"),
29 N_("git notes [--ref <notes_ref>] show [<object>]"),
30 N_("git notes [--ref <notes_ref>] merge [-v | -q] [-s <strategy> ] <notes_ref>"),
31 N_("git notes merge --commit [-v | -q]"),
32 N_("git notes merge --abort [-v | -q]"),
33 N_("git notes [--ref <notes_ref>] remove [<object>...]"),
34 N_("git notes [--ref <notes_ref>] prune [-n | -v]"),
35 N_("git notes [--ref <notes_ref>] get-ref"),
39 static const char * const git_notes_list_usage[] = {
40 N_("git notes [list [<object>]]"),
44 static const char * const git_notes_add_usage[] = {
45 N_("git notes add [<options>] [<object>]"),
49 static const char * const git_notes_copy_usage[] = {
50 N_("git notes copy [<options>] <from-object> <to-object>"),
51 N_("git notes copy --stdin [<from-object> <to-object>]..."),
55 static const char * const git_notes_append_usage[] = {
56 N_("git notes append [<options>] [<object>]"),
60 static const char * const git_notes_edit_usage[] = {
61 N_("git notes edit [<object>]"),
65 static const char * const git_notes_show_usage[] = {
66 N_("git notes show [<object>]"),
70 static const char * const git_notes_merge_usage[] = {
71 N_("git notes merge [<options>] <notes_ref>"),
72 N_("git notes merge --commit [<options>]"),
73 N_("git notes merge --abort [<options>]"),
77 static const char * const git_notes_remove_usage[] = {
78 N_("git notes remove [<object>]"),
82 static const char * const git_notes_prune_usage[] = {
83 N_("git notes prune [<options>]"),
87 static const char * const git_notes_get_ref_usage[] = {
88 N_("git notes get-ref"),
92 static const char note_template[] =
93 "\nWrite/edit the notes for the following object:\n";
101 static int list_each_note(const unsigned char *object_sha1,
102 const unsigned char *note_sha1, char *note_path,
105 printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
109 static void copy_obj_to_fd(int fd, const unsigned char *sha1)
112 enum object_type type;
113 char *buf = read_sha1_file(sha1, &type, &size);
116 write_or_die(fd, buf, size);
121 static void write_commented_object(int fd, const unsigned char *object)
123 const char *show_args[5] =
124 {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
125 struct child_process show = CHILD_PROCESS_INIT;
126 struct strbuf buf = STRBUF_INIT;
127 struct strbuf cbuf = STRBUF_INIT;
129 /* Invoke "git show --stat --no-notes $object" */
130 show.argv = show_args;
135 if (start_command(&show))
136 die(_("unable to start 'show' for object '%s'"),
137 sha1_to_hex(object));
139 if (strbuf_read(&buf, show.out, 0) < 0)
140 die_errno(_("could not read 'show' output"));
141 strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
142 write_or_die(fd, cbuf.buf, cbuf.len);
144 strbuf_release(&cbuf);
145 strbuf_release(&buf);
147 if (finish_command(&show))
148 die(_("failed to finish 'show' for object '%s'"),
149 sha1_to_hex(object));
152 static void create_note(const unsigned char *object, struct note_data *d,
153 int append_only, const unsigned char *prev,
154 unsigned char *result)
158 if (d->use_editor || !d->given) {
160 struct strbuf buf = STRBUF_INIT;
162 /* write the template message before editing: */
163 path = git_pathdup("NOTES_EDITMSG");
164 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
166 die_errno(_("could not create file '%s'"), path);
169 write_or_die(fd, d->buf.buf, d->buf.len);
170 else if (prev && !append_only)
171 copy_obj_to_fd(fd, prev);
173 strbuf_addch(&buf, '\n');
174 strbuf_add_commented_lines(&buf, note_template, strlen(note_template));
175 strbuf_addch(&buf, '\n');
176 write_or_die(fd, buf.buf, buf.len);
178 write_commented_object(fd, object);
181 strbuf_release(&buf);
182 strbuf_reset(&d->buf);
184 if (launch_editor(path, &d->buf, NULL)) {
185 die(_("Please supply the note contents using either -m or -F option"));
187 stripspace(&d->buf, 1);
190 if (prev && append_only) {
191 /* Append buf to previous note contents */
193 enum object_type type;
194 char *prev_buf = read_sha1_file(prev, &type, &size);
196 strbuf_grow(&d->buf, size + 1);
197 if (d->buf.len && prev_buf && size)
198 strbuf_insert(&d->buf, 0, "\n", 1);
199 if (prev_buf && size)
200 strbuf_insert(&d->buf, 0, prev_buf, size);
205 fprintf(stderr, _("Removing note for object %s\n"),
206 sha1_to_hex(object));
209 if (write_sha1_file(d->buf.buf, d->buf.len, blob_type, result)) {
210 error(_("unable to write note object"));
212 error(_("The note contents have been left in %s"),
219 unlink_or_warn(path);
224 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
226 struct note_data *d = opt->value;
228 strbuf_grow(&d->buf, strlen(arg) + 2);
230 strbuf_addch(&d->buf, '\n');
231 strbuf_addstr(&d->buf, arg);
232 stripspace(&d->buf, 0);
238 static int parse_file_arg(const struct option *opt, const char *arg, int unset)
240 struct note_data *d = opt->value;
243 strbuf_addch(&d->buf, '\n');
244 if (!strcmp(arg, "-")) {
245 if (strbuf_read(&d->buf, 0, 1024) < 0)
246 die_errno(_("cannot read '%s'"), arg);
247 } else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
248 die_errno(_("could not open or read '%s'"), arg);
249 stripspace(&d->buf, 0);
255 static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
257 struct note_data *d = opt->value;
259 unsigned char object[20];
260 enum object_type type;
264 strbuf_addch(&d->buf, '\n');
266 if (get_sha1(arg, object))
267 die(_("Failed to resolve '%s' as a valid ref."), arg);
268 if (!(buf = read_sha1_file(object, &type, &len))) {
270 die(_("Failed to read object '%s'."), arg);
272 if (type != OBJ_BLOB) {
274 die(_("Cannot read note data from non-blob object '%s'."), arg);
276 strbuf_add(&d->buf, buf, len);
283 static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
285 struct note_data *d = opt->value;
287 return parse_reuse_arg(opt, arg, unset);
290 static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
292 struct strbuf buf = STRBUF_INIT;
293 struct notes_rewrite_cfg *c = NULL;
294 struct notes_tree *t = NULL;
296 const char *msg = "Notes added by 'git notes copy'";
299 c = init_copy_notes_for_rewrite(rewrite_cmd);
303 init_notes(NULL, NULL, NULL, 0);
304 t = &default_notes_tree;
307 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
308 unsigned char from_obj[20], to_obj[20];
309 struct strbuf **split;
312 split = strbuf_split(&buf, ' ');
313 if (!split[0] || !split[1])
314 die(_("Malformed input line: '%s'."), buf.buf);
315 strbuf_rtrim(split[0]);
316 strbuf_rtrim(split[1]);
317 if (get_sha1(split[0]->buf, from_obj))
318 die(_("Failed to resolve '%s' as a valid ref."), split[0]->buf);
319 if (get_sha1(split[1]->buf, to_obj))
320 die(_("Failed to resolve '%s' as a valid ref."), split[1]->buf);
323 err = copy_note_for_rewrite(c, from_obj, to_obj);
325 err = copy_note(t, from_obj, to_obj, force,
326 combine_notes_overwrite);
329 error(_("Failed to copy notes from '%s' to '%s'"),
330 split[0]->buf, split[1]->buf);
334 strbuf_list_free(split);
338 commit_notes(t, msg);
341 finish_copy_notes_for_rewrite(c, msg);
346 static struct notes_tree *init_notes_check(const char *subcommand)
348 struct notes_tree *t;
349 init_notes(NULL, NULL, NULL, 0);
350 t = &default_notes_tree;
352 if (!starts_with(t->ref, "refs/notes/"))
353 die("Refusing to %s notes in %s (outside of refs/notes/)",
358 static int list(int argc, const char **argv, const char *prefix)
360 struct notes_tree *t;
361 unsigned char object[20];
362 const unsigned char *note;
364 struct option options[] = {
369 argc = parse_options(argc, argv, prefix, options,
370 git_notes_list_usage, 0);
373 error(_("too many parameters"));
374 usage_with_options(git_notes_list_usage, options);
377 t = init_notes_check("list");
379 if (get_sha1(argv[0], object))
380 die(_("Failed to resolve '%s' as a valid ref."), argv[0]);
381 note = get_note(t, object);
383 puts(sha1_to_hex(note));
386 retval = error(_("No note found for object %s."),
387 sha1_to_hex(object));
389 retval = for_each_note(t, 0, list_each_note, NULL);
395 static int append_edit(int argc, const char **argv, const char *prefix);
397 static int add(int argc, const char **argv, const char *prefix)
399 int retval = 0, force = 0;
400 const char *object_ref;
401 struct notes_tree *t;
402 unsigned char object[20], new_note[20];
404 const unsigned char *note;
405 struct note_data d = { 0, 0, STRBUF_INIT };
406 struct option options[] = {
407 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
408 N_("note contents as a string"), PARSE_OPT_NONEG,
410 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
411 N_("note contents in a file"), PARSE_OPT_NONEG,
413 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
414 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
416 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
417 N_("reuse specified note object"), PARSE_OPT_NONEG,
419 OPT__FORCE(&force, N_("replace existing notes")),
423 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
424 PARSE_OPT_KEEP_ARGV0);
427 error(_("too many parameters"));
428 usage_with_options(git_notes_add_usage, options);
431 object_ref = argc > 1 ? argv[1] : "HEAD";
433 if (get_sha1(object_ref, object))
434 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
436 t = init_notes_check("add");
437 note = get_note(t, object);
443 * Redirect to "edit" subcommand.
445 * We only end up here if none of -m/-F/-c/-C
446 * or -f are given. The original args are
447 * therefore still in argv[0-1].
451 return append_edit(argc, argv, prefix);
453 retval = error(_("Cannot add notes. Found existing notes "
454 "for object %s. Use '-f' to overwrite "
455 "existing notes"), sha1_to_hex(object));
458 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
459 sha1_to_hex(object));
462 create_note(object, &d, 0, note, new_note);
464 if (is_null_sha1(new_note))
465 remove_note(t, object);
466 else if (add_note(t, object, new_note, combine_notes_overwrite))
467 die("BUG: combine_notes_overwrite failed");
469 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
470 is_null_sha1(new_note) ? "removed" : "added", "add");
471 commit_notes(t, logmsg);
474 strbuf_release(&d.buf);
478 static int copy(int argc, const char **argv, const char *prefix)
480 int retval = 0, force = 0, from_stdin = 0;
481 const unsigned char *from_note, *note;
482 const char *object_ref;
483 unsigned char object[20], from_obj[20];
484 struct notes_tree *t;
485 const char *rewrite_cmd = NULL;
486 struct option options[] = {
487 OPT__FORCE(&force, N_("replace existing notes")),
488 OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")),
489 OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"),
490 N_("load rewriting config for <command> (implies "
495 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
498 if (from_stdin || rewrite_cmd) {
500 error(_("too many parameters"));
501 usage_with_options(git_notes_copy_usage, options);
503 return notes_copy_from_stdin(force, rewrite_cmd);
508 error(_("too few parameters"));
509 usage_with_options(git_notes_copy_usage, options);
512 error(_("too many parameters"));
513 usage_with_options(git_notes_copy_usage, options);
516 if (get_sha1(argv[0], from_obj))
517 die(_("Failed to resolve '%s' as a valid ref."), argv[0]);
519 object_ref = 1 < argc ? argv[1] : "HEAD";
521 if (get_sha1(object_ref, object))
522 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
524 t = init_notes_check("copy");
525 note = get_note(t, object);
529 retval = error(_("Cannot copy notes. Found existing "
530 "notes for object %s. Use '-f' to "
531 "overwrite existing notes"),
532 sha1_to_hex(object));
535 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
536 sha1_to_hex(object));
539 from_note = get_note(t, from_obj);
541 retval = error(_("Missing notes on source object %s. Cannot "
542 "copy."), sha1_to_hex(from_obj));
546 if (add_note(t, object, from_note, combine_notes_overwrite))
547 die("BUG: combine_notes_overwrite failed");
548 commit_notes(t, "Notes added by 'git notes copy'");
554 static int append_edit(int argc, const char **argv, const char *prefix)
556 const char *object_ref;
557 struct notes_tree *t;
558 unsigned char object[20], new_note[20];
559 const unsigned char *note;
561 const char * const *usage;
562 struct note_data d = { 0, 0, STRBUF_INIT };
563 struct option options[] = {
564 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
565 N_("note contents as a string"), PARSE_OPT_NONEG,
567 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
568 N_("note contents in a file"), PARSE_OPT_NONEG,
570 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
571 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
573 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
574 N_("reuse specified note object"), PARSE_OPT_NONEG,
578 int edit = !strcmp(argv[0], "edit");
580 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
581 argc = parse_options(argc, argv, prefix, options, usage,
582 PARSE_OPT_KEEP_ARGV0);
585 error(_("too many parameters"));
586 usage_with_options(usage, options);
590 fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
591 "for the 'edit' subcommand.\n"
592 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
594 object_ref = 1 < argc ? argv[1] : "HEAD";
596 if (get_sha1(object_ref, object))
597 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
599 t = init_notes_check(argv[0]);
600 note = get_note(t, object);
602 create_note(object, &d, !edit, note, new_note);
604 if (is_null_sha1(new_note))
605 remove_note(t, object);
606 else if (add_note(t, object, new_note, combine_notes_overwrite))
607 die("BUG: combine_notes_overwrite failed");
609 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
610 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
611 commit_notes(t, logmsg);
613 strbuf_release(&d.buf);
617 static int show(int argc, const char **argv, const char *prefix)
619 const char *object_ref;
620 struct notes_tree *t;
621 unsigned char object[20];
622 const unsigned char *note;
624 struct option options[] = {
628 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
632 error(_("too many parameters"));
633 usage_with_options(git_notes_show_usage, options);
636 object_ref = argc ? argv[0] : "HEAD";
638 if (get_sha1(object_ref, object))
639 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
641 t = init_notes_check("show");
642 note = get_note(t, object);
645 retval = error(_("No note found for object %s."),
646 sha1_to_hex(object));
648 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
649 retval = execv_git_cmd(show_args);
655 static int merge_abort(struct notes_merge_options *o)
660 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
661 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
664 if (delete_ref("NOTES_MERGE_PARTIAL", NULL, 0))
665 ret += error("Failed to delete ref NOTES_MERGE_PARTIAL");
666 if (delete_ref("NOTES_MERGE_REF", NULL, REF_NODEREF))
667 ret += error("Failed to delete ref NOTES_MERGE_REF");
668 if (notes_merge_abort(o))
669 ret += error("Failed to remove 'git notes merge' worktree");
673 static int merge_commit(struct notes_merge_options *o)
675 struct strbuf msg = STRBUF_INIT;
676 unsigned char sha1[20], parent_sha1[20];
677 struct notes_tree *t;
678 struct commit *partial;
679 struct pretty_print_context pretty_ctx;
680 void *local_ref_to_free;
684 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
685 * and target notes ref from .git/NOTES_MERGE_REF.
688 if (get_sha1("NOTES_MERGE_PARTIAL", sha1))
689 die("Failed to read ref NOTES_MERGE_PARTIAL");
690 else if (!(partial = lookup_commit_reference(sha1)))
691 die("Could not find commit from NOTES_MERGE_PARTIAL.");
692 else if (parse_commit(partial))
693 die("Could not parse commit from NOTES_MERGE_PARTIAL.");
695 if (partial->parents)
696 hashcpy(parent_sha1, partial->parents->item->object.sha1);
698 hashclr(parent_sha1);
700 t = xcalloc(1, sizeof(struct notes_tree));
701 init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
703 o->local_ref = local_ref_to_free =
704 resolve_refdup("NOTES_MERGE_REF", 0, sha1, NULL);
706 die("Failed to resolve NOTES_MERGE_REF");
708 if (notes_merge_commit(o, t, partial, sha1))
709 die("Failed to finalize notes merge");
711 /* Reuse existing commit message in reflog message */
712 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
713 format_commit_message(partial, "%s", &msg, &pretty_ctx);
715 strbuf_insert(&msg, 0, "notes: ", 7);
716 update_ref(msg.buf, o->local_ref, sha1,
717 is_null_sha1(parent_sha1) ? NULL : parent_sha1,
718 0, UPDATE_REFS_DIE_ON_ERR);
721 strbuf_release(&msg);
722 ret = merge_abort(o);
723 free(local_ref_to_free);
727 static int merge(int argc, const char **argv, const char *prefix)
729 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
730 unsigned char result_sha1[20];
731 struct notes_tree *t;
732 struct notes_merge_options o;
733 int do_merge = 0, do_commit = 0, do_abort = 0;
734 int verbosity = 0, result;
735 const char *strategy = NULL;
736 struct option options[] = {
737 OPT_GROUP(N_("General options")),
738 OPT__VERBOSITY(&verbosity),
739 OPT_GROUP(N_("Merge options")),
740 OPT_STRING('s', "strategy", &strategy, N_("strategy"),
741 N_("resolve notes conflicts using the given strategy "
742 "(manual/ours/theirs/union/cat_sort_uniq)")),
743 OPT_GROUP(N_("Committing unmerged notes")),
744 { OPTION_SET_INT, 0, "commit", &do_commit, NULL,
745 N_("finalize notes merge by committing unmerged notes"),
746 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
747 OPT_GROUP(N_("Aborting notes merge resolution")),
748 { OPTION_SET_INT, 0, "abort", &do_abort, NULL,
749 N_("abort notes merge"),
750 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
754 argc = parse_options(argc, argv, prefix, options,
755 git_notes_merge_usage, 0);
757 if (strategy || do_commit + do_abort == 0)
759 if (do_merge + do_commit + do_abort != 1) {
760 error("cannot mix --commit, --abort or -s/--strategy");
761 usage_with_options(git_notes_merge_usage, options);
764 if (do_merge && argc != 1) {
765 error("Must specify a notes ref to merge");
766 usage_with_options(git_notes_merge_usage, options);
767 } else if (!do_merge && argc) {
768 error("too many parameters");
769 usage_with_options(git_notes_merge_usage, options);
772 init_notes_merge_options(&o);
773 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
776 return merge_abort(&o);
778 return merge_commit(&o);
780 o.local_ref = default_notes_ref();
781 strbuf_addstr(&remote_ref, argv[0]);
782 expand_notes_ref(&remote_ref);
783 o.remote_ref = remote_ref.buf;
786 if (!strcmp(strategy, "manual"))
787 o.strategy = NOTES_MERGE_RESOLVE_MANUAL;
788 else if (!strcmp(strategy, "ours"))
789 o.strategy = NOTES_MERGE_RESOLVE_OURS;
790 else if (!strcmp(strategy, "theirs"))
791 o.strategy = NOTES_MERGE_RESOLVE_THEIRS;
792 else if (!strcmp(strategy, "union"))
793 o.strategy = NOTES_MERGE_RESOLVE_UNION;
794 else if (!strcmp(strategy, "cat_sort_uniq"))
795 o.strategy = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ;
797 error("Unknown -s/--strategy: %s", strategy);
798 usage_with_options(git_notes_merge_usage, options);
802 t = init_notes_check("merge");
804 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
805 remote_ref.buf, default_notes_ref());
806 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
808 result = notes_merge(&o, t, result_sha1);
810 if (result >= 0) /* Merge resulted (trivially) in result_sha1 */
811 /* Update default notes ref with new commit */
812 update_ref(msg.buf, default_notes_ref(), result_sha1, NULL,
813 0, UPDATE_REFS_DIE_ON_ERR);
814 else { /* Merge has unresolved conflicts */
815 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
816 update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_sha1, NULL,
817 0, UPDATE_REFS_DIE_ON_ERR);
818 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
819 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
820 die("Failed to store link to current notes ref (%s)",
821 default_notes_ref());
822 printf("Automatic notes merge failed. Fix conflicts in %s and "
823 "commit the result with 'git notes merge --commit', or "
824 "abort the merge with 'git notes merge --abort'.\n",
825 git_path(NOTES_MERGE_WORKTREE));
829 strbuf_release(&remote_ref);
830 strbuf_release(&msg);
831 return result < 0; /* return non-zero on conflicts */
834 #define IGNORE_MISSING 1
836 static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
839 unsigned char sha1[20];
840 if (get_sha1(name, sha1))
841 return error(_("Failed to resolve '%s' as a valid ref."), name);
842 status = remove_note(t, sha1);
844 fprintf(stderr, _("Object %s has no note\n"), name);
846 fprintf(stderr, _("Removing note for object %s\n"), name);
847 return (flag & IGNORE_MISSING) ? 0 : status;
850 static int remove_cmd(int argc, const char **argv, const char *prefix)
854 struct option options[] = {
855 OPT_BIT(0, "ignore-missing", &flag,
856 N_("attempt to remove non-existent note is not an error"),
858 OPT_BOOL(0, "stdin", &from_stdin,
859 N_("read object names from the standard input")),
862 struct notes_tree *t;
865 argc = parse_options(argc, argv, prefix, options,
866 git_notes_remove_usage, 0);
868 t = init_notes_check("remove");
870 if (!argc && !from_stdin) {
871 retval = remove_one_note(t, "HEAD", flag);
874 retval |= remove_one_note(t, *argv, flag);
879 struct strbuf sb = STRBUF_INIT;
880 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
882 retval |= remove_one_note(t, sb.buf, flag);
887 commit_notes(t, "Notes removed by 'git notes remove'");
892 static int prune(int argc, const char **argv, const char *prefix)
894 struct notes_tree *t;
895 int show_only = 0, verbose = 0;
896 struct option options[] = {
897 OPT__DRY_RUN(&show_only, "do not remove, show only"),
898 OPT__VERBOSE(&verbose, "report pruned notes"),
902 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
906 error(_("too many parameters"));
907 usage_with_options(git_notes_prune_usage, options);
910 t = init_notes_check("prune");
912 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
913 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
915 commit_notes(t, "Notes removed by 'git notes prune'");
920 static int get_ref(int argc, const char **argv, const char *prefix)
922 struct option options[] = { OPT_END() };
923 argc = parse_options(argc, argv, prefix, options,
924 git_notes_get_ref_usage, 0);
927 error("too many parameters");
928 usage_with_options(git_notes_get_ref_usage, options);
931 puts(default_notes_ref());
935 int cmd_notes(int argc, const char **argv, const char *prefix)
938 const char *override_notes_ref = NULL;
939 struct option options[] = {
940 OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
941 N_("use notes from <notes_ref>")),
945 git_config(git_default_config, NULL);
946 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
947 PARSE_OPT_STOP_AT_NON_OPTION);
949 if (override_notes_ref) {
950 struct strbuf sb = STRBUF_INIT;
951 strbuf_addstr(&sb, override_notes_ref);
952 expand_notes_ref(&sb);
953 setenv("GIT_NOTES_REF", sb.buf, 1);
957 if (argc < 1 || !strcmp(argv[0], "list"))
958 result = list(argc, argv, prefix);
959 else if (!strcmp(argv[0], "add"))
960 result = add(argc, argv, prefix);
961 else if (!strcmp(argv[0], "copy"))
962 result = copy(argc, argv, prefix);
963 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
964 result = append_edit(argc, argv, prefix);
965 else if (!strcmp(argv[0], "show"))
966 result = show(argc, argv, prefix);
967 else if (!strcmp(argv[0], "merge"))
968 result = merge(argc, argv, prefix);
969 else if (!strcmp(argv[0], "remove"))
970 result = remove_cmd(argc, argv, prefix);
971 else if (!strcmp(argv[0], "prune"))
972 result = prune(argc, argv, prefix);
973 else if (!strcmp(argv[0], "get-ref"))
974 result = get_ref(argc, argv, prefix);
976 result = error(_("Unknown subcommand: %s"), argv[0]);
977 usage_with_options(git_notes_usage, options);
980 return result ? 1 : 0;