notes-merge: use ssize_t for write_in_full() return value
[git.git] / builtin / notes.c
blob77573cf1ea8cb4d998597e265263487e22fde592
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 "blob.h"
15 #include "commit.h"
16 #include "refs.h"
17 #include "exec_cmd.h"
18 #include "run-command.h"
19 #include "parse-options.h"
20 #include "string-list.h"
21 #include "notes-merge.h"
22 #include "notes-utils.h"
23 #include "worktree.h"
25 static const char * const git_notes_usage[] = {
26 N_("git notes [--ref <notes-ref>] [list [<object>]]"),
27 N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
28 N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"),
29 N_("git notes [--ref <notes-ref>] append [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
30 N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
31 N_("git notes [--ref <notes-ref>] show [<object>]"),
32 N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"),
33 N_("git notes merge --commit [-v | -q]"),
34 N_("git notes merge --abort [-v | -q]"),
35 N_("git notes [--ref <notes-ref>] remove [<object>...]"),
36 N_("git notes [--ref <notes-ref>] prune [-n | -v]"),
37 N_("git notes [--ref <notes-ref>] get-ref"),
38 NULL
41 static const char * const git_notes_list_usage[] = {
42 N_("git notes [list [<object>]]"),
43 NULL
46 static const char * const git_notes_add_usage[] = {
47 N_("git notes add [<options>] [<object>]"),
48 NULL
51 static const char * const git_notes_copy_usage[] = {
52 N_("git notes copy [<options>] <from-object> <to-object>"),
53 N_("git notes copy --stdin [<from-object> <to-object>]..."),
54 NULL
57 static const char * const git_notes_append_usage[] = {
58 N_("git notes append [<options>] [<object>]"),
59 NULL
62 static const char * const git_notes_edit_usage[] = {
63 N_("git notes edit [<object>]"),
64 NULL
67 static const char * const git_notes_show_usage[] = {
68 N_("git notes show [<object>]"),
69 NULL
72 static const char * const git_notes_merge_usage[] = {
73 N_("git notes merge [<options>] <notes-ref>"),
74 N_("git notes merge --commit [<options>]"),
75 N_("git notes merge --abort [<options>]"),
76 NULL
79 static const char * const git_notes_remove_usage[] = {
80 N_("git notes remove [<object>]"),
81 NULL
84 static const char * const git_notes_prune_usage[] = {
85 N_("git notes prune [<options>]"),
86 NULL
89 static const char * const git_notes_get_ref_usage[] = {
90 N_("git notes get-ref"),
91 NULL
94 static const char note_template[] =
95 N_("Write/edit the notes for the following object:");
97 struct note_data {
98 int given;
99 int use_editor;
100 char *edit_path;
101 struct strbuf buf;
104 static void free_note_data(struct note_data *d)
106 if (d->edit_path) {
107 unlink_or_warn(d->edit_path);
108 free(d->edit_path);
110 strbuf_release(&d->buf);
113 static int list_each_note(const struct object_id *object_oid,
114 const struct object_id *note_oid, char *note_path,
115 void *cb_data)
117 printf("%s %s\n", oid_to_hex(note_oid), oid_to_hex(object_oid));
118 return 0;
121 static void copy_obj_to_fd(int fd, const unsigned char *sha1)
123 unsigned long size;
124 enum object_type type;
125 char *buf = read_sha1_file(sha1, &type, &size);
126 if (buf) {
127 if (size)
128 write_or_die(fd, buf, size);
129 free(buf);
133 static void write_commented_object(int fd, const struct object_id *object)
135 const char *show_args[5] =
136 {"show", "--stat", "--no-notes", oid_to_hex(object), NULL};
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 show.argv = show_args;
143 show.no_stdin = 1;
144 show.out = -1;
145 show.err = 0;
146 show.git_cmd = 1;
147 if (start_command(&show))
148 die(_("unable to start 'show' for object '%s'"),
149 oid_to_hex(object));
151 if (strbuf_read(&buf, show.out, 0) < 0)
152 die_errno(_("could not read 'show' output"));
153 strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
154 write_or_die(fd, cbuf.buf, cbuf.len);
156 strbuf_release(&cbuf);
157 strbuf_release(&buf);
159 if (finish_command(&show))
160 die(_("failed to finish 'show' for object '%s'"),
161 oid_to_hex(object));
164 static void prepare_note_data(const struct object_id *object, struct note_data *d,
165 const unsigned char *old_note)
167 if (d->use_editor || !d->given) {
168 int fd;
169 struct strbuf buf = STRBUF_INIT;
171 /* write the template message before editing: */
172 d->edit_path = git_pathdup("NOTES_EDITMSG");
173 fd = open(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
174 if (fd < 0)
175 die_errno(_("could not create file '%s'"), d->edit_path);
177 if (d->given)
178 write_or_die(fd, d->buf.buf, d->buf.len);
179 else if (old_note)
180 copy_obj_to_fd(fd, old_note);
182 strbuf_addch(&buf, '\n');
183 strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
184 strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)));
185 strbuf_addch(&buf, '\n');
186 write_or_die(fd, buf.buf, buf.len);
188 write_commented_object(fd, object);
190 close(fd);
191 strbuf_release(&buf);
192 strbuf_reset(&d->buf);
194 if (launch_editor(d->edit_path, &d->buf, NULL)) {
195 die(_("please supply the note contents using either -m or -F option"));
197 strbuf_stripspace(&d->buf, 1);
201 static void write_note_data(struct note_data *d, unsigned char *sha1)
203 if (write_sha1_file(d->buf.buf, d->buf.len, blob_type, sha1)) {
204 error(_("unable to write note object"));
205 if (d->edit_path)
206 error(_("the note contents have been left in %s"),
207 d->edit_path);
208 exit(128);
212 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
214 struct note_data *d = opt->value;
216 strbuf_grow(&d->buf, strlen(arg) + 2);
217 if (d->buf.len)
218 strbuf_addch(&d->buf, '\n');
219 strbuf_addstr(&d->buf, arg);
220 strbuf_stripspace(&d->buf, 0);
222 d->given = 1;
223 return 0;
226 static int parse_file_arg(const struct option *opt, const char *arg, int unset)
228 struct note_data *d = opt->value;
230 if (d->buf.len)
231 strbuf_addch(&d->buf, '\n');
232 if (!strcmp(arg, "-")) {
233 if (strbuf_read(&d->buf, 0, 1024) < 0)
234 die_errno(_("cannot read '%s'"), arg);
235 } else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
236 die_errno(_("could not open or read '%s'"), arg);
237 strbuf_stripspace(&d->buf, 0);
239 d->given = 1;
240 return 0;
243 static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
245 struct note_data *d = opt->value;
246 char *buf;
247 struct object_id object;
248 enum object_type type;
249 unsigned long len;
251 if (d->buf.len)
252 strbuf_addch(&d->buf, '\n');
254 if (get_oid(arg, &object))
255 die(_("failed to resolve '%s' as a valid ref."), arg);
256 if (!(buf = read_sha1_file(object.hash, &type, &len))) {
257 free(buf);
258 die(_("failed to read object '%s'."), arg);
260 if (type != OBJ_BLOB) {
261 free(buf);
262 die(_("cannot read note data from non-blob object '%s'."), arg);
264 strbuf_add(&d->buf, buf, len);
265 free(buf);
267 d->given = 1;
268 return 0;
271 static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
273 struct note_data *d = opt->value;
274 d->use_editor = 1;
275 return parse_reuse_arg(opt, arg, unset);
278 static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
280 struct strbuf buf = STRBUF_INIT;
281 struct notes_rewrite_cfg *c = NULL;
282 struct notes_tree *t = NULL;
283 int ret = 0;
284 const char *msg = "Notes added by 'git notes copy'";
286 if (rewrite_cmd) {
287 c = init_copy_notes_for_rewrite(rewrite_cmd);
288 if (!c)
289 return 0;
290 } else {
291 init_notes(NULL, NULL, NULL, NOTES_INIT_WRITABLE);
292 t = &default_notes_tree;
295 while (strbuf_getline_lf(&buf, stdin) != EOF) {
296 struct object_id from_obj, to_obj;
297 struct strbuf **split;
298 int err;
300 split = strbuf_split(&buf, ' ');
301 if (!split[0] || !split[1])
302 die(_("malformed input line: '%s'."), buf.buf);
303 strbuf_rtrim(split[0]);
304 strbuf_rtrim(split[1]);
305 if (get_oid(split[0]->buf, &from_obj))
306 die(_("failed to resolve '%s' as a valid ref."), split[0]->buf);
307 if (get_oid(split[1]->buf, &to_obj))
308 die(_("failed to resolve '%s' as a valid ref."), split[1]->buf);
310 if (rewrite_cmd)
311 err = copy_note_for_rewrite(c, &from_obj, &to_obj);
312 else
313 err = copy_note(t, &from_obj, &to_obj, force,
314 combine_notes_overwrite);
316 if (err) {
317 error(_("failed to copy notes from '%s' to '%s'"),
318 split[0]->buf, split[1]->buf);
319 ret = 1;
322 strbuf_list_free(split);
325 if (!rewrite_cmd) {
326 commit_notes(t, msg);
327 free_notes(t);
328 } else {
329 finish_copy_notes_for_rewrite(c, msg);
331 return ret;
334 static struct notes_tree *init_notes_check(const char *subcommand,
335 int flags)
337 struct notes_tree *t;
338 const char *ref;
339 init_notes(NULL, NULL, NULL, flags);
340 t = &default_notes_tree;
342 ref = (flags & NOTES_INIT_WRITABLE) ? t->update_ref : t->ref;
343 if (!starts_with(ref, "refs/notes/"))
345 * TRANSLATORS: the first %s will be replaced by a git
346 * notes command: 'add', 'merge', 'remove', etc.
348 die(_("refusing to %s notes in %s (outside of refs/notes/)"),
349 subcommand, ref);
350 return t;
353 static int list(int argc, const char **argv, const char *prefix)
355 struct notes_tree *t;
356 struct object_id object;
357 const struct object_id *note;
358 int retval = -1;
359 struct option options[] = {
360 OPT_END()
363 if (argc)
364 argc = parse_options(argc, argv, prefix, options,
365 git_notes_list_usage, 0);
367 if (1 < argc) {
368 error(_("too many parameters"));
369 usage_with_options(git_notes_list_usage, options);
372 t = init_notes_check("list", 0);
373 if (argc) {
374 if (get_oid(argv[0], &object))
375 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
376 note = get_note(t, &object);
377 if (note) {
378 puts(oid_to_hex(note));
379 retval = 0;
380 } else
381 retval = error(_("no note found for object %s."),
382 oid_to_hex(&object));
383 } else
384 retval = for_each_note(t, 0, list_each_note, NULL);
386 free_notes(t);
387 return retval;
390 static int append_edit(int argc, const char **argv, const char *prefix);
392 static int add(int argc, const char **argv, const char *prefix)
394 int force = 0, allow_empty = 0;
395 const char *object_ref;
396 struct notes_tree *t;
397 struct object_id object, new_note;
398 const struct object_id *note;
399 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
400 struct option options[] = {
401 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
402 N_("note contents as a string"), PARSE_OPT_NONEG,
403 parse_msg_arg},
404 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
405 N_("note contents in a file"), PARSE_OPT_NONEG,
406 parse_file_arg},
407 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
408 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
409 parse_reedit_arg},
410 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
411 N_("reuse specified note object"), PARSE_OPT_NONEG,
412 parse_reuse_arg},
413 OPT_BOOL(0, "allow-empty", &allow_empty,
414 N_("allow storing empty note")),
415 OPT__FORCE(&force, N_("replace existing notes")),
416 OPT_END()
419 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
420 PARSE_OPT_KEEP_ARGV0);
422 if (2 < argc) {
423 error(_("too many parameters"));
424 usage_with_options(git_notes_add_usage, options);
427 object_ref = argc > 1 ? argv[1] : "HEAD";
429 if (get_oid(object_ref, &object))
430 die(_("failed to resolve '%s' as a valid ref."), object_ref);
432 t = init_notes_check("add", NOTES_INIT_WRITABLE);
433 note = get_note(t, &object);
435 if (note) {
436 if (!force) {
437 free_notes(t);
438 if (d.given) {
439 free_note_data(&d);
440 return error(_("Cannot add notes. "
441 "Found existing notes for object %s. "
442 "Use '-f' to overwrite existing notes"),
443 oid_to_hex(&object));
446 * Redirect to "edit" subcommand.
448 * We only end up here if none of -m/-F/-c/-C or -f are
449 * given. The original args are therefore still in
450 * argv[0-1].
452 argv[0] = "edit";
453 return append_edit(argc, argv, prefix);
455 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
456 oid_to_hex(&object));
459 prepare_note_data(&object, &d, note->hash);
460 if (d.buf.len || allow_empty) {
461 write_note_data(&d, new_note.hash);
462 if (add_note(t, &object, &new_note, combine_notes_overwrite))
463 die("BUG: combine_notes_overwrite failed");
464 commit_notes(t, "Notes added by 'git notes add'");
465 } else {
466 fprintf(stderr, _("Removing note for object %s\n"),
467 oid_to_hex(&object));
468 remove_note(t, object.hash);
469 commit_notes(t, "Notes removed by 'git notes add'");
472 free_note_data(&d);
473 free_notes(t);
474 return 0;
477 static int copy(int argc, const char **argv, const char *prefix)
479 int retval = 0, force = 0, from_stdin = 0;
480 const struct object_id *from_note, *note;
481 const char *object_ref;
482 struct object_id object, from_obj;
483 struct notes_tree *t;
484 const char *rewrite_cmd = NULL;
485 struct option options[] = {
486 OPT__FORCE(&force, N_("replace existing notes")),
487 OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")),
488 OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"),
489 N_("load rewriting config for <command> (implies "
490 "--stdin)")),
491 OPT_END()
494 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
497 if (from_stdin || rewrite_cmd) {
498 if (argc) {
499 error(_("too many parameters"));
500 usage_with_options(git_notes_copy_usage, options);
501 } else {
502 return notes_copy_from_stdin(force, rewrite_cmd);
506 if (argc < 2) {
507 error(_("too few parameters"));
508 usage_with_options(git_notes_copy_usage, options);
510 if (2 < argc) {
511 error(_("too many parameters"));
512 usage_with_options(git_notes_copy_usage, options);
515 if (get_oid(argv[0], &from_obj))
516 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
518 object_ref = 1 < argc ? argv[1] : "HEAD";
520 if (get_oid(object_ref, &object))
521 die(_("failed to resolve '%s' as a valid ref."), object_ref);
523 t = init_notes_check("copy", NOTES_INIT_WRITABLE);
524 note = get_note(t, &object);
526 if (note) {
527 if (!force) {
528 retval = error(_("Cannot copy notes. Found existing "
529 "notes for object %s. Use '-f' to "
530 "overwrite existing notes"),
531 oid_to_hex(&object));
532 goto out;
534 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
535 oid_to_hex(&object));
538 from_note = get_note(t, &from_obj);
539 if (!from_note) {
540 retval = error(_("missing notes on source object %s. Cannot "
541 "copy."), oid_to_hex(&from_obj));
542 goto out;
545 if (add_note(t, &object, from_note, combine_notes_overwrite))
546 die("BUG: combine_notes_overwrite failed");
547 commit_notes(t, "Notes added by 'git notes copy'");
548 out:
549 free_notes(t);
550 return retval;
553 static int append_edit(int argc, const char **argv, const char *prefix)
555 int allow_empty = 0;
556 const char *object_ref;
557 struct notes_tree *t;
558 struct object_id object, new_note;
559 const struct object_id *note;
560 char *logmsg;
561 const char * const *usage;
562 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
563 struct option options[] = {
564 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
565 N_("note contents as a string"), PARSE_OPT_NONEG,
566 parse_msg_arg},
567 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
568 N_("note contents in a file"), PARSE_OPT_NONEG,
569 parse_file_arg},
570 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
571 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
572 parse_reedit_arg},
573 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
574 N_("reuse specified note object"), PARSE_OPT_NONEG,
575 parse_reuse_arg},
576 OPT_BOOL(0, "allow-empty", &allow_empty,
577 N_("allow storing empty note")),
578 OPT_END()
580 int edit = !strcmp(argv[0], "edit");
582 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
583 argc = parse_options(argc, argv, prefix, options, usage,
584 PARSE_OPT_KEEP_ARGV0);
586 if (2 < argc) {
587 error(_("too many parameters"));
588 usage_with_options(usage, options);
591 if (d.given && edit)
592 fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
593 "for the 'edit' subcommand.\n"
594 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
596 object_ref = 1 < argc ? argv[1] : "HEAD";
598 if (get_oid(object_ref, &object))
599 die(_("failed to resolve '%s' as a valid ref."), object_ref);
601 t = init_notes_check(argv[0], NOTES_INIT_WRITABLE);
602 note = get_note(t, &object);
604 prepare_note_data(&object, &d, edit && note ? note->hash : NULL);
606 if (note && !edit) {
607 /* Append buf to previous note contents */
608 unsigned long size;
609 enum object_type type;
610 char *prev_buf = read_sha1_file(note->hash, &type, &size);
612 strbuf_grow(&d.buf, size + 1);
613 if (d.buf.len && prev_buf && size)
614 strbuf_insert(&d.buf, 0, "\n", 1);
615 if (prev_buf && size)
616 strbuf_insert(&d.buf, 0, prev_buf, size);
617 free(prev_buf);
620 if (d.buf.len || allow_empty) {
621 write_note_data(&d, new_note.hash);
622 if (add_note(t, &object, &new_note, combine_notes_overwrite))
623 die("BUG: combine_notes_overwrite failed");
624 logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]);
625 } else {
626 fprintf(stderr, _("Removing note for object %s\n"),
627 oid_to_hex(&object));
628 remove_note(t, object.hash);
629 logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]);
631 commit_notes(t, logmsg);
633 free(logmsg);
634 free_note_data(&d);
635 free_notes(t);
636 return 0;
639 static int show(int argc, const char **argv, const char *prefix)
641 const char *object_ref;
642 struct notes_tree *t;
643 struct object_id object;
644 const struct object_id *note;
645 int retval;
646 struct option options[] = {
647 OPT_END()
650 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
653 if (1 < argc) {
654 error(_("too many parameters"));
655 usage_with_options(git_notes_show_usage, options);
658 object_ref = argc ? argv[0] : "HEAD";
660 if (get_oid(object_ref, &object))
661 die(_("failed to resolve '%s' as a valid ref."), object_ref);
663 t = init_notes_check("show", 0);
664 note = get_note(t, &object);
666 if (!note)
667 retval = error(_("no note found for object %s."),
668 oid_to_hex(&object));
669 else {
670 const char *show_args[3] = {"show", oid_to_hex(note), NULL};
671 retval = execv_git_cmd(show_args);
673 free_notes(t);
674 return retval;
677 static int merge_abort(struct notes_merge_options *o)
679 int ret = 0;
682 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
683 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
686 if (delete_ref(NULL, "NOTES_MERGE_PARTIAL", NULL, 0))
687 ret += error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
688 if (delete_ref(NULL, "NOTES_MERGE_REF", NULL, REF_NODEREF))
689 ret += error(_("failed to delete ref NOTES_MERGE_REF"));
690 if (notes_merge_abort(o))
691 ret += error(_("failed to remove 'git notes merge' worktree"));
692 return ret;
695 static int merge_commit(struct notes_merge_options *o)
697 struct strbuf msg = STRBUF_INIT;
698 struct object_id oid, parent_oid;
699 struct notes_tree *t;
700 struct commit *partial;
701 struct pretty_print_context pretty_ctx;
702 void *local_ref_to_free;
703 int ret;
706 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
707 * and target notes ref from .git/NOTES_MERGE_REF.
710 if (get_oid("NOTES_MERGE_PARTIAL", &oid))
711 die(_("failed to read ref NOTES_MERGE_PARTIAL"));
712 else if (!(partial = lookup_commit_reference(&oid)))
713 die(_("could not find commit from NOTES_MERGE_PARTIAL."));
714 else if (parse_commit(partial))
715 die(_("could not parse commit from NOTES_MERGE_PARTIAL."));
717 if (partial->parents)
718 oidcpy(&parent_oid, &partial->parents->item->object.oid);
719 else
720 oidclr(&parent_oid);
722 t = xcalloc(1, sizeof(struct notes_tree));
723 init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
725 o->local_ref = local_ref_to_free =
726 resolve_refdup("NOTES_MERGE_REF", 0, oid.hash, NULL);
727 if (!o->local_ref)
728 die(_("failed to resolve NOTES_MERGE_REF"));
730 if (notes_merge_commit(o, t, partial, &oid))
731 die(_("failed to finalize notes merge"));
733 /* Reuse existing commit message in reflog message */
734 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
735 format_commit_message(partial, "%s", &msg, &pretty_ctx);
736 strbuf_trim(&msg);
737 strbuf_insert(&msg, 0, "notes: ", 7);
738 update_ref(msg.buf, o->local_ref, oid.hash,
739 is_null_oid(&parent_oid) ? NULL : parent_oid.hash,
740 0, UPDATE_REFS_DIE_ON_ERR);
742 free_notes(t);
743 strbuf_release(&msg);
744 ret = merge_abort(o);
745 free(local_ref_to_free);
746 return ret;
749 static int git_config_get_notes_strategy(const char *key,
750 enum notes_merge_strategy *strategy)
752 char *value;
754 if (git_config_get_string(key, &value))
755 return 1;
756 if (parse_notes_merge_strategy(value, strategy))
757 git_die_config(key, _("unknown notes merge strategy %s"), value);
759 free(value);
760 return 0;
763 static int merge(int argc, const char **argv, const char *prefix)
765 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
766 struct object_id result_oid;
767 struct notes_tree *t;
768 struct notes_merge_options o;
769 int do_merge = 0, do_commit = 0, do_abort = 0;
770 int verbosity = 0, result;
771 const char *strategy = NULL;
772 struct option options[] = {
773 OPT_GROUP(N_("General options")),
774 OPT__VERBOSITY(&verbosity),
775 OPT_GROUP(N_("Merge options")),
776 OPT_STRING('s', "strategy", &strategy, N_("strategy"),
777 N_("resolve notes conflicts using the given strategy "
778 "(manual/ours/theirs/union/cat_sort_uniq)")),
779 OPT_GROUP(N_("Committing unmerged notes")),
780 { OPTION_SET_INT, 0, "commit", &do_commit, NULL,
781 N_("finalize notes merge by committing unmerged notes"),
782 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
783 OPT_GROUP(N_("Aborting notes merge resolution")),
784 { OPTION_SET_INT, 0, "abort", &do_abort, NULL,
785 N_("abort notes merge"),
786 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
787 OPT_END()
790 argc = parse_options(argc, argv, prefix, options,
791 git_notes_merge_usage, 0);
793 if (strategy || do_commit + do_abort == 0)
794 do_merge = 1;
795 if (do_merge + do_commit + do_abort != 1) {
796 error(_("cannot mix --commit, --abort or -s/--strategy"));
797 usage_with_options(git_notes_merge_usage, options);
800 if (do_merge && argc != 1) {
801 error(_("must specify a notes ref to merge"));
802 usage_with_options(git_notes_merge_usage, options);
803 } else if (!do_merge && argc) {
804 error(_("too many parameters"));
805 usage_with_options(git_notes_merge_usage, options);
808 init_notes_merge_options(&o);
809 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
811 if (do_abort)
812 return merge_abort(&o);
813 if (do_commit)
814 return merge_commit(&o);
816 o.local_ref = default_notes_ref();
817 strbuf_addstr(&remote_ref, argv[0]);
818 expand_loose_notes_ref(&remote_ref);
819 o.remote_ref = remote_ref.buf;
821 t = init_notes_check("merge", NOTES_INIT_WRITABLE);
823 if (strategy) {
824 if (parse_notes_merge_strategy(strategy, &o.strategy)) {
825 error(_("unknown -s/--strategy: %s"), strategy);
826 usage_with_options(git_notes_merge_usage, options);
828 } else {
829 struct strbuf merge_key = STRBUF_INIT;
830 const char *short_ref = NULL;
832 if (!skip_prefix(o.local_ref, "refs/notes/", &short_ref))
833 die("BUG: local ref %s is outside of refs/notes/",
834 o.local_ref);
836 strbuf_addf(&merge_key, "notes.%s.mergeStrategy", short_ref);
838 if (git_config_get_notes_strategy(merge_key.buf, &o.strategy))
839 git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy);
841 strbuf_release(&merge_key);
844 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
845 remote_ref.buf, default_notes_ref());
846 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
848 result = notes_merge(&o, t, &result_oid);
850 if (result >= 0) /* Merge resulted (trivially) in result_oid */
851 /* Update default notes ref with new commit */
852 update_ref(msg.buf, default_notes_ref(), result_oid.hash, NULL,
853 0, UPDATE_REFS_DIE_ON_ERR);
854 else { /* Merge has unresolved conflicts */
855 const struct worktree *wt;
856 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
857 update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_oid.hash, NULL,
858 0, UPDATE_REFS_DIE_ON_ERR);
859 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
860 wt = find_shared_symref("NOTES_MERGE_REF", default_notes_ref());
861 if (wt)
862 die(_("a notes merge into %s is already in-progress at %s"),
863 default_notes_ref(), wt->path);
864 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
865 die(_("failed to store link to current notes ref (%s)"),
866 default_notes_ref());
867 printf(_("Automatic notes merge failed. Fix conflicts in %s and "
868 "commit the result with 'git notes merge --commit', or "
869 "abort the merge with 'git notes merge --abort'.\n"),
870 git_path(NOTES_MERGE_WORKTREE));
873 free_notes(t);
874 strbuf_release(&remote_ref);
875 strbuf_release(&msg);
876 return result < 0; /* return non-zero on conflicts */
879 #define IGNORE_MISSING 1
881 static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
883 int status;
884 struct object_id oid;
885 if (get_oid(name, &oid))
886 return error(_("Failed to resolve '%s' as a valid ref."), name);
887 status = remove_note(t, oid.hash);
888 if (status)
889 fprintf(stderr, _("Object %s has no note\n"), name);
890 else
891 fprintf(stderr, _("Removing note for object %s\n"), name);
892 return (flag & IGNORE_MISSING) ? 0 : status;
895 static int remove_cmd(int argc, const char **argv, const char *prefix)
897 unsigned flag = 0;
898 int from_stdin = 0;
899 struct option options[] = {
900 OPT_BIT(0, "ignore-missing", &flag,
901 N_("attempt to remove non-existent note is not an error"),
902 IGNORE_MISSING),
903 OPT_BOOL(0, "stdin", &from_stdin,
904 N_("read object names from the standard input")),
905 OPT_END()
907 struct notes_tree *t;
908 int retval = 0;
910 argc = parse_options(argc, argv, prefix, options,
911 git_notes_remove_usage, 0);
913 t = init_notes_check("remove", NOTES_INIT_WRITABLE);
915 if (!argc && !from_stdin) {
916 retval = remove_one_note(t, "HEAD", flag);
917 } else {
918 while (*argv) {
919 retval |= remove_one_note(t, *argv, flag);
920 argv++;
923 if (from_stdin) {
924 struct strbuf sb = STRBUF_INIT;
925 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
926 strbuf_rtrim(&sb);
927 retval |= remove_one_note(t, sb.buf, flag);
929 strbuf_release(&sb);
931 if (!retval)
932 commit_notes(t, "Notes removed by 'git notes remove'");
933 free_notes(t);
934 return retval;
937 static int prune(int argc, const char **argv, const char *prefix)
939 struct notes_tree *t;
940 int show_only = 0, verbose = 0;
941 struct option options[] = {
942 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
943 OPT__VERBOSE(&verbose, N_("report pruned notes")),
944 OPT_END()
947 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
950 if (argc) {
951 error(_("too many parameters"));
952 usage_with_options(git_notes_prune_usage, options);
955 t = init_notes_check("prune", NOTES_INIT_WRITABLE);
957 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
958 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
959 if (!show_only)
960 commit_notes(t, "Notes removed by 'git notes prune'");
961 free_notes(t);
962 return 0;
965 static int get_ref(int argc, const char **argv, const char *prefix)
967 struct option options[] = { OPT_END() };
968 argc = parse_options(argc, argv, prefix, options,
969 git_notes_get_ref_usage, 0);
971 if (argc) {
972 error(_("too many parameters"));
973 usage_with_options(git_notes_get_ref_usage, options);
976 puts(default_notes_ref());
977 return 0;
980 int cmd_notes(int argc, const char **argv, const char *prefix)
982 int result;
983 const char *override_notes_ref = NULL;
984 struct option options[] = {
985 OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
986 N_("use notes from <notes-ref>")),
987 OPT_END()
990 git_config(git_default_config, NULL);
991 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
992 PARSE_OPT_STOP_AT_NON_OPTION);
994 if (override_notes_ref) {
995 struct strbuf sb = STRBUF_INIT;
996 strbuf_addstr(&sb, override_notes_ref);
997 expand_notes_ref(&sb);
998 setenv("GIT_NOTES_REF", sb.buf, 1);
999 strbuf_release(&sb);
1002 if (argc < 1 || !strcmp(argv[0], "list"))
1003 result = list(argc, argv, prefix);
1004 else if (!strcmp(argv[0], "add"))
1005 result = add(argc, argv, prefix);
1006 else if (!strcmp(argv[0], "copy"))
1007 result = copy(argc, argv, prefix);
1008 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
1009 result = append_edit(argc, argv, prefix);
1010 else if (!strcmp(argv[0], "show"))
1011 result = show(argc, argv, prefix);
1012 else if (!strcmp(argv[0], "merge"))
1013 result = merge(argc, argv, prefix);
1014 else if (!strcmp(argv[0], "remove"))
1015 result = remove_cmd(argc, argv, prefix);
1016 else if (!strcmp(argv[0], "prune"))
1017 result = prune(argc, argv, prefix);
1018 else if (!strcmp(argv[0], "get-ref"))
1019 result = get_ref(argc, argv, prefix);
1020 else {
1021 result = error(_("unknown subcommand: %s"), argv[0]);
1022 usage_with_options(git_notes_usage, options);
1025 return result ? 1 : 0;