The seventh batch
[git.git] / builtin / notes.c
blob72c8a51cfacf72e1f115774487311b3d4464d204
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 */
9 #define USE_THE_REPOSITORY_VARIABLE
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"
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>] [-e]"),
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>] [-e]"),
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 use_editor;
118 int stripspace;
119 char *edit_path;
120 struct strbuf buf;
121 struct note_msg **messages;
122 size_t msg_nr;
123 size_t msg_alloc;
126 static void free_note_data(struct note_data *d)
128 if (d->edit_path) {
129 unlink_or_warn(d->edit_path);
130 free(d->edit_path);
132 strbuf_release(&d->buf);
134 while (d->msg_nr--) {
135 strbuf_release(&d->messages[d->msg_nr]->buf);
136 free(d->messages[d->msg_nr]);
138 free(d->messages);
141 static int list_each_note(const struct object_id *object_oid,
142 const struct object_id *note_oid,
143 char *note_path UNUSED,
144 void *cb_data UNUSED)
146 printf("%s %s\n", oid_to_hex(note_oid), oid_to_hex(object_oid));
147 return 0;
150 static void copy_obj_to_fd(int fd, const struct object_id *oid)
152 unsigned long size;
153 enum object_type type;
154 char *buf = repo_read_object_file(the_repository, oid, &type, &size);
155 if (buf) {
156 if (size)
157 write_or_die(fd, buf, size);
158 free(buf);
162 static void write_commented_object(int fd, const struct object_id *object)
164 struct child_process show = CHILD_PROCESS_INIT;
165 struct strbuf buf = STRBUF_INIT;
166 struct strbuf cbuf = STRBUF_INIT;
168 /* Invoke "git show --stat --no-notes $object" */
169 strvec_pushl(&show.args, "show", "--stat", "--no-notes",
170 oid_to_hex(object), NULL);
171 show.no_stdin = 1;
172 show.out = -1;
173 show.err = 0;
174 show.git_cmd = 1;
175 if (start_command(&show))
176 die(_("unable to start 'show' for object '%s'"),
177 oid_to_hex(object));
179 if (strbuf_read(&buf, show.out, 0) < 0)
180 die_errno(_("could not read 'show' output"));
181 strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_str);
182 write_or_die(fd, cbuf.buf, cbuf.len);
184 strbuf_release(&cbuf);
185 strbuf_release(&buf);
187 if (finish_command(&show))
188 die(_("failed to finish 'show' for object '%s'"),
189 oid_to_hex(object));
192 static void prepare_note_data(const struct object_id *object, struct note_data *d,
193 const struct object_id *old_note)
195 if (d->use_editor || !d->msg_nr) {
196 int fd;
197 struct strbuf buf = STRBUF_INIT;
199 /* write the template message before editing: */
200 d->edit_path = git_pathdup("NOTES_EDITMSG");
201 fd = xopen(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
203 if (d->msg_nr)
204 write_or_die(fd, d->buf.buf, d->buf.len);
205 else if (old_note)
206 copy_obj_to_fd(fd, old_note);
208 strbuf_addch(&buf, '\n');
209 strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_str);
210 strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)),
211 comment_line_str);
212 strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_str);
213 write_or_die(fd, buf.buf, buf.len);
215 write_commented_object(fd, object);
217 close(fd);
218 strbuf_release(&buf);
219 strbuf_reset(&d->buf);
221 if (launch_editor(d->edit_path, &d->buf, NULL)) {
222 die(_("please supply the note contents using either -m or -F option"));
224 if (d->stripspace)
225 strbuf_stripspace(&d->buf, comment_line_str);
229 static void write_note_data(struct note_data *d, struct object_id *oid)
231 if (write_object_file(d->buf.buf, d->buf.len, OBJ_BLOB, oid)) {
232 int status = die_message(_("unable to write note object"));
234 if (d->edit_path)
235 die_message(_("the note contents have been left in %s"),
236 d->edit_path);
237 exit(status);
241 static void append_separator(struct strbuf *message)
243 size_t sep_len = 0;
245 if (!separator)
246 return;
247 else if ((sep_len = strlen(separator)) && separator[sep_len - 1] == '\n')
248 strbuf_addstr(message, separator);
249 else
250 strbuf_addf(message, "%s%s", separator, "\n");
253 static void concat_messages(struct note_data *d)
255 struct strbuf msg = STRBUF_INIT;
256 size_t i;
258 for (i = 0; i < d->msg_nr ; i++) {
259 if (d->buf.len)
260 append_separator(&d->buf);
261 strbuf_add(&msg, d->messages[i]->buf.buf, d->messages[i]->buf.len);
262 strbuf_addbuf(&d->buf, &msg);
263 if ((d->stripspace == UNSPECIFIED &&
264 d->messages[i]->stripspace == STRIPSPACE) ||
265 d->stripspace == STRIPSPACE)
266 strbuf_stripspace(&d->buf, NULL);
267 strbuf_reset(&msg);
269 strbuf_release(&msg);
272 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
274 struct note_data *d = opt->value;
275 struct note_msg *msg = xmalloc(sizeof(*msg));
277 BUG_ON_OPT_NEG(unset);
279 strbuf_init(&msg->buf, strlen(arg));
280 strbuf_addstr(&msg->buf, arg);
281 ALLOC_GROW_BY(d->messages, d->msg_nr, 1, d->msg_alloc);
282 d->messages[d->msg_nr - 1] = msg;
283 msg->stripspace = STRIPSPACE;
284 return 0;
287 static int parse_file_arg(const struct option *opt, const char *arg, int unset)
289 struct note_data *d = opt->value;
290 struct note_msg *msg = xmalloc(sizeof(*msg));
292 BUG_ON_OPT_NEG(unset);
294 strbuf_init(&msg->buf , 0);
295 if (!strcmp(arg, "-")) {
296 if (strbuf_read(&msg->buf, 0, 1024) < 0)
297 die_errno(_("cannot read '%s'"), arg);
298 } else if (strbuf_read_file(&msg->buf, arg, 1024) < 0)
299 die_errno(_("could not open or read '%s'"), arg);
301 ALLOC_GROW_BY(d->messages, d->msg_nr, 1, d->msg_alloc);
302 d->messages[d->msg_nr - 1] = msg;
303 msg->stripspace = STRIPSPACE;
304 return 0;
307 static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
309 struct note_data *d = opt->value;
310 struct note_msg *msg = xmalloc(sizeof(*msg));
311 char *value;
312 struct object_id object;
313 enum object_type type;
314 unsigned long len;
316 BUG_ON_OPT_NEG(unset);
318 strbuf_init(&msg->buf, 0);
319 if (repo_get_oid(the_repository, arg, &object))
320 die(_("failed to resolve '%s' as a valid ref."), arg);
321 if (!(value = repo_read_object_file(the_repository, &object, &type, &len)))
322 die(_("failed to read object '%s'."), arg);
323 if (type != OBJ_BLOB) {
324 strbuf_release(&msg->buf);
325 free(value);
326 free(msg);
327 die(_("cannot read note data from non-blob object '%s'."), arg);
330 strbuf_add(&msg->buf, value, len);
331 free(value);
333 msg->buf.len = len;
334 ALLOC_GROW_BY(d->messages, d->msg_nr, 1, d->msg_alloc);
335 d->messages[d->msg_nr - 1] = msg;
336 msg->stripspace = NO_STRIPSPACE;
337 return 0;
340 static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
342 struct note_data *d = opt->value;
343 BUG_ON_OPT_NEG(unset);
344 d->use_editor = 1;
345 return parse_reuse_arg(opt, arg, unset);
348 static int parse_separator_arg(const struct option *opt, const char *arg,
349 int unset)
351 if (unset)
352 *(const char **)opt->value = NULL;
353 else
354 *(const char **)opt->value = arg ? arg : "\n";
355 return 0;
358 static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
360 struct strbuf buf = STRBUF_INIT;
361 struct notes_rewrite_cfg *c = NULL;
362 struct notes_tree *t = NULL;
363 int ret = 0;
364 const char *msg = "Notes added by 'git notes copy'";
366 if (rewrite_cmd) {
367 c = init_copy_notes_for_rewrite(rewrite_cmd);
368 if (!c)
369 return 0;
370 } else {
371 init_notes(NULL, NULL, NULL, NOTES_INIT_WRITABLE);
372 t = &default_notes_tree;
375 while (strbuf_getline_lf(&buf, stdin) != EOF) {
376 struct object_id from_obj, to_obj;
377 struct strbuf **split;
378 int err;
380 split = strbuf_split(&buf, ' ');
381 if (!split[0] || !split[1])
382 die(_("malformed input line: '%s'."), buf.buf);
383 strbuf_rtrim(split[0]);
384 strbuf_rtrim(split[1]);
385 if (repo_get_oid(the_repository, split[0]->buf, &from_obj))
386 die(_("failed to resolve '%s' as a valid ref."), split[0]->buf);
387 if (repo_get_oid(the_repository, split[1]->buf, &to_obj))
388 die(_("failed to resolve '%s' as a valid ref."), split[1]->buf);
390 if (rewrite_cmd)
391 err = copy_note_for_rewrite(c, &from_obj, &to_obj);
392 else
393 err = copy_note(t, &from_obj, &to_obj, force,
394 combine_notes_overwrite);
396 if (err) {
397 error(_("failed to copy notes from '%s' to '%s'"),
398 split[0]->buf, split[1]->buf);
399 ret = 1;
402 strbuf_list_free(split);
405 if (!rewrite_cmd) {
406 commit_notes(the_repository, t, msg);
407 free_notes(t);
408 } else {
409 finish_copy_notes_for_rewrite(the_repository, c, msg);
411 strbuf_release(&buf);
412 return ret;
415 static struct notes_tree *init_notes_check(const char *subcommand,
416 int flags)
418 struct notes_tree *t;
419 const char *ref;
420 init_notes(NULL, NULL, NULL, flags);
421 t = &default_notes_tree;
423 ref = (flags & NOTES_INIT_WRITABLE) ? t->update_ref : t->ref;
424 if (!starts_with(ref, "refs/notes/"))
426 * TRANSLATORS: the first %s will be replaced by a git
427 * notes command: 'add', 'merge', 'remove', etc.
429 die(_("refusing to %s notes in %s (outside of refs/notes/)"),
430 subcommand, ref);
431 return t;
434 static int list(int argc, const char **argv, const char *prefix)
436 struct notes_tree *t;
437 struct object_id object;
438 const struct object_id *note;
439 int retval = -1;
440 struct option options[] = {
441 OPT_END()
444 if (argc)
445 argc = parse_options(argc, argv, prefix, options,
446 git_notes_list_usage, 0);
448 if (1 < argc) {
449 error(_("too many arguments"));
450 usage_with_options(git_notes_list_usage, options);
453 t = init_notes_check("list", 0);
454 if (argc) {
455 if (repo_get_oid(the_repository, argv[0], &object))
456 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
457 note = get_note(t, &object);
458 if (note) {
459 puts(oid_to_hex(note));
460 retval = 0;
461 } else
462 retval = error(_("no note found for object %s."),
463 oid_to_hex(&object));
464 } else
465 retval = for_each_note(t, 0, list_each_note, NULL);
467 free_notes(t);
468 return retval;
471 static int append_edit(int argc, const char **argv, const char *prefix);
473 static int add(int argc, const char **argv, const char *prefix)
475 int force = 0, allow_empty = 0;
476 const char *object_ref;
477 struct notes_tree *t;
478 struct object_id object, new_note;
479 const struct object_id *note;
480 struct note_data d = { .buf = STRBUF_INIT, .stripspace = UNSPECIFIED };
482 struct option options[] = {
483 OPT_CALLBACK_F('m', "message", &d, N_("message"),
484 N_("note contents as a string"), PARSE_OPT_NONEG,
485 parse_msg_arg),
486 OPT_CALLBACK_F('F', "file", &d, N_("file"),
487 N_("note contents in a file"), PARSE_OPT_NONEG,
488 parse_file_arg),
489 OPT_CALLBACK_F('c', "reedit-message", &d, N_("object"),
490 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
491 parse_reedit_arg),
492 OPT_BOOL('e', "edit", &d.use_editor,
493 N_("edit note message in editor")),
494 OPT_CALLBACK_F('C', "reuse-message", &d, N_("object"),
495 N_("reuse specified note object"), PARSE_OPT_NONEG,
496 parse_reuse_arg),
497 OPT_BOOL(0, "allow-empty", &allow_empty,
498 N_("allow storing empty note")),
499 OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE),
500 OPT_CALLBACK_F(0, "separator", &separator,
501 N_("<paragraph-break>"),
502 N_("insert <paragraph-break> between paragraphs"),
503 PARSE_OPT_OPTARG, parse_separator_arg),
504 OPT_BOOL(0, "stripspace", &d.stripspace,
505 N_("remove unnecessary whitespace")),
506 OPT_END()
509 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
510 PARSE_OPT_KEEP_ARGV0);
512 if (2 < argc) {
513 error(_("too many arguments"));
514 usage_with_options(git_notes_add_usage, options);
517 if (d.msg_nr)
518 concat_messages(&d);
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.msg_nr) {
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('e', "edit", &d.use_editor,
673 N_("edit note message in editor")),
674 OPT_BOOL(0, "allow-empty", &allow_empty,
675 N_("allow storing empty note")),
676 OPT_CALLBACK_F(0, "separator", &separator,
677 N_("<paragraph-break>"),
678 N_("insert <paragraph-break> between paragraphs"),
679 PARSE_OPT_OPTARG, parse_separator_arg),
680 OPT_BOOL(0, "stripspace", &d.stripspace,
681 N_("remove unnecessary whitespace")),
682 OPT_END()
684 int edit = !strcmp(argv[0], "edit");
686 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
687 argc = parse_options(argc, argv, prefix, options, usage,
688 PARSE_OPT_KEEP_ARGV0);
690 if (2 < argc) {
691 error(_("too many arguments"));
692 usage_with_options(usage, options);
695 if (d.msg_nr) {
696 concat_messages(&d);
697 if (edit)
698 fprintf(stderr, _("The -m/-F/-c/-C options have been "
699 "deprecated for the 'edit' subcommand.\n"
700 "Please use 'git notes add -f -m/-F/-c/-C' "
701 "instead.\n"));
704 object_ref = 1 < argc ? argv[1] : "HEAD";
706 if (repo_get_oid(the_repository, object_ref, &object))
707 die(_("failed to resolve '%s' as a valid ref."), object_ref);
709 t = init_notes_check(argv[0], NOTES_INIT_WRITABLE);
710 note = get_note(t, &object);
712 prepare_note_data(&object, &d, edit && note ? note : NULL);
714 if (note && !edit) {
715 /* Append buf to previous note contents */
716 unsigned long size;
717 enum object_type type;
718 struct strbuf buf = STRBUF_INIT;
719 char *prev_buf = repo_read_object_file(the_repository, note, &type, &size);
721 if (!prev_buf)
722 die(_("unable to read %s"), oid_to_hex(note));
723 if (size)
724 strbuf_add(&buf, prev_buf, size);
725 if (d.buf.len && size)
726 append_separator(&buf);
727 strbuf_insert(&d.buf, 0, buf.buf, buf.len);
729 free(prev_buf);
730 strbuf_release(&buf);
733 if (d.buf.len || allow_empty) {
734 write_note_data(&d, &new_note);
735 if (add_note(t, &object, &new_note, combine_notes_overwrite))
736 BUG("combine_notes_overwrite failed");
737 logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]);
738 } else {
739 fprintf(stderr, _("Removing note for object %s\n"),
740 oid_to_hex(&object));
741 remove_note(t, object.hash);
742 logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]);
744 commit_notes(the_repository, t, logmsg);
746 free(logmsg);
747 free_note_data(&d);
748 free_notes(t);
749 return 0;
752 static int show(int argc, const char **argv, const char *prefix)
754 const char *object_ref;
755 struct notes_tree *t;
756 struct object_id object;
757 const struct object_id *note;
758 int retval;
759 struct option options[] = {
760 OPT_END()
763 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
766 if (1 < argc) {
767 error(_("too many arguments"));
768 usage_with_options(git_notes_show_usage, options);
771 object_ref = argc ? argv[0] : "HEAD";
773 if (repo_get_oid(the_repository, object_ref, &object))
774 die(_("failed to resolve '%s' as a valid ref."), object_ref);
776 t = init_notes_check("show", 0);
777 note = get_note(t, &object);
779 if (!note)
780 retval = error(_("no note found for object %s."),
781 oid_to_hex(&object));
782 else {
783 const char *show_args[3] = {"show", oid_to_hex(note), NULL};
784 retval = execv_git_cmd(show_args);
786 free_notes(t);
787 return retval;
790 static int merge_abort(struct notes_merge_options *o)
792 int ret = 0;
795 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
796 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
799 if (refs_delete_ref(get_main_ref_store(the_repository), NULL, "NOTES_MERGE_PARTIAL", NULL, 0))
800 ret += error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
801 if (refs_delete_ref(get_main_ref_store(the_repository), NULL, "NOTES_MERGE_REF", NULL, REF_NO_DEREF))
802 ret += error(_("failed to delete ref NOTES_MERGE_REF"));
803 if (notes_merge_abort(o))
804 ret += error(_("failed to remove 'git notes merge' worktree"));
805 return ret;
808 static int merge_commit(struct notes_merge_options *o)
810 struct strbuf msg = STRBUF_INIT;
811 struct object_id oid, parent_oid;
812 struct notes_tree t = {0};
813 struct commit *partial;
814 struct pretty_print_context pretty_ctx;
815 void *local_ref_to_free;
816 int ret;
819 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
820 * and target notes ref from .git/NOTES_MERGE_REF.
823 if (repo_get_oid(the_repository, "NOTES_MERGE_PARTIAL", &oid))
824 die(_("failed to read ref NOTES_MERGE_PARTIAL"));
825 else if (!(partial = lookup_commit_reference(the_repository, &oid)))
826 die(_("could not find commit from NOTES_MERGE_PARTIAL."));
827 else if (repo_parse_commit(the_repository, partial))
828 die(_("could not parse commit from NOTES_MERGE_PARTIAL."));
830 if (partial->parents)
831 oidcpy(&parent_oid, &partial->parents->item->object.oid);
832 else
833 oidclr(&parent_oid, the_repository->hash_algo);
835 init_notes(&t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
837 o->local_ref = local_ref_to_free =
838 refs_resolve_refdup(get_main_ref_store(the_repository),
839 "NOTES_MERGE_REF", 0, &oid, NULL);
840 if (!o->local_ref)
841 die(_("failed to resolve NOTES_MERGE_REF"));
843 if (notes_merge_commit(o, &t, partial, &oid))
844 die(_("failed to finalize notes merge"));
846 /* Reuse existing commit message in reflog message */
847 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
848 repo_format_commit_message(the_repository, partial, "%s", &msg,
849 &pretty_ctx);
850 strbuf_trim(&msg);
851 strbuf_insertstr(&msg, 0, "notes: ");
852 refs_update_ref(get_main_ref_store(the_repository), msg.buf,
853 o->local_ref, &oid,
854 is_null_oid(&parent_oid) ? NULL : &parent_oid,
855 0, UPDATE_REFS_DIE_ON_ERR);
857 free_notes(&t);
858 strbuf_release(&msg);
859 ret = merge_abort(o);
860 free(local_ref_to_free);
861 return ret;
864 static int git_config_get_notes_strategy(const char *key,
865 enum notes_merge_strategy *strategy)
867 char *value;
869 if (git_config_get_string(key, &value))
870 return 1;
871 if (parse_notes_merge_strategy(value, strategy))
872 git_die_config(the_repository, key, _("unknown notes merge strategy %s"), value);
874 free(value);
875 return 0;
878 static int merge(int argc, const char **argv, const char *prefix)
880 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
881 struct object_id result_oid;
882 struct notes_tree *t;
883 struct notes_merge_options o;
884 int do_merge = 0, do_commit = 0, do_abort = 0;
885 int verbosity = 0, result;
886 const char *strategy = NULL;
887 struct option options[] = {
888 OPT_GROUP(N_("General options")),
889 OPT__VERBOSITY(&verbosity),
890 OPT_GROUP(N_("Merge options")),
891 OPT_STRING('s', "strategy", &strategy, N_("strategy"),
892 N_("resolve notes conflicts using the given strategy "
893 "(manual/ours/theirs/union/cat_sort_uniq)")),
894 OPT_GROUP(N_("Committing unmerged notes")),
895 OPT_SET_INT_F(0, "commit", &do_commit,
896 N_("finalize notes merge by committing unmerged notes"),
897 1, PARSE_OPT_NONEG),
898 OPT_GROUP(N_("Aborting notes merge resolution")),
899 OPT_SET_INT_F(0, "abort", &do_abort,
900 N_("abort notes merge"),
901 1, PARSE_OPT_NONEG),
902 OPT_END()
904 char *notes_ref;
906 argc = parse_options(argc, argv, prefix, options,
907 git_notes_merge_usage, 0);
909 if (strategy || do_commit + do_abort == 0)
910 do_merge = 1;
911 if (do_merge + do_commit + do_abort != 1) {
912 error(_("cannot mix --commit, --abort or -s/--strategy"));
913 usage_with_options(git_notes_merge_usage, options);
916 if (do_merge && argc != 1) {
917 error(_("must specify a notes ref to merge"));
918 usage_with_options(git_notes_merge_usage, options);
919 } else if (!do_merge && argc) {
920 error(_("too many arguments"));
921 usage_with_options(git_notes_merge_usage, options);
924 init_notes_merge_options(the_repository, &o);
925 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
927 if (do_abort)
928 return merge_abort(&o);
929 if (do_commit)
930 return merge_commit(&o);
932 notes_ref = default_notes_ref(the_repository);
933 o.local_ref = notes_ref;
934 strbuf_addstr(&remote_ref, argv[0]);
935 expand_loose_notes_ref(&remote_ref);
936 o.remote_ref = remote_ref.buf;
938 t = init_notes_check("merge", NOTES_INIT_WRITABLE);
940 if (strategy) {
941 if (parse_notes_merge_strategy(strategy, &o.strategy)) {
942 error(_("unknown -s/--strategy: %s"), strategy);
943 usage_with_options(git_notes_merge_usage, options);
945 } else {
946 struct strbuf merge_key = STRBUF_INIT;
947 const char *short_ref = NULL;
949 if (!skip_prefix(o.local_ref, "refs/notes/", &short_ref))
950 BUG("local ref %s is outside of refs/notes/",
951 o.local_ref);
953 strbuf_addf(&merge_key, "notes.%s.mergeStrategy", short_ref);
955 if (git_config_get_notes_strategy(merge_key.buf, &o.strategy))
956 git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy);
958 strbuf_release(&merge_key);
961 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
962 remote_ref.buf, notes_ref);
963 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
965 result = notes_merge(&o, t, &result_oid);
967 if (result >= 0) /* Merge resulted (trivially) in result_oid */
968 /* Update default notes ref with new commit */
969 refs_update_ref(get_main_ref_store(the_repository), msg.buf,
970 notes_ref, &result_oid, NULL, 0,
971 UPDATE_REFS_DIE_ON_ERR);
972 else { /* Merge has unresolved conflicts */
973 struct worktree **worktrees;
974 const struct worktree *wt;
975 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
976 refs_update_ref(get_main_ref_store(the_repository), msg.buf,
977 "NOTES_MERGE_PARTIAL", &result_oid, NULL,
978 0, UPDATE_REFS_DIE_ON_ERR);
979 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
980 worktrees = get_worktrees();
981 wt = find_shared_symref(worktrees, "NOTES_MERGE_REF",
982 notes_ref);
983 if (wt)
984 die(_("a notes merge into %s is already in-progress at %s"),
985 notes_ref, wt->path);
986 free_worktrees(worktrees);
987 if (refs_update_symref(get_main_ref_store(the_repository), "NOTES_MERGE_REF", notes_ref, NULL))
988 die(_("failed to store link to current notes ref (%s)"),
989 notes_ref);
990 fprintf(stderr, _("Automatic notes merge failed. Fix conflicts in %s "
991 "and commit the result with 'git notes merge --commit', "
992 "or abort the merge with 'git notes merge --abort'.\n"),
993 git_path(NOTES_MERGE_WORKTREE));
996 free_notes(t);
997 free(notes_ref);
998 strbuf_release(&remote_ref);
999 strbuf_release(&msg);
1000 return result < 0; /* return non-zero on conflicts */
1003 #define IGNORE_MISSING 1
1005 static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
1007 int status;
1008 struct object_id oid;
1009 if (repo_get_oid(the_repository, name, &oid))
1010 return error(_("Failed to resolve '%s' as a valid ref."), name);
1011 status = remove_note(t, oid.hash);
1012 if (status)
1013 fprintf(stderr, _("Object %s has no note\n"), name);
1014 else
1015 fprintf(stderr, _("Removing note for object %s\n"), name);
1016 return (flag & IGNORE_MISSING) ? 0 : status;
1019 static int remove_cmd(int argc, const char **argv, const char *prefix)
1021 unsigned flag = 0;
1022 int from_stdin = 0;
1023 struct option options[] = {
1024 OPT_BIT(0, "ignore-missing", &flag,
1025 N_("attempt to remove non-existent note is not an error"),
1026 IGNORE_MISSING),
1027 OPT_BOOL(0, "stdin", &from_stdin,
1028 N_("read object names from the standard input")),
1029 OPT_END()
1031 struct notes_tree *t;
1032 int retval = 0;
1034 argc = parse_options(argc, argv, prefix, options,
1035 git_notes_remove_usage, 0);
1037 t = init_notes_check("remove", NOTES_INIT_WRITABLE);
1039 if (!argc && !from_stdin) {
1040 retval = remove_one_note(t, "HEAD", flag);
1041 } else {
1042 while (*argv) {
1043 retval |= remove_one_note(t, *argv, flag);
1044 argv++;
1047 if (from_stdin) {
1048 struct strbuf sb = STRBUF_INIT;
1049 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1050 strbuf_rtrim(&sb);
1051 retval |= remove_one_note(t, sb.buf, flag);
1053 strbuf_release(&sb);
1055 if (!retval)
1056 commit_notes(the_repository, t,
1057 "Notes removed by 'git notes remove'");
1058 free_notes(t);
1059 return retval;
1062 static int prune(int argc, const char **argv, const char *prefix)
1064 struct notes_tree *t;
1065 int show_only = 0, verbose = 0;
1066 struct option options[] = {
1067 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
1068 OPT__VERBOSE(&verbose, N_("report pruned notes")),
1069 OPT_END()
1072 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
1075 if (argc) {
1076 error(_("too many arguments"));
1077 usage_with_options(git_notes_prune_usage, options);
1080 t = init_notes_check("prune", NOTES_INIT_WRITABLE);
1082 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
1083 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
1084 if (!show_only)
1085 commit_notes(the_repository, t,
1086 "Notes removed by 'git notes prune'");
1087 free_notes(t);
1088 return 0;
1091 static int get_ref(int argc, const char **argv, const char *prefix)
1093 struct option options[] = { OPT_END() };
1094 char *notes_ref;
1095 argc = parse_options(argc, argv, prefix, options,
1096 git_notes_get_ref_usage, 0);
1098 if (argc) {
1099 error(_("too many arguments"));
1100 usage_with_options(git_notes_get_ref_usage, options);
1103 notes_ref = default_notes_ref(the_repository);
1104 puts(notes_ref);
1105 free(notes_ref);
1106 return 0;
1109 int cmd_notes(int argc,
1110 const char **argv,
1111 const char *prefix,
1112 struct repository *repo UNUSED)
1114 const char *override_notes_ref = NULL;
1115 parse_opt_subcommand_fn *fn = NULL;
1116 struct option options[] = {
1117 OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
1118 N_("use notes from <notes-ref>")),
1119 OPT_SUBCOMMAND("list", &fn, list),
1120 OPT_SUBCOMMAND("add", &fn, add),
1121 OPT_SUBCOMMAND("copy", &fn, copy),
1122 OPT_SUBCOMMAND("append", &fn, append_edit),
1123 OPT_SUBCOMMAND("edit", &fn, append_edit),
1124 OPT_SUBCOMMAND("show", &fn, show),
1125 OPT_SUBCOMMAND("merge", &fn, merge),
1126 OPT_SUBCOMMAND("remove", &fn, remove_cmd),
1127 OPT_SUBCOMMAND("prune", &fn, prune),
1128 OPT_SUBCOMMAND("get-ref", &fn, get_ref),
1129 OPT_END()
1132 git_config(git_default_config, NULL);
1133 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
1134 PARSE_OPT_SUBCOMMAND_OPTIONAL);
1135 if (!fn) {
1136 if (argc) {
1137 error(_("unknown subcommand: `%s'"), argv[0]);
1138 usage_with_options(git_notes_usage, options);
1140 fn = list;
1143 if (override_notes_ref) {
1144 struct strbuf sb = STRBUF_INIT;
1145 strbuf_addstr(&sb, override_notes_ref);
1146 expand_notes_ref(&sb);
1147 setenv("GIT_NOTES_REF", sb.buf, 1);
1148 strbuf_release(&sb);
1151 return !!fn(argc, argv, prefix);