midx: use strvec_pushf() for pack-objects base name
[alt-git.git] / builtin / notes.c
blobcaf20fd5bdd4b7c1cb44191d58ecbb389643b748
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 "builtin.h"
11 #include "config.h"
12 #include "editor.h"
13 #include "environment.h"
14 #include "gettext.h"
15 #include "hex.h"
16 #include "notes.h"
17 #include "object-name.h"
18 #include "object-store-ll.h"
19 #include "path.h"
20 #include "repository.h"
21 #include "pretty.h"
22 #include "refs.h"
23 #include "exec-cmd.h"
24 #include "run-command.h"
25 #include "parse-options.h"
26 #include "string-list.h"
27 #include "notes-merge.h"
28 #include "notes-utils.h"
29 #include "worktree.h"
30 #include "write-or-die.h"
32 static const char *separator = "\n";
33 static const char * const git_notes_usage[] = {
34 N_("git notes [--ref <notes-ref>] [list [<object>]]"),
35 N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [--[no-]separator|--separator=<paragraph-break>] [--[no-]stripspace] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
36 N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"),
37 N_("git notes [--ref <notes-ref>] append [--allow-empty] [--[no-]separator|--separator=<paragraph-break>] [--[no-]stripspace] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
38 N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
39 N_("git notes [--ref <notes-ref>] show [<object>]"),
40 N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"),
41 "git notes merge --commit [-v | -q]",
42 "git notes merge --abort [-v | -q]",
43 N_("git notes [--ref <notes-ref>] remove [<object>...]"),
44 N_("git notes [--ref <notes-ref>] prune [-n] [-v]"),
45 N_("git notes [--ref <notes-ref>] get-ref"),
46 NULL
49 static const char * const git_notes_list_usage[] = {
50 N_("git notes [list [<object>]]"),
51 NULL
54 static const char * const git_notes_add_usage[] = {
55 N_("git notes add [<options>] [<object>]"),
56 NULL
59 static const char * const git_notes_copy_usage[] = {
60 N_("git notes copy [<options>] <from-object> <to-object>"),
61 N_("git notes copy --stdin [<from-object> <to-object>]..."),
62 NULL
65 static const char * const git_notes_append_usage[] = {
66 N_("git notes append [<options>] [<object>]"),
67 NULL
70 static const char * const git_notes_edit_usage[] = {
71 N_("git notes edit [<object>]"),
72 NULL
75 static const char * const git_notes_show_usage[] = {
76 N_("git notes show [<object>]"),
77 NULL
80 static const char * const git_notes_merge_usage[] = {
81 N_("git notes merge [<options>] <notes-ref>"),
82 N_("git notes merge --commit [<options>]"),
83 N_("git notes merge --abort [<options>]"),
84 NULL
87 static const char * const git_notes_remove_usage[] = {
88 N_("git notes remove [<object>]"),
89 NULL
92 static const char * const git_notes_prune_usage[] = {
93 N_("git notes prune [<options>]"),
94 NULL
97 static const char * const git_notes_get_ref_usage[] = {
98 "git notes get-ref",
99 NULL
102 static const char note_template[] =
103 N_("Write/edit the notes for the following object:");
105 enum notes_stripspace {
106 UNSPECIFIED = -1,
107 NO_STRIPSPACE = 0,
108 STRIPSPACE = 1,
111 struct note_msg {
112 enum notes_stripspace stripspace;
113 struct strbuf buf;
116 struct note_data {
117 int given;
118 int use_editor;
119 int stripspace;
120 char *edit_path;
121 struct strbuf buf;
122 struct note_msg **messages;
123 size_t msg_nr;
124 size_t msg_alloc;
127 static void free_note_data(struct note_data *d)
129 if (d->edit_path) {
130 unlink_or_warn(d->edit_path);
131 free(d->edit_path);
133 strbuf_release(&d->buf);
135 while (d->msg_nr--) {
136 strbuf_release(&d->messages[d->msg_nr]->buf);
137 free(d->messages[d->msg_nr]);
139 free(d->messages);
142 static int list_each_note(const struct object_id *object_oid,
143 const struct object_id *note_oid,
144 char *note_path UNUSED,
145 void *cb_data UNUSED)
147 printf("%s %s\n", oid_to_hex(note_oid), oid_to_hex(object_oid));
148 return 0;
151 static void copy_obj_to_fd(int fd, const struct object_id *oid)
153 unsigned long size;
154 enum object_type type;
155 char *buf = repo_read_object_file(the_repository, oid, &type, &size);
156 if (buf) {
157 if (size)
158 write_or_die(fd, buf, size);
159 free(buf);
163 static void write_commented_object(int fd, const struct object_id *object)
165 struct child_process show = CHILD_PROCESS_INIT;
166 struct strbuf buf = STRBUF_INIT;
167 struct strbuf cbuf = STRBUF_INIT;
169 /* Invoke "git show --stat --no-notes $object" */
170 strvec_pushl(&show.args, "show", "--stat", "--no-notes",
171 oid_to_hex(object), NULL);
172 show.no_stdin = 1;
173 show.out = -1;
174 show.err = 0;
175 show.git_cmd = 1;
176 if (start_command(&show))
177 die(_("unable to start 'show' for object '%s'"),
178 oid_to_hex(object));
180 if (strbuf_read(&buf, show.out, 0) < 0)
181 die_errno(_("could not read 'show' output"));
182 strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_char);
183 write_or_die(fd, cbuf.buf, cbuf.len);
185 strbuf_release(&cbuf);
186 strbuf_release(&buf);
188 if (finish_command(&show))
189 die(_("failed to finish 'show' for object '%s'"),
190 oid_to_hex(object));
193 static void prepare_note_data(const struct object_id *object, struct note_data *d,
194 const struct object_id *old_note)
196 if (d->use_editor || !d->given) {
197 int fd;
198 struct strbuf buf = STRBUF_INIT;
200 /* write the template message before editing: */
201 d->edit_path = git_pathdup("NOTES_EDITMSG");
202 fd = xopen(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
204 if (d->given)
205 write_or_die(fd, d->buf.buf, d->buf.len);
206 else if (old_note)
207 copy_obj_to_fd(fd, old_note);
209 strbuf_addch(&buf, '\n');
210 strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
211 strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)),
212 comment_line_char);
213 strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
214 write_or_die(fd, buf.buf, buf.len);
216 write_commented_object(fd, object);
218 close(fd);
219 strbuf_release(&buf);
220 strbuf_reset(&d->buf);
222 if (launch_editor(d->edit_path, &d->buf, NULL)) {
223 die(_("please supply the note contents using either -m or -F option"));
225 if (d->stripspace)
226 strbuf_stripspace(&d->buf, comment_line_char);
230 static void write_note_data(struct note_data *d, struct object_id *oid)
232 if (write_object_file(d->buf.buf, d->buf.len, OBJ_BLOB, oid)) {
233 int status = die_message(_("unable to write note object"));
235 if (d->edit_path)
236 die_message(_("the note contents have been left in %s"),
237 d->edit_path);
238 exit(status);
242 static void append_separator(struct strbuf *message)
244 size_t sep_len = 0;
246 if (!separator)
247 return;
248 else if ((sep_len = strlen(separator)) && separator[sep_len - 1] == '\n')
249 strbuf_addstr(message, separator);
250 else
251 strbuf_addf(message, "%s%s", separator, "\n");
254 static void concat_messages(struct note_data *d)
256 struct strbuf msg = STRBUF_INIT;
257 size_t i;
259 for (i = 0; i < d->msg_nr ; i++) {
260 if (d->buf.len)
261 append_separator(&d->buf);
262 strbuf_add(&msg, d->messages[i]->buf.buf, d->messages[i]->buf.len);
263 strbuf_addbuf(&d->buf, &msg);
264 if ((d->stripspace == UNSPECIFIED &&
265 d->messages[i]->stripspace == STRIPSPACE) ||
266 d->stripspace == STRIPSPACE)
267 strbuf_stripspace(&d->buf, 0);
268 strbuf_reset(&msg);
270 strbuf_release(&msg);
273 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
275 struct note_data *d = opt->value;
276 struct note_msg *msg = xmalloc(sizeof(*msg));
278 BUG_ON_OPT_NEG(unset);
280 strbuf_init(&msg->buf, strlen(arg));
281 strbuf_addstr(&msg->buf, arg);
282 ALLOC_GROW_BY(d->messages, d->msg_nr, 1, d->msg_alloc);
283 d->messages[d->msg_nr - 1] = msg;
284 msg->stripspace = STRIPSPACE;
285 return 0;
288 static int parse_file_arg(const struct option *opt, const char *arg, int unset)
290 struct note_data *d = opt->value;
291 struct note_msg *msg = xmalloc(sizeof(*msg));
293 BUG_ON_OPT_NEG(unset);
295 strbuf_init(&msg->buf , 0);
296 if (!strcmp(arg, "-")) {
297 if (strbuf_read(&msg->buf, 0, 1024) < 0)
298 die_errno(_("cannot read '%s'"), arg);
299 } else if (strbuf_read_file(&msg->buf, arg, 1024) < 0)
300 die_errno(_("could not open or read '%s'"), arg);
302 ALLOC_GROW_BY(d->messages, d->msg_nr, 1, d->msg_alloc);
303 d->messages[d->msg_nr - 1] = msg;
304 msg->stripspace = STRIPSPACE;
305 return 0;
308 static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
310 struct note_data *d = opt->value;
311 struct note_msg *msg = xmalloc(sizeof(*msg));
312 char *value;
313 struct object_id object;
314 enum object_type type;
315 unsigned long len;
317 BUG_ON_OPT_NEG(unset);
319 strbuf_init(&msg->buf, 0);
320 if (repo_get_oid(the_repository, arg, &object))
321 die(_("failed to resolve '%s' as a valid ref."), arg);
322 if (!(value = repo_read_object_file(the_repository, &object, &type, &len)))
323 die(_("failed to read object '%s'."), arg);
324 if (type != OBJ_BLOB) {
325 strbuf_release(&msg->buf);
326 free(value);
327 free(msg);
328 die(_("cannot read note data from non-blob object '%s'."), arg);
331 strbuf_add(&msg->buf, value, len);
332 free(value);
334 msg->buf.len = len;
335 ALLOC_GROW_BY(d->messages, d->msg_nr, 1, d->msg_alloc);
336 d->messages[d->msg_nr - 1] = msg;
337 msg->stripspace = NO_STRIPSPACE;
338 return 0;
341 static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
343 struct note_data *d = opt->value;
344 BUG_ON_OPT_NEG(unset);
345 d->use_editor = 1;
346 return parse_reuse_arg(opt, arg, unset);
349 static int parse_separator_arg(const struct option *opt, const char *arg,
350 int unset)
352 if (unset)
353 *(const char **)opt->value = NULL;
354 else
355 *(const char **)opt->value = arg ? arg : "\n";
356 return 0;
359 static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
361 struct strbuf buf = STRBUF_INIT;
362 struct notes_rewrite_cfg *c = NULL;
363 struct notes_tree *t = NULL;
364 int ret = 0;
365 const char *msg = "Notes added by 'git notes copy'";
367 if (rewrite_cmd) {
368 c = init_copy_notes_for_rewrite(rewrite_cmd);
369 if (!c)
370 return 0;
371 } else {
372 init_notes(NULL, NULL, NULL, NOTES_INIT_WRITABLE);
373 t = &default_notes_tree;
376 while (strbuf_getline_lf(&buf, stdin) != EOF) {
377 struct object_id from_obj, to_obj;
378 struct strbuf **split;
379 int err;
381 split = strbuf_split(&buf, ' ');
382 if (!split[0] || !split[1])
383 die(_("malformed input line: '%s'."), buf.buf);
384 strbuf_rtrim(split[0]);
385 strbuf_rtrim(split[1]);
386 if (repo_get_oid(the_repository, split[0]->buf, &from_obj))
387 die(_("failed to resolve '%s' as a valid ref."), split[0]->buf);
388 if (repo_get_oid(the_repository, split[1]->buf, &to_obj))
389 die(_("failed to resolve '%s' as a valid ref."), split[1]->buf);
391 if (rewrite_cmd)
392 err = copy_note_for_rewrite(c, &from_obj, &to_obj);
393 else
394 err = copy_note(t, &from_obj, &to_obj, force,
395 combine_notes_overwrite);
397 if (err) {
398 error(_("failed to copy notes from '%s' to '%s'"),
399 split[0]->buf, split[1]->buf);
400 ret = 1;
403 strbuf_list_free(split);
406 if (!rewrite_cmd) {
407 commit_notes(the_repository, t, msg);
408 free_notes(t);
409 } else {
410 finish_copy_notes_for_rewrite(the_repository, c, msg);
412 strbuf_release(&buf);
413 return ret;
416 static struct notes_tree *init_notes_check(const char *subcommand,
417 int flags)
419 struct notes_tree *t;
420 const char *ref;
421 init_notes(NULL, NULL, NULL, flags);
422 t = &default_notes_tree;
424 ref = (flags & NOTES_INIT_WRITABLE) ? t->update_ref : t->ref;
425 if (!starts_with(ref, "refs/notes/"))
427 * TRANSLATORS: the first %s will be replaced by a git
428 * notes command: 'add', 'merge', 'remove', etc.
430 die(_("refusing to %s notes in %s (outside of refs/notes/)"),
431 subcommand, ref);
432 return t;
435 static int list(int argc, const char **argv, const char *prefix)
437 struct notes_tree *t;
438 struct object_id object;
439 const struct object_id *note;
440 int retval = -1;
441 struct option options[] = {
442 OPT_END()
445 if (argc)
446 argc = parse_options(argc, argv, prefix, options,
447 git_notes_list_usage, 0);
449 if (1 < argc) {
450 error(_("too many arguments"));
451 usage_with_options(git_notes_list_usage, options);
454 t = init_notes_check("list", 0);
455 if (argc) {
456 if (repo_get_oid(the_repository, argv[0], &object))
457 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
458 note = get_note(t, &object);
459 if (note) {
460 puts(oid_to_hex(note));
461 retval = 0;
462 } else
463 retval = error(_("no note found for object %s."),
464 oid_to_hex(&object));
465 } else
466 retval = for_each_note(t, 0, list_each_note, NULL);
468 free_notes(t);
469 return retval;
472 static int append_edit(int argc, const char **argv, const char *prefix);
474 static int add(int argc, const char **argv, const char *prefix)
476 int force = 0, allow_empty = 0;
477 const char *object_ref;
478 struct notes_tree *t;
479 struct object_id object, new_note;
480 const struct object_id *note;
481 struct note_data d = { .buf = STRBUF_INIT, .stripspace = UNSPECIFIED };
483 struct option options[] = {
484 OPT_CALLBACK_F('m', "message", &d, N_("message"),
485 N_("note contents as a string"), PARSE_OPT_NONEG,
486 parse_msg_arg),
487 OPT_CALLBACK_F('F', "file", &d, N_("file"),
488 N_("note contents in a file"), PARSE_OPT_NONEG,
489 parse_file_arg),
490 OPT_CALLBACK_F('c', "reedit-message", &d, N_("object"),
491 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
492 parse_reedit_arg),
493 OPT_CALLBACK_F('C', "reuse-message", &d, N_("object"),
494 N_("reuse specified note object"), PARSE_OPT_NONEG,
495 parse_reuse_arg),
496 OPT_BOOL(0, "allow-empty", &allow_empty,
497 N_("allow storing empty note")),
498 OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE),
499 OPT_CALLBACK_F(0, "separator", &separator,
500 N_("<paragraph-break>"),
501 N_("insert <paragraph-break> between paragraphs"),
502 PARSE_OPT_OPTARG, parse_separator_arg),
503 OPT_BOOL(0, "stripspace", &d.stripspace,
504 N_("remove unnecessary whitespace")),
505 OPT_END()
508 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
509 PARSE_OPT_KEEP_ARGV0);
511 if (2 < argc) {
512 error(_("too many arguments"));
513 usage_with_options(git_notes_add_usage, options);
516 if (d.msg_nr)
517 concat_messages(&d);
518 d.given = !!d.buf.len;
520 object_ref = argc > 1 ? argv[1] : "HEAD";
522 if (repo_get_oid(the_repository, object_ref, &object))
523 die(_("failed to resolve '%s' as a valid ref."), object_ref);
525 t = init_notes_check("add", NOTES_INIT_WRITABLE);
526 note = get_note(t, &object);
528 if (note) {
529 if (!force) {
530 free_notes(t);
531 if (d.given) {
532 free_note_data(&d);
533 return error(_("Cannot add notes. "
534 "Found existing notes for object %s. "
535 "Use '-f' to overwrite existing notes"),
536 oid_to_hex(&object));
539 * Redirect to "edit" subcommand.
541 * We only end up here if none of -m/-F/-c/-C or -f are
542 * given. The original args are therefore still in
543 * argv[0-1].
545 argv[0] = "edit";
546 return append_edit(argc, argv, prefix);
548 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
549 oid_to_hex(&object));
552 prepare_note_data(&object, &d, note);
553 if (d.buf.len || allow_empty) {
554 write_note_data(&d, &new_note);
555 if (add_note(t, &object, &new_note, combine_notes_overwrite))
556 BUG("combine_notes_overwrite failed");
557 commit_notes(the_repository, t,
558 "Notes added by 'git notes add'");
559 } else {
560 fprintf(stderr, _("Removing note for object %s\n"),
561 oid_to_hex(&object));
562 remove_note(t, object.hash);
563 commit_notes(the_repository, t,
564 "Notes removed by 'git notes add'");
567 free_note_data(&d);
568 free_notes(t);
569 return 0;
572 static int copy(int argc, const char **argv, const char *prefix)
574 int retval = 0, force = 0, from_stdin = 0;
575 const struct object_id *from_note, *note;
576 const char *object_ref;
577 struct object_id object, from_obj;
578 struct notes_tree *t;
579 const char *rewrite_cmd = NULL;
580 struct option options[] = {
581 OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE),
582 OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")),
583 OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"),
584 N_("load rewriting config for <command> (implies "
585 "--stdin)")),
586 OPT_END()
589 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
592 if (from_stdin || rewrite_cmd) {
593 if (argc) {
594 error(_("too many arguments"));
595 usage_with_options(git_notes_copy_usage, options);
596 } else {
597 return notes_copy_from_stdin(force, rewrite_cmd);
601 if (argc < 1) {
602 error(_("too few arguments"));
603 usage_with_options(git_notes_copy_usage, options);
605 if (2 < argc) {
606 error(_("too many arguments"));
607 usage_with_options(git_notes_copy_usage, options);
610 if (repo_get_oid(the_repository, argv[0], &from_obj))
611 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
613 object_ref = 1 < argc ? argv[1] : "HEAD";
615 if (repo_get_oid(the_repository, object_ref, &object))
616 die(_("failed to resolve '%s' as a valid ref."), object_ref);
618 t = init_notes_check("copy", NOTES_INIT_WRITABLE);
619 note = get_note(t, &object);
621 if (note) {
622 if (!force) {
623 retval = error(_("Cannot copy notes. Found existing "
624 "notes for object %s. Use '-f' to "
625 "overwrite existing notes"),
626 oid_to_hex(&object));
627 goto out;
629 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
630 oid_to_hex(&object));
633 from_note = get_note(t, &from_obj);
634 if (!from_note) {
635 retval = error(_("missing notes on source object %s. Cannot "
636 "copy."), oid_to_hex(&from_obj));
637 goto out;
640 if (add_note(t, &object, from_note, combine_notes_overwrite))
641 BUG("combine_notes_overwrite failed");
642 commit_notes(the_repository, t,
643 "Notes added by 'git notes copy'");
644 out:
645 free_notes(t);
646 return retval;
649 static int append_edit(int argc, const char **argv, const char *prefix)
651 int allow_empty = 0;
652 const char *object_ref;
653 struct notes_tree *t;
654 struct object_id object, new_note;
655 const struct object_id *note;
656 char *logmsg;
657 const char * const *usage;
658 struct note_data d = { .buf = STRBUF_INIT, .stripspace = UNSPECIFIED };
659 struct option options[] = {
660 OPT_CALLBACK_F('m', "message", &d, N_("message"),
661 N_("note contents as a string"), PARSE_OPT_NONEG,
662 parse_msg_arg),
663 OPT_CALLBACK_F('F', "file", &d, N_("file"),
664 N_("note contents in a file"), PARSE_OPT_NONEG,
665 parse_file_arg),
666 OPT_CALLBACK_F('c', "reedit-message", &d, N_("object"),
667 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
668 parse_reedit_arg),
669 OPT_CALLBACK_F('C', "reuse-message", &d, N_("object"),
670 N_("reuse specified note object"), PARSE_OPT_NONEG,
671 parse_reuse_arg),
672 OPT_BOOL(0, "allow-empty", &allow_empty,
673 N_("allow storing empty note")),
674 OPT_CALLBACK_F(0, "separator", &separator,
675 N_("<paragraph-break>"),
676 N_("insert <paragraph-break> between paragraphs"),
677 PARSE_OPT_OPTARG, parse_separator_arg),
678 OPT_BOOL(0, "stripspace", &d.stripspace,
679 N_("remove unnecessary whitespace")),
680 OPT_END()
682 int edit = !strcmp(argv[0], "edit");
684 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
685 argc = parse_options(argc, argv, prefix, options, usage,
686 PARSE_OPT_KEEP_ARGV0);
688 if (2 < argc) {
689 error(_("too many arguments"));
690 usage_with_options(usage, options);
693 if (d.msg_nr)
694 concat_messages(&d);
695 d.given = !!d.buf.len;
697 if (d.given && edit)
698 fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
699 "for the 'edit' subcommand.\n"
700 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
702 object_ref = 1 < argc ? argv[1] : "HEAD";
704 if (repo_get_oid(the_repository, object_ref, &object))
705 die(_("failed to resolve '%s' as a valid ref."), object_ref);
707 t = init_notes_check(argv[0], NOTES_INIT_WRITABLE);
708 note = get_note(t, &object);
710 prepare_note_data(&object, &d, edit && note ? note : NULL);
712 if (note && !edit) {
713 /* Append buf to previous note contents */
714 unsigned long size;
715 enum object_type type;
716 struct strbuf buf = STRBUF_INIT;
717 char *prev_buf = repo_read_object_file(the_repository, note, &type, &size);
719 if (!prev_buf)
720 die(_("unable to read %s"), oid_to_hex(note));
721 if (size)
722 strbuf_add(&buf, prev_buf, size);
723 if (d.buf.len && size)
724 append_separator(&buf);
725 strbuf_insert(&d.buf, 0, buf.buf, buf.len);
727 free(prev_buf);
728 strbuf_release(&buf);
731 if (d.buf.len || allow_empty) {
732 write_note_data(&d, &new_note);
733 if (add_note(t, &object, &new_note, combine_notes_overwrite))
734 BUG("combine_notes_overwrite failed");
735 logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]);
736 } else {
737 fprintf(stderr, _("Removing note for object %s\n"),
738 oid_to_hex(&object));
739 remove_note(t, object.hash);
740 logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]);
742 commit_notes(the_repository, t, logmsg);
744 free(logmsg);
745 free_note_data(&d);
746 free_notes(t);
747 return 0;
750 static int show(int argc, const char **argv, const char *prefix)
752 const char *object_ref;
753 struct notes_tree *t;
754 struct object_id object;
755 const struct object_id *note;
756 int retval;
757 struct option options[] = {
758 OPT_END()
761 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
764 if (1 < argc) {
765 error(_("too many arguments"));
766 usage_with_options(git_notes_show_usage, options);
769 object_ref = argc ? argv[0] : "HEAD";
771 if (repo_get_oid(the_repository, object_ref, &object))
772 die(_("failed to resolve '%s' as a valid ref."), object_ref);
774 t = init_notes_check("show", 0);
775 note = get_note(t, &object);
777 if (!note)
778 retval = error(_("no note found for object %s."),
779 oid_to_hex(&object));
780 else {
781 const char *show_args[3] = {"show", oid_to_hex(note), NULL};
782 retval = execv_git_cmd(show_args);
784 free_notes(t);
785 return retval;
788 static int merge_abort(struct notes_merge_options *o)
790 int ret = 0;
793 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
794 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
797 if (delete_ref(NULL, "NOTES_MERGE_PARTIAL", NULL, 0))
798 ret += error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
799 if (delete_ref(NULL, "NOTES_MERGE_REF", NULL, REF_NO_DEREF))
800 ret += error(_("failed to delete ref NOTES_MERGE_REF"));
801 if (notes_merge_abort(o))
802 ret += error(_("failed to remove 'git notes merge' worktree"));
803 return ret;
806 static int merge_commit(struct notes_merge_options *o)
808 struct strbuf msg = STRBUF_INIT;
809 struct object_id oid, parent_oid;
810 struct notes_tree *t;
811 struct commit *partial;
812 struct pretty_print_context pretty_ctx;
813 void *local_ref_to_free;
814 int ret;
817 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
818 * and target notes ref from .git/NOTES_MERGE_REF.
821 if (repo_get_oid(the_repository, "NOTES_MERGE_PARTIAL", &oid))
822 die(_("failed to read ref NOTES_MERGE_PARTIAL"));
823 else if (!(partial = lookup_commit_reference(the_repository, &oid)))
824 die(_("could not find commit from NOTES_MERGE_PARTIAL."));
825 else if (repo_parse_commit(the_repository, partial))
826 die(_("could not parse commit from NOTES_MERGE_PARTIAL."));
828 if (partial->parents)
829 oidcpy(&parent_oid, &partial->parents->item->object.oid);
830 else
831 oidclr(&parent_oid);
833 CALLOC_ARRAY(t, 1);
834 init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
836 o->local_ref = local_ref_to_free =
837 resolve_refdup("NOTES_MERGE_REF", 0, &oid, NULL);
838 if (!o->local_ref)
839 die(_("failed to resolve NOTES_MERGE_REF"));
841 if (notes_merge_commit(o, t, partial, &oid))
842 die(_("failed to finalize notes merge"));
844 /* Reuse existing commit message in reflog message */
845 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
846 repo_format_commit_message(the_repository, partial, "%s", &msg,
847 &pretty_ctx);
848 strbuf_trim(&msg);
849 strbuf_insertstr(&msg, 0, "notes: ");
850 update_ref(msg.buf, o->local_ref, &oid,
851 is_null_oid(&parent_oid) ? NULL : &parent_oid,
852 0, UPDATE_REFS_DIE_ON_ERR);
854 free_notes(t);
855 strbuf_release(&msg);
856 ret = merge_abort(o);
857 free(local_ref_to_free);
858 return ret;
861 static int git_config_get_notes_strategy(const char *key,
862 enum notes_merge_strategy *strategy)
864 char *value;
866 if (git_config_get_string(key, &value))
867 return 1;
868 if (parse_notes_merge_strategy(value, strategy))
869 git_die_config(key, _("unknown notes merge strategy %s"), value);
871 free(value);
872 return 0;
875 static int merge(int argc, const char **argv, const char *prefix)
877 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
878 struct object_id result_oid;
879 struct notes_tree *t;
880 struct notes_merge_options o;
881 int do_merge = 0, do_commit = 0, do_abort = 0;
882 int verbosity = 0, result;
883 const char *strategy = NULL;
884 struct option options[] = {
885 OPT_GROUP(N_("General options")),
886 OPT__VERBOSITY(&verbosity),
887 OPT_GROUP(N_("Merge options")),
888 OPT_STRING('s', "strategy", &strategy, N_("strategy"),
889 N_("resolve notes conflicts using the given strategy "
890 "(manual/ours/theirs/union/cat_sort_uniq)")),
891 OPT_GROUP(N_("Committing unmerged notes")),
892 OPT_SET_INT_F(0, "commit", &do_commit,
893 N_("finalize notes merge by committing unmerged notes"),
894 1, PARSE_OPT_NONEG),
895 OPT_GROUP(N_("Aborting notes merge resolution")),
896 OPT_SET_INT_F(0, "abort", &do_abort,
897 N_("abort notes merge"),
898 1, PARSE_OPT_NONEG),
899 OPT_END()
902 argc = parse_options(argc, argv, prefix, options,
903 git_notes_merge_usage, 0);
905 if (strategy || do_commit + do_abort == 0)
906 do_merge = 1;
907 if (do_merge + do_commit + do_abort != 1) {
908 error(_("cannot mix --commit, --abort or -s/--strategy"));
909 usage_with_options(git_notes_merge_usage, options);
912 if (do_merge && argc != 1) {
913 error(_("must specify a notes ref to merge"));
914 usage_with_options(git_notes_merge_usage, options);
915 } else if (!do_merge && argc) {
916 error(_("too many arguments"));
917 usage_with_options(git_notes_merge_usage, options);
920 init_notes_merge_options(the_repository, &o);
921 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
923 if (do_abort)
924 return merge_abort(&o);
925 if (do_commit)
926 return merge_commit(&o);
928 o.local_ref = default_notes_ref();
929 strbuf_addstr(&remote_ref, argv[0]);
930 expand_loose_notes_ref(&remote_ref);
931 o.remote_ref = remote_ref.buf;
933 t = init_notes_check("merge", NOTES_INIT_WRITABLE);
935 if (strategy) {
936 if (parse_notes_merge_strategy(strategy, &o.strategy)) {
937 error(_("unknown -s/--strategy: %s"), strategy);
938 usage_with_options(git_notes_merge_usage, options);
940 } else {
941 struct strbuf merge_key = STRBUF_INIT;
942 const char *short_ref = NULL;
944 if (!skip_prefix(o.local_ref, "refs/notes/", &short_ref))
945 BUG("local ref %s is outside of refs/notes/",
946 o.local_ref);
948 strbuf_addf(&merge_key, "notes.%s.mergeStrategy", short_ref);
950 if (git_config_get_notes_strategy(merge_key.buf, &o.strategy))
951 git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy);
953 strbuf_release(&merge_key);
956 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
957 remote_ref.buf, default_notes_ref());
958 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
960 result = notes_merge(&o, t, &result_oid);
962 if (result >= 0) /* Merge resulted (trivially) in result_oid */
963 /* Update default notes ref with new commit */
964 update_ref(msg.buf, default_notes_ref(), &result_oid, NULL, 0,
965 UPDATE_REFS_DIE_ON_ERR);
966 else { /* Merge has unresolved conflicts */
967 struct worktree **worktrees;
968 const struct worktree *wt;
969 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
970 update_ref(msg.buf, "NOTES_MERGE_PARTIAL", &result_oid, NULL,
971 0, UPDATE_REFS_DIE_ON_ERR);
972 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
973 worktrees = get_worktrees();
974 wt = find_shared_symref(worktrees, "NOTES_MERGE_REF",
975 default_notes_ref());
976 if (wt)
977 die(_("a notes merge into %s is already in-progress at %s"),
978 default_notes_ref(), wt->path);
979 free_worktrees(worktrees);
980 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
981 die(_("failed to store link to current notes ref (%s)"),
982 default_notes_ref());
983 fprintf(stderr, _("Automatic notes merge failed. Fix conflicts in %s "
984 "and commit the result with 'git notes merge --commit', "
985 "or abort the merge with 'git notes merge --abort'.\n"),
986 git_path(NOTES_MERGE_WORKTREE));
989 free_notes(t);
990 strbuf_release(&remote_ref);
991 strbuf_release(&msg);
992 return result < 0; /* return non-zero on conflicts */
995 #define IGNORE_MISSING 1
997 static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
999 int status;
1000 struct object_id oid;
1001 if (repo_get_oid(the_repository, name, &oid))
1002 return error(_("Failed to resolve '%s' as a valid ref."), name);
1003 status = remove_note(t, oid.hash);
1004 if (status)
1005 fprintf(stderr, _("Object %s has no note\n"), name);
1006 else
1007 fprintf(stderr, _("Removing note for object %s\n"), name);
1008 return (flag & IGNORE_MISSING) ? 0 : status;
1011 static int remove_cmd(int argc, const char **argv, const char *prefix)
1013 unsigned flag = 0;
1014 int from_stdin = 0;
1015 struct option options[] = {
1016 OPT_BIT(0, "ignore-missing", &flag,
1017 N_("attempt to remove non-existent note is not an error"),
1018 IGNORE_MISSING),
1019 OPT_BOOL(0, "stdin", &from_stdin,
1020 N_("read object names from the standard input")),
1021 OPT_END()
1023 struct notes_tree *t;
1024 int retval = 0;
1026 argc = parse_options(argc, argv, prefix, options,
1027 git_notes_remove_usage, 0);
1029 t = init_notes_check("remove", NOTES_INIT_WRITABLE);
1031 if (!argc && !from_stdin) {
1032 retval = remove_one_note(t, "HEAD", flag);
1033 } else {
1034 while (*argv) {
1035 retval |= remove_one_note(t, *argv, flag);
1036 argv++;
1039 if (from_stdin) {
1040 struct strbuf sb = STRBUF_INIT;
1041 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1042 strbuf_rtrim(&sb);
1043 retval |= remove_one_note(t, sb.buf, flag);
1045 strbuf_release(&sb);
1047 if (!retval)
1048 commit_notes(the_repository, t,
1049 "Notes removed by 'git notes remove'");
1050 free_notes(t);
1051 return retval;
1054 static int prune(int argc, const char **argv, const char *prefix)
1056 struct notes_tree *t;
1057 int show_only = 0, verbose = 0;
1058 struct option options[] = {
1059 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
1060 OPT__VERBOSE(&verbose, N_("report pruned notes")),
1061 OPT_END()
1064 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
1067 if (argc) {
1068 error(_("too many arguments"));
1069 usage_with_options(git_notes_prune_usage, options);
1072 t = init_notes_check("prune", NOTES_INIT_WRITABLE);
1074 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
1075 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
1076 if (!show_only)
1077 commit_notes(the_repository, t,
1078 "Notes removed by 'git notes prune'");
1079 free_notes(t);
1080 return 0;
1083 static int get_ref(int argc, const char **argv, const char *prefix)
1085 struct option options[] = { OPT_END() };
1086 argc = parse_options(argc, argv, prefix, options,
1087 git_notes_get_ref_usage, 0);
1089 if (argc) {
1090 error(_("too many arguments"));
1091 usage_with_options(git_notes_get_ref_usage, options);
1094 puts(default_notes_ref());
1095 return 0;
1098 int cmd_notes(int argc, const char **argv, const char *prefix)
1100 const char *override_notes_ref = NULL;
1101 parse_opt_subcommand_fn *fn = NULL;
1102 struct option options[] = {
1103 OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
1104 N_("use notes from <notes-ref>")),
1105 OPT_SUBCOMMAND("list", &fn, list),
1106 OPT_SUBCOMMAND("add", &fn, add),
1107 OPT_SUBCOMMAND("copy", &fn, copy),
1108 OPT_SUBCOMMAND("append", &fn, append_edit),
1109 OPT_SUBCOMMAND("edit", &fn, append_edit),
1110 OPT_SUBCOMMAND("show", &fn, show),
1111 OPT_SUBCOMMAND("merge", &fn, merge),
1112 OPT_SUBCOMMAND("remove", &fn, remove_cmd),
1113 OPT_SUBCOMMAND("prune", &fn, prune),
1114 OPT_SUBCOMMAND("get-ref", &fn, get_ref),
1115 OPT_END()
1118 git_config(git_default_config, NULL);
1119 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
1120 PARSE_OPT_SUBCOMMAND_OPTIONAL);
1121 if (!fn) {
1122 if (argc) {
1123 error(_("unknown subcommand: `%s'"), argv[0]);
1124 usage_with_options(git_notes_usage, options);
1126 fn = list;
1129 if (override_notes_ref) {
1130 struct strbuf sb = STRBUF_INIT;
1131 strbuf_addstr(&sb, override_notes_ref);
1132 expand_notes_ref(&sb);
1133 setenv("GIT_NOTES_REF", sb.buf, 1);
1134 strbuf_release(&sb);
1137 return !!fn(argc, argv, prefix);