t0301: fixes for windows compatibility
[git/debian.git] / builtin / notes.c
blob71c59583a17f8da6eb948248e19a262781091f28
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 = xopen(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
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, struct object_id *oid)
203 if (write_object_file(d->buf.buf, d->buf.len, blob_type, oid)) {
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 BUG_ON_OPT_NEG(unset);
218 strbuf_grow(&d->buf, strlen(arg) + 2);
219 if (d->buf.len)
220 strbuf_addch(&d->buf, '\n');
221 strbuf_addstr(&d->buf, arg);
222 strbuf_stripspace(&d->buf, 0);
224 d->given = 1;
225 return 0;
228 static int parse_file_arg(const struct option *opt, const char *arg, int unset)
230 struct note_data *d = opt->value;
232 BUG_ON_OPT_NEG(unset);
234 if (d->buf.len)
235 strbuf_addch(&d->buf, '\n');
236 if (!strcmp(arg, "-")) {
237 if (strbuf_read(&d->buf, 0, 1024) < 0)
238 die_errno(_("cannot read '%s'"), arg);
239 } else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
240 die_errno(_("could not open or read '%s'"), arg);
241 strbuf_stripspace(&d->buf, 0);
243 d->given = 1;
244 return 0;
247 static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
249 struct note_data *d = opt->value;
250 char *buf;
251 struct object_id object;
252 enum object_type type;
253 unsigned long len;
255 BUG_ON_OPT_NEG(unset);
257 if (d->buf.len)
258 strbuf_addch(&d->buf, '\n');
260 if (get_oid(arg, &object))
261 die(_("failed to resolve '%s' as a valid ref."), arg);
262 if (!(buf = read_object_file(&object, &type, &len)))
263 die(_("failed to read object '%s'."), arg);
264 if (type != OBJ_BLOB) {
265 free(buf);
266 die(_("cannot read note data from non-blob object '%s'."), arg);
268 strbuf_add(&d->buf, buf, len);
269 free(buf);
271 d->given = 1;
272 return 0;
275 static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
277 struct note_data *d = opt->value;
278 BUG_ON_OPT_NEG(unset);
279 d->use_editor = 1;
280 return parse_reuse_arg(opt, arg, unset);
283 static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
285 struct strbuf buf = STRBUF_INIT;
286 struct notes_rewrite_cfg *c = NULL;
287 struct notes_tree *t = NULL;
288 int ret = 0;
289 const char *msg = "Notes added by 'git notes copy'";
291 if (rewrite_cmd) {
292 c = init_copy_notes_for_rewrite(rewrite_cmd);
293 if (!c)
294 return 0;
295 } else {
296 init_notes(NULL, NULL, NULL, NOTES_INIT_WRITABLE);
297 t = &default_notes_tree;
300 while (strbuf_getline_lf(&buf, stdin) != EOF) {
301 struct object_id from_obj, to_obj;
302 struct strbuf **split;
303 int err;
305 split = strbuf_split(&buf, ' ');
306 if (!split[0] || !split[1])
307 die(_("malformed input line: '%s'."), buf.buf);
308 strbuf_rtrim(split[0]);
309 strbuf_rtrim(split[1]);
310 if (get_oid(split[0]->buf, &from_obj))
311 die(_("failed to resolve '%s' as a valid ref."), split[0]->buf);
312 if (get_oid(split[1]->buf, &to_obj))
313 die(_("failed to resolve '%s' as a valid ref."), split[1]->buf);
315 if (rewrite_cmd)
316 err = copy_note_for_rewrite(c, &from_obj, &to_obj);
317 else
318 err = copy_note(t, &from_obj, &to_obj, force,
319 combine_notes_overwrite);
321 if (err) {
322 error(_("failed to copy notes from '%s' to '%s'"),
323 split[0]->buf, split[1]->buf);
324 ret = 1;
327 strbuf_list_free(split);
330 if (!rewrite_cmd) {
331 commit_notes(the_repository, t, msg);
332 free_notes(t);
333 } else {
334 finish_copy_notes_for_rewrite(the_repository, c, msg);
336 strbuf_release(&buf);
337 return ret;
340 static struct notes_tree *init_notes_check(const char *subcommand,
341 int flags)
343 struct notes_tree *t;
344 const char *ref;
345 init_notes(NULL, NULL, NULL, flags);
346 t = &default_notes_tree;
348 ref = (flags & NOTES_INIT_WRITABLE) ? t->update_ref : t->ref;
349 if (!starts_with(ref, "refs/notes/"))
351 * TRANSLATORS: the first %s will be replaced by a git
352 * notes command: 'add', 'merge', 'remove', etc.
354 die(_("refusing to %s notes in %s (outside of refs/notes/)"),
355 subcommand, ref);
356 return t;
359 static int list(int argc, const char **argv, const char *prefix)
361 struct notes_tree *t;
362 struct object_id object;
363 const struct object_id *note;
364 int retval = -1;
365 struct option options[] = {
366 OPT_END()
369 if (argc)
370 argc = parse_options(argc, argv, prefix, options,
371 git_notes_list_usage, 0);
373 if (1 < argc) {
374 error(_("too many arguments"));
375 usage_with_options(git_notes_list_usage, options);
378 t = init_notes_check("list", 0);
379 if (argc) {
380 if (get_oid(argv[0], &object))
381 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
382 note = get_note(t, &object);
383 if (note) {
384 puts(oid_to_hex(note));
385 retval = 0;
386 } else
387 retval = error(_("no note found for object %s."),
388 oid_to_hex(&object));
389 } else
390 retval = for_each_note(t, 0, list_each_note, NULL);
392 free_notes(t);
393 return retval;
396 static int append_edit(int argc, const char **argv, const char *prefix);
398 static int add(int argc, const char **argv, const char *prefix)
400 int force = 0, allow_empty = 0;
401 const char *object_ref;
402 struct notes_tree *t;
403 struct object_id object, new_note;
404 const struct object_id *note;
405 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
406 struct option options[] = {
407 OPT_CALLBACK_F('m', "message", &d, N_("message"),
408 N_("note contents as a string"), PARSE_OPT_NONEG,
409 parse_msg_arg),
410 OPT_CALLBACK_F('F', "file", &d, N_("file"),
411 N_("note contents in a file"), PARSE_OPT_NONEG,
412 parse_file_arg),
413 OPT_CALLBACK_F('c', "reedit-message", &d, N_("object"),
414 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
415 parse_reedit_arg),
416 OPT_CALLBACK_F('C', "reuse-message", &d, N_("object"),
417 N_("reuse specified note object"), PARSE_OPT_NONEG,
418 parse_reuse_arg),
419 OPT_BOOL(0, "allow-empty", &allow_empty,
420 N_("allow storing empty note")),
421 OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE),
422 OPT_END()
425 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
426 PARSE_OPT_KEEP_ARGV0);
428 if (2 < argc) {
429 error(_("too many arguments"));
430 usage_with_options(git_notes_add_usage, options);
433 object_ref = argc > 1 ? argv[1] : "HEAD";
435 if (get_oid(object_ref, &object))
436 die(_("failed to resolve '%s' as a valid ref."), object_ref);
438 t = init_notes_check("add", NOTES_INIT_WRITABLE);
439 note = get_note(t, &object);
441 if (note) {
442 if (!force) {
443 free_notes(t);
444 if (d.given) {
445 free_note_data(&d);
446 return error(_("Cannot add notes. "
447 "Found existing notes for object %s. "
448 "Use '-f' to overwrite existing notes"),
449 oid_to_hex(&object));
452 * Redirect to "edit" subcommand.
454 * We only end up here if none of -m/-F/-c/-C or -f are
455 * given. The original args are therefore still in
456 * argv[0-1].
458 argv[0] = "edit";
459 return append_edit(argc, argv, prefix);
461 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
462 oid_to_hex(&object));
465 prepare_note_data(&object, &d, note);
466 if (d.buf.len || allow_empty) {
467 write_note_data(&d, &new_note);
468 if (add_note(t, &object, &new_note, combine_notes_overwrite))
469 BUG("combine_notes_overwrite failed");
470 commit_notes(the_repository, t,
471 "Notes added by 'git notes add'");
472 } else {
473 fprintf(stderr, _("Removing note for object %s\n"),
474 oid_to_hex(&object));
475 remove_note(t, object.hash);
476 commit_notes(the_repository, t,
477 "Notes removed by 'git notes add'");
480 free_note_data(&d);
481 free_notes(t);
482 return 0;
485 static int copy(int argc, const char **argv, const char *prefix)
487 int retval = 0, force = 0, from_stdin = 0;
488 const struct object_id *from_note, *note;
489 const char *object_ref;
490 struct object_id object, from_obj;
491 struct notes_tree *t;
492 const char *rewrite_cmd = NULL;
493 struct option options[] = {
494 OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE),
495 OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")),
496 OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"),
497 N_("load rewriting config for <command> (implies "
498 "--stdin)")),
499 OPT_END()
502 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
505 if (from_stdin || rewrite_cmd) {
506 if (argc) {
507 error(_("too many arguments"));
508 usage_with_options(git_notes_copy_usage, options);
509 } else {
510 return notes_copy_from_stdin(force, rewrite_cmd);
514 if (argc < 1) {
515 error(_("too few arguments"));
516 usage_with_options(git_notes_copy_usage, options);
518 if (2 < argc) {
519 error(_("too many arguments"));
520 usage_with_options(git_notes_copy_usage, options);
523 if (get_oid(argv[0], &from_obj))
524 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
526 object_ref = 1 < argc ? argv[1] : "HEAD";
528 if (get_oid(object_ref, &object))
529 die(_("failed to resolve '%s' as a valid ref."), object_ref);
531 t = init_notes_check("copy", NOTES_INIT_WRITABLE);
532 note = get_note(t, &object);
534 if (note) {
535 if (!force) {
536 retval = error(_("Cannot copy notes. Found existing "
537 "notes for object %s. Use '-f' to "
538 "overwrite existing notes"),
539 oid_to_hex(&object));
540 goto out;
542 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
543 oid_to_hex(&object));
546 from_note = get_note(t, &from_obj);
547 if (!from_note) {
548 retval = error(_("missing notes on source object %s. Cannot "
549 "copy."), oid_to_hex(&from_obj));
550 goto out;
553 if (add_note(t, &object, from_note, combine_notes_overwrite))
554 BUG("combine_notes_overwrite failed");
555 commit_notes(the_repository, t,
556 "Notes added by 'git notes copy'");
557 out:
558 free_notes(t);
559 return retval;
562 static int append_edit(int argc, const char **argv, const char *prefix)
564 int allow_empty = 0;
565 const char *object_ref;
566 struct notes_tree *t;
567 struct object_id object, new_note;
568 const struct object_id *note;
569 char *logmsg;
570 const char * const *usage;
571 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
572 struct option options[] = {
573 OPT_CALLBACK_F('m', "message", &d, N_("message"),
574 N_("note contents as a string"), PARSE_OPT_NONEG,
575 parse_msg_arg),
576 OPT_CALLBACK_F('F', "file", &d, N_("file"),
577 N_("note contents in a file"), PARSE_OPT_NONEG,
578 parse_file_arg),
579 OPT_CALLBACK_F('c', "reedit-message", &d, N_("object"),
580 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
581 parse_reedit_arg),
582 OPT_CALLBACK_F('C', "reuse-message", &d, N_("object"),
583 N_("reuse specified note object"), PARSE_OPT_NONEG,
584 parse_reuse_arg),
585 OPT_BOOL(0, "allow-empty", &allow_empty,
586 N_("allow storing empty note")),
587 OPT_END()
589 int edit = !strcmp(argv[0], "edit");
591 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
592 argc = parse_options(argc, argv, prefix, options, usage,
593 PARSE_OPT_KEEP_ARGV0);
595 if (2 < argc) {
596 error(_("too many arguments"));
597 usage_with_options(usage, options);
600 if (d.given && edit)
601 fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
602 "for the 'edit' subcommand.\n"
603 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
605 object_ref = 1 < argc ? argv[1] : "HEAD";
607 if (get_oid(object_ref, &object))
608 die(_("failed to resolve '%s' as a valid ref."), object_ref);
610 t = init_notes_check(argv[0], NOTES_INIT_WRITABLE);
611 note = get_note(t, &object);
613 prepare_note_data(&object, &d, edit && note ? note : NULL);
615 if (note && !edit) {
616 /* Append buf to previous note contents */
617 unsigned long size;
618 enum object_type type;
619 char *prev_buf = read_object_file(note, &type, &size);
621 strbuf_grow(&d.buf, size + 1);
622 if (d.buf.len && prev_buf && size)
623 strbuf_insertstr(&d.buf, 0, "\n");
624 if (prev_buf && size)
625 strbuf_insert(&d.buf, 0, prev_buf, size);
626 free(prev_buf);
629 if (d.buf.len || allow_empty) {
630 write_note_data(&d, &new_note);
631 if (add_note(t, &object, &new_note, combine_notes_overwrite))
632 BUG("combine_notes_overwrite failed");
633 logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]);
634 } else {
635 fprintf(stderr, _("Removing note for object %s\n"),
636 oid_to_hex(&object));
637 remove_note(t, object.hash);
638 logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]);
640 commit_notes(the_repository, t, logmsg);
642 free(logmsg);
643 free_note_data(&d);
644 free_notes(t);
645 return 0;
648 static int show(int argc, const char **argv, const char *prefix)
650 const char *object_ref;
651 struct notes_tree *t;
652 struct object_id object;
653 const struct object_id *note;
654 int retval;
655 struct option options[] = {
656 OPT_END()
659 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
662 if (1 < argc) {
663 error(_("too many arguments"));
664 usage_with_options(git_notes_show_usage, options);
667 object_ref = argc ? argv[0] : "HEAD";
669 if (get_oid(object_ref, &object))
670 die(_("failed to resolve '%s' as a valid ref."), object_ref);
672 t = init_notes_check("show", 0);
673 note = get_note(t, &object);
675 if (!note)
676 retval = error(_("no note found for object %s."),
677 oid_to_hex(&object));
678 else {
679 const char *show_args[3] = {"show", oid_to_hex(note), NULL};
680 retval = execv_git_cmd(show_args);
682 free_notes(t);
683 return retval;
686 static int merge_abort(struct notes_merge_options *o)
688 int ret = 0;
691 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
692 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
695 if (delete_ref(NULL, "NOTES_MERGE_PARTIAL", NULL, 0))
696 ret += error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
697 if (delete_ref(NULL, "NOTES_MERGE_REF", NULL, REF_NO_DEREF))
698 ret += error(_("failed to delete ref NOTES_MERGE_REF"));
699 if (notes_merge_abort(o))
700 ret += error(_("failed to remove 'git notes merge' worktree"));
701 return ret;
704 static int merge_commit(struct notes_merge_options *o)
706 struct strbuf msg = STRBUF_INIT;
707 struct object_id oid, parent_oid;
708 struct notes_tree *t;
709 struct commit *partial;
710 struct pretty_print_context pretty_ctx;
711 void *local_ref_to_free;
712 int ret;
715 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
716 * and target notes ref from .git/NOTES_MERGE_REF.
719 if (get_oid("NOTES_MERGE_PARTIAL", &oid))
720 die(_("failed to read ref NOTES_MERGE_PARTIAL"));
721 else if (!(partial = lookup_commit_reference(the_repository, &oid)))
722 die(_("could not find commit from NOTES_MERGE_PARTIAL."));
723 else if (parse_commit(partial))
724 die(_("could not parse commit from NOTES_MERGE_PARTIAL."));
726 if (partial->parents)
727 oidcpy(&parent_oid, &partial->parents->item->object.oid);
728 else
729 oidclr(&parent_oid);
731 CALLOC_ARRAY(t, 1);
732 init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
734 o->local_ref = local_ref_to_free =
735 resolve_refdup("NOTES_MERGE_REF", 0, &oid, NULL);
736 if (!o->local_ref)
737 die(_("failed to resolve NOTES_MERGE_REF"));
739 if (notes_merge_commit(o, t, partial, &oid))
740 die(_("failed to finalize notes merge"));
742 /* Reuse existing commit message in reflog message */
743 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
744 format_commit_message(partial, "%s", &msg, &pretty_ctx);
745 strbuf_trim(&msg);
746 strbuf_insertstr(&msg, 0, "notes: ");
747 update_ref(msg.buf, o->local_ref, &oid,
748 is_null_oid(&parent_oid) ? NULL : &parent_oid,
749 0, UPDATE_REFS_DIE_ON_ERR);
751 free_notes(t);
752 strbuf_release(&msg);
753 ret = merge_abort(o);
754 free(local_ref_to_free);
755 return ret;
758 static int git_config_get_notes_strategy(const char *key,
759 enum notes_merge_strategy *strategy)
761 char *value;
763 if (git_config_get_string(key, &value))
764 return 1;
765 if (parse_notes_merge_strategy(value, strategy))
766 git_die_config(key, _("unknown notes merge strategy %s"), value);
768 free(value);
769 return 0;
772 static int merge(int argc, const char **argv, const char *prefix)
774 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
775 struct object_id result_oid;
776 struct notes_tree *t;
777 struct notes_merge_options o;
778 int do_merge = 0, do_commit = 0, do_abort = 0;
779 int verbosity = 0, result;
780 const char *strategy = NULL;
781 struct option options[] = {
782 OPT_GROUP(N_("General options")),
783 OPT__VERBOSITY(&verbosity),
784 OPT_GROUP(N_("Merge options")),
785 OPT_STRING('s', "strategy", &strategy, N_("strategy"),
786 N_("resolve notes conflicts using the given strategy "
787 "(manual/ours/theirs/union/cat_sort_uniq)")),
788 OPT_GROUP(N_("Committing unmerged notes")),
789 OPT_SET_INT_F(0, "commit", &do_commit,
790 N_("finalize notes merge by committing unmerged notes"),
791 1, PARSE_OPT_NONEG),
792 OPT_GROUP(N_("Aborting notes merge resolution")),
793 OPT_SET_INT_F(0, "abort", &do_abort,
794 N_("abort notes merge"),
795 1, PARSE_OPT_NONEG),
796 OPT_END()
799 argc = parse_options(argc, argv, prefix, options,
800 git_notes_merge_usage, 0);
802 if (strategy || do_commit + do_abort == 0)
803 do_merge = 1;
804 if (do_merge + do_commit + do_abort != 1) {
805 error(_("cannot mix --commit, --abort or -s/--strategy"));
806 usage_with_options(git_notes_merge_usage, options);
809 if (do_merge && argc != 1) {
810 error(_("must specify a notes ref to merge"));
811 usage_with_options(git_notes_merge_usage, options);
812 } else if (!do_merge && argc) {
813 error(_("too many arguments"));
814 usage_with_options(git_notes_merge_usage, options);
817 init_notes_merge_options(the_repository, &o);
818 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
820 if (do_abort)
821 return merge_abort(&o);
822 if (do_commit)
823 return merge_commit(&o);
825 o.local_ref = default_notes_ref();
826 strbuf_addstr(&remote_ref, argv[0]);
827 expand_loose_notes_ref(&remote_ref);
828 o.remote_ref = remote_ref.buf;
830 t = init_notes_check("merge", NOTES_INIT_WRITABLE);
832 if (strategy) {
833 if (parse_notes_merge_strategy(strategy, &o.strategy)) {
834 error(_("unknown -s/--strategy: %s"), strategy);
835 usage_with_options(git_notes_merge_usage, options);
837 } else {
838 struct strbuf merge_key = STRBUF_INIT;
839 const char *short_ref = NULL;
841 if (!skip_prefix(o.local_ref, "refs/notes/", &short_ref))
842 BUG("local ref %s is outside of refs/notes/",
843 o.local_ref);
845 strbuf_addf(&merge_key, "notes.%s.mergeStrategy", short_ref);
847 if (git_config_get_notes_strategy(merge_key.buf, &o.strategy))
848 git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy);
850 strbuf_release(&merge_key);
853 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
854 remote_ref.buf, default_notes_ref());
855 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
857 result = notes_merge(&o, t, &result_oid);
859 if (result >= 0) /* Merge resulted (trivially) in result_oid */
860 /* Update default notes ref with new commit */
861 update_ref(msg.buf, default_notes_ref(), &result_oid, NULL, 0,
862 UPDATE_REFS_DIE_ON_ERR);
863 else { /* Merge has unresolved conflicts */
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 wt = find_shared_symref("NOTES_MERGE_REF", default_notes_ref());
870 if (wt)
871 die(_("a notes merge into %s is already in-progress at %s"),
872 default_notes_ref(), wt->path);
873 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
874 die(_("failed to store link to current notes ref (%s)"),
875 default_notes_ref());
876 fprintf(stderr, _("Automatic notes merge failed. Fix conflicts in %s "
877 "and commit the result with 'git notes merge --commit', "
878 "or abort the merge with 'git notes merge --abort'.\n"),
879 git_path(NOTES_MERGE_WORKTREE));
882 free_notes(t);
883 strbuf_release(&remote_ref);
884 strbuf_release(&msg);
885 return result < 0; /* return non-zero on conflicts */
888 #define IGNORE_MISSING 1
890 static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
892 int status;
893 struct object_id oid;
894 if (get_oid(name, &oid))
895 return error(_("Failed to resolve '%s' as a valid ref."), name);
896 status = remove_note(t, oid.hash);
897 if (status)
898 fprintf(stderr, _("Object %s has no note\n"), name);
899 else
900 fprintf(stderr, _("Removing note for object %s\n"), name);
901 return (flag & IGNORE_MISSING) ? 0 : status;
904 static int remove_cmd(int argc, const char **argv, const char *prefix)
906 unsigned flag = 0;
907 int from_stdin = 0;
908 struct option options[] = {
909 OPT_BIT(0, "ignore-missing", &flag,
910 N_("attempt to remove non-existent note is not an error"),
911 IGNORE_MISSING),
912 OPT_BOOL(0, "stdin", &from_stdin,
913 N_("read object names from the standard input")),
914 OPT_END()
916 struct notes_tree *t;
917 int retval = 0;
919 argc = parse_options(argc, argv, prefix, options,
920 git_notes_remove_usage, 0);
922 t = init_notes_check("remove", NOTES_INIT_WRITABLE);
924 if (!argc && !from_stdin) {
925 retval = remove_one_note(t, "HEAD", flag);
926 } else {
927 while (*argv) {
928 retval |= remove_one_note(t, *argv, flag);
929 argv++;
932 if (from_stdin) {
933 struct strbuf sb = STRBUF_INIT;
934 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
935 strbuf_rtrim(&sb);
936 retval |= remove_one_note(t, sb.buf, flag);
938 strbuf_release(&sb);
940 if (!retval)
941 commit_notes(the_repository, t,
942 "Notes removed by 'git notes remove'");
943 free_notes(t);
944 return retval;
947 static int prune(int argc, const char **argv, const char *prefix)
949 struct notes_tree *t;
950 int show_only = 0, verbose = 0;
951 struct option options[] = {
952 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
953 OPT__VERBOSE(&verbose, N_("report pruned notes")),
954 OPT_END()
957 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
960 if (argc) {
961 error(_("too many arguments"));
962 usage_with_options(git_notes_prune_usage, options);
965 t = init_notes_check("prune", NOTES_INIT_WRITABLE);
967 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
968 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
969 if (!show_only)
970 commit_notes(the_repository, t,
971 "Notes removed by 'git notes prune'");
972 free_notes(t);
973 return 0;
976 static int get_ref(int argc, const char **argv, const char *prefix)
978 struct option options[] = { OPT_END() };
979 argc = parse_options(argc, argv, prefix, options,
980 git_notes_get_ref_usage, 0);
982 if (argc) {
983 error(_("too many arguments"));
984 usage_with_options(git_notes_get_ref_usage, options);
987 puts(default_notes_ref());
988 return 0;
991 int cmd_notes(int argc, const char **argv, const char *prefix)
993 int result;
994 const char *override_notes_ref = NULL;
995 struct option options[] = {
996 OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
997 N_("use notes from <notes-ref>")),
998 OPT_END()
1001 git_config(git_default_config, NULL);
1002 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
1003 PARSE_OPT_STOP_AT_NON_OPTION);
1005 if (override_notes_ref) {
1006 struct strbuf sb = STRBUF_INIT;
1007 strbuf_addstr(&sb, override_notes_ref);
1008 expand_notes_ref(&sb);
1009 setenv("GIT_NOTES_REF", sb.buf, 1);
1010 strbuf_release(&sb);
1013 if (argc < 1 || !strcmp(argv[0], "list"))
1014 result = list(argc, argv, prefix);
1015 else if (!strcmp(argv[0], "add"))
1016 result = add(argc, argv, prefix);
1017 else if (!strcmp(argv[0], "copy"))
1018 result = copy(argc, argv, prefix);
1019 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
1020 result = append_edit(argc, argv, prefix);
1021 else if (!strcmp(argv[0], "show"))
1022 result = show(argc, argv, prefix);
1023 else if (!strcmp(argv[0], "merge"))
1024 result = merge(argc, argv, prefix);
1025 else if (!strcmp(argv[0], "remove"))
1026 result = remove_cmd(argc, argv, prefix);
1027 else if (!strcmp(argv[0], "prune"))
1028 result = prune(argc, argv, prefix);
1029 else if (!strcmp(argv[0], "get-ref"))
1030 result = get_ref(argc, argv, prefix);
1031 else {
1032 result = error(_("unknown subcommand: %s"), argv[0]);
1033 usage_with_options(git_notes_usage, options);
1036 return result ? 1 : 0;