get_oid_with_context(): match prototype and implementation
[git.git] / builtin / notes.c
blob02e97f55c5a01be452bb3eaa97ec396f58e2ed99
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 const char *show_args[5] =
138 {"show", "--stat", "--no-notes", oid_to_hex(object), NULL};
139 struct child_process show = CHILD_PROCESS_INIT;
140 struct strbuf buf = STRBUF_INIT;
141 struct strbuf cbuf = STRBUF_INIT;
143 /* Invoke "git show --stat --no-notes $object" */
144 show.argv = show_args;
145 show.no_stdin = 1;
146 show.out = -1;
147 show.err = 0;
148 show.git_cmd = 1;
149 if (start_command(&show))
150 die(_("unable to start 'show' for object '%s'"),
151 oid_to_hex(object));
153 if (strbuf_read(&buf, show.out, 0) < 0)
154 die_errno(_("could not read 'show' output"));
155 strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
156 write_or_die(fd, cbuf.buf, cbuf.len);
158 strbuf_release(&cbuf);
159 strbuf_release(&buf);
161 if (finish_command(&show))
162 die(_("failed to finish 'show' for object '%s'"),
163 oid_to_hex(object));
166 static void prepare_note_data(const struct object_id *object, struct note_data *d,
167 const struct object_id *old_note)
169 if (d->use_editor || !d->given) {
170 int fd;
171 struct strbuf buf = STRBUF_INIT;
173 /* write the template message before editing: */
174 d->edit_path = git_pathdup("NOTES_EDITMSG");
175 fd = open(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
176 if (fd < 0)
177 die_errno(_("could not create file '%s'"), d->edit_path);
179 if (d->given)
180 write_or_die(fd, d->buf.buf, d->buf.len);
181 else if (old_note)
182 copy_obj_to_fd(fd, old_note);
184 strbuf_addch(&buf, '\n');
185 strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
186 strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)));
187 strbuf_addch(&buf, '\n');
188 write_or_die(fd, buf.buf, buf.len);
190 write_commented_object(fd, object);
192 close(fd);
193 strbuf_release(&buf);
194 strbuf_reset(&d->buf);
196 if (launch_editor(d->edit_path, &d->buf, NULL)) {
197 die(_("please supply the note contents using either -m or -F option"));
199 strbuf_stripspace(&d->buf, 1);
203 static void write_note_data(struct note_data *d, struct object_id *oid)
205 if (write_object_file(d->buf.buf, d->buf.len, blob_type, oid)) {
206 error(_("unable to write note object"));
207 if (d->edit_path)
208 error(_("the note contents have been left in %s"),
209 d->edit_path);
210 exit(128);
214 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
216 struct note_data *d = opt->value;
218 BUG_ON_OPT_NEG(unset);
220 strbuf_grow(&d->buf, strlen(arg) + 2);
221 if (d->buf.len)
222 strbuf_addch(&d->buf, '\n');
223 strbuf_addstr(&d->buf, arg);
224 strbuf_stripspace(&d->buf, 0);
226 d->given = 1;
227 return 0;
230 static int parse_file_arg(const struct option *opt, const char *arg, int unset)
232 struct note_data *d = opt->value;
234 BUG_ON_OPT_NEG(unset);
236 if (d->buf.len)
237 strbuf_addch(&d->buf, '\n');
238 if (!strcmp(arg, "-")) {
239 if (strbuf_read(&d->buf, 0, 1024) < 0)
240 die_errno(_("cannot read '%s'"), arg);
241 } else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
242 die_errno(_("could not open or read '%s'"), arg);
243 strbuf_stripspace(&d->buf, 0);
245 d->given = 1;
246 return 0;
249 static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
251 struct note_data *d = opt->value;
252 char *buf;
253 struct object_id object;
254 enum object_type type;
255 unsigned long len;
257 BUG_ON_OPT_NEG(unset);
259 if (d->buf.len)
260 strbuf_addch(&d->buf, '\n');
262 if (get_oid(arg, &object))
263 die(_("failed to resolve '%s' as a valid ref."), arg);
264 if (!(buf = read_object_file(&object, &type, &len)))
265 die(_("failed to read object '%s'."), arg);
266 if (type != OBJ_BLOB) {
267 free(buf);
268 die(_("cannot read note data from non-blob object '%s'."), arg);
270 strbuf_add(&d->buf, buf, len);
271 free(buf);
273 d->given = 1;
274 return 0;
277 static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
279 struct note_data *d = opt->value;
280 BUG_ON_OPT_NEG(unset);
281 d->use_editor = 1;
282 return parse_reuse_arg(opt, arg, unset);
285 static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
287 struct strbuf buf = STRBUF_INIT;
288 struct notes_rewrite_cfg *c = NULL;
289 struct notes_tree *t = NULL;
290 int ret = 0;
291 const char *msg = "Notes added by 'git notes copy'";
293 if (rewrite_cmd) {
294 c = init_copy_notes_for_rewrite(rewrite_cmd);
295 if (!c)
296 return 0;
297 } else {
298 init_notes(NULL, NULL, NULL, NOTES_INIT_WRITABLE);
299 t = &default_notes_tree;
302 while (strbuf_getline_lf(&buf, stdin) != EOF) {
303 struct object_id from_obj, to_obj;
304 struct strbuf **split;
305 int err;
307 split = strbuf_split(&buf, ' ');
308 if (!split[0] || !split[1])
309 die(_("malformed input line: '%s'."), buf.buf);
310 strbuf_rtrim(split[0]);
311 strbuf_rtrim(split[1]);
312 if (get_oid(split[0]->buf, &from_obj))
313 die(_("failed to resolve '%s' as a valid ref."), split[0]->buf);
314 if (get_oid(split[1]->buf, &to_obj))
315 die(_("failed to resolve '%s' as a valid ref."), split[1]->buf);
317 if (rewrite_cmd)
318 err = copy_note_for_rewrite(c, &from_obj, &to_obj);
319 else
320 err = copy_note(t, &from_obj, &to_obj, force,
321 combine_notes_overwrite);
323 if (err) {
324 error(_("failed to copy notes from '%s' to '%s'"),
325 split[0]->buf, split[1]->buf);
326 ret = 1;
329 strbuf_list_free(split);
332 if (!rewrite_cmd) {
333 commit_notes(the_repository, t, msg);
334 free_notes(t);
335 } else {
336 finish_copy_notes_for_rewrite(the_repository, c, msg);
338 strbuf_release(&buf);
339 return ret;
342 static struct notes_tree *init_notes_check(const char *subcommand,
343 int flags)
345 struct notes_tree *t;
346 const char *ref;
347 init_notes(NULL, NULL, NULL, flags);
348 t = &default_notes_tree;
350 ref = (flags & NOTES_INIT_WRITABLE) ? t->update_ref : t->ref;
351 if (!starts_with(ref, "refs/notes/"))
353 * TRANSLATORS: the first %s will be replaced by a git
354 * notes command: 'add', 'merge', 'remove', etc.
356 die(_("refusing to %s notes in %s (outside of refs/notes/)"),
357 subcommand, ref);
358 return t;
361 static int list(int argc, const char **argv, const char *prefix)
363 struct notes_tree *t;
364 struct object_id object;
365 const struct object_id *note;
366 int retval = -1;
367 struct option options[] = {
368 OPT_END()
371 if (argc)
372 argc = parse_options(argc, argv, prefix, options,
373 git_notes_list_usage, 0);
375 if (1 < argc) {
376 error(_("too many parameters"));
377 usage_with_options(git_notes_list_usage, options);
380 t = init_notes_check("list", 0);
381 if (argc) {
382 if (get_oid(argv[0], &object))
383 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
384 note = get_note(t, &object);
385 if (note) {
386 puts(oid_to_hex(note));
387 retval = 0;
388 } else
389 retval = error(_("no note found for object %s."),
390 oid_to_hex(&object));
391 } else
392 retval = for_each_note(t, 0, list_each_note, NULL);
394 free_notes(t);
395 return retval;
398 static int append_edit(int argc, const char **argv, const char *prefix);
400 static int add(int argc, const char **argv, const char *prefix)
402 int force = 0, allow_empty = 0;
403 const char *object_ref;
404 struct notes_tree *t;
405 struct object_id object, new_note;
406 const struct object_id *note;
407 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
408 struct option options[] = {
409 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
410 N_("note contents as a string"), PARSE_OPT_NONEG,
411 parse_msg_arg},
412 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
413 N_("note contents in a file"), PARSE_OPT_NONEG,
414 parse_file_arg},
415 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
416 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
417 parse_reedit_arg},
418 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
419 N_("reuse specified note object"), PARSE_OPT_NONEG,
420 parse_reuse_arg},
421 OPT_BOOL(0, "allow-empty", &allow_empty,
422 N_("allow storing empty note")),
423 OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE),
424 OPT_END()
427 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
428 PARSE_OPT_KEEP_ARGV0);
430 if (2 < argc) {
431 error(_("too many parameters"));
432 usage_with_options(git_notes_add_usage, options);
435 object_ref = argc > 1 ? argv[1] : "HEAD";
437 if (get_oid(object_ref, &object))
438 die(_("failed to resolve '%s' as a valid ref."), object_ref);
440 t = init_notes_check("add", NOTES_INIT_WRITABLE);
441 note = get_note(t, &object);
443 if (note) {
444 if (!force) {
445 free_notes(t);
446 if (d.given) {
447 free_note_data(&d);
448 return error(_("Cannot add notes. "
449 "Found existing notes for object %s. "
450 "Use '-f' to overwrite existing notes"),
451 oid_to_hex(&object));
454 * Redirect to "edit" subcommand.
456 * We only end up here if none of -m/-F/-c/-C or -f are
457 * given. The original args are therefore still in
458 * argv[0-1].
460 argv[0] = "edit";
461 return append_edit(argc, argv, prefix);
463 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
464 oid_to_hex(&object));
467 prepare_note_data(&object, &d, note);
468 if (d.buf.len || allow_empty) {
469 write_note_data(&d, &new_note);
470 if (add_note(t, &object, &new_note, combine_notes_overwrite))
471 BUG("combine_notes_overwrite failed");
472 commit_notes(the_repository, t,
473 "Notes added by 'git notes add'");
474 } else {
475 fprintf(stderr, _("Removing note for object %s\n"),
476 oid_to_hex(&object));
477 remove_note(t, object.hash);
478 commit_notes(the_repository, t,
479 "Notes removed by 'git notes add'");
482 free_note_data(&d);
483 free_notes(t);
484 return 0;
487 static int copy(int argc, const char **argv, const char *prefix)
489 int retval = 0, force = 0, from_stdin = 0;
490 const struct object_id *from_note, *note;
491 const char *object_ref;
492 struct object_id object, from_obj;
493 struct notes_tree *t;
494 const char *rewrite_cmd = NULL;
495 struct option options[] = {
496 OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE),
497 OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")),
498 OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"),
499 N_("load rewriting config for <command> (implies "
500 "--stdin)")),
501 OPT_END()
504 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
507 if (from_stdin || rewrite_cmd) {
508 if (argc) {
509 error(_("too many parameters"));
510 usage_with_options(git_notes_copy_usage, options);
511 } else {
512 return notes_copy_from_stdin(force, rewrite_cmd);
516 if (argc < 2) {
517 error(_("too few parameters"));
518 usage_with_options(git_notes_copy_usage, options);
520 if (2 < argc) {
521 error(_("too many parameters"));
522 usage_with_options(git_notes_copy_usage, options);
525 if (get_oid(argv[0], &from_obj))
526 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
528 object_ref = 1 < argc ? argv[1] : "HEAD";
530 if (get_oid(object_ref, &object))
531 die(_("failed to resolve '%s' as a valid ref."), object_ref);
533 t = init_notes_check("copy", NOTES_INIT_WRITABLE);
534 note = get_note(t, &object);
536 if (note) {
537 if (!force) {
538 retval = error(_("Cannot copy notes. Found existing "
539 "notes for object %s. Use '-f' to "
540 "overwrite existing notes"),
541 oid_to_hex(&object));
542 goto out;
544 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
545 oid_to_hex(&object));
548 from_note = get_note(t, &from_obj);
549 if (!from_note) {
550 retval = error(_("missing notes on source object %s. Cannot "
551 "copy."), oid_to_hex(&from_obj));
552 goto out;
555 if (add_note(t, &object, from_note, combine_notes_overwrite))
556 BUG("combine_notes_overwrite failed");
557 commit_notes(the_repository, t,
558 "Notes added by 'git notes copy'");
559 out:
560 free_notes(t);
561 return retval;
564 static int append_edit(int argc, const char **argv, const char *prefix)
566 int allow_empty = 0;
567 const char *object_ref;
568 struct notes_tree *t;
569 struct object_id object, new_note;
570 const struct object_id *note;
571 char *logmsg;
572 const char * const *usage;
573 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
574 struct option options[] = {
575 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
576 N_("note contents as a string"), PARSE_OPT_NONEG,
577 parse_msg_arg},
578 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
579 N_("note contents in a file"), PARSE_OPT_NONEG,
580 parse_file_arg},
581 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
582 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
583 parse_reedit_arg},
584 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
585 N_("reuse specified note object"), PARSE_OPT_NONEG,
586 parse_reuse_arg},
587 OPT_BOOL(0, "allow-empty", &allow_empty,
588 N_("allow storing empty note")),
589 OPT_END()
591 int edit = !strcmp(argv[0], "edit");
593 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
594 argc = parse_options(argc, argv, prefix, options, usage,
595 PARSE_OPT_KEEP_ARGV0);
597 if (2 < argc) {
598 error(_("too many parameters"));
599 usage_with_options(usage, options);
602 if (d.given && edit)
603 fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
604 "for the 'edit' subcommand.\n"
605 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
607 object_ref = 1 < argc ? argv[1] : "HEAD";
609 if (get_oid(object_ref, &object))
610 die(_("failed to resolve '%s' as a valid ref."), object_ref);
612 t = init_notes_check(argv[0], NOTES_INIT_WRITABLE);
613 note = get_note(t, &object);
615 prepare_note_data(&object, &d, edit && note ? note : NULL);
617 if (note && !edit) {
618 /* Append buf to previous note contents */
619 unsigned long size;
620 enum object_type type;
621 char *prev_buf = read_object_file(note, &type, &size);
623 strbuf_grow(&d.buf, size + 1);
624 if (d.buf.len && prev_buf && size)
625 strbuf_insert(&d.buf, 0, "\n", 1);
626 if (prev_buf && size)
627 strbuf_insert(&d.buf, 0, prev_buf, size);
628 free(prev_buf);
631 if (d.buf.len || allow_empty) {
632 write_note_data(&d, &new_note);
633 if (add_note(t, &object, &new_note, combine_notes_overwrite))
634 BUG("combine_notes_overwrite failed");
635 logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]);
636 } else {
637 fprintf(stderr, _("Removing note for object %s\n"),
638 oid_to_hex(&object));
639 remove_note(t, object.hash);
640 logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]);
642 commit_notes(the_repository, t, logmsg);
644 free(logmsg);
645 free_note_data(&d);
646 free_notes(t);
647 return 0;
650 static int show(int argc, const char **argv, const char *prefix)
652 const char *object_ref;
653 struct notes_tree *t;
654 struct object_id object;
655 const struct object_id *note;
656 int retval;
657 struct option options[] = {
658 OPT_END()
661 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
664 if (1 < argc) {
665 error(_("too many parameters"));
666 usage_with_options(git_notes_show_usage, options);
669 object_ref = argc ? argv[0] : "HEAD";
671 if (get_oid(object_ref, &object))
672 die(_("failed to resolve '%s' as a valid ref."), object_ref);
674 t = init_notes_check("show", 0);
675 note = get_note(t, &object);
677 if (!note)
678 retval = error(_("no note found for object %s."),
679 oid_to_hex(&object));
680 else {
681 const char *show_args[3] = {"show", oid_to_hex(note), NULL};
682 retval = execv_git_cmd(show_args);
684 free_notes(t);
685 return retval;
688 static int merge_abort(struct notes_merge_options *o)
690 int ret = 0;
693 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
694 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
697 if (delete_ref(NULL, "NOTES_MERGE_PARTIAL", NULL, 0))
698 ret += error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
699 if (delete_ref(NULL, "NOTES_MERGE_REF", NULL, REF_NO_DEREF))
700 ret += error(_("failed to delete ref NOTES_MERGE_REF"));
701 if (notes_merge_abort(o))
702 ret += error(_("failed to remove 'git notes merge' worktree"));
703 return ret;
706 static int merge_commit(struct notes_merge_options *o)
708 struct strbuf msg = STRBUF_INIT;
709 struct object_id oid, parent_oid;
710 struct notes_tree *t;
711 struct commit *partial;
712 struct pretty_print_context pretty_ctx;
713 void *local_ref_to_free;
714 int ret;
717 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
718 * and target notes ref from .git/NOTES_MERGE_REF.
721 if (get_oid("NOTES_MERGE_PARTIAL", &oid))
722 die(_("failed to read ref NOTES_MERGE_PARTIAL"));
723 else if (!(partial = lookup_commit_reference(the_repository, &oid)))
724 die(_("could not find commit from NOTES_MERGE_PARTIAL."));
725 else if (parse_commit(partial))
726 die(_("could not parse commit from NOTES_MERGE_PARTIAL."));
728 if (partial->parents)
729 oidcpy(&parent_oid, &partial->parents->item->object.oid);
730 else
731 oidclr(&parent_oid);
733 t = xcalloc(1, sizeof(struct notes_tree));
734 init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
736 o->local_ref = local_ref_to_free =
737 resolve_refdup("NOTES_MERGE_REF", 0, &oid, NULL);
738 if (!o->local_ref)
739 die(_("failed to resolve NOTES_MERGE_REF"));
741 if (notes_merge_commit(o, t, partial, &oid))
742 die(_("failed to finalize notes merge"));
744 /* Reuse existing commit message in reflog message */
745 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
746 format_commit_message(partial, "%s", &msg, &pretty_ctx);
747 strbuf_trim(&msg);
748 strbuf_insert(&msg, 0, "notes: ", 7);
749 update_ref(msg.buf, o->local_ref, &oid,
750 is_null_oid(&parent_oid) ? NULL : &parent_oid,
751 0, UPDATE_REFS_DIE_ON_ERR);
753 free_notes(t);
754 strbuf_release(&msg);
755 ret = merge_abort(o);
756 free(local_ref_to_free);
757 return ret;
760 static int git_config_get_notes_strategy(const char *key,
761 enum notes_merge_strategy *strategy)
763 char *value;
765 if (git_config_get_string(key, &value))
766 return 1;
767 if (parse_notes_merge_strategy(value, strategy))
768 git_die_config(key, _("unknown notes merge strategy %s"), value);
770 free(value);
771 return 0;
774 static int merge(int argc, const char **argv, const char *prefix)
776 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
777 struct object_id result_oid;
778 struct notes_tree *t;
779 struct notes_merge_options o;
780 int do_merge = 0, do_commit = 0, do_abort = 0;
781 int verbosity = 0, result;
782 const char *strategy = NULL;
783 struct option options[] = {
784 OPT_GROUP(N_("General options")),
785 OPT__VERBOSITY(&verbosity),
786 OPT_GROUP(N_("Merge options")),
787 OPT_STRING('s', "strategy", &strategy, N_("strategy"),
788 N_("resolve notes conflicts using the given strategy "
789 "(manual/ours/theirs/union/cat_sort_uniq)")),
790 OPT_GROUP(N_("Committing unmerged notes")),
791 OPT_SET_INT_F(0, "commit", &do_commit,
792 N_("finalize notes merge by committing unmerged notes"),
793 1, PARSE_OPT_NONEG),
794 OPT_GROUP(N_("Aborting notes merge resolution")),
795 OPT_SET_INT_F(0, "abort", &do_abort,
796 N_("abort notes merge"),
797 1, PARSE_OPT_NONEG),
798 OPT_END()
801 argc = parse_options(argc, argv, prefix, options,
802 git_notes_merge_usage, 0);
804 if (strategy || do_commit + do_abort == 0)
805 do_merge = 1;
806 if (do_merge + do_commit + do_abort != 1) {
807 error(_("cannot mix --commit, --abort or -s/--strategy"));
808 usage_with_options(git_notes_merge_usage, options);
811 if (do_merge && argc != 1) {
812 error(_("must specify a notes ref to merge"));
813 usage_with_options(git_notes_merge_usage, options);
814 } else if (!do_merge && argc) {
815 error(_("too many parameters"));
816 usage_with_options(git_notes_merge_usage, options);
819 init_notes_merge_options(the_repository, &o);
820 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
822 if (do_abort)
823 return merge_abort(&o);
824 if (do_commit)
825 return merge_commit(&o);
827 o.local_ref = default_notes_ref();
828 strbuf_addstr(&remote_ref, argv[0]);
829 expand_loose_notes_ref(&remote_ref);
830 o.remote_ref = remote_ref.buf;
832 t = init_notes_check("merge", NOTES_INIT_WRITABLE);
834 if (strategy) {
835 if (parse_notes_merge_strategy(strategy, &o.strategy)) {
836 error(_("unknown -s/--strategy: %s"), strategy);
837 usage_with_options(git_notes_merge_usage, options);
839 } else {
840 struct strbuf merge_key = STRBUF_INIT;
841 const char *short_ref = NULL;
843 if (!skip_prefix(o.local_ref, "refs/notes/", &short_ref))
844 BUG("local ref %s is outside of refs/notes/",
845 o.local_ref);
847 strbuf_addf(&merge_key, "notes.%s.mergeStrategy", short_ref);
849 if (git_config_get_notes_strategy(merge_key.buf, &o.strategy))
850 git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy);
852 strbuf_release(&merge_key);
855 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
856 remote_ref.buf, default_notes_ref());
857 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
859 result = notes_merge(&o, t, &result_oid);
861 if (result >= 0) /* Merge resulted (trivially) in result_oid */
862 /* Update default notes ref with new commit */
863 update_ref(msg.buf, default_notes_ref(), &result_oid, NULL, 0,
864 UPDATE_REFS_DIE_ON_ERR);
865 else { /* Merge has unresolved conflicts */
866 const struct worktree *wt;
867 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
868 update_ref(msg.buf, "NOTES_MERGE_PARTIAL", &result_oid, NULL,
869 0, UPDATE_REFS_DIE_ON_ERR);
870 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
871 wt = find_shared_symref("NOTES_MERGE_REF", 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 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
876 die(_("failed to store link to current notes ref (%s)"),
877 default_notes_ref());
878 fprintf(stderr, _("Automatic notes merge failed. Fix conflicts in %s "
879 "and commit the result with 'git notes merge --commit', "
880 "or abort the merge with 'git notes merge --abort'.\n"),
881 git_path(NOTES_MERGE_WORKTREE));
884 free_notes(t);
885 strbuf_release(&remote_ref);
886 strbuf_release(&msg);
887 return result < 0; /* return non-zero on conflicts */
890 #define IGNORE_MISSING 1
892 static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
894 int status;
895 struct object_id oid;
896 if (get_oid(name, &oid))
897 return error(_("Failed to resolve '%s' as a valid ref."), name);
898 status = remove_note(t, oid.hash);
899 if (status)
900 fprintf(stderr, _("Object %s has no note\n"), name);
901 else
902 fprintf(stderr, _("Removing note for object %s\n"), name);
903 return (flag & IGNORE_MISSING) ? 0 : status;
906 static int remove_cmd(int argc, const char **argv, const char *prefix)
908 unsigned flag = 0;
909 int from_stdin = 0;
910 struct option options[] = {
911 OPT_BIT(0, "ignore-missing", &flag,
912 N_("attempt to remove non-existent note is not an error"),
913 IGNORE_MISSING),
914 OPT_BOOL(0, "stdin", &from_stdin,
915 N_("read object names from the standard input")),
916 OPT_END()
918 struct notes_tree *t;
919 int retval = 0;
921 argc = parse_options(argc, argv, prefix, options,
922 git_notes_remove_usage, 0);
924 t = init_notes_check("remove", NOTES_INIT_WRITABLE);
926 if (!argc && !from_stdin) {
927 retval = remove_one_note(t, "HEAD", flag);
928 } else {
929 while (*argv) {
930 retval |= remove_one_note(t, *argv, flag);
931 argv++;
934 if (from_stdin) {
935 struct strbuf sb = STRBUF_INIT;
936 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
937 strbuf_rtrim(&sb);
938 retval |= remove_one_note(t, sb.buf, flag);
940 strbuf_release(&sb);
942 if (!retval)
943 commit_notes(the_repository, t,
944 "Notes removed by 'git notes remove'");
945 free_notes(t);
946 return retval;
949 static int prune(int argc, const char **argv, const char *prefix)
951 struct notes_tree *t;
952 int show_only = 0, verbose = 0;
953 struct option options[] = {
954 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
955 OPT__VERBOSE(&verbose, N_("report pruned notes")),
956 OPT_END()
959 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
962 if (argc) {
963 error(_("too many parameters"));
964 usage_with_options(git_notes_prune_usage, options);
967 t = init_notes_check("prune", NOTES_INIT_WRITABLE);
969 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
970 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
971 if (!show_only)
972 commit_notes(the_repository, t,
973 "Notes removed by 'git notes prune'");
974 free_notes(t);
975 return 0;
978 static int get_ref(int argc, const char **argv, const char *prefix)
980 struct option options[] = { OPT_END() };
981 argc = parse_options(argc, argv, prefix, options,
982 git_notes_get_ref_usage, 0);
984 if (argc) {
985 error(_("too many parameters"));
986 usage_with_options(git_notes_get_ref_usage, options);
989 puts(default_notes_ref());
990 return 0;
993 int cmd_notes(int argc, const char **argv, const char *prefix)
995 int result;
996 const char *override_notes_ref = NULL;
997 struct option options[] = {
998 OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
999 N_("use notes from <notes-ref>")),
1000 OPT_END()
1003 git_config(git_default_config, NULL);
1004 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
1005 PARSE_OPT_STOP_AT_NON_OPTION);
1007 if (override_notes_ref) {
1008 struct strbuf sb = STRBUF_INIT;
1009 strbuf_addstr(&sb, override_notes_ref);
1010 expand_notes_ref(&sb);
1011 setenv("GIT_NOTES_REF", sb.buf, 1);
1012 strbuf_release(&sb);
1015 if (argc < 1 || !strcmp(argv[0], "list"))
1016 result = list(argc, argv, prefix);
1017 else if (!strcmp(argv[0], "add"))
1018 result = add(argc, argv, prefix);
1019 else if (!strcmp(argv[0], "copy"))
1020 result = copy(argc, argv, prefix);
1021 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
1022 result = append_edit(argc, argv, prefix);
1023 else if (!strcmp(argv[0], "show"))
1024 result = show(argc, argv, prefix);
1025 else if (!strcmp(argv[0], "merge"))
1026 result = merge(argc, argv, prefix);
1027 else if (!strcmp(argv[0], "remove"))
1028 result = remove_cmd(argc, argv, prefix);
1029 else if (!strcmp(argv[0], "prune"))
1030 result = prune(argc, argv, prefix);
1031 else if (!strcmp(argv[0], "get-ref"))
1032 result = get_ref(argc, argv, prefix);
1033 else {
1034 result = error(_("unknown subcommand: %s"), argv[0]);
1035 usage_with_options(git_notes_usage, options);
1038 return result ? 1 : 0;