notes.c: use designated initializers for clarity
[git/debian.git] / builtin / notes.c
blob9d8ca795827af25a9a3a90ba5b59fed73a37a886
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 "config.h"
12 #include "builtin.h"
13 #include "gettext.h"
14 #include "hex.h"
15 #include "notes.h"
16 #include "object-store.h"
17 #include "repository.h"
18 #include "blob.h"
19 #include "pretty.h"
20 #include "refs.h"
21 #include "exec-cmd.h"
22 #include "run-command.h"
23 #include "parse-options.h"
24 #include "string-list.h"
25 #include "notes-merge.h"
26 #include "notes-utils.h"
27 #include "worktree.h"
28 #include "write-or-die.h"
30 static const char * const git_notes_usage[] = {
31 N_("git notes [--ref <notes-ref>] [list [<object>]]"),
32 N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
33 N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"),
34 N_("git notes [--ref <notes-ref>] append [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
35 N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
36 N_("git notes [--ref <notes-ref>] show [<object>]"),
37 N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"),
38 "git notes merge --commit [-v | -q]",
39 "git notes merge --abort [-v | -q]",
40 N_("git notes [--ref <notes-ref>] remove [<object>...]"),
41 N_("git notes [--ref <notes-ref>] prune [-n] [-v]"),
42 N_("git notes [--ref <notes-ref>] get-ref"),
43 NULL
46 static const char * const git_notes_list_usage[] = {
47 N_("git notes [list [<object>]]"),
48 NULL
51 static const char * const git_notes_add_usage[] = {
52 N_("git notes add [<options>] [<object>]"),
53 NULL
56 static const char * const git_notes_copy_usage[] = {
57 N_("git notes copy [<options>] <from-object> <to-object>"),
58 N_("git notes copy --stdin [<from-object> <to-object>]..."),
59 NULL
62 static const char * const git_notes_append_usage[] = {
63 N_("git notes append [<options>] [<object>]"),
64 NULL
67 static const char * const git_notes_edit_usage[] = {
68 N_("git notes edit [<object>]"),
69 NULL
72 static const char * const git_notes_show_usage[] = {
73 N_("git notes show [<object>]"),
74 NULL
77 static const char * const git_notes_merge_usage[] = {
78 N_("git notes merge [<options>] <notes-ref>"),
79 N_("git notes merge --commit [<options>]"),
80 N_("git notes merge --abort [<options>]"),
81 NULL
84 static const char * const git_notes_remove_usage[] = {
85 N_("git notes remove [<object>]"),
86 NULL
89 static const char * const git_notes_prune_usage[] = {
90 N_("git notes prune [<options>]"),
91 NULL
94 static const char * const git_notes_get_ref_usage[] = {
95 "git notes get-ref",
96 NULL
99 static const char note_template[] =
100 N_("Write/edit the notes for the following object:");
102 struct note_data {
103 int given;
104 int use_editor;
105 char *edit_path;
106 struct strbuf buf;
109 static void free_note_data(struct note_data *d)
111 if (d->edit_path) {
112 unlink_or_warn(d->edit_path);
113 free(d->edit_path);
115 strbuf_release(&d->buf);
118 static int list_each_note(const struct object_id *object_oid,
119 const struct object_id *note_oid,
120 char *note_path UNUSED,
121 void *cb_data UNUSED)
123 printf("%s %s\n", oid_to_hex(note_oid), oid_to_hex(object_oid));
124 return 0;
127 static void copy_obj_to_fd(int fd, const struct object_id *oid)
129 unsigned long size;
130 enum object_type type;
131 char *buf = repo_read_object_file(the_repository, oid, &type, &size);
132 if (buf) {
133 if (size)
134 write_or_die(fd, buf, size);
135 free(buf);
139 static void write_commented_object(int fd, const struct object_id *object)
141 struct child_process show = CHILD_PROCESS_INIT;
142 struct strbuf buf = STRBUF_INIT;
143 struct strbuf cbuf = STRBUF_INIT;
145 /* Invoke "git show --stat --no-notes $object" */
146 strvec_pushl(&show.args, "show", "--stat", "--no-notes",
147 oid_to_hex(object), NULL);
148 show.no_stdin = 1;
149 show.out = -1;
150 show.err = 0;
151 show.git_cmd = 1;
152 if (start_command(&show))
153 die(_("unable to start 'show' for object '%s'"),
154 oid_to_hex(object));
156 if (strbuf_read(&buf, show.out, 0) < 0)
157 die_errno(_("could not read 'show' output"));
158 strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
159 write_or_die(fd, cbuf.buf, cbuf.len);
161 strbuf_release(&cbuf);
162 strbuf_release(&buf);
164 if (finish_command(&show))
165 die(_("failed to finish 'show' for object '%s'"),
166 oid_to_hex(object));
169 static void prepare_note_data(const struct object_id *object, struct note_data *d,
170 const struct object_id *old_note)
172 if (d->use_editor || !d->given) {
173 int fd;
174 struct strbuf buf = STRBUF_INIT;
176 /* write the template message before editing: */
177 d->edit_path = git_pathdup("NOTES_EDITMSG");
178 fd = xopen(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
180 if (d->given)
181 write_or_die(fd, d->buf.buf, d->buf.len);
182 else if (old_note)
183 copy_obj_to_fd(fd, old_note);
185 strbuf_addch(&buf, '\n');
186 strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
187 strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)));
188 strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
189 write_or_die(fd, buf.buf, buf.len);
191 write_commented_object(fd, object);
193 close(fd);
194 strbuf_release(&buf);
195 strbuf_reset(&d->buf);
197 if (launch_editor(d->edit_path, &d->buf, NULL)) {
198 die(_("please supply the note contents using either -m or -F option"));
200 strbuf_stripspace(&d->buf, 1);
204 static void write_note_data(struct note_data *d, struct object_id *oid)
206 if (write_object_file(d->buf.buf, d->buf.len, OBJ_BLOB, oid)) {
207 int status = die_message(_("unable to write note object"));
209 if (d->edit_path)
210 die_message(_("the note contents have been left in %s"),
211 d->edit_path);
212 exit(status);
216 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
218 struct note_data *d = opt->value;
220 BUG_ON_OPT_NEG(unset);
222 if (d->buf.len)
223 strbuf_addch(&d->buf, '\n');
224 strbuf_addstr(&d->buf, arg);
225 strbuf_stripspace(&d->buf, 0);
227 d->given = 1;
228 return 0;
231 static int parse_file_arg(const struct option *opt, const char *arg, int unset)
233 struct note_data *d = opt->value;
235 BUG_ON_OPT_NEG(unset);
237 if (d->buf.len)
238 strbuf_addch(&d->buf, '\n');
239 if (!strcmp(arg, "-")) {
240 if (strbuf_read(&d->buf, 0, 1024) < 0)
241 die_errno(_("cannot read '%s'"), arg);
242 } else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
243 die_errno(_("could not open or read '%s'"), arg);
244 strbuf_stripspace(&d->buf, 0);
246 d->given = 1;
247 return 0;
250 static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
252 struct note_data *d = opt->value;
253 char *buf;
254 struct object_id object;
255 enum object_type type;
256 unsigned long len;
258 BUG_ON_OPT_NEG(unset);
260 if (d->buf.len)
261 strbuf_addch(&d->buf, '\n');
263 if (repo_get_oid(the_repository, arg, &object))
264 die(_("failed to resolve '%s' as a valid ref."), arg);
265 if (!(buf = repo_read_object_file(the_repository, &object, &type, &len)))
266 die(_("failed to read object '%s'."), arg);
267 if (type != OBJ_BLOB) {
268 free(buf);
269 die(_("cannot read note data from non-blob object '%s'."), arg);
271 strbuf_add(&d->buf, buf, len);
272 free(buf);
274 d->given = 1;
275 return 0;
278 static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
280 struct note_data *d = opt->value;
281 BUG_ON_OPT_NEG(unset);
282 d->use_editor = 1;
283 return parse_reuse_arg(opt, arg, unset);
286 static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
288 struct strbuf buf = STRBUF_INIT;
289 struct notes_rewrite_cfg *c = NULL;
290 struct notes_tree *t = NULL;
291 int ret = 0;
292 const char *msg = "Notes added by 'git notes copy'";
294 if (rewrite_cmd) {
295 c = init_copy_notes_for_rewrite(rewrite_cmd);
296 if (!c)
297 return 0;
298 } else {
299 init_notes(NULL, NULL, NULL, NOTES_INIT_WRITABLE);
300 t = &default_notes_tree;
303 while (strbuf_getline_lf(&buf, stdin) != EOF) {
304 struct object_id from_obj, to_obj;
305 struct strbuf **split;
306 int err;
308 split = strbuf_split(&buf, ' ');
309 if (!split[0] || !split[1])
310 die(_("malformed input line: '%s'."), buf.buf);
311 strbuf_rtrim(split[0]);
312 strbuf_rtrim(split[1]);
313 if (repo_get_oid(the_repository, split[0]->buf, &from_obj))
314 die(_("failed to resolve '%s' as a valid ref."), split[0]->buf);
315 if (repo_get_oid(the_repository, split[1]->buf, &to_obj))
316 die(_("failed to resolve '%s' as a valid ref."), split[1]->buf);
318 if (rewrite_cmd)
319 err = copy_note_for_rewrite(c, &from_obj, &to_obj);
320 else
321 err = copy_note(t, &from_obj, &to_obj, force,
322 combine_notes_overwrite);
324 if (err) {
325 error(_("failed to copy notes from '%s' to '%s'"),
326 split[0]->buf, split[1]->buf);
327 ret = 1;
330 strbuf_list_free(split);
333 if (!rewrite_cmd) {
334 commit_notes(the_repository, t, msg);
335 free_notes(t);
336 } else {
337 finish_copy_notes_for_rewrite(the_repository, c, msg);
339 strbuf_release(&buf);
340 return ret;
343 static struct notes_tree *init_notes_check(const char *subcommand,
344 int flags)
346 struct notes_tree *t;
347 const char *ref;
348 init_notes(NULL, NULL, NULL, flags);
349 t = &default_notes_tree;
351 ref = (flags & NOTES_INIT_WRITABLE) ? t->update_ref : t->ref;
352 if (!starts_with(ref, "refs/notes/"))
354 * TRANSLATORS: the first %s will be replaced by a git
355 * notes command: 'add', 'merge', 'remove', etc.
357 die(_("refusing to %s notes in %s (outside of refs/notes/)"),
358 subcommand, ref);
359 return t;
362 static int list(int argc, const char **argv, const char *prefix)
364 struct notes_tree *t;
365 struct object_id object;
366 const struct object_id *note;
367 int retval = -1;
368 struct option options[] = {
369 OPT_END()
372 if (argc)
373 argc = parse_options(argc, argv, prefix, options,
374 git_notes_list_usage, 0);
376 if (1 < argc) {
377 error(_("too many arguments"));
378 usage_with_options(git_notes_list_usage, options);
381 t = init_notes_check("list", 0);
382 if (argc) {
383 if (repo_get_oid(the_repository, argv[0], &object))
384 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
385 note = get_note(t, &object);
386 if (note) {
387 puts(oid_to_hex(note));
388 retval = 0;
389 } else
390 retval = error(_("no note found for object %s."),
391 oid_to_hex(&object));
392 } else
393 retval = for_each_note(t, 0, list_each_note, NULL);
395 free_notes(t);
396 return retval;
399 static int append_edit(int argc, const char **argv, const char *prefix);
401 static int add(int argc, const char **argv, const char *prefix)
403 int force = 0, allow_empty = 0;
404 const char *object_ref;
405 struct notes_tree *t;
406 struct object_id object, new_note;
407 const struct object_id *note;
408 struct note_data d = { .buf = STRBUF_INIT };
409 struct option options[] = {
410 OPT_CALLBACK_F('m', "message", &d, N_("message"),
411 N_("note contents as a string"), PARSE_OPT_NONEG,
412 parse_msg_arg),
413 OPT_CALLBACK_F('F', "file", &d, N_("file"),
414 N_("note contents in a file"), PARSE_OPT_NONEG,
415 parse_file_arg),
416 OPT_CALLBACK_F('c', "reedit-message", &d, N_("object"),
417 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
418 parse_reedit_arg),
419 OPT_CALLBACK_F('C', "reuse-message", &d, N_("object"),
420 N_("reuse specified note object"), PARSE_OPT_NONEG,
421 parse_reuse_arg),
422 OPT_BOOL(0, "allow-empty", &allow_empty,
423 N_("allow storing empty note")),
424 OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE),
425 OPT_END()
428 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
429 PARSE_OPT_KEEP_ARGV0);
431 if (2 < argc) {
432 error(_("too many arguments"));
433 usage_with_options(git_notes_add_usage, options);
436 object_ref = argc > 1 ? argv[1] : "HEAD";
438 if (repo_get_oid(the_repository, object_ref, &object))
439 die(_("failed to resolve '%s' as a valid ref."), object_ref);
441 t = init_notes_check("add", NOTES_INIT_WRITABLE);
442 note = get_note(t, &object);
444 if (note) {
445 if (!force) {
446 free_notes(t);
447 if (d.given) {
448 free_note_data(&d);
449 return error(_("Cannot add notes. "
450 "Found existing notes for object %s. "
451 "Use '-f' to overwrite existing notes"),
452 oid_to_hex(&object));
455 * Redirect to "edit" subcommand.
457 * We only end up here if none of -m/-F/-c/-C or -f are
458 * given. The original args are therefore still in
459 * argv[0-1].
461 argv[0] = "edit";
462 return append_edit(argc, argv, prefix);
464 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
465 oid_to_hex(&object));
468 prepare_note_data(&object, &d, note);
469 if (d.buf.len || allow_empty) {
470 write_note_data(&d, &new_note);
471 if (add_note(t, &object, &new_note, combine_notes_overwrite))
472 BUG("combine_notes_overwrite failed");
473 commit_notes(the_repository, t,
474 "Notes added by 'git notes add'");
475 } else {
476 fprintf(stderr, _("Removing note for object %s\n"),
477 oid_to_hex(&object));
478 remove_note(t, object.hash);
479 commit_notes(the_repository, t,
480 "Notes removed by 'git notes add'");
483 free_note_data(&d);
484 free_notes(t);
485 return 0;
488 static int copy(int argc, const char **argv, const char *prefix)
490 int retval = 0, force = 0, from_stdin = 0;
491 const struct object_id *from_note, *note;
492 const char *object_ref;
493 struct object_id object, from_obj;
494 struct notes_tree *t;
495 const char *rewrite_cmd = NULL;
496 struct option options[] = {
497 OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE),
498 OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")),
499 OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"),
500 N_("load rewriting config for <command> (implies "
501 "--stdin)")),
502 OPT_END()
505 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
508 if (from_stdin || rewrite_cmd) {
509 if (argc) {
510 error(_("too many arguments"));
511 usage_with_options(git_notes_copy_usage, options);
512 } else {
513 return notes_copy_from_stdin(force, rewrite_cmd);
517 if (argc < 1) {
518 error(_("too few arguments"));
519 usage_with_options(git_notes_copy_usage, options);
521 if (2 < argc) {
522 error(_("too many arguments"));
523 usage_with_options(git_notes_copy_usage, options);
526 if (repo_get_oid(the_repository, argv[0], &from_obj))
527 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
529 object_ref = 1 < argc ? argv[1] : "HEAD";
531 if (repo_get_oid(the_repository, object_ref, &object))
532 die(_("failed to resolve '%s' as a valid ref."), object_ref);
534 t = init_notes_check("copy", NOTES_INIT_WRITABLE);
535 note = get_note(t, &object);
537 if (note) {
538 if (!force) {
539 retval = error(_("Cannot copy notes. Found existing "
540 "notes for object %s. Use '-f' to "
541 "overwrite existing notes"),
542 oid_to_hex(&object));
543 goto out;
545 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
546 oid_to_hex(&object));
549 from_note = get_note(t, &from_obj);
550 if (!from_note) {
551 retval = error(_("missing notes on source object %s. Cannot "
552 "copy."), oid_to_hex(&from_obj));
553 goto out;
556 if (add_note(t, &object, from_note, combine_notes_overwrite))
557 BUG("combine_notes_overwrite failed");
558 commit_notes(the_repository, t,
559 "Notes added by 'git notes copy'");
560 out:
561 free_notes(t);
562 return retval;
565 static int append_edit(int argc, const char **argv, const char *prefix)
567 int allow_empty = 0;
568 const char *object_ref;
569 struct notes_tree *t;
570 struct object_id object, new_note;
571 const struct object_id *note;
572 char *logmsg;
573 const char * const *usage;
574 struct note_data d = { .buf = STRBUF_INIT };
575 struct option options[] = {
576 OPT_CALLBACK_F('m', "message", &d, N_("message"),
577 N_("note contents as a string"), PARSE_OPT_NONEG,
578 parse_msg_arg),
579 OPT_CALLBACK_F('F', "file", &d, N_("file"),
580 N_("note contents in a file"), PARSE_OPT_NONEG,
581 parse_file_arg),
582 OPT_CALLBACK_F('c', "reedit-message", &d, N_("object"),
583 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
584 parse_reedit_arg),
585 OPT_CALLBACK_F('C', "reuse-message", &d, N_("object"),
586 N_("reuse specified note object"), PARSE_OPT_NONEG,
587 parse_reuse_arg),
588 OPT_BOOL(0, "allow-empty", &allow_empty,
589 N_("allow storing empty note")),
590 OPT_END()
592 int edit = !strcmp(argv[0], "edit");
594 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
595 argc = parse_options(argc, argv, prefix, options, usage,
596 PARSE_OPT_KEEP_ARGV0);
598 if (2 < argc) {
599 error(_("too many arguments"));
600 usage_with_options(usage, options);
603 if (d.given && edit)
604 fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
605 "for the 'edit' subcommand.\n"
606 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
608 object_ref = 1 < argc ? argv[1] : "HEAD";
610 if (repo_get_oid(the_repository, object_ref, &object))
611 die(_("failed to resolve '%s' as a valid ref."), object_ref);
613 t = init_notes_check(argv[0], NOTES_INIT_WRITABLE);
614 note = get_note(t, &object);
616 prepare_note_data(&object, &d, edit && note ? note : NULL);
618 if (note && !edit) {
619 /* Append buf to previous note contents */
620 unsigned long size;
621 enum object_type type;
622 char *prev_buf = repo_read_object_file(the_repository, note,
623 &type, &size);
625 if (d.buf.len && prev_buf && size)
626 strbuf_insertstr(&d.buf, 0, "\n");
627 if (prev_buf && size)
628 strbuf_insert(&d.buf, 0, prev_buf, size);
629 free(prev_buf);
632 if (d.buf.len || allow_empty) {
633 write_note_data(&d, &new_note);
634 if (add_note(t, &object, &new_note, combine_notes_overwrite))
635 BUG("combine_notes_overwrite failed");
636 logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]);
637 } else {
638 fprintf(stderr, _("Removing note for object %s\n"),
639 oid_to_hex(&object));
640 remove_note(t, object.hash);
641 logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]);
643 commit_notes(the_repository, t, logmsg);
645 free(logmsg);
646 free_note_data(&d);
647 free_notes(t);
648 return 0;
651 static int show(int argc, const char **argv, const char *prefix)
653 const char *object_ref;
654 struct notes_tree *t;
655 struct object_id object;
656 const struct object_id *note;
657 int retval;
658 struct option options[] = {
659 OPT_END()
662 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
665 if (1 < argc) {
666 error(_("too many arguments"));
667 usage_with_options(git_notes_show_usage, options);
670 object_ref = argc ? argv[0] : "HEAD";
672 if (repo_get_oid(the_repository, object_ref, &object))
673 die(_("failed to resolve '%s' as a valid ref."), object_ref);
675 t = init_notes_check("show", 0);
676 note = get_note(t, &object);
678 if (!note)
679 retval = error(_("no note found for object %s."),
680 oid_to_hex(&object));
681 else {
682 const char *show_args[3] = {"show", oid_to_hex(note), NULL};
683 retval = execv_git_cmd(show_args);
685 free_notes(t);
686 return retval;
689 static int merge_abort(struct notes_merge_options *o)
691 int ret = 0;
694 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
695 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
698 if (delete_ref(NULL, "NOTES_MERGE_PARTIAL", NULL, 0))
699 ret += error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
700 if (delete_ref(NULL, "NOTES_MERGE_REF", NULL, REF_NO_DEREF))
701 ret += error(_("failed to delete ref NOTES_MERGE_REF"));
702 if (notes_merge_abort(o))
703 ret += error(_("failed to remove 'git notes merge' worktree"));
704 return ret;
707 static int merge_commit(struct notes_merge_options *o)
709 struct strbuf msg = STRBUF_INIT;
710 struct object_id oid, parent_oid;
711 struct notes_tree *t;
712 struct commit *partial;
713 struct pretty_print_context pretty_ctx;
714 void *local_ref_to_free;
715 int ret;
718 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
719 * and target notes ref from .git/NOTES_MERGE_REF.
722 if (repo_get_oid(the_repository, "NOTES_MERGE_PARTIAL", &oid))
723 die(_("failed to read ref NOTES_MERGE_PARTIAL"));
724 else if (!(partial = lookup_commit_reference(the_repository, &oid)))
725 die(_("could not find commit from NOTES_MERGE_PARTIAL."));
726 else if (repo_parse_commit(the_repository, partial))
727 die(_("could not parse commit from NOTES_MERGE_PARTIAL."));
729 if (partial->parents)
730 oidcpy(&parent_oid, &partial->parents->item->object.oid);
731 else
732 oidclr(&parent_oid);
734 CALLOC_ARRAY(t, 1);
735 init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
737 o->local_ref = local_ref_to_free =
738 resolve_refdup("NOTES_MERGE_REF", 0, &oid, NULL);
739 if (!o->local_ref)
740 die(_("failed to resolve NOTES_MERGE_REF"));
742 if (notes_merge_commit(o, t, partial, &oid))
743 die(_("failed to finalize notes merge"));
745 /* Reuse existing commit message in reflog message */
746 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
747 repo_format_commit_message(the_repository, partial, "%s", &msg,
748 &pretty_ctx);
749 strbuf_trim(&msg);
750 strbuf_insertstr(&msg, 0, "notes: ");
751 update_ref(msg.buf, o->local_ref, &oid,
752 is_null_oid(&parent_oid) ? NULL : &parent_oid,
753 0, UPDATE_REFS_DIE_ON_ERR);
755 free_notes(t);
756 strbuf_release(&msg);
757 ret = merge_abort(o);
758 free(local_ref_to_free);
759 return ret;
762 static int git_config_get_notes_strategy(const char *key,
763 enum notes_merge_strategy *strategy)
765 char *value;
767 if (git_config_get_string(key, &value))
768 return 1;
769 if (parse_notes_merge_strategy(value, strategy))
770 git_die_config(key, _("unknown notes merge strategy %s"), value);
772 free(value);
773 return 0;
776 static int merge(int argc, const char **argv, const char *prefix)
778 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
779 struct object_id result_oid;
780 struct notes_tree *t;
781 struct notes_merge_options o;
782 int do_merge = 0, do_commit = 0, do_abort = 0;
783 int verbosity = 0, result;
784 const char *strategy = NULL;
785 struct option options[] = {
786 OPT_GROUP(N_("General options")),
787 OPT__VERBOSITY(&verbosity),
788 OPT_GROUP(N_("Merge options")),
789 OPT_STRING('s', "strategy", &strategy, N_("strategy"),
790 N_("resolve notes conflicts using the given strategy "
791 "(manual/ours/theirs/union/cat_sort_uniq)")),
792 OPT_GROUP(N_("Committing unmerged notes")),
793 OPT_SET_INT_F(0, "commit", &do_commit,
794 N_("finalize notes merge by committing unmerged notes"),
795 1, PARSE_OPT_NONEG),
796 OPT_GROUP(N_("Aborting notes merge resolution")),
797 OPT_SET_INT_F(0, "abort", &do_abort,
798 N_("abort notes merge"),
799 1, PARSE_OPT_NONEG),
800 OPT_END()
803 argc = parse_options(argc, argv, prefix, options,
804 git_notes_merge_usage, 0);
806 if (strategy || do_commit + do_abort == 0)
807 do_merge = 1;
808 if (do_merge + do_commit + do_abort != 1) {
809 error(_("cannot mix --commit, --abort or -s/--strategy"));
810 usage_with_options(git_notes_merge_usage, options);
813 if (do_merge && argc != 1) {
814 error(_("must specify a notes ref to merge"));
815 usage_with_options(git_notes_merge_usage, options);
816 } else if (!do_merge && argc) {
817 error(_("too many arguments"));
818 usage_with_options(git_notes_merge_usage, options);
821 init_notes_merge_options(the_repository, &o);
822 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
824 if (do_abort)
825 return merge_abort(&o);
826 if (do_commit)
827 return merge_commit(&o);
829 o.local_ref = default_notes_ref();
830 strbuf_addstr(&remote_ref, argv[0]);
831 expand_loose_notes_ref(&remote_ref);
832 o.remote_ref = remote_ref.buf;
834 t = init_notes_check("merge", NOTES_INIT_WRITABLE);
836 if (strategy) {
837 if (parse_notes_merge_strategy(strategy, &o.strategy)) {
838 error(_("unknown -s/--strategy: %s"), strategy);
839 usage_with_options(git_notes_merge_usage, options);
841 } else {
842 struct strbuf merge_key = STRBUF_INIT;
843 const char *short_ref = NULL;
845 if (!skip_prefix(o.local_ref, "refs/notes/", &short_ref))
846 BUG("local ref %s is outside of refs/notes/",
847 o.local_ref);
849 strbuf_addf(&merge_key, "notes.%s.mergeStrategy", short_ref);
851 if (git_config_get_notes_strategy(merge_key.buf, &o.strategy))
852 git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy);
854 strbuf_release(&merge_key);
857 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
858 remote_ref.buf, default_notes_ref());
859 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
861 result = notes_merge(&o, t, &result_oid);
863 if (result >= 0) /* Merge resulted (trivially) in result_oid */
864 /* Update default notes ref with new commit */
865 update_ref(msg.buf, default_notes_ref(), &result_oid, NULL, 0,
866 UPDATE_REFS_DIE_ON_ERR);
867 else { /* Merge has unresolved conflicts */
868 struct worktree **worktrees;
869 const struct worktree *wt;
870 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
871 update_ref(msg.buf, "NOTES_MERGE_PARTIAL", &result_oid, NULL,
872 0, UPDATE_REFS_DIE_ON_ERR);
873 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
874 worktrees = get_worktrees();
875 wt = find_shared_symref(worktrees, "NOTES_MERGE_REF",
876 default_notes_ref());
877 if (wt)
878 die(_("a notes merge into %s is already in-progress at %s"),
879 default_notes_ref(), wt->path);
880 free_worktrees(worktrees);
881 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
882 die(_("failed to store link to current notes ref (%s)"),
883 default_notes_ref());
884 fprintf(stderr, _("Automatic notes merge failed. Fix conflicts in %s "
885 "and commit the result with 'git notes merge --commit', "
886 "or abort the merge with 'git notes merge --abort'.\n"),
887 git_path(NOTES_MERGE_WORKTREE));
890 free_notes(t);
891 strbuf_release(&remote_ref);
892 strbuf_release(&msg);
893 return result < 0; /* return non-zero on conflicts */
896 #define IGNORE_MISSING 1
898 static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
900 int status;
901 struct object_id oid;
902 if (repo_get_oid(the_repository, name, &oid))
903 return error(_("Failed to resolve '%s' as a valid ref."), name);
904 status = remove_note(t, oid.hash);
905 if (status)
906 fprintf(stderr, _("Object %s has no note\n"), name);
907 else
908 fprintf(stderr, _("Removing note for object %s\n"), name);
909 return (flag & IGNORE_MISSING) ? 0 : status;
912 static int remove_cmd(int argc, const char **argv, const char *prefix)
914 unsigned flag = 0;
915 int from_stdin = 0;
916 struct option options[] = {
917 OPT_BIT(0, "ignore-missing", &flag,
918 N_("attempt to remove non-existent note is not an error"),
919 IGNORE_MISSING),
920 OPT_BOOL(0, "stdin", &from_stdin,
921 N_("read object names from the standard input")),
922 OPT_END()
924 struct notes_tree *t;
925 int retval = 0;
927 argc = parse_options(argc, argv, prefix, options,
928 git_notes_remove_usage, 0);
930 t = init_notes_check("remove", NOTES_INIT_WRITABLE);
932 if (!argc && !from_stdin) {
933 retval = remove_one_note(t, "HEAD", flag);
934 } else {
935 while (*argv) {
936 retval |= remove_one_note(t, *argv, flag);
937 argv++;
940 if (from_stdin) {
941 struct strbuf sb = STRBUF_INIT;
942 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
943 strbuf_rtrim(&sb);
944 retval |= remove_one_note(t, sb.buf, flag);
946 strbuf_release(&sb);
948 if (!retval)
949 commit_notes(the_repository, t,
950 "Notes removed by 'git notes remove'");
951 free_notes(t);
952 return retval;
955 static int prune(int argc, const char **argv, const char *prefix)
957 struct notes_tree *t;
958 int show_only = 0, verbose = 0;
959 struct option options[] = {
960 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
961 OPT__VERBOSE(&verbose, N_("report pruned notes")),
962 OPT_END()
965 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
968 if (argc) {
969 error(_("too many arguments"));
970 usage_with_options(git_notes_prune_usage, options);
973 t = init_notes_check("prune", NOTES_INIT_WRITABLE);
975 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
976 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
977 if (!show_only)
978 commit_notes(the_repository, t,
979 "Notes removed by 'git notes prune'");
980 free_notes(t);
981 return 0;
984 static int get_ref(int argc, const char **argv, const char *prefix)
986 struct option options[] = { OPT_END() };
987 argc = parse_options(argc, argv, prefix, options,
988 git_notes_get_ref_usage, 0);
990 if (argc) {
991 error(_("too many arguments"));
992 usage_with_options(git_notes_get_ref_usage, options);
995 puts(default_notes_ref());
996 return 0;
999 int cmd_notes(int argc, const char **argv, const char *prefix)
1001 const char *override_notes_ref = NULL;
1002 parse_opt_subcommand_fn *fn = NULL;
1003 struct option options[] = {
1004 OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
1005 N_("use notes from <notes-ref>")),
1006 OPT_SUBCOMMAND("list", &fn, list),
1007 OPT_SUBCOMMAND("add", &fn, add),
1008 OPT_SUBCOMMAND("copy", &fn, copy),
1009 OPT_SUBCOMMAND("append", &fn, append_edit),
1010 OPT_SUBCOMMAND("edit", &fn, append_edit),
1011 OPT_SUBCOMMAND("show", &fn, show),
1012 OPT_SUBCOMMAND("merge", &fn, merge),
1013 OPT_SUBCOMMAND("remove", &fn, remove_cmd),
1014 OPT_SUBCOMMAND("prune", &fn, prune),
1015 OPT_SUBCOMMAND("get-ref", &fn, get_ref),
1016 OPT_END()
1019 git_config(git_default_config, NULL);
1020 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
1021 PARSE_OPT_SUBCOMMAND_OPTIONAL);
1022 if (!fn) {
1023 if (argc) {
1024 error(_("unknown subcommand: `%s'"), argv[0]);
1025 usage_with_options(git_notes_usage, options);
1027 fn = list;
1030 if (override_notes_ref) {
1031 struct strbuf sb = STRBUF_INIT;
1032 strbuf_addstr(&sb, override_notes_ref);
1033 expand_notes_ref(&sb);
1034 setenv("GIT_NOTES_REF", sb.buf, 1);
1035 strbuf_release(&sb);
1038 return !!fn(argc, argv, prefix);