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.
14 #include "object-store.h"
15 #include "repository.h"
20 #include "run-command.h"
21 #include "parse-options.h"
22 #include "string-list.h"
23 #include "notes-merge.h"
24 #include "notes-utils.h"
27 static const char * const git_notes_usage[] = {
28 N_("git notes [--ref <notes-ref>] [list [<object>]]"),
29 N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
30 N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"),
31 N_("git notes [--ref <notes-ref>] append [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
32 N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
33 N_("git notes [--ref <notes-ref>] show [<object>]"),
34 N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"),
35 N_("git notes merge --commit [-v | -q]"),
36 N_("git notes merge --abort [-v | -q]"),
37 N_("git notes [--ref <notes-ref>] remove [<object>...]"),
38 N_("git notes [--ref <notes-ref>] prune [-n] [-v]"),
39 N_("git notes [--ref <notes-ref>] get-ref"),
43 static const char * const git_notes_list_usage[] = {
44 N_("git notes [list [<object>]]"),
48 static const char * const git_notes_add_usage[] = {
49 N_("git notes add [<options>] [<object>]"),
53 static const char * const git_notes_copy_usage[] = {
54 N_("git notes copy [<options>] <from-object> <to-object>"),
55 N_("git notes copy --stdin [<from-object> <to-object>]..."),
59 static const char * const git_notes_append_usage[] = {
60 N_("git notes append [<options>] [<object>]"),
64 static const char * const git_notes_edit_usage[] = {
65 N_("git notes edit [<object>]"),
69 static const char * const git_notes_show_usage[] = {
70 N_("git notes show [<object>]"),
74 static const char * const git_notes_merge_usage[] = {
75 N_("git notes merge [<options>] <notes-ref>"),
76 N_("git notes merge --commit [<options>]"),
77 N_("git notes merge --abort [<options>]"),
81 static const char * const git_notes_remove_usage[] = {
82 N_("git notes remove [<object>]"),
86 static const char * const git_notes_prune_usage[] = {
87 N_("git notes prune [<options>]"),
91 static const char * const git_notes_get_ref_usage[] = {
92 N_("git notes get-ref"),
96 static const char note_template[] =
97 N_("Write/edit the notes for the following object:");
106 static void free_note_data(struct note_data *d)
109 unlink_or_warn(d->edit_path);
112 strbuf_release(&d->buf);
115 static int list_each_note(const struct object_id *object_oid,
116 const struct object_id *note_oid, char *note_path,
119 printf("%s %s\n", oid_to_hex(note_oid), oid_to_hex(object_oid));
123 static void copy_obj_to_fd(int fd, const struct object_id *oid)
126 enum object_type type;
127 char *buf = read_object_file(oid, &type, &size);
130 write_or_die(fd, buf, size);
135 static void write_commented_object(int fd, const struct object_id *object)
137 const char *show_args[5] =
138 {"show", "--stat", "--no-notes", oid_to_hex(object), NULL};
139 struct child_process show = CHILD_PROCESS_INIT;
140 struct strbuf buf = STRBUF_INIT;
141 struct strbuf cbuf = STRBUF_INIT;
143 /* Invoke "git show --stat --no-notes $object" */
144 show.argv = show_args;
149 if (start_command(&show))
150 die(_("unable to start 'show' for object '%s'"),
153 if (strbuf_read(&buf, show.out, 0) < 0)
154 die_errno(_("could not read 'show' output"));
155 strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
156 write_or_die(fd, cbuf.buf, cbuf.len);
158 strbuf_release(&cbuf);
159 strbuf_release(&buf);
161 if (finish_command(&show))
162 die(_("failed to finish 'show' for object '%s'"),
166 static void prepare_note_data(const struct object_id *object, struct note_data *d,
167 const struct object_id *old_note)
169 if (d->use_editor || !d->given) {
171 struct strbuf buf = STRBUF_INIT;
173 /* write the template message before editing: */
174 d->edit_path = git_pathdup("NOTES_EDITMSG");
175 fd = open(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
177 die_errno(_("could not create file '%s'"), d->edit_path);
180 write_or_die(fd, d->buf.buf, d->buf.len);
182 copy_obj_to_fd(fd, old_note);
184 strbuf_addch(&buf, '\n');
185 strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
186 strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)));
187 strbuf_addch(&buf, '\n');
188 write_or_die(fd, buf.buf, buf.len);
190 write_commented_object(fd, object);
193 strbuf_release(&buf);
194 strbuf_reset(&d->buf);
196 if (launch_editor(d->edit_path, &d->buf, NULL)) {
197 die(_("please supply the note contents using either -m or -F option"));
199 strbuf_stripspace(&d->buf, 1);
203 static void write_note_data(struct note_data *d, struct object_id *oid)
205 if (write_object_file(d->buf.buf, d->buf.len, blob_type, oid)) {
206 error(_("unable to write note object"));
208 error(_("the note contents have been left in %s"),
214 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
216 struct note_data *d = opt->value;
218 BUG_ON_OPT_NEG(unset);
220 strbuf_grow(&d->buf, strlen(arg) + 2);
222 strbuf_addch(&d->buf, '\n');
223 strbuf_addstr(&d->buf, arg);
224 strbuf_stripspace(&d->buf, 0);
230 static int parse_file_arg(const struct option *opt, const char *arg, int unset)
232 struct note_data *d = opt->value;
234 BUG_ON_OPT_NEG(unset);
237 strbuf_addch(&d->buf, '\n');
238 if (!strcmp(arg, "-")) {
239 if (strbuf_read(&d->buf, 0, 1024) < 0)
240 die_errno(_("cannot read '%s'"), arg);
241 } else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
242 die_errno(_("could not open or read '%s'"), arg);
243 strbuf_stripspace(&d->buf, 0);
249 static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
251 struct note_data *d = opt->value;
253 struct object_id object;
254 enum object_type type;
257 BUG_ON_OPT_NEG(unset);
260 strbuf_addch(&d->buf, '\n');
262 if (get_oid(arg, &object))
263 die(_("failed to resolve '%s' as a valid ref."), arg);
264 if (!(buf = read_object_file(&object, &type, &len)))
265 die(_("failed to read object '%s'."), arg);
266 if (type != OBJ_BLOB) {
268 die(_("cannot read note data from non-blob object '%s'."), arg);
270 strbuf_add(&d->buf, buf, len);
277 static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
279 struct note_data *d = opt->value;
280 BUG_ON_OPT_NEG(unset);
282 return parse_reuse_arg(opt, arg, unset);
285 static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
287 struct strbuf buf = STRBUF_INIT;
288 struct notes_rewrite_cfg *c = NULL;
289 struct notes_tree *t = NULL;
291 const char *msg = "Notes added by 'git notes copy'";
294 c = init_copy_notes_for_rewrite(rewrite_cmd);
298 init_notes(NULL, NULL, NULL, NOTES_INIT_WRITABLE);
299 t = &default_notes_tree;
302 while (strbuf_getline_lf(&buf, stdin) != EOF) {
303 struct object_id from_obj, to_obj;
304 struct strbuf **split;
307 split = strbuf_split(&buf, ' ');
308 if (!split[0] || !split[1])
309 die(_("malformed input line: '%s'."), buf.buf);
310 strbuf_rtrim(split[0]);
311 strbuf_rtrim(split[1]);
312 if (get_oid(split[0]->buf, &from_obj))
313 die(_("failed to resolve '%s' as a valid ref."), split[0]->buf);
314 if (get_oid(split[1]->buf, &to_obj))
315 die(_("failed to resolve '%s' as a valid ref."), split[1]->buf);
318 err = copy_note_for_rewrite(c, &from_obj, &to_obj);
320 err = copy_note(t, &from_obj, &to_obj, force,
321 combine_notes_overwrite);
324 error(_("failed to copy notes from '%s' to '%s'"),
325 split[0]->buf, split[1]->buf);
329 strbuf_list_free(split);
333 commit_notes(t, msg);
336 finish_copy_notes_for_rewrite(c, msg);
338 strbuf_release(&buf);
342 static struct notes_tree *init_notes_check(const char *subcommand,
345 struct notes_tree *t;
347 init_notes(NULL, NULL, NULL, flags);
348 t = &default_notes_tree;
350 ref = (flags & NOTES_INIT_WRITABLE) ? t->update_ref : t->ref;
351 if (!starts_with(ref, "refs/notes/"))
353 * TRANSLATORS: the first %s will be replaced by a git
354 * notes command: 'add', 'merge', 'remove', etc.
356 die(_("refusing to %s notes in %s (outside of refs/notes/)"),
361 static int list(int argc, const char **argv, const char *prefix)
363 struct notes_tree *t;
364 struct object_id object;
365 const struct object_id *note;
367 struct option options[] = {
372 argc = parse_options(argc, argv, prefix, options,
373 git_notes_list_usage, 0);
376 error(_("too many parameters"));
377 usage_with_options(git_notes_list_usage, options);
380 t = init_notes_check("list", 0);
382 if (get_oid(argv[0], &object))
383 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
384 note = get_note(t, &object);
386 puts(oid_to_hex(note));
389 retval = error(_("no note found for object %s."),
390 oid_to_hex(&object));
392 retval = for_each_note(t, 0, list_each_note, NULL);
398 static int append_edit(int argc, const char **argv, const char *prefix);
400 static int add(int argc, const char **argv, const char *prefix)
402 int force = 0, allow_empty = 0;
403 const char *object_ref;
404 struct notes_tree *t;
405 struct object_id object, new_note;
406 const struct object_id *note;
407 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
408 struct option options[] = {
409 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
410 N_("note contents as a string"), PARSE_OPT_NONEG,
412 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
413 N_("note contents in a file"), PARSE_OPT_NONEG,
415 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
416 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
418 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
419 N_("reuse specified note object"), PARSE_OPT_NONEG,
421 OPT_BOOL(0, "allow-empty", &allow_empty,
422 N_("allow storing empty note")),
423 OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE),
427 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
428 PARSE_OPT_KEEP_ARGV0);
431 error(_("too many parameters"));
432 usage_with_options(git_notes_add_usage, options);
435 object_ref = argc > 1 ? argv[1] : "HEAD";
437 if (get_oid(object_ref, &object))
438 die(_("failed to resolve '%s' as a valid ref."), object_ref);
440 t = init_notes_check("add", NOTES_INIT_WRITABLE);
441 note = get_note(t, &object);
448 return error(_("Cannot add notes. "
449 "Found existing notes for object %s. "
450 "Use '-f' to overwrite existing notes"),
451 oid_to_hex(&object));
454 * Redirect to "edit" subcommand.
456 * We only end up here if none of -m/-F/-c/-C or -f are
457 * given. The original args are therefore still in
461 return append_edit(argc, argv, prefix);
463 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
464 oid_to_hex(&object));
467 prepare_note_data(&object, &d, note);
468 if (d.buf.len || allow_empty) {
469 write_note_data(&d, &new_note);
470 if (add_note(t, &object, &new_note, combine_notes_overwrite))
471 BUG("combine_notes_overwrite failed");
472 commit_notes(t, "Notes added by 'git notes add'");
474 fprintf(stderr, _("Removing note for object %s\n"),
475 oid_to_hex(&object));
476 remove_note(t, object.hash);
477 commit_notes(t, "Notes removed by 'git notes add'");
485 static int copy(int argc, const char **argv, const char *prefix)
487 int retval = 0, force = 0, from_stdin = 0;
488 const struct object_id *from_note, *note;
489 const char *object_ref;
490 struct object_id object, from_obj;
491 struct notes_tree *t;
492 const char *rewrite_cmd = NULL;
493 struct option options[] = {
494 OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE),
495 OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")),
496 OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"),
497 N_("load rewriting config for <command> (implies "
502 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
505 if (from_stdin || rewrite_cmd) {
507 error(_("too many parameters"));
508 usage_with_options(git_notes_copy_usage, options);
510 return notes_copy_from_stdin(force, rewrite_cmd);
515 error(_("too few parameters"));
516 usage_with_options(git_notes_copy_usage, options);
519 error(_("too many parameters"));
520 usage_with_options(git_notes_copy_usage, options);
523 if (get_oid(argv[0], &from_obj))
524 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
526 object_ref = 1 < argc ? argv[1] : "HEAD";
528 if (get_oid(object_ref, &object))
529 die(_("failed to resolve '%s' as a valid ref."), object_ref);
531 t = init_notes_check("copy", NOTES_INIT_WRITABLE);
532 note = get_note(t, &object);
536 retval = error(_("Cannot copy notes. Found existing "
537 "notes for object %s. Use '-f' to "
538 "overwrite existing notes"),
539 oid_to_hex(&object));
542 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
543 oid_to_hex(&object));
546 from_note = get_note(t, &from_obj);
548 retval = error(_("missing notes on source object %s. Cannot "
549 "copy."), oid_to_hex(&from_obj));
553 if (add_note(t, &object, from_note, combine_notes_overwrite))
554 BUG("combine_notes_overwrite failed");
555 commit_notes(t, "Notes added by 'git notes copy'");
561 static int append_edit(int argc, const char **argv, const char *prefix)
564 const char *object_ref;
565 struct notes_tree *t;
566 struct object_id object, new_note;
567 const struct object_id *note;
569 const char * const *usage;
570 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
571 struct option options[] = {
572 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
573 N_("note contents as a string"), PARSE_OPT_NONEG,
575 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
576 N_("note contents in a file"), PARSE_OPT_NONEG,
578 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
579 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
581 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
582 N_("reuse specified note object"), PARSE_OPT_NONEG,
584 OPT_BOOL(0, "allow-empty", &allow_empty,
585 N_("allow storing empty note")),
588 int edit = !strcmp(argv[0], "edit");
590 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
591 argc = parse_options(argc, argv, prefix, options, usage,
592 PARSE_OPT_KEEP_ARGV0);
595 error(_("too many parameters"));
596 usage_with_options(usage, options);
600 fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
601 "for the 'edit' subcommand.\n"
602 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
604 object_ref = 1 < argc ? argv[1] : "HEAD";
606 if (get_oid(object_ref, &object))
607 die(_("failed to resolve '%s' as a valid ref."), object_ref);
609 t = init_notes_check(argv[0], NOTES_INIT_WRITABLE);
610 note = get_note(t, &object);
612 prepare_note_data(&object, &d, edit && note ? note : NULL);
615 /* Append buf to previous note contents */
617 enum object_type type;
618 char *prev_buf = read_object_file(note, &type, &size);
620 strbuf_grow(&d.buf, size + 1);
621 if (d.buf.len && prev_buf && size)
622 strbuf_insert(&d.buf, 0, "\n", 1);
623 if (prev_buf && size)
624 strbuf_insert(&d.buf, 0, prev_buf, size);
628 if (d.buf.len || allow_empty) {
629 write_note_data(&d, &new_note);
630 if (add_note(t, &object, &new_note, combine_notes_overwrite))
631 BUG("combine_notes_overwrite failed");
632 logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]);
634 fprintf(stderr, _("Removing note for object %s\n"),
635 oid_to_hex(&object));
636 remove_note(t, object.hash);
637 logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]);
639 commit_notes(t, logmsg);
647 static int show(int argc, const char **argv, const char *prefix)
649 const char *object_ref;
650 struct notes_tree *t;
651 struct object_id object;
652 const struct object_id *note;
654 struct option options[] = {
658 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
662 error(_("too many parameters"));
663 usage_with_options(git_notes_show_usage, options);
666 object_ref = argc ? argv[0] : "HEAD";
668 if (get_oid(object_ref, &object))
669 die(_("failed to resolve '%s' as a valid ref."), object_ref);
671 t = init_notes_check("show", 0);
672 note = get_note(t, &object);
675 retval = error(_("no note found for object %s."),
676 oid_to_hex(&object));
678 const char *show_args[3] = {"show", oid_to_hex(note), NULL};
679 retval = execv_git_cmd(show_args);
685 static int merge_abort(struct notes_merge_options *o)
690 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
691 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
694 if (delete_ref(NULL, "NOTES_MERGE_PARTIAL", NULL, 0))
695 ret += error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
696 if (delete_ref(NULL, "NOTES_MERGE_REF", NULL, REF_NO_DEREF))
697 ret += error(_("failed to delete ref NOTES_MERGE_REF"));
698 if (notes_merge_abort(o))
699 ret += error(_("failed to remove 'git notes merge' worktree"));
703 static int merge_commit(struct notes_merge_options *o)
705 struct strbuf msg = STRBUF_INIT;
706 struct object_id oid, parent_oid;
707 struct notes_tree *t;
708 struct commit *partial;
709 struct pretty_print_context pretty_ctx;
710 void *local_ref_to_free;
714 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
715 * and target notes ref from .git/NOTES_MERGE_REF.
718 if (get_oid("NOTES_MERGE_PARTIAL", &oid))
719 die(_("failed to read ref NOTES_MERGE_PARTIAL"));
720 else if (!(partial = lookup_commit_reference(the_repository, &oid)))
721 die(_("could not find commit from NOTES_MERGE_PARTIAL."));
722 else if (parse_commit(partial))
723 die(_("could not parse commit from NOTES_MERGE_PARTIAL."));
725 if (partial->parents)
726 oidcpy(&parent_oid, &partial->parents->item->object.oid);
730 t = xcalloc(1, sizeof(struct notes_tree));
731 init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
733 o->local_ref = local_ref_to_free =
734 resolve_refdup("NOTES_MERGE_REF", 0, &oid, NULL);
736 die(_("failed to resolve NOTES_MERGE_REF"));
738 if (notes_merge_commit(o, t, partial, &oid))
739 die(_("failed to finalize notes merge"));
741 /* Reuse existing commit message in reflog message */
742 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
743 format_commit_message(partial, "%s", &msg, &pretty_ctx);
745 strbuf_insert(&msg, 0, "notes: ", 7);
746 update_ref(msg.buf, o->local_ref, &oid,
747 is_null_oid(&parent_oid) ? NULL : &parent_oid,
748 0, UPDATE_REFS_DIE_ON_ERR);
751 strbuf_release(&msg);
752 ret = merge_abort(o);
753 free(local_ref_to_free);
757 static int git_config_get_notes_strategy(const char *key,
758 enum notes_merge_strategy *strategy)
762 if (git_config_get_string(key, &value))
764 if (parse_notes_merge_strategy(value, strategy))
765 git_die_config(key, _("unknown notes merge strategy %s"), value);
771 static int merge(int argc, const char **argv, const char *prefix)
773 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
774 struct object_id result_oid;
775 struct notes_tree *t;
776 struct notes_merge_options o;
777 int do_merge = 0, do_commit = 0, do_abort = 0;
778 int verbosity = 0, result;
779 const char *strategy = NULL;
780 struct option options[] = {
781 OPT_GROUP(N_("General options")),
782 OPT__VERBOSITY(&verbosity),
783 OPT_GROUP(N_("Merge options")),
784 OPT_STRING('s', "strategy", &strategy, N_("strategy"),
785 N_("resolve notes conflicts using the given strategy "
786 "(manual/ours/theirs/union/cat_sort_uniq)")),
787 OPT_GROUP(N_("Committing unmerged notes")),
788 OPT_SET_INT_F(0, "commit", &do_commit,
789 N_("finalize notes merge by committing unmerged notes"),
791 OPT_GROUP(N_("Aborting notes merge resolution")),
792 OPT_SET_INT_F(0, "abort", &do_abort,
793 N_("abort notes merge"),
798 argc = parse_options(argc, argv, prefix, options,
799 git_notes_merge_usage, 0);
801 if (strategy || do_commit + do_abort == 0)
803 if (do_merge + do_commit + do_abort != 1) {
804 error(_("cannot mix --commit, --abort or -s/--strategy"));
805 usage_with_options(git_notes_merge_usage, options);
808 if (do_merge && argc != 1) {
809 error(_("must specify a notes ref to merge"));
810 usage_with_options(git_notes_merge_usage, options);
811 } else if (!do_merge && argc) {
812 error(_("too many parameters"));
813 usage_with_options(git_notes_merge_usage, options);
816 init_notes_merge_options(the_repository, &o);
817 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
820 return merge_abort(&o);
822 return merge_commit(&o);
824 o.local_ref = default_notes_ref();
825 strbuf_addstr(&remote_ref, argv[0]);
826 expand_loose_notes_ref(&remote_ref);
827 o.remote_ref = remote_ref.buf;
829 t = init_notes_check("merge", NOTES_INIT_WRITABLE);
832 if (parse_notes_merge_strategy(strategy, &o.strategy)) {
833 error(_("unknown -s/--strategy: %s"), strategy);
834 usage_with_options(git_notes_merge_usage, options);
837 struct strbuf merge_key = STRBUF_INIT;
838 const char *short_ref = NULL;
840 if (!skip_prefix(o.local_ref, "refs/notes/", &short_ref))
841 BUG("local ref %s is outside of refs/notes/",
844 strbuf_addf(&merge_key, "notes.%s.mergeStrategy", short_ref);
846 if (git_config_get_notes_strategy(merge_key.buf, &o.strategy))
847 git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy);
849 strbuf_release(&merge_key);
852 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
853 remote_ref.buf, default_notes_ref());
854 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
856 result = notes_merge(&o, t, &result_oid);
858 if (result >= 0) /* Merge resulted (trivially) in result_oid */
859 /* Update default notes ref with new commit */
860 update_ref(msg.buf, default_notes_ref(), &result_oid, NULL, 0,
861 UPDATE_REFS_DIE_ON_ERR);
862 else { /* Merge has unresolved conflicts */
863 const struct worktree *wt;
864 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
865 update_ref(msg.buf, "NOTES_MERGE_PARTIAL", &result_oid, NULL,
866 0, UPDATE_REFS_DIE_ON_ERR);
867 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
868 wt = find_shared_symref("NOTES_MERGE_REF", default_notes_ref());
870 die(_("a notes merge into %s is already in-progress at %s"),
871 default_notes_ref(), wt->path);
872 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
873 die(_("failed to store link to current notes ref (%s)"),
874 default_notes_ref());
875 fprintf(stderr, _("Automatic notes merge failed. Fix conflicts in %s "
876 "and commit the result with 'git notes merge --commit', "
877 "or abort the merge with 'git notes merge --abort'.\n"),
878 git_path(NOTES_MERGE_WORKTREE));
882 strbuf_release(&remote_ref);
883 strbuf_release(&msg);
884 return result < 0; /* return non-zero on conflicts */
887 #define IGNORE_MISSING 1
889 static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
892 struct object_id oid;
893 if (get_oid(name, &oid))
894 return error(_("Failed to resolve '%s' as a valid ref."), name);
895 status = remove_note(t, oid.hash);
897 fprintf(stderr, _("Object %s has no note\n"), name);
899 fprintf(stderr, _("Removing note for object %s\n"), name);
900 return (flag & IGNORE_MISSING) ? 0 : status;
903 static int remove_cmd(int argc, const char **argv, const char *prefix)
907 struct option options[] = {
908 OPT_BIT(0, "ignore-missing", &flag,
909 N_("attempt to remove non-existent note is not an error"),
911 OPT_BOOL(0, "stdin", &from_stdin,
912 N_("read object names from the standard input")),
915 struct notes_tree *t;
918 argc = parse_options(argc, argv, prefix, options,
919 git_notes_remove_usage, 0);
921 t = init_notes_check("remove", NOTES_INIT_WRITABLE);
923 if (!argc && !from_stdin) {
924 retval = remove_one_note(t, "HEAD", flag);
927 retval |= remove_one_note(t, *argv, flag);
932 struct strbuf sb = STRBUF_INIT;
933 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
935 retval |= remove_one_note(t, sb.buf, flag);
940 commit_notes(t, "Notes removed by 'git notes remove'");
945 static int prune(int argc, const char **argv, const char *prefix)
947 struct notes_tree *t;
948 int show_only = 0, verbose = 0;
949 struct option options[] = {
950 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
951 OPT__VERBOSE(&verbose, N_("report pruned notes")),
955 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
959 error(_("too many parameters"));
960 usage_with_options(git_notes_prune_usage, options);
963 t = init_notes_check("prune", NOTES_INIT_WRITABLE);
965 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
966 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
968 commit_notes(t, "Notes removed by 'git notes prune'");
973 static int get_ref(int argc, const char **argv, const char *prefix)
975 struct option options[] = { OPT_END() };
976 argc = parse_options(argc, argv, prefix, options,
977 git_notes_get_ref_usage, 0);
980 error(_("too many parameters"));
981 usage_with_options(git_notes_get_ref_usage, options);
984 puts(default_notes_ref());
988 int cmd_notes(int argc, const char **argv, const char *prefix)
991 const char *override_notes_ref = NULL;
992 struct option options[] = {
993 OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
994 N_("use notes from <notes-ref>")),
998 git_config(git_default_config, NULL);
999 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
1000 PARSE_OPT_STOP_AT_NON_OPTION);
1002 if (override_notes_ref) {
1003 struct strbuf sb = STRBUF_INIT;
1004 strbuf_addstr(&sb, override_notes_ref);
1005 expand_notes_ref(&sb);
1006 setenv("GIT_NOTES_REF", sb.buf, 1);
1007 strbuf_release(&sb);
1010 if (argc < 1 || !strcmp(argv[0], "list"))
1011 result = list(argc, argv, prefix);
1012 else if (!strcmp(argv[0], "add"))
1013 result = add(argc, argv, prefix);
1014 else if (!strcmp(argv[0], "copy"))
1015 result = copy(argc, argv, prefix);
1016 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
1017 result = append_edit(argc, argv, prefix);
1018 else if (!strcmp(argv[0], "show"))
1019 result = show(argc, argv, prefix);
1020 else if (!strcmp(argv[0], "merge"))
1021 result = merge(argc, argv, prefix);
1022 else if (!strcmp(argv[0], "remove"))
1023 result = remove_cmd(argc, argv, prefix);
1024 else if (!strcmp(argv[0], "prune"))
1025 result = prune(argc, argv, prefix);
1026 else if (!strcmp(argv[0], "get-ref"))
1027 result = get_ref(argc, argv, prefix);
1029 result = error(_("unknown subcommand: %s"), argv[0]);
1030 usage_with_options(git_notes_usage, options);
1033 return result ? 1 : 0;