grep: use grep_or_expr() in compile_pattern_or()
[git/debian.git] / builtin / notes.c
blob118db9e455d3e61403d54931bd41ad0332693204
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 "notes.h"
14 #include "object-store.h"
15 #include "repository.h"
16 #include "blob.h"
17 #include "pretty.h"
18 #include "refs.h"
19 #include "exec-cmd.h"
20 #include "run-command.h"
21 #include "parse-options.h"
22 #include "string-list.h"
23 #include "notes-merge.h"
24 #include "notes-utils.h"
25 #include "worktree.h"
27 static const char * const git_notes_usage[] = {
28 N_("git notes [--ref <notes-ref>] [list [<object>]]"),
29 N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
30 N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"),
31 N_("git notes [--ref <notes-ref>] append [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
32 N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
33 N_("git notes [--ref <notes-ref>] show [<object>]"),
34 N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"),
35 N_("git notes merge --commit [-v | -q]"),
36 N_("git notes merge --abort [-v | -q]"),
37 N_("git notes [--ref <notes-ref>] remove [<object>...]"),
38 N_("git notes [--ref <notes-ref>] prune [-n] [-v]"),
39 N_("git notes [--ref <notes-ref>] get-ref"),
40 NULL
43 static const char * const git_notes_list_usage[] = {
44 N_("git notes [list [<object>]]"),
45 NULL
48 static const char * const git_notes_add_usage[] = {
49 N_("git notes add [<options>] [<object>]"),
50 NULL
53 static const char * const git_notes_copy_usage[] = {
54 N_("git notes copy [<options>] <from-object> <to-object>"),
55 N_("git notes copy --stdin [<from-object> <to-object>]..."),
56 NULL
59 static const char * const git_notes_append_usage[] = {
60 N_("git notes append [<options>] [<object>]"),
61 NULL
64 static const char * const git_notes_edit_usage[] = {
65 N_("git notes edit [<object>]"),
66 NULL
69 static const char * const git_notes_show_usage[] = {
70 N_("git notes show [<object>]"),
71 NULL
74 static const char * const git_notes_merge_usage[] = {
75 N_("git notes merge [<options>] <notes-ref>"),
76 N_("git notes merge --commit [<options>]"),
77 N_("git notes merge --abort [<options>]"),
78 NULL
81 static const char * const git_notes_remove_usage[] = {
82 N_("git notes remove [<object>]"),
83 NULL
86 static const char * const git_notes_prune_usage[] = {
87 N_("git notes prune [<options>]"),
88 NULL
91 static const char * const git_notes_get_ref_usage[] = {
92 N_("git notes get-ref"),
93 NULL
96 static const char note_template[] =
97 N_("Write/edit the notes for the following object:");
99 struct note_data {
100 int given;
101 int use_editor;
102 char *edit_path;
103 struct strbuf buf;
106 static void free_note_data(struct note_data *d)
108 if (d->edit_path) {
109 unlink_or_warn(d->edit_path);
110 free(d->edit_path);
112 strbuf_release(&d->buf);
115 static int list_each_note(const struct object_id *object_oid,
116 const struct object_id *note_oid, char *note_path,
117 void *cb_data)
119 printf("%s %s\n", oid_to_hex(note_oid), oid_to_hex(object_oid));
120 return 0;
123 static void copy_obj_to_fd(int fd, const struct object_id *oid)
125 unsigned long size;
126 enum object_type type;
127 char *buf = read_object_file(oid, &type, &size);
128 if (buf) {
129 if (size)
130 write_or_die(fd, buf, size);
131 free(buf);
135 static void write_commented_object(int fd, const struct object_id *object)
137 struct child_process show = CHILD_PROCESS_INIT;
138 struct strbuf buf = STRBUF_INIT;
139 struct strbuf cbuf = STRBUF_INIT;
141 /* Invoke "git show --stat --no-notes $object" */
142 strvec_pushl(&show.args, "show", "--stat", "--no-notes",
143 oid_to_hex(object), NULL);
144 show.no_stdin = 1;
145 show.out = -1;
146 show.err = 0;
147 show.git_cmd = 1;
148 if (start_command(&show))
149 die(_("unable to start 'show' for object '%s'"),
150 oid_to_hex(object));
152 if (strbuf_read(&buf, show.out, 0) < 0)
153 die_errno(_("could not read 'show' output"));
154 strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
155 write_or_die(fd, cbuf.buf, cbuf.len);
157 strbuf_release(&cbuf);
158 strbuf_release(&buf);
160 if (finish_command(&show))
161 die(_("failed to finish 'show' for object '%s'"),
162 oid_to_hex(object));
165 static void prepare_note_data(const struct object_id *object, struct note_data *d,
166 const struct object_id *old_note)
168 if (d->use_editor || !d->given) {
169 int fd;
170 struct strbuf buf = STRBUF_INIT;
172 /* write the template message before editing: */
173 d->edit_path = git_pathdup("NOTES_EDITMSG");
174 fd = xopen(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
176 if (d->given)
177 write_or_die(fd, d->buf.buf, d->buf.len);
178 else if (old_note)
179 copy_obj_to_fd(fd, old_note);
181 strbuf_addch(&buf, '\n');
182 strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
183 strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)));
184 strbuf_addch(&buf, '\n');
185 write_or_die(fd, buf.buf, buf.len);
187 write_commented_object(fd, object);
189 close(fd);
190 strbuf_release(&buf);
191 strbuf_reset(&d->buf);
193 if (launch_editor(d->edit_path, &d->buf, NULL)) {
194 die(_("please supply the note contents using either -m or -F option"));
196 strbuf_stripspace(&d->buf, 1);
200 static void write_note_data(struct note_data *d, struct object_id *oid)
202 if (write_object_file(d->buf.buf, d->buf.len, blob_type, oid)) {
203 error(_("unable to write note object"));
204 if (d->edit_path)
205 error(_("the note contents have been left in %s"),
206 d->edit_path);
207 exit(128);
211 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
213 struct note_data *d = opt->value;
215 BUG_ON_OPT_NEG(unset);
217 strbuf_grow(&d->buf, strlen(arg) + 2);
218 if (d->buf.len)
219 strbuf_addch(&d->buf, '\n');
220 strbuf_addstr(&d->buf, arg);
221 strbuf_stripspace(&d->buf, 0);
223 d->given = 1;
224 return 0;
227 static int parse_file_arg(const struct option *opt, const char *arg, int unset)
229 struct note_data *d = opt->value;
231 BUG_ON_OPT_NEG(unset);
233 if (d->buf.len)
234 strbuf_addch(&d->buf, '\n');
235 if (!strcmp(arg, "-")) {
236 if (strbuf_read(&d->buf, 0, 1024) < 0)
237 die_errno(_("cannot read '%s'"), arg);
238 } else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
239 die_errno(_("could not open or read '%s'"), arg);
240 strbuf_stripspace(&d->buf, 0);
242 d->given = 1;
243 return 0;
246 static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
248 struct note_data *d = opt->value;
249 char *buf;
250 struct object_id object;
251 enum object_type type;
252 unsigned long len;
254 BUG_ON_OPT_NEG(unset);
256 if (d->buf.len)
257 strbuf_addch(&d->buf, '\n');
259 if (get_oid(arg, &object))
260 die(_("failed to resolve '%s' as a valid ref."), arg);
261 if (!(buf = read_object_file(&object, &type, &len)))
262 die(_("failed to read object '%s'."), arg);
263 if (type != OBJ_BLOB) {
264 free(buf);
265 die(_("cannot read note data from non-blob object '%s'."), arg);
267 strbuf_add(&d->buf, buf, len);
268 free(buf);
270 d->given = 1;
271 return 0;
274 static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
276 struct note_data *d = opt->value;
277 BUG_ON_OPT_NEG(unset);
278 d->use_editor = 1;
279 return parse_reuse_arg(opt, arg, unset);
282 static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
284 struct strbuf buf = STRBUF_INIT;
285 struct notes_rewrite_cfg *c = NULL;
286 struct notes_tree *t = NULL;
287 int ret = 0;
288 const char *msg = "Notes added by 'git notes copy'";
290 if (rewrite_cmd) {
291 c = init_copy_notes_for_rewrite(rewrite_cmd);
292 if (!c)
293 return 0;
294 } else {
295 init_notes(NULL, NULL, NULL, NOTES_INIT_WRITABLE);
296 t = &default_notes_tree;
299 while (strbuf_getline_lf(&buf, stdin) != EOF) {
300 struct object_id from_obj, to_obj;
301 struct strbuf **split;
302 int err;
304 split = strbuf_split(&buf, ' ');
305 if (!split[0] || !split[1])
306 die(_("malformed input line: '%s'."), buf.buf);
307 strbuf_rtrim(split[0]);
308 strbuf_rtrim(split[1]);
309 if (get_oid(split[0]->buf, &from_obj))
310 die(_("failed to resolve '%s' as a valid ref."), split[0]->buf);
311 if (get_oid(split[1]->buf, &to_obj))
312 die(_("failed to resolve '%s' as a valid ref."), split[1]->buf);
314 if (rewrite_cmd)
315 err = copy_note_for_rewrite(c, &from_obj, &to_obj);
316 else
317 err = copy_note(t, &from_obj, &to_obj, force,
318 combine_notes_overwrite);
320 if (err) {
321 error(_("failed to copy notes from '%s' to '%s'"),
322 split[0]->buf, split[1]->buf);
323 ret = 1;
326 strbuf_list_free(split);
329 if (!rewrite_cmd) {
330 commit_notes(the_repository, t, msg);
331 free_notes(t);
332 } else {
333 finish_copy_notes_for_rewrite(the_repository, c, msg);
335 strbuf_release(&buf);
336 return ret;
339 static struct notes_tree *init_notes_check(const char *subcommand,
340 int flags)
342 struct notes_tree *t;
343 const char *ref;
344 init_notes(NULL, NULL, NULL, flags);
345 t = &default_notes_tree;
347 ref = (flags & NOTES_INIT_WRITABLE) ? t->update_ref : t->ref;
348 if (!starts_with(ref, "refs/notes/"))
350 * TRANSLATORS: the first %s will be replaced by a git
351 * notes command: 'add', 'merge', 'remove', etc.
353 die(_("refusing to %s notes in %s (outside of refs/notes/)"),
354 subcommand, ref);
355 return t;
358 static int list(int argc, const char **argv, const char *prefix)
360 struct notes_tree *t;
361 struct object_id object;
362 const struct object_id *note;
363 int retval = -1;
364 struct option options[] = {
365 OPT_END()
368 if (argc)
369 argc = parse_options(argc, argv, prefix, options,
370 git_notes_list_usage, 0);
372 if (1 < argc) {
373 error(_("too many arguments"));
374 usage_with_options(git_notes_list_usage, options);
377 t = init_notes_check("list", 0);
378 if (argc) {
379 if (get_oid(argv[0], &object))
380 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
381 note = get_note(t, &object);
382 if (note) {
383 puts(oid_to_hex(note));
384 retval = 0;
385 } else
386 retval = error(_("no note found for object %s."),
387 oid_to_hex(&object));
388 } else
389 retval = for_each_note(t, 0, list_each_note, NULL);
391 free_notes(t);
392 return retval;
395 static int append_edit(int argc, const char **argv, const char *prefix);
397 static int add(int argc, const char **argv, const char *prefix)
399 int force = 0, allow_empty = 0;
400 const char *object_ref;
401 struct notes_tree *t;
402 struct object_id object, new_note;
403 const struct object_id *note;
404 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
405 struct option options[] = {
406 OPT_CALLBACK_F('m', "message", &d, N_("message"),
407 N_("note contents as a string"), PARSE_OPT_NONEG,
408 parse_msg_arg),
409 OPT_CALLBACK_F('F', "file", &d, N_("file"),
410 N_("note contents in a file"), PARSE_OPT_NONEG,
411 parse_file_arg),
412 OPT_CALLBACK_F('c', "reedit-message", &d, N_("object"),
413 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
414 parse_reedit_arg),
415 OPT_CALLBACK_F('C', "reuse-message", &d, N_("object"),
416 N_("reuse specified note object"), PARSE_OPT_NONEG,
417 parse_reuse_arg),
418 OPT_BOOL(0, "allow-empty", &allow_empty,
419 N_("allow storing empty note")),
420 OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE),
421 OPT_END()
424 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
425 PARSE_OPT_KEEP_ARGV0);
427 if (2 < argc) {
428 error(_("too many arguments"));
429 usage_with_options(git_notes_add_usage, options);
432 object_ref = argc > 1 ? argv[1] : "HEAD";
434 if (get_oid(object_ref, &object))
435 die(_("failed to resolve '%s' as a valid ref."), object_ref);
437 t = init_notes_check("add", NOTES_INIT_WRITABLE);
438 note = get_note(t, &object);
440 if (note) {
441 if (!force) {
442 free_notes(t);
443 if (d.given) {
444 free_note_data(&d);
445 return error(_("Cannot add notes. "
446 "Found existing notes for object %s. "
447 "Use '-f' to overwrite existing notes"),
448 oid_to_hex(&object));
451 * Redirect to "edit" subcommand.
453 * We only end up here if none of -m/-F/-c/-C or -f are
454 * given. The original args are therefore still in
455 * argv[0-1].
457 argv[0] = "edit";
458 return append_edit(argc, argv, prefix);
460 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
461 oid_to_hex(&object));
464 prepare_note_data(&object, &d, note);
465 if (d.buf.len || allow_empty) {
466 write_note_data(&d, &new_note);
467 if (add_note(t, &object, &new_note, combine_notes_overwrite))
468 BUG("combine_notes_overwrite failed");
469 commit_notes(the_repository, t,
470 "Notes added by 'git notes add'");
471 } else {
472 fprintf(stderr, _("Removing note for object %s\n"),
473 oid_to_hex(&object));
474 remove_note(t, object.hash);
475 commit_notes(the_repository, t,
476 "Notes removed by 'git notes add'");
479 free_note_data(&d);
480 free_notes(t);
481 return 0;
484 static int copy(int argc, const char **argv, const char *prefix)
486 int retval = 0, force = 0, from_stdin = 0;
487 const struct object_id *from_note, *note;
488 const char *object_ref;
489 struct object_id object, from_obj;
490 struct notes_tree *t;
491 const char *rewrite_cmd = NULL;
492 struct option options[] = {
493 OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE),
494 OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")),
495 OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"),
496 N_("load rewriting config for <command> (implies "
497 "--stdin)")),
498 OPT_END()
501 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
504 if (from_stdin || rewrite_cmd) {
505 if (argc) {
506 error(_("too many arguments"));
507 usage_with_options(git_notes_copy_usage, options);
508 } else {
509 return notes_copy_from_stdin(force, rewrite_cmd);
513 if (argc < 1) {
514 error(_("too few arguments"));
515 usage_with_options(git_notes_copy_usage, options);
517 if (2 < argc) {
518 error(_("too many arguments"));
519 usage_with_options(git_notes_copy_usage, options);
522 if (get_oid(argv[0], &from_obj))
523 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
525 object_ref = 1 < argc ? argv[1] : "HEAD";
527 if (get_oid(object_ref, &object))
528 die(_("failed to resolve '%s' as a valid ref."), object_ref);
530 t = init_notes_check("copy", NOTES_INIT_WRITABLE);
531 note = get_note(t, &object);
533 if (note) {
534 if (!force) {
535 retval = error(_("Cannot copy notes. Found existing "
536 "notes for object %s. Use '-f' to "
537 "overwrite existing notes"),
538 oid_to_hex(&object));
539 goto out;
541 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
542 oid_to_hex(&object));
545 from_note = get_note(t, &from_obj);
546 if (!from_note) {
547 retval = error(_("missing notes on source object %s. Cannot "
548 "copy."), oid_to_hex(&from_obj));
549 goto out;
552 if (add_note(t, &object, from_note, combine_notes_overwrite))
553 BUG("combine_notes_overwrite failed");
554 commit_notes(the_repository, t,
555 "Notes added by 'git notes copy'");
556 out:
557 free_notes(t);
558 return retval;
561 static int append_edit(int argc, const char **argv, const char *prefix)
563 int allow_empty = 0;
564 const char *object_ref;
565 struct notes_tree *t;
566 struct object_id object, new_note;
567 const struct object_id *note;
568 char *logmsg;
569 const char * const *usage;
570 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
571 struct option options[] = {
572 OPT_CALLBACK_F('m', "message", &d, N_("message"),
573 N_("note contents as a string"), PARSE_OPT_NONEG,
574 parse_msg_arg),
575 OPT_CALLBACK_F('F', "file", &d, N_("file"),
576 N_("note contents in a file"), PARSE_OPT_NONEG,
577 parse_file_arg),
578 OPT_CALLBACK_F('c', "reedit-message", &d, N_("object"),
579 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
580 parse_reedit_arg),
581 OPT_CALLBACK_F('C', "reuse-message", &d, N_("object"),
582 N_("reuse specified note object"), PARSE_OPT_NONEG,
583 parse_reuse_arg),
584 OPT_BOOL(0, "allow-empty", &allow_empty,
585 N_("allow storing empty note")),
586 OPT_END()
588 int edit = !strcmp(argv[0], "edit");
590 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
591 argc = parse_options(argc, argv, prefix, options, usage,
592 PARSE_OPT_KEEP_ARGV0);
594 if (2 < argc) {
595 error(_("too many arguments"));
596 usage_with_options(usage, options);
599 if (d.given && edit)
600 fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
601 "for the 'edit' subcommand.\n"
602 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
604 object_ref = 1 < argc ? argv[1] : "HEAD";
606 if (get_oid(object_ref, &object))
607 die(_("failed to resolve '%s' as a valid ref."), object_ref);
609 t = init_notes_check(argv[0], NOTES_INIT_WRITABLE);
610 note = get_note(t, &object);
612 prepare_note_data(&object, &d, edit && note ? note : NULL);
614 if (note && !edit) {
615 /* Append buf to previous note contents */
616 unsigned long size;
617 enum object_type type;
618 char *prev_buf = read_object_file(note, &type, &size);
620 strbuf_grow(&d.buf, size + 1);
621 if (d.buf.len && prev_buf && size)
622 strbuf_insertstr(&d.buf, 0, "\n");
623 if (prev_buf && size)
624 strbuf_insert(&d.buf, 0, prev_buf, size);
625 free(prev_buf);
628 if (d.buf.len || allow_empty) {
629 write_note_data(&d, &new_note);
630 if (add_note(t, &object, &new_note, combine_notes_overwrite))
631 BUG("combine_notes_overwrite failed");
632 logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]);
633 } else {
634 fprintf(stderr, _("Removing note for object %s\n"),
635 oid_to_hex(&object));
636 remove_note(t, object.hash);
637 logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]);
639 commit_notes(the_repository, t, logmsg);
641 free(logmsg);
642 free_note_data(&d);
643 free_notes(t);
644 return 0;
647 static int show(int argc, const char **argv, const char *prefix)
649 const char *object_ref;
650 struct notes_tree *t;
651 struct object_id object;
652 const struct object_id *note;
653 int retval;
654 struct option options[] = {
655 OPT_END()
658 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
661 if (1 < argc) {
662 error(_("too many arguments"));
663 usage_with_options(git_notes_show_usage, options);
666 object_ref = argc ? argv[0] : "HEAD";
668 if (get_oid(object_ref, &object))
669 die(_("failed to resolve '%s' as a valid ref."), object_ref);
671 t = init_notes_check("show", 0);
672 note = get_note(t, &object);
674 if (!note)
675 retval = error(_("no note found for object %s."),
676 oid_to_hex(&object));
677 else {
678 const char *show_args[3] = {"show", oid_to_hex(note), NULL};
679 retval = execv_git_cmd(show_args);
681 free_notes(t);
682 return retval;
685 static int merge_abort(struct notes_merge_options *o)
687 int ret = 0;
690 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
691 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
694 if (delete_ref(NULL, "NOTES_MERGE_PARTIAL", NULL, 0))
695 ret += error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
696 if (delete_ref(NULL, "NOTES_MERGE_REF", NULL, REF_NO_DEREF))
697 ret += error(_("failed to delete ref NOTES_MERGE_REF"));
698 if (notes_merge_abort(o))
699 ret += error(_("failed to remove 'git notes merge' worktree"));
700 return ret;
703 static int merge_commit(struct notes_merge_options *o)
705 struct strbuf msg = STRBUF_INIT;
706 struct object_id oid, parent_oid;
707 struct notes_tree *t;
708 struct commit *partial;
709 struct pretty_print_context pretty_ctx;
710 void *local_ref_to_free;
711 int ret;
714 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
715 * and target notes ref from .git/NOTES_MERGE_REF.
718 if (get_oid("NOTES_MERGE_PARTIAL", &oid))
719 die(_("failed to read ref NOTES_MERGE_PARTIAL"));
720 else if (!(partial = lookup_commit_reference(the_repository, &oid)))
721 die(_("could not find commit from NOTES_MERGE_PARTIAL."));
722 else if (parse_commit(partial))
723 die(_("could not parse commit from NOTES_MERGE_PARTIAL."));
725 if (partial->parents)
726 oidcpy(&parent_oid, &partial->parents->item->object.oid);
727 else
728 oidclr(&parent_oid);
730 CALLOC_ARRAY(t, 1);
731 init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
733 o->local_ref = local_ref_to_free =
734 resolve_refdup("NOTES_MERGE_REF", 0, &oid, NULL);
735 if (!o->local_ref)
736 die(_("failed to resolve NOTES_MERGE_REF"));
738 if (notes_merge_commit(o, t, partial, &oid))
739 die(_("failed to finalize notes merge"));
741 /* Reuse existing commit message in reflog message */
742 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
743 format_commit_message(partial, "%s", &msg, &pretty_ctx);
744 strbuf_trim(&msg);
745 strbuf_insertstr(&msg, 0, "notes: ");
746 update_ref(msg.buf, o->local_ref, &oid,
747 is_null_oid(&parent_oid) ? NULL : &parent_oid,
748 0, UPDATE_REFS_DIE_ON_ERR);
750 free_notes(t);
751 strbuf_release(&msg);
752 ret = merge_abort(o);
753 free(local_ref_to_free);
754 return ret;
757 static int git_config_get_notes_strategy(const char *key,
758 enum notes_merge_strategy *strategy)
760 char *value;
762 if (git_config_get_string(key, &value))
763 return 1;
764 if (parse_notes_merge_strategy(value, strategy))
765 git_die_config(key, _("unknown notes merge strategy %s"), value);
767 free(value);
768 return 0;
771 static int merge(int argc, const char **argv, const char *prefix)
773 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
774 struct object_id result_oid;
775 struct notes_tree *t;
776 struct notes_merge_options o;
777 int do_merge = 0, do_commit = 0, do_abort = 0;
778 int verbosity = 0, result;
779 const char *strategy = NULL;
780 struct option options[] = {
781 OPT_GROUP(N_("General options")),
782 OPT__VERBOSITY(&verbosity),
783 OPT_GROUP(N_("Merge options")),
784 OPT_STRING('s', "strategy", &strategy, N_("strategy"),
785 N_("resolve notes conflicts using the given strategy "
786 "(manual/ours/theirs/union/cat_sort_uniq)")),
787 OPT_GROUP(N_("Committing unmerged notes")),
788 OPT_SET_INT_F(0, "commit", &do_commit,
789 N_("finalize notes merge by committing unmerged notes"),
790 1, PARSE_OPT_NONEG),
791 OPT_GROUP(N_("Aborting notes merge resolution")),
792 OPT_SET_INT_F(0, "abort", &do_abort,
793 N_("abort notes merge"),
794 1, PARSE_OPT_NONEG),
795 OPT_END()
798 argc = parse_options(argc, argv, prefix, options,
799 git_notes_merge_usage, 0);
801 if (strategy || do_commit + do_abort == 0)
802 do_merge = 1;
803 if (do_merge + do_commit + do_abort != 1) {
804 error(_("cannot mix --commit, --abort or -s/--strategy"));
805 usage_with_options(git_notes_merge_usage, options);
808 if (do_merge && argc != 1) {
809 error(_("must specify a notes ref to merge"));
810 usage_with_options(git_notes_merge_usage, options);
811 } else if (!do_merge && argc) {
812 error(_("too many arguments"));
813 usage_with_options(git_notes_merge_usage, options);
816 init_notes_merge_options(the_repository, &o);
817 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
819 if (do_abort)
820 return merge_abort(&o);
821 if (do_commit)
822 return merge_commit(&o);
824 o.local_ref = default_notes_ref();
825 strbuf_addstr(&remote_ref, argv[0]);
826 expand_loose_notes_ref(&remote_ref);
827 o.remote_ref = remote_ref.buf;
829 t = init_notes_check("merge", NOTES_INIT_WRITABLE);
831 if (strategy) {
832 if (parse_notes_merge_strategy(strategy, &o.strategy)) {
833 error(_("unknown -s/--strategy: %s"), strategy);
834 usage_with_options(git_notes_merge_usage, options);
836 } else {
837 struct strbuf merge_key = STRBUF_INIT;
838 const char *short_ref = NULL;
840 if (!skip_prefix(o.local_ref, "refs/notes/", &short_ref))
841 BUG("local ref %s is outside of refs/notes/",
842 o.local_ref);
844 strbuf_addf(&merge_key, "notes.%s.mergeStrategy", short_ref);
846 if (git_config_get_notes_strategy(merge_key.buf, &o.strategy))
847 git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy);
849 strbuf_release(&merge_key);
852 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
853 remote_ref.buf, default_notes_ref());
854 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
856 result = notes_merge(&o, t, &result_oid);
858 if (result >= 0) /* Merge resulted (trivially) in result_oid */
859 /* Update default notes ref with new commit */
860 update_ref(msg.buf, default_notes_ref(), &result_oid, NULL, 0,
861 UPDATE_REFS_DIE_ON_ERR);
862 else { /* Merge has unresolved conflicts */
863 struct worktree **worktrees;
864 const struct worktree *wt;
865 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
866 update_ref(msg.buf, "NOTES_MERGE_PARTIAL", &result_oid, NULL,
867 0, UPDATE_REFS_DIE_ON_ERR);
868 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
869 worktrees = get_worktrees();
870 wt = find_shared_symref(worktrees, "NOTES_MERGE_REF",
871 default_notes_ref());
872 if (wt)
873 die(_("a notes merge into %s is already in-progress at %s"),
874 default_notes_ref(), wt->path);
875 free_worktrees(worktrees);
876 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
877 die(_("failed to store link to current notes ref (%s)"),
878 default_notes_ref());
879 fprintf(stderr, _("Automatic notes merge failed. Fix conflicts in %s "
880 "and commit the result with 'git notes merge --commit', "
881 "or abort the merge with 'git notes merge --abort'.\n"),
882 git_path(NOTES_MERGE_WORKTREE));
885 free_notes(t);
886 strbuf_release(&remote_ref);
887 strbuf_release(&msg);
888 return result < 0; /* return non-zero on conflicts */
891 #define IGNORE_MISSING 1
893 static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
895 int status;
896 struct object_id oid;
897 if (get_oid(name, &oid))
898 return error(_("Failed to resolve '%s' as a valid ref."), name);
899 status = remove_note(t, oid.hash);
900 if (status)
901 fprintf(stderr, _("Object %s has no note\n"), name);
902 else
903 fprintf(stderr, _("Removing note for object %s\n"), name);
904 return (flag & IGNORE_MISSING) ? 0 : status;
907 static int remove_cmd(int argc, const char **argv, const char *prefix)
909 unsigned flag = 0;
910 int from_stdin = 0;
911 struct option options[] = {
912 OPT_BIT(0, "ignore-missing", &flag,
913 N_("attempt to remove non-existent note is not an error"),
914 IGNORE_MISSING),
915 OPT_BOOL(0, "stdin", &from_stdin,
916 N_("read object names from the standard input")),
917 OPT_END()
919 struct notes_tree *t;
920 int retval = 0;
922 argc = parse_options(argc, argv, prefix, options,
923 git_notes_remove_usage, 0);
925 t = init_notes_check("remove", NOTES_INIT_WRITABLE);
927 if (!argc && !from_stdin) {
928 retval = remove_one_note(t, "HEAD", flag);
929 } else {
930 while (*argv) {
931 retval |= remove_one_note(t, *argv, flag);
932 argv++;
935 if (from_stdin) {
936 struct strbuf sb = STRBUF_INIT;
937 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
938 strbuf_rtrim(&sb);
939 retval |= remove_one_note(t, sb.buf, flag);
941 strbuf_release(&sb);
943 if (!retval)
944 commit_notes(the_repository, t,
945 "Notes removed by 'git notes remove'");
946 free_notes(t);
947 return retval;
950 static int prune(int argc, const char **argv, const char *prefix)
952 struct notes_tree *t;
953 int show_only = 0, verbose = 0;
954 struct option options[] = {
955 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
956 OPT__VERBOSE(&verbose, N_("report pruned notes")),
957 OPT_END()
960 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
963 if (argc) {
964 error(_("too many arguments"));
965 usage_with_options(git_notes_prune_usage, options);
968 t = init_notes_check("prune", NOTES_INIT_WRITABLE);
970 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
971 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
972 if (!show_only)
973 commit_notes(the_repository, t,
974 "Notes removed by 'git notes prune'");
975 free_notes(t);
976 return 0;
979 static int get_ref(int argc, const char **argv, const char *prefix)
981 struct option options[] = { OPT_END() };
982 argc = parse_options(argc, argv, prefix, options,
983 git_notes_get_ref_usage, 0);
985 if (argc) {
986 error(_("too many arguments"));
987 usage_with_options(git_notes_get_ref_usage, options);
990 puts(default_notes_ref());
991 return 0;
994 int cmd_notes(int argc, const char **argv, const char *prefix)
996 int result;
997 const char *override_notes_ref = NULL;
998 struct option options[] = {
999 OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
1000 N_("use notes from <notes-ref>")),
1001 OPT_END()
1004 git_config(git_default_config, NULL);
1005 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
1006 PARSE_OPT_STOP_AT_NON_OPTION);
1008 if (override_notes_ref) {
1009 struct strbuf sb = STRBUF_INIT;
1010 strbuf_addstr(&sb, override_notes_ref);
1011 expand_notes_ref(&sb);
1012 setenv("GIT_NOTES_REF", sb.buf, 1);
1013 strbuf_release(&sb);
1016 if (argc < 1 || !strcmp(argv[0], "list"))
1017 result = list(argc, argv, prefix);
1018 else if (!strcmp(argv[0], "add"))
1019 result = add(argc, argv, prefix);
1020 else if (!strcmp(argv[0], "copy"))
1021 result = copy(argc, argv, prefix);
1022 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
1023 result = append_edit(argc, argv, prefix);
1024 else if (!strcmp(argv[0], "show"))
1025 result = show(argc, argv, prefix);
1026 else if (!strcmp(argv[0], "merge"))
1027 result = merge(argc, argv, prefix);
1028 else if (!strcmp(argv[0], "remove"))
1029 result = remove_cmd(argc, argv, prefix);
1030 else if (!strcmp(argv[0], "prune"))
1031 result = prune(argc, argv, prefix);
1032 else if (!strcmp(argv[0], "get-ref"))
1033 result = get_ref(argc, argv, prefix);
1034 else {
1035 result = error(_("unknown subcommand: %s"), argv[0]);
1036 usage_with_options(git_notes_usage, options);
1039 return result ? 1 : 0;