doc: remove unsupported parameter from patch-id
[git/debian.git] / builtin / notes.c
blobf848b896927a34e884f8377ea9c98271237be5fc
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 "builtin.h"
12 #include "notes.h"
13 #include "blob.h"
14 #include "commit.h"
15 #include "refs.h"
16 #include "exec_cmd.h"
17 #include "run-command.h"
18 #include "parse-options.h"
19 #include "string-list.h"
20 #include "notes-merge.h"
21 #include "notes-utils.h"
22 #include "worktree.h"
24 static const char * const git_notes_usage[] = {
25 N_("git notes [--ref <notes-ref>] [list [<object>]]"),
26 N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
27 N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"),
28 N_("git notes [--ref <notes-ref>] append [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
29 N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
30 N_("git notes [--ref <notes-ref>] show [<object>]"),
31 N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"),
32 N_("git notes merge --commit [-v | -q]"),
33 N_("git notes merge --abort [-v | -q]"),
34 N_("git notes [--ref <notes-ref>] remove [<object>...]"),
35 N_("git notes [--ref <notes-ref>] prune [-n | -v]"),
36 N_("git notes [--ref <notes-ref>] get-ref"),
37 NULL
40 static const char * const git_notes_list_usage[] = {
41 N_("git notes [list [<object>]]"),
42 NULL
45 static const char * const git_notes_add_usage[] = {
46 N_("git notes add [<options>] [<object>]"),
47 NULL
50 static const char * const git_notes_copy_usage[] = {
51 N_("git notes copy [<options>] <from-object> <to-object>"),
52 N_("git notes copy --stdin [<from-object> <to-object>]..."),
53 NULL
56 static const char * const git_notes_append_usage[] = {
57 N_("git notes append [<options>] [<object>]"),
58 NULL
61 static const char * const git_notes_edit_usage[] = {
62 N_("git notes edit [<object>]"),
63 NULL
66 static const char * const git_notes_show_usage[] = {
67 N_("git notes show [<object>]"),
68 NULL
71 static const char * const git_notes_merge_usage[] = {
72 N_("git notes merge [<options>] <notes-ref>"),
73 N_("git notes merge --commit [<options>]"),
74 N_("git notes merge --abort [<options>]"),
75 NULL
78 static const char * const git_notes_remove_usage[] = {
79 N_("git notes remove [<object>]"),
80 NULL
83 static const char * const git_notes_prune_usage[] = {
84 N_("git notes prune [<options>]"),
85 NULL
88 static const char * const git_notes_get_ref_usage[] = {
89 N_("git notes get-ref"),
90 NULL
93 static const char note_template[] =
94 N_("Write/edit the notes for the following object:");
96 struct note_data {
97 int given;
98 int use_editor;
99 char *edit_path;
100 struct strbuf buf;
103 static void free_note_data(struct note_data *d)
105 if (d->edit_path) {
106 unlink_or_warn(d->edit_path);
107 free(d->edit_path);
109 strbuf_release(&d->buf);
112 static int list_each_note(const unsigned char *object_sha1,
113 const unsigned char *note_sha1, char *note_path,
114 void *cb_data)
116 printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
117 return 0;
120 static void copy_obj_to_fd(int fd, const unsigned char *sha1)
122 unsigned long size;
123 enum object_type type;
124 char *buf = read_sha1_file(sha1, &type, &size);
125 if (buf) {
126 if (size)
127 write_or_die(fd, buf, size);
128 free(buf);
132 static void write_commented_object(int fd, const unsigned char *object)
134 const char *show_args[5] =
135 {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
136 struct child_process show = CHILD_PROCESS_INIT;
137 struct strbuf buf = STRBUF_INIT;
138 struct strbuf cbuf = STRBUF_INIT;
140 /* Invoke "git show --stat --no-notes $object" */
141 show.argv = show_args;
142 show.no_stdin = 1;
143 show.out = -1;
144 show.err = 0;
145 show.git_cmd = 1;
146 if (start_command(&show))
147 die(_("unable to start 'show' for object '%s'"),
148 sha1_to_hex(object));
150 if (strbuf_read(&buf, show.out, 0) < 0)
151 die_errno(_("could not read 'show' output"));
152 strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
153 write_or_die(fd, cbuf.buf, cbuf.len);
155 strbuf_release(&cbuf);
156 strbuf_release(&buf);
158 if (finish_command(&show))
159 die(_("failed to finish 'show' for object '%s'"),
160 sha1_to_hex(object));
163 static void prepare_note_data(const unsigned char *object, struct note_data *d,
164 const unsigned char *old_note)
166 if (d->use_editor || !d->given) {
167 int fd;
168 struct strbuf buf = STRBUF_INIT;
170 /* write the template message before editing: */
171 d->edit_path = git_pathdup("NOTES_EDITMSG");
172 fd = open(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
173 if (fd < 0)
174 die_errno(_("could not create file '%s'"), d->edit_path);
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, unsigned char *sha1)
202 if (write_sha1_file(d->buf.buf, d->buf.len, blob_type, sha1)) {
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 strbuf_grow(&d->buf, strlen(arg) + 2);
216 if (d->buf.len)
217 strbuf_addch(&d->buf, '\n');
218 strbuf_addstr(&d->buf, arg);
219 strbuf_stripspace(&d->buf, 0);
221 d->given = 1;
222 return 0;
225 static int parse_file_arg(const struct option *opt, const char *arg, int unset)
227 struct note_data *d = opt->value;
229 if (d->buf.len)
230 strbuf_addch(&d->buf, '\n');
231 if (!strcmp(arg, "-")) {
232 if (strbuf_read(&d->buf, 0, 1024) < 0)
233 die_errno(_("cannot read '%s'"), arg);
234 } else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
235 die_errno(_("could not open or read '%s'"), arg);
236 strbuf_stripspace(&d->buf, 0);
238 d->given = 1;
239 return 0;
242 static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
244 struct note_data *d = opt->value;
245 char *buf;
246 unsigned char object[20];
247 enum object_type type;
248 unsigned long len;
250 if (d->buf.len)
251 strbuf_addch(&d->buf, '\n');
253 if (get_sha1(arg, object))
254 die(_("Failed to resolve '%s' as a valid ref."), arg);
255 if (!(buf = read_sha1_file(object, &type, &len))) {
256 free(buf);
257 die(_("Failed to read object '%s'."), arg);
259 if (type != OBJ_BLOB) {
260 free(buf);
261 die(_("Cannot read note data from non-blob object '%s'."), arg);
263 strbuf_add(&d->buf, buf, len);
264 free(buf);
266 d->given = 1;
267 return 0;
270 static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
272 struct note_data *d = opt->value;
273 d->use_editor = 1;
274 return parse_reuse_arg(opt, arg, unset);
277 static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
279 struct strbuf buf = STRBUF_INIT;
280 struct notes_rewrite_cfg *c = NULL;
281 struct notes_tree *t = NULL;
282 int ret = 0;
283 const char *msg = "Notes added by 'git notes copy'";
285 if (rewrite_cmd) {
286 c = init_copy_notes_for_rewrite(rewrite_cmd);
287 if (!c)
288 return 0;
289 } else {
290 init_notes(NULL, NULL, NULL, NOTES_INIT_WRITABLE);
291 t = &default_notes_tree;
294 while (strbuf_getline_lf(&buf, stdin) != EOF) {
295 unsigned char from_obj[20], to_obj[20];
296 struct strbuf **split;
297 int err;
299 split = strbuf_split(&buf, ' ');
300 if (!split[0] || !split[1])
301 die(_("Malformed input line: '%s'."), buf.buf);
302 strbuf_rtrim(split[0]);
303 strbuf_rtrim(split[1]);
304 if (get_sha1(split[0]->buf, from_obj))
305 die(_("Failed to resolve '%s' as a valid ref."), split[0]->buf);
306 if (get_sha1(split[1]->buf, to_obj))
307 die(_("Failed to resolve '%s' as a valid ref."), split[1]->buf);
309 if (rewrite_cmd)
310 err = copy_note_for_rewrite(c, from_obj, to_obj);
311 else
312 err = copy_note(t, from_obj, to_obj, force,
313 combine_notes_overwrite);
315 if (err) {
316 error(_("Failed to copy notes from '%s' to '%s'"),
317 split[0]->buf, split[1]->buf);
318 ret = 1;
321 strbuf_list_free(split);
324 if (!rewrite_cmd) {
325 commit_notes(t, msg);
326 free_notes(t);
327 } else {
328 finish_copy_notes_for_rewrite(c, msg);
330 return ret;
333 static struct notes_tree *init_notes_check(const char *subcommand,
334 int flags)
336 struct notes_tree *t;
337 const char *ref;
338 init_notes(NULL, NULL, NULL, flags);
339 t = &default_notes_tree;
341 ref = (flags & NOTES_INIT_WRITABLE) ? t->update_ref : t->ref;
342 if (!starts_with(ref, "refs/notes/"))
343 die("Refusing to %s notes in %s (outside of refs/notes/)",
344 subcommand, ref);
345 return t;
348 static int list(int argc, const char **argv, const char *prefix)
350 struct notes_tree *t;
351 unsigned char object[20];
352 const unsigned char *note;
353 int retval = -1;
354 struct option options[] = {
355 OPT_END()
358 if (argc)
359 argc = parse_options(argc, argv, prefix, options,
360 git_notes_list_usage, 0);
362 if (1 < argc) {
363 error(_("too many parameters"));
364 usage_with_options(git_notes_list_usage, options);
367 t = init_notes_check("list", 0);
368 if (argc) {
369 if (get_sha1(argv[0], object))
370 die(_("Failed to resolve '%s' as a valid ref."), argv[0]);
371 note = get_note(t, object);
372 if (note) {
373 puts(sha1_to_hex(note));
374 retval = 0;
375 } else
376 retval = error(_("No note found for object %s."),
377 sha1_to_hex(object));
378 } else
379 retval = for_each_note(t, 0, list_each_note, NULL);
381 free_notes(t);
382 return retval;
385 static int append_edit(int argc, const char **argv, const char *prefix);
387 static int add(int argc, const char **argv, const char *prefix)
389 int force = 0, allow_empty = 0;
390 const char *object_ref;
391 struct notes_tree *t;
392 unsigned char object[20], new_note[20];
393 const unsigned char *note;
394 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
395 struct option options[] = {
396 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
397 N_("note contents as a string"), PARSE_OPT_NONEG,
398 parse_msg_arg},
399 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
400 N_("note contents in a file"), PARSE_OPT_NONEG,
401 parse_file_arg},
402 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
403 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
404 parse_reedit_arg},
405 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
406 N_("reuse specified note object"), PARSE_OPT_NONEG,
407 parse_reuse_arg},
408 OPT_BOOL(0, "allow-empty", &allow_empty,
409 N_("allow storing empty note")),
410 OPT__FORCE(&force, N_("replace existing notes")),
411 OPT_END()
414 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
415 PARSE_OPT_KEEP_ARGV0);
417 if (2 < argc) {
418 error(_("too many parameters"));
419 usage_with_options(git_notes_add_usage, options);
422 object_ref = argc > 1 ? argv[1] : "HEAD";
424 if (get_sha1(object_ref, object))
425 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
427 t = init_notes_check("add", NOTES_INIT_WRITABLE);
428 note = get_note(t, object);
430 if (note) {
431 if (!force) {
432 free_notes(t);
433 if (d.given) {
434 free_note_data(&d);
435 return error(_("Cannot add notes. "
436 "Found existing notes for object %s. "
437 "Use '-f' to overwrite existing notes"),
438 sha1_to_hex(object));
441 * Redirect to "edit" subcommand.
443 * We only end up here if none of -m/-F/-c/-C or -f are
444 * given. The original args are therefore still in
445 * argv[0-1].
447 argv[0] = "edit";
448 return append_edit(argc, argv, prefix);
450 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
451 sha1_to_hex(object));
454 prepare_note_data(object, &d, note);
455 if (d.buf.len || allow_empty) {
456 write_note_data(&d, new_note);
457 if (add_note(t, object, new_note, combine_notes_overwrite))
458 die("BUG: combine_notes_overwrite failed");
459 commit_notes(t, "Notes added by 'git notes add'");
460 } else {
461 fprintf(stderr, _("Removing note for object %s\n"),
462 sha1_to_hex(object));
463 remove_note(t, object);
464 commit_notes(t, "Notes removed by 'git notes add'");
467 free_note_data(&d);
468 free_notes(t);
469 return 0;
472 static int copy(int argc, const char **argv, const char *prefix)
474 int retval = 0, force = 0, from_stdin = 0;
475 const unsigned char *from_note, *note;
476 const char *object_ref;
477 unsigned char object[20], from_obj[20];
478 struct notes_tree *t;
479 const char *rewrite_cmd = NULL;
480 struct option options[] = {
481 OPT__FORCE(&force, N_("replace existing notes")),
482 OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")),
483 OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"),
484 N_("load rewriting config for <command> (implies "
485 "--stdin)")),
486 OPT_END()
489 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
492 if (from_stdin || rewrite_cmd) {
493 if (argc) {
494 error(_("too many parameters"));
495 usage_with_options(git_notes_copy_usage, options);
496 } else {
497 return notes_copy_from_stdin(force, rewrite_cmd);
501 if (argc < 2) {
502 error(_("too few parameters"));
503 usage_with_options(git_notes_copy_usage, options);
505 if (2 < argc) {
506 error(_("too many parameters"));
507 usage_with_options(git_notes_copy_usage, options);
510 if (get_sha1(argv[0], from_obj))
511 die(_("Failed to resolve '%s' as a valid ref."), argv[0]);
513 object_ref = 1 < argc ? argv[1] : "HEAD";
515 if (get_sha1(object_ref, object))
516 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
518 t = init_notes_check("copy", NOTES_INIT_WRITABLE);
519 note = get_note(t, object);
521 if (note) {
522 if (!force) {
523 retval = error(_("Cannot copy notes. Found existing "
524 "notes for object %s. Use '-f' to "
525 "overwrite existing notes"),
526 sha1_to_hex(object));
527 goto out;
529 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
530 sha1_to_hex(object));
533 from_note = get_note(t, from_obj);
534 if (!from_note) {
535 retval = error(_("Missing notes on source object %s. Cannot "
536 "copy."), sha1_to_hex(from_obj));
537 goto out;
540 if (add_note(t, object, from_note, combine_notes_overwrite))
541 die("BUG: combine_notes_overwrite failed");
542 commit_notes(t, "Notes added by 'git notes copy'");
543 out:
544 free_notes(t);
545 return retval;
548 static int append_edit(int argc, const char **argv, const char *prefix)
550 int allow_empty = 0;
551 const char *object_ref;
552 struct notes_tree *t;
553 unsigned char object[20], new_note[20];
554 const unsigned char *note;
555 char logmsg[100];
556 const char * const *usage;
557 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
558 struct option options[] = {
559 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
560 N_("note contents as a string"), PARSE_OPT_NONEG,
561 parse_msg_arg},
562 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
563 N_("note contents in a file"), PARSE_OPT_NONEG,
564 parse_file_arg},
565 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
566 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
567 parse_reedit_arg},
568 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
569 N_("reuse specified note object"), PARSE_OPT_NONEG,
570 parse_reuse_arg},
571 OPT_BOOL(0, "allow-empty", &allow_empty,
572 N_("allow storing empty note")),
573 OPT_END()
575 int edit = !strcmp(argv[0], "edit");
577 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
578 argc = parse_options(argc, argv, prefix, options, usage,
579 PARSE_OPT_KEEP_ARGV0);
581 if (2 < argc) {
582 error(_("too many parameters"));
583 usage_with_options(usage, options);
586 if (d.given && edit)
587 fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
588 "for the 'edit' subcommand.\n"
589 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
591 object_ref = 1 < argc ? argv[1] : "HEAD";
593 if (get_sha1(object_ref, object))
594 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
596 t = init_notes_check(argv[0], NOTES_INIT_WRITABLE);
597 note = get_note(t, object);
599 prepare_note_data(object, &d, edit ? note : NULL);
601 if (note && !edit) {
602 /* Append buf to previous note contents */
603 unsigned long size;
604 enum object_type type;
605 char *prev_buf = read_sha1_file(note, &type, &size);
607 strbuf_grow(&d.buf, size + 1);
608 if (d.buf.len && prev_buf && size)
609 strbuf_insert(&d.buf, 0, "\n", 1);
610 if (prev_buf && size)
611 strbuf_insert(&d.buf, 0, prev_buf, size);
612 free(prev_buf);
615 if (d.buf.len || allow_empty) {
616 write_note_data(&d, new_note);
617 if (add_note(t, object, new_note, combine_notes_overwrite))
618 die("BUG: combine_notes_overwrite failed");
619 snprintf(logmsg, sizeof(logmsg), "Notes added by 'git notes %s'",
620 argv[0]);
621 } else {
622 fprintf(stderr, _("Removing note for object %s\n"),
623 sha1_to_hex(object));
624 remove_note(t, object);
625 snprintf(logmsg, sizeof(logmsg), "Notes removed by 'git notes %s'",
626 argv[0]);
628 commit_notes(t, logmsg);
630 free_note_data(&d);
631 free_notes(t);
632 return 0;
635 static int show(int argc, const char **argv, const char *prefix)
637 const char *object_ref;
638 struct notes_tree *t;
639 unsigned char object[20];
640 const unsigned char *note;
641 int retval;
642 struct option options[] = {
643 OPT_END()
646 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
649 if (1 < argc) {
650 error(_("too many parameters"));
651 usage_with_options(git_notes_show_usage, options);
654 object_ref = argc ? argv[0] : "HEAD";
656 if (get_sha1(object_ref, object))
657 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
659 t = init_notes_check("show", 0);
660 note = get_note(t, object);
662 if (!note)
663 retval = error(_("No note found for object %s."),
664 sha1_to_hex(object));
665 else {
666 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
667 retval = execv_git_cmd(show_args);
669 free_notes(t);
670 return retval;
673 static int merge_abort(struct notes_merge_options *o)
675 int ret = 0;
678 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
679 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
682 if (delete_ref("NOTES_MERGE_PARTIAL", NULL, 0))
683 ret += error("Failed to delete ref NOTES_MERGE_PARTIAL");
684 if (delete_ref("NOTES_MERGE_REF", NULL, REF_NODEREF))
685 ret += error("Failed to delete ref NOTES_MERGE_REF");
686 if (notes_merge_abort(o))
687 ret += error("Failed to remove 'git notes merge' worktree");
688 return ret;
691 static int merge_commit(struct notes_merge_options *o)
693 struct strbuf msg = STRBUF_INIT;
694 unsigned char sha1[20], parent_sha1[20];
695 struct notes_tree *t;
696 struct commit *partial;
697 struct pretty_print_context pretty_ctx;
698 void *local_ref_to_free;
699 int ret;
702 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
703 * and target notes ref from .git/NOTES_MERGE_REF.
706 if (get_sha1("NOTES_MERGE_PARTIAL", sha1))
707 die("Failed to read ref NOTES_MERGE_PARTIAL");
708 else if (!(partial = lookup_commit_reference(sha1)))
709 die("Could not find commit from NOTES_MERGE_PARTIAL.");
710 else if (parse_commit(partial))
711 die("Could not parse commit from NOTES_MERGE_PARTIAL.");
713 if (partial->parents)
714 hashcpy(parent_sha1, partial->parents->item->object.oid.hash);
715 else
716 hashclr(parent_sha1);
718 t = xcalloc(1, sizeof(struct notes_tree));
719 init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
721 o->local_ref = local_ref_to_free =
722 resolve_refdup("NOTES_MERGE_REF", 0, sha1, NULL);
723 if (!o->local_ref)
724 die("Failed to resolve NOTES_MERGE_REF");
726 if (notes_merge_commit(o, t, partial, sha1))
727 die("Failed to finalize notes merge");
729 /* Reuse existing commit message in reflog message */
730 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
731 format_commit_message(partial, "%s", &msg, &pretty_ctx);
732 strbuf_trim(&msg);
733 strbuf_insert(&msg, 0, "notes: ", 7);
734 update_ref(msg.buf, o->local_ref, sha1,
735 is_null_sha1(parent_sha1) ? NULL : parent_sha1,
736 0, UPDATE_REFS_DIE_ON_ERR);
738 free_notes(t);
739 strbuf_release(&msg);
740 ret = merge_abort(o);
741 free(local_ref_to_free);
742 return ret;
745 static int git_config_get_notes_strategy(const char *key,
746 enum notes_merge_strategy *strategy)
748 char *value;
750 if (git_config_get_string(key, &value))
751 return 1;
752 if (parse_notes_merge_strategy(value, strategy))
753 git_die_config(key, _("unknown notes merge strategy %s"), value);
755 free(value);
756 return 0;
759 static int merge(int argc, const char **argv, const char *prefix)
761 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
762 unsigned char result_sha1[20];
763 struct notes_tree *t;
764 struct notes_merge_options o;
765 int do_merge = 0, do_commit = 0, do_abort = 0;
766 int verbosity = 0, result;
767 const char *strategy = NULL;
768 struct option options[] = {
769 OPT_GROUP(N_("General options")),
770 OPT__VERBOSITY(&verbosity),
771 OPT_GROUP(N_("Merge options")),
772 OPT_STRING('s', "strategy", &strategy, N_("strategy"),
773 N_("resolve notes conflicts using the given strategy "
774 "(manual/ours/theirs/union/cat_sort_uniq)")),
775 OPT_GROUP(N_("Committing unmerged notes")),
776 { OPTION_SET_INT, 0, "commit", &do_commit, NULL,
777 N_("finalize notes merge by committing unmerged notes"),
778 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
779 OPT_GROUP(N_("Aborting notes merge resolution")),
780 { OPTION_SET_INT, 0, "abort", &do_abort, NULL,
781 N_("abort notes merge"),
782 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
783 OPT_END()
786 argc = parse_options(argc, argv, prefix, options,
787 git_notes_merge_usage, 0);
789 if (strategy || do_commit + do_abort == 0)
790 do_merge = 1;
791 if (do_merge + do_commit + do_abort != 1) {
792 error(_("cannot mix --commit, --abort or -s/--strategy"));
793 usage_with_options(git_notes_merge_usage, options);
796 if (do_merge && argc != 1) {
797 error(_("Must specify a notes ref to merge"));
798 usage_with_options(git_notes_merge_usage, options);
799 } else if (!do_merge && argc) {
800 error(_("too many parameters"));
801 usage_with_options(git_notes_merge_usage, options);
804 init_notes_merge_options(&o);
805 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
807 if (do_abort)
808 return merge_abort(&o);
809 if (do_commit)
810 return merge_commit(&o);
812 o.local_ref = default_notes_ref();
813 strbuf_addstr(&remote_ref, argv[0]);
814 expand_loose_notes_ref(&remote_ref);
815 o.remote_ref = remote_ref.buf;
817 t = init_notes_check("merge", NOTES_INIT_WRITABLE);
819 if (strategy) {
820 if (parse_notes_merge_strategy(strategy, &o.strategy)) {
821 error(_("Unknown -s/--strategy: %s"), strategy);
822 usage_with_options(git_notes_merge_usage, options);
824 } else {
825 struct strbuf merge_key = STRBUF_INIT;
826 const char *short_ref = NULL;
828 if (!skip_prefix(o.local_ref, "refs/notes/", &short_ref))
829 die("BUG: local ref %s is outside of refs/notes/",
830 o.local_ref);
832 strbuf_addf(&merge_key, "notes.%s.mergeStrategy", short_ref);
834 if (git_config_get_notes_strategy(merge_key.buf, &o.strategy))
835 git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy);
837 strbuf_release(&merge_key);
840 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
841 remote_ref.buf, default_notes_ref());
842 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
844 result = notes_merge(&o, t, result_sha1);
846 if (result >= 0) /* Merge resulted (trivially) in result_sha1 */
847 /* Update default notes ref with new commit */
848 update_ref(msg.buf, default_notes_ref(), result_sha1, NULL,
849 0, UPDATE_REFS_DIE_ON_ERR);
850 else { /* Merge has unresolved conflicts */
851 const struct worktree *wt;
852 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
853 update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_sha1, NULL,
854 0, UPDATE_REFS_DIE_ON_ERR);
855 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
856 wt = find_shared_symref("NOTES_MERGE_REF", default_notes_ref());
857 if (wt)
858 die(_("A notes merge into %s is already in-progress at %s"),
859 default_notes_ref(), wt->path);
860 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
861 die(_("Failed to store link to current notes ref (%s)"),
862 default_notes_ref());
863 printf(_("Automatic notes merge failed. Fix conflicts in %s and "
864 "commit the result with 'git notes merge --commit', or "
865 "abort the merge with 'git notes merge --abort'.\n"),
866 git_path(NOTES_MERGE_WORKTREE));
869 free_notes(t);
870 strbuf_release(&remote_ref);
871 strbuf_release(&msg);
872 return result < 0; /* return non-zero on conflicts */
875 #define IGNORE_MISSING 1
877 static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
879 int status;
880 unsigned char sha1[20];
881 if (get_sha1(name, sha1))
882 return error(_("Failed to resolve '%s' as a valid ref."), name);
883 status = remove_note(t, sha1);
884 if (status)
885 fprintf(stderr, _("Object %s has no note\n"), name);
886 else
887 fprintf(stderr, _("Removing note for object %s\n"), name);
888 return (flag & IGNORE_MISSING) ? 0 : status;
891 static int remove_cmd(int argc, const char **argv, const char *prefix)
893 unsigned flag = 0;
894 int from_stdin = 0;
895 struct option options[] = {
896 OPT_BIT(0, "ignore-missing", &flag,
897 N_("attempt to remove non-existent note is not an error"),
898 IGNORE_MISSING),
899 OPT_BOOL(0, "stdin", &from_stdin,
900 N_("read object names from the standard input")),
901 OPT_END()
903 struct notes_tree *t;
904 int retval = 0;
906 argc = parse_options(argc, argv, prefix, options,
907 git_notes_remove_usage, 0);
909 t = init_notes_check("remove", NOTES_INIT_WRITABLE);
911 if (!argc && !from_stdin) {
912 retval = remove_one_note(t, "HEAD", flag);
913 } else {
914 while (*argv) {
915 retval |= remove_one_note(t, *argv, flag);
916 argv++;
919 if (from_stdin) {
920 struct strbuf sb = STRBUF_INIT;
921 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
922 strbuf_rtrim(&sb);
923 retval |= remove_one_note(t, sb.buf, flag);
925 strbuf_release(&sb);
927 if (!retval)
928 commit_notes(t, "Notes removed by 'git notes remove'");
929 free_notes(t);
930 return retval;
933 static int prune(int argc, const char **argv, const char *prefix)
935 struct notes_tree *t;
936 int show_only = 0, verbose = 0;
937 struct option options[] = {
938 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
939 OPT__VERBOSE(&verbose, N_("report pruned notes")),
940 OPT_END()
943 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
946 if (argc) {
947 error(_("too many parameters"));
948 usage_with_options(git_notes_prune_usage, options);
951 t = init_notes_check("prune", NOTES_INIT_WRITABLE);
953 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
954 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
955 if (!show_only)
956 commit_notes(t, "Notes removed by 'git notes prune'");
957 free_notes(t);
958 return 0;
961 static int get_ref(int argc, const char **argv, const char *prefix)
963 struct option options[] = { OPT_END() };
964 argc = parse_options(argc, argv, prefix, options,
965 git_notes_get_ref_usage, 0);
967 if (argc) {
968 error(_("too many parameters"));
969 usage_with_options(git_notes_get_ref_usage, options);
972 puts(default_notes_ref());
973 return 0;
976 int cmd_notes(int argc, const char **argv, const char *prefix)
978 int result;
979 const char *override_notes_ref = NULL;
980 struct option options[] = {
981 OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
982 N_("use notes from <notes-ref>")),
983 OPT_END()
986 git_config(git_default_config, NULL);
987 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
988 PARSE_OPT_STOP_AT_NON_OPTION);
990 if (override_notes_ref) {
991 struct strbuf sb = STRBUF_INIT;
992 strbuf_addstr(&sb, override_notes_ref);
993 expand_notes_ref(&sb);
994 setenv("GIT_NOTES_REF", sb.buf, 1);
995 strbuf_release(&sb);
998 if (argc < 1 || !strcmp(argv[0], "list"))
999 result = list(argc, argv, prefix);
1000 else if (!strcmp(argv[0], "add"))
1001 result = add(argc, argv, prefix);
1002 else if (!strcmp(argv[0], "copy"))
1003 result = copy(argc, argv, prefix);
1004 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
1005 result = append_edit(argc, argv, prefix);
1006 else if (!strcmp(argv[0], "show"))
1007 result = show(argc, argv, prefix);
1008 else if (!strcmp(argv[0], "merge"))
1009 result = merge(argc, argv, prefix);
1010 else if (!strcmp(argv[0], "remove"))
1011 result = remove_cmd(argc, argv, prefix);
1012 else if (!strcmp(argv[0], "prune"))
1013 result = prune(argc, argv, prefix);
1014 else if (!strcmp(argv[0], "get-ref"))
1015 result = get_ref(argc, argv, prefix);
1016 else {
1017 result = error(_("Unknown subcommand: %s"), argv[0]);
1018 usage_with_options(git_notes_usage, options);
1021 return result ? 1 : 0;