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"
24 static const char * const git_notes_usage[] = {
25 N_("git notes [--ref <notes-ref>] [list [<object>]]"),
26 N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
27 N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"),
28 N_("git notes [--ref <notes-ref>] append [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
29 N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
30 N_("git notes [--ref <notes-ref>] show [<object>]"),
31 N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"),
32 N_("git notes merge --commit [-v | -q]"),
33 N_("git notes merge --abort [-v | -q]"),
34 N_("git notes [--ref <notes-ref>] remove [<object>...]"),
35 N_("git notes [--ref <notes-ref>] prune [-n | -v]"),
36 N_("git notes [--ref <notes-ref>] get-ref"),
40 static const char * const git_notes_list_usage[] = {
41 N_("git notes [list [<object>]]"),
45 static const char * const git_notes_add_usage[] = {
46 N_("git notes add [<options>] [<object>]"),
50 static const char * const git_notes_copy_usage[] = {
51 N_("git notes copy [<options>] <from-object> <to-object>"),
52 N_("git notes copy --stdin [<from-object> <to-object>]..."),
56 static const char * const git_notes_append_usage[] = {
57 N_("git notes append [<options>] [<object>]"),
61 static const char * const git_notes_edit_usage[] = {
62 N_("git notes edit [<object>]"),
66 static const char * const git_notes_show_usage[] = {
67 N_("git notes show [<object>]"),
71 static const char * const git_notes_merge_usage[] = {
72 N_("git notes merge [<options>] <notes-ref>"),
73 N_("git notes merge --commit [<options>]"),
74 N_("git notes merge --abort [<options>]"),
78 static const char * const git_notes_remove_usage[] = {
79 N_("git notes remove [<object>]"),
83 static const char * const git_notes_prune_usage[] = {
84 N_("git notes prune [<options>]"),
88 static const char * const git_notes_get_ref_usage[] = {
89 N_("git notes get-ref"),
93 static const char note_template[] =
94 N_("Write/edit the notes for the following object:");
103 static void free_note_data(struct note_data *d)
106 unlink_or_warn(d->edit_path);
109 strbuf_release(&d->buf);
112 static int list_each_note(const unsigned char *object_sha1,
113 const unsigned char *note_sha1, char *note_path,
116 printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
120 static void copy_obj_to_fd(int fd, const unsigned char *sha1)
123 enum object_type type;
124 char *buf = read_sha1_file(sha1, &type, &size);
127 write_or_die(fd, buf, size);
132 static void write_commented_object(int fd, const unsigned char *object)
134 const char *show_args[5] =
135 {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
136 struct child_process show = CHILD_PROCESS_INIT;
137 struct strbuf buf = STRBUF_INIT;
138 struct strbuf cbuf = STRBUF_INIT;
140 /* Invoke "git show --stat --no-notes $object" */
141 show.argv = show_args;
146 if (start_command(&show))
147 die(_("unable to start 'show' for object '%s'"),
148 sha1_to_hex(object));
150 if (strbuf_read(&buf, show.out, 0) < 0)
151 die_errno(_("could not read 'show' output"));
152 strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
153 write_or_die(fd, cbuf.buf, cbuf.len);
155 strbuf_release(&cbuf);
156 strbuf_release(&buf);
158 if (finish_command(&show))
159 die(_("failed to finish 'show' for object '%s'"),
160 sha1_to_hex(object));
163 static void prepare_note_data(const unsigned char *object, struct note_data *d,
164 const unsigned char *old_note)
166 if (d->use_editor || !d->given) {
168 struct strbuf buf = STRBUF_INIT;
170 /* write the template message before editing: */
171 d->edit_path = git_pathdup("NOTES_EDITMSG");
172 fd = open(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
174 die_errno(_("could not create file '%s'"), d->edit_path);
177 write_or_die(fd, d->buf.buf, d->buf.len);
179 copy_obj_to_fd(fd, old_note);
181 strbuf_addch(&buf, '\n');
182 strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
183 strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)));
184 strbuf_addch(&buf, '\n');
185 write_or_die(fd, buf.buf, buf.len);
187 write_commented_object(fd, object);
190 strbuf_release(&buf);
191 strbuf_reset(&d->buf);
193 if (launch_editor(d->edit_path, &d->buf, NULL)) {
194 die(_("please supply the note contents using either -m or -F option"));
196 strbuf_stripspace(&d->buf, 1);
200 static void write_note_data(struct note_data *d, unsigned char *sha1)
202 if (write_sha1_file(d->buf.buf, d->buf.len, blob_type, sha1)) {
203 error(_("unable to write note object"));
205 error(_("the note contents have been left in %s"),
211 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
213 struct note_data *d = opt->value;
215 strbuf_grow(&d->buf, strlen(arg) + 2);
217 strbuf_addch(&d->buf, '\n');
218 strbuf_addstr(&d->buf, arg);
219 strbuf_stripspace(&d->buf, 0);
225 static int parse_file_arg(const struct option *opt, const char *arg, int unset)
227 struct note_data *d = opt->value;
230 strbuf_addch(&d->buf, '\n');
231 if (!strcmp(arg, "-")) {
232 if (strbuf_read(&d->buf, 0, 1024) < 0)
233 die_errno(_("cannot read '%s'"), arg);
234 } else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
235 die_errno(_("could not open or read '%s'"), arg);
236 strbuf_stripspace(&d->buf, 0);
242 static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
244 struct note_data *d = opt->value;
246 unsigned char object[20];
247 enum object_type type;
251 strbuf_addch(&d->buf, '\n');
253 if (get_sha1(arg, object))
254 die(_("failed to resolve '%s' as a valid ref."), arg);
255 if (!(buf = read_sha1_file(object, &type, &len))) {
257 die(_("failed to read object '%s'."), arg);
259 if (type != OBJ_BLOB) {
261 die(_("cannot read note data from non-blob object '%s'."), arg);
263 strbuf_add(&d->buf, buf, len);
270 static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
272 struct note_data *d = opt->value;
274 return parse_reuse_arg(opt, arg, unset);
277 static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
279 struct strbuf buf = STRBUF_INIT;
280 struct notes_rewrite_cfg *c = NULL;
281 struct notes_tree *t = NULL;
283 const char *msg = "Notes added by 'git notes copy'";
286 c = init_copy_notes_for_rewrite(rewrite_cmd);
290 init_notes(NULL, NULL, NULL, NOTES_INIT_WRITABLE);
291 t = &default_notes_tree;
294 while (strbuf_getline_lf(&buf, stdin) != EOF) {
295 unsigned char from_obj[20], to_obj[20];
296 struct strbuf **split;
299 split = strbuf_split(&buf, ' ');
300 if (!split[0] || !split[1])
301 die(_("malformed input line: '%s'."), buf.buf);
302 strbuf_rtrim(split[0]);
303 strbuf_rtrim(split[1]);
304 if (get_sha1(split[0]->buf, from_obj))
305 die(_("failed to resolve '%s' as a valid ref."), split[0]->buf);
306 if (get_sha1(split[1]->buf, to_obj))
307 die(_("failed to resolve '%s' as a valid ref."), split[1]->buf);
310 err = copy_note_for_rewrite(c, from_obj, to_obj);
312 err = copy_note(t, from_obj, to_obj, force,
313 combine_notes_overwrite);
316 error(_("failed to copy notes from '%s' to '%s'"),
317 split[0]->buf, split[1]->buf);
321 strbuf_list_free(split);
325 commit_notes(t, msg);
328 finish_copy_notes_for_rewrite(c, msg);
333 static struct notes_tree *init_notes_check(const char *subcommand,
336 struct notes_tree *t;
338 init_notes(NULL, NULL, NULL, flags);
339 t = &default_notes_tree;
341 ref = (flags & NOTES_INIT_WRITABLE) ? t->update_ref : t->ref;
342 if (!starts_with(ref, "refs/notes/"))
343 /* TRANSLATORS: the first %s will be replaced by a
344 git notes command: 'add', 'merge', 'remove', etc.*/
345 die(_("refusing to %s notes in %s (outside of refs/notes/)"),
350 static int list(int argc, const char **argv, const char *prefix)
352 struct notes_tree *t;
353 unsigned char object[20];
354 const unsigned char *note;
356 struct option options[] = {
361 argc = parse_options(argc, argv, prefix, options,
362 git_notes_list_usage, 0);
365 error(_("too many parameters"));
366 usage_with_options(git_notes_list_usage, options);
369 t = init_notes_check("list", 0);
371 if (get_sha1(argv[0], object))
372 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
373 note = get_note(t, object);
375 puts(sha1_to_hex(note));
378 retval = error(_("no note found for object %s."),
379 sha1_to_hex(object));
381 retval = for_each_note(t, 0, list_each_note, NULL);
387 static int append_edit(int argc, const char **argv, const char *prefix);
389 static int add(int argc, const char **argv, const char *prefix)
391 int force = 0, allow_empty = 0;
392 const char *object_ref;
393 struct notes_tree *t;
394 unsigned char object[20], new_note[20];
395 const unsigned char *note;
396 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
397 struct option options[] = {
398 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
399 N_("note contents as a string"), PARSE_OPT_NONEG,
401 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
402 N_("note contents in a file"), PARSE_OPT_NONEG,
404 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
405 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
407 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
408 N_("reuse specified note object"), PARSE_OPT_NONEG,
410 OPT_BOOL(0, "allow-empty", &allow_empty,
411 N_("allow storing empty note")),
412 OPT__FORCE(&force, N_("replace existing notes")),
416 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
417 PARSE_OPT_KEEP_ARGV0);
420 error(_("too many parameters"));
421 usage_with_options(git_notes_add_usage, options);
424 object_ref = argc > 1 ? argv[1] : "HEAD";
426 if (get_sha1(object_ref, object))
427 die(_("failed to resolve '%s' as a valid ref."), object_ref);
429 t = init_notes_check("add", NOTES_INIT_WRITABLE);
430 note = get_note(t, object);
437 return error(_("Cannot add notes. "
438 "Found existing notes for object %s. "
439 "Use '-f' to overwrite existing notes"),
440 sha1_to_hex(object));
443 * Redirect to "edit" subcommand.
445 * We only end up here if none of -m/-F/-c/-C or -f are
446 * given. The original args are therefore still in
450 return append_edit(argc, argv, prefix);
452 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
453 sha1_to_hex(object));
456 prepare_note_data(object, &d, note);
457 if (d.buf.len || allow_empty) {
458 write_note_data(&d, new_note);
459 if (add_note(t, object, new_note, combine_notes_overwrite))
460 die("BUG: combine_notes_overwrite failed");
461 commit_notes(t, "Notes added by 'git notes add'");
463 fprintf(stderr, _("Removing note for object %s\n"),
464 sha1_to_hex(object));
465 remove_note(t, object);
466 commit_notes(t, "Notes removed by 'git notes add'");
474 static int copy(int argc, const char **argv, const char *prefix)
476 int retval = 0, force = 0, from_stdin = 0;
477 const unsigned char *from_note, *note;
478 const char *object_ref;
479 unsigned char object[20], from_obj[20];
480 struct notes_tree *t;
481 const char *rewrite_cmd = NULL;
482 struct option options[] = {
483 OPT__FORCE(&force, N_("replace existing notes")),
484 OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")),
485 OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"),
486 N_("load rewriting config for <command> (implies "
491 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
494 if (from_stdin || rewrite_cmd) {
496 error(_("too many parameters"));
497 usage_with_options(git_notes_copy_usage, options);
499 return notes_copy_from_stdin(force, rewrite_cmd);
504 error(_("too few parameters"));
505 usage_with_options(git_notes_copy_usage, options);
508 error(_("too many parameters"));
509 usage_with_options(git_notes_copy_usage, options);
512 if (get_sha1(argv[0], from_obj))
513 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
515 object_ref = 1 < argc ? argv[1] : "HEAD";
517 if (get_sha1(object_ref, object))
518 die(_("failed to resolve '%s' as a valid ref."), object_ref);
520 t = init_notes_check("copy", NOTES_INIT_WRITABLE);
521 note = get_note(t, object);
525 retval = error(_("Cannot copy notes. Found existing "
526 "notes for object %s. Use '-f' to "
527 "overwrite existing notes"),
528 sha1_to_hex(object));
531 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
532 sha1_to_hex(object));
535 from_note = get_note(t, from_obj);
537 retval = error(_("missing notes on source object %s. Cannot "
538 "copy."), sha1_to_hex(from_obj));
542 if (add_note(t, object, from_note, combine_notes_overwrite))
543 die("BUG: combine_notes_overwrite failed");
544 commit_notes(t, "Notes added by 'git notes copy'");
550 static int append_edit(int argc, const char **argv, const char *prefix)
553 const char *object_ref;
554 struct notes_tree *t;
555 unsigned char object[20], new_note[20];
556 const unsigned char *note;
558 const char * const *usage;
559 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
560 struct option options[] = {
561 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
562 N_("note contents as a string"), PARSE_OPT_NONEG,
564 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
565 N_("note contents in a file"), PARSE_OPT_NONEG,
567 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
568 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
570 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
571 N_("reuse specified note object"), PARSE_OPT_NONEG,
573 OPT_BOOL(0, "allow-empty", &allow_empty,
574 N_("allow storing empty note")),
577 int edit = !strcmp(argv[0], "edit");
579 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
580 argc = parse_options(argc, argv, prefix, options, usage,
581 PARSE_OPT_KEEP_ARGV0);
584 error(_("too many parameters"));
585 usage_with_options(usage, options);
589 fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
590 "for the 'edit' subcommand.\n"
591 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
593 object_ref = 1 < argc ? argv[1] : "HEAD";
595 if (get_sha1(object_ref, object))
596 die(_("failed to resolve '%s' as a valid ref."), object_ref);
598 t = init_notes_check(argv[0], NOTES_INIT_WRITABLE);
599 note = get_note(t, object);
601 prepare_note_data(object, &d, edit ? note : NULL);
604 /* Append buf to previous note contents */
606 enum object_type type;
607 char *prev_buf = read_sha1_file(note, &type, &size);
609 strbuf_grow(&d.buf, size + 1);
610 if (d.buf.len && prev_buf && size)
611 strbuf_insert(&d.buf, 0, "\n", 1);
612 if (prev_buf && size)
613 strbuf_insert(&d.buf, 0, prev_buf, size);
617 if (d.buf.len || allow_empty) {
618 write_note_data(&d, new_note);
619 if (add_note(t, object, new_note, combine_notes_overwrite))
620 die("BUG: combine_notes_overwrite failed");
621 logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]);
623 fprintf(stderr, _("Removing note for object %s\n"),
624 sha1_to_hex(object));
625 remove_note(t, object);
626 logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]);
628 commit_notes(t, logmsg);
636 static int show(int argc, const char **argv, const char *prefix)
638 const char *object_ref;
639 struct notes_tree *t;
640 unsigned char object[20];
641 const unsigned char *note;
643 struct option options[] = {
647 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
651 error(_("too many parameters"));
652 usage_with_options(git_notes_show_usage, options);
655 object_ref = argc ? argv[0] : "HEAD";
657 if (get_sha1(object_ref, object))
658 die(_("failed to resolve '%s' as a valid ref."), object_ref);
660 t = init_notes_check("show", 0);
661 note = get_note(t, object);
664 retval = error(_("no note found for object %s."),
665 sha1_to_hex(object));
667 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
668 retval = execv_git_cmd(show_args);
674 static int merge_abort(struct notes_merge_options *o)
679 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
680 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
683 if (delete_ref(NULL, "NOTES_MERGE_PARTIAL", NULL, 0))
684 ret += error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
685 if (delete_ref(NULL, "NOTES_MERGE_REF", NULL, REF_NODEREF))
686 ret += error(_("failed to delete ref NOTES_MERGE_REF"));
687 if (notes_merge_abort(o))
688 ret += error(_("failed to remove 'git notes merge' worktree"));
692 static int merge_commit(struct notes_merge_options *o)
694 struct strbuf msg = STRBUF_INIT;
695 struct object_id oid, parent_oid;
696 struct notes_tree *t;
697 struct commit *partial;
698 struct pretty_print_context pretty_ctx;
699 void *local_ref_to_free;
703 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
704 * and target notes ref from .git/NOTES_MERGE_REF.
707 if (get_oid("NOTES_MERGE_PARTIAL", &oid))
708 die(_("failed to read ref NOTES_MERGE_PARTIAL"));
709 else if (!(partial = lookup_commit_reference(oid.hash)))
710 die(_("could not find commit from NOTES_MERGE_PARTIAL."));
711 else if (parse_commit(partial))
712 die(_("could not parse commit from NOTES_MERGE_PARTIAL."));
714 if (partial->parents)
715 oidcpy(&parent_oid, &partial->parents->item->object.oid);
719 t = xcalloc(1, sizeof(struct notes_tree));
720 init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
722 o->local_ref = local_ref_to_free =
723 resolve_refdup("NOTES_MERGE_REF", 0, oid.hash, NULL);
725 die(_("failed to resolve NOTES_MERGE_REF"));
727 if (notes_merge_commit(o, t, partial, oid.hash))
728 die(_("failed to finalize notes merge"));
730 /* Reuse existing commit message in reflog message */
731 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
732 format_commit_message(partial, "%s", &msg, &pretty_ctx);
734 strbuf_insert(&msg, 0, "notes: ", 7);
735 update_ref(msg.buf, o->local_ref, oid.hash,
736 is_null_oid(&parent_oid) ? NULL : parent_oid.hash,
737 0, UPDATE_REFS_DIE_ON_ERR);
740 strbuf_release(&msg);
741 ret = merge_abort(o);
742 free(local_ref_to_free);
746 static int git_config_get_notes_strategy(const char *key,
747 enum notes_merge_strategy *strategy)
751 if (git_config_get_string(key, &value))
753 if (parse_notes_merge_strategy(value, strategy))
754 git_die_config(key, _("unknown notes merge strategy %s"), value);
760 static int merge(int argc, const char **argv, const char *prefix)
762 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
763 unsigned char result_sha1[20];
764 struct notes_tree *t;
765 struct notes_merge_options o;
766 int do_merge = 0, do_commit = 0, do_abort = 0;
767 int verbosity = 0, result;
768 const char *strategy = NULL;
769 struct option options[] = {
770 OPT_GROUP(N_("General options")),
771 OPT__VERBOSITY(&verbosity),
772 OPT_GROUP(N_("Merge options")),
773 OPT_STRING('s', "strategy", &strategy, N_("strategy"),
774 N_("resolve notes conflicts using the given strategy "
775 "(manual/ours/theirs/union/cat_sort_uniq)")),
776 OPT_GROUP(N_("Committing unmerged notes")),
777 { OPTION_SET_INT, 0, "commit", &do_commit, NULL,
778 N_("finalize notes merge by committing unmerged notes"),
779 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
780 OPT_GROUP(N_("Aborting notes merge resolution")),
781 { OPTION_SET_INT, 0, "abort", &do_abort, NULL,
782 N_("abort notes merge"),
783 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
787 argc = parse_options(argc, argv, prefix, options,
788 git_notes_merge_usage, 0);
790 if (strategy || do_commit + do_abort == 0)
792 if (do_merge + do_commit + do_abort != 1) {
793 error(_("cannot mix --commit, --abort or -s/--strategy"));
794 usage_with_options(git_notes_merge_usage, options);
797 if (do_merge && argc != 1) {
798 error(_("must specify a notes ref to merge"));
799 usage_with_options(git_notes_merge_usage, options);
800 } else if (!do_merge && argc) {
801 error(_("too many parameters"));
802 usage_with_options(git_notes_merge_usage, options);
805 init_notes_merge_options(&o);
806 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
809 return merge_abort(&o);
811 return merge_commit(&o);
813 o.local_ref = default_notes_ref();
814 strbuf_addstr(&remote_ref, argv[0]);
815 expand_loose_notes_ref(&remote_ref);
816 o.remote_ref = remote_ref.buf;
818 t = init_notes_check("merge", NOTES_INIT_WRITABLE);
821 if (parse_notes_merge_strategy(strategy, &o.strategy)) {
822 error(_("unknown -s/--strategy: %s"), strategy);
823 usage_with_options(git_notes_merge_usage, options);
826 struct strbuf merge_key = STRBUF_INIT;
827 const char *short_ref = NULL;
829 if (!skip_prefix(o.local_ref, "refs/notes/", &short_ref))
830 die("BUG: local ref %s is outside of refs/notes/",
833 strbuf_addf(&merge_key, "notes.%s.mergeStrategy", short_ref);
835 if (git_config_get_notes_strategy(merge_key.buf, &o.strategy))
836 git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy);
838 strbuf_release(&merge_key);
841 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
842 remote_ref.buf, default_notes_ref());
843 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
845 result = notes_merge(&o, t, result_sha1);
847 if (result >= 0) /* Merge resulted (trivially) in result_sha1 */
848 /* Update default notes ref with new commit */
849 update_ref(msg.buf, default_notes_ref(), result_sha1, NULL,
850 0, UPDATE_REFS_DIE_ON_ERR);
851 else { /* Merge has unresolved conflicts */
852 const struct worktree *wt;
853 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
854 update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_sha1, NULL,
855 0, UPDATE_REFS_DIE_ON_ERR);
856 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
857 wt = find_shared_symref("NOTES_MERGE_REF", default_notes_ref());
859 die(_("a notes merge into %s is already in-progress at %s"),
860 default_notes_ref(), wt->path);
861 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
862 die(_("failed to store link to current notes ref (%s)"),
863 default_notes_ref());
864 printf(_("Automatic notes merge failed. Fix conflicts in %s and "
865 "commit the result with 'git notes merge --commit', or "
866 "abort the merge with 'git notes merge --abort'.\n"),
867 git_path(NOTES_MERGE_WORKTREE));
871 strbuf_release(&remote_ref);
872 strbuf_release(&msg);
873 return result < 0; /* return non-zero on conflicts */
876 #define IGNORE_MISSING 1
878 static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
881 unsigned char sha1[20];
882 if (get_sha1(name, sha1))
883 return error(_("Failed to resolve '%s' as a valid ref."), name);
884 status = remove_note(t, sha1);
886 fprintf(stderr, _("Object %s has no note\n"), name);
888 fprintf(stderr, _("Removing note for object %s\n"), name);
889 return (flag & IGNORE_MISSING) ? 0 : status;
892 static int remove_cmd(int argc, const char **argv, const char *prefix)
896 struct option options[] = {
897 OPT_BIT(0, "ignore-missing", &flag,
898 N_("attempt to remove non-existent note is not an error"),
900 OPT_BOOL(0, "stdin", &from_stdin,
901 N_("read object names from the standard input")),
904 struct notes_tree *t;
907 argc = parse_options(argc, argv, prefix, options,
908 git_notes_remove_usage, 0);
910 t = init_notes_check("remove", NOTES_INIT_WRITABLE);
912 if (!argc && !from_stdin) {
913 retval = remove_one_note(t, "HEAD", flag);
916 retval |= remove_one_note(t, *argv, flag);
921 struct strbuf sb = STRBUF_INIT;
922 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
924 retval |= remove_one_note(t, sb.buf, flag);
929 commit_notes(t, "Notes removed by 'git notes remove'");
934 static int prune(int argc, const char **argv, const char *prefix)
936 struct notes_tree *t;
937 int show_only = 0, verbose = 0;
938 struct option options[] = {
939 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
940 OPT__VERBOSE(&verbose, N_("report pruned notes")),
944 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
948 error(_("too many parameters"));
949 usage_with_options(git_notes_prune_usage, options);
952 t = init_notes_check("prune", NOTES_INIT_WRITABLE);
954 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
955 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
957 commit_notes(t, "Notes removed by 'git notes prune'");
962 static int get_ref(int argc, const char **argv, const char *prefix)
964 struct option options[] = { OPT_END() };
965 argc = parse_options(argc, argv, prefix, options,
966 git_notes_get_ref_usage, 0);
969 error(_("too many parameters"));
970 usage_with_options(git_notes_get_ref_usage, options);
973 puts(default_notes_ref());
977 int cmd_notes(int argc, const char **argv, const char *prefix)
980 const char *override_notes_ref = NULL;
981 struct option options[] = {
982 OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
983 N_("use notes from <notes-ref>")),
987 git_config(git_default_config, NULL);
988 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
989 PARSE_OPT_STOP_AT_NON_OPTION);
991 if (override_notes_ref) {
992 struct strbuf sb = STRBUF_INIT;
993 strbuf_addstr(&sb, override_notes_ref);
994 expand_notes_ref(&sb);
995 setenv("GIT_NOTES_REF", sb.buf, 1);
999 if (argc < 1 || !strcmp(argv[0], "list"))
1000 result = list(argc, argv, prefix);
1001 else if (!strcmp(argv[0], "add"))
1002 result = add(argc, argv, prefix);
1003 else if (!strcmp(argv[0], "copy"))
1004 result = copy(argc, argv, prefix);
1005 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
1006 result = append_edit(argc, argv, prefix);
1007 else if (!strcmp(argv[0], "show"))
1008 result = show(argc, argv, prefix);
1009 else if (!strcmp(argv[0], "merge"))
1010 result = merge(argc, argv, prefix);
1011 else if (!strcmp(argv[0], "remove"))
1012 result = remove_cmd(argc, argv, prefix);
1013 else if (!strcmp(argv[0], "prune"))
1014 result = prune(argc, argv, prefix);
1015 else if (!strcmp(argv[0], "get-ref"))
1016 result = get_ref(argc, argv, prefix);
1018 result = error(_("unknown subcommand: %s"), argv[0]);
1019 usage_with_options(git_notes_usage, options);
1022 return result ? 1 : 0;