docs: describe ANSI 256-color mode
[git/debian.git] / builtin / notes.c
blob820c34135cab439e2168dd08947bd79d7ed7ae71
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"
23 static const char * const git_notes_usage[] = {
24 N_("git notes [--ref <notes_ref>] [list [<object>]]"),
25 N_("git notes [--ref <notes_ref>] add [-f] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
26 N_("git notes [--ref <notes_ref>] copy [-f] <from-object> <to-object>"),
27 N_("git notes [--ref <notes_ref>] append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
28 N_("git notes [--ref <notes_ref>] edit [<object>]"),
29 N_("git notes [--ref <notes_ref>] show [<object>]"),
30 N_("git notes [--ref <notes_ref>] merge [-v | -q] [-s <strategy> ] <notes_ref>"),
31 N_("git notes merge --commit [-v | -q]"),
32 N_("git notes merge --abort [-v | -q]"),
33 N_("git notes [--ref <notes_ref>] remove [<object>...]"),
34 N_("git notes [--ref <notes_ref>] prune [-n | -v]"),
35 N_("git notes [--ref <notes_ref>] get-ref"),
36 NULL
39 static const char * const git_notes_list_usage[] = {
40 N_("git notes [list [<object>]]"),
41 NULL
44 static const char * const git_notes_add_usage[] = {
45 N_("git notes add [<options>] [<object>]"),
46 NULL
49 static const char * const git_notes_copy_usage[] = {
50 N_("git notes copy [<options>] <from-object> <to-object>"),
51 N_("git notes copy --stdin [<from-object> <to-object>]..."),
52 NULL
55 static const char * const git_notes_append_usage[] = {
56 N_("git notes append [<options>] [<object>]"),
57 NULL
60 static const char * const git_notes_edit_usage[] = {
61 N_("git notes edit [<object>]"),
62 NULL
65 static const char * const git_notes_show_usage[] = {
66 N_("git notes show [<object>]"),
67 NULL
70 static const char * const git_notes_merge_usage[] = {
71 N_("git notes merge [<options>] <notes_ref>"),
72 N_("git notes merge --commit [<options>]"),
73 N_("git notes merge --abort [<options>]"),
74 NULL
77 static const char * const git_notes_remove_usage[] = {
78 N_("git notes remove [<object>]"),
79 NULL
82 static const char * const git_notes_prune_usage[] = {
83 N_("git notes prune [<options>]"),
84 NULL
87 static const char * const git_notes_get_ref_usage[] = {
88 N_("git notes get-ref"),
89 NULL
92 static const char note_template[] =
93 "\nWrite/edit the notes for the following object:\n";
95 struct msg_arg {
96 int given;
97 int use_editor;
98 struct strbuf buf;
101 static int list_each_note(const unsigned char *object_sha1,
102 const unsigned char *note_sha1, char *note_path,
103 void *cb_data)
105 printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
106 return 0;
109 static void write_note_data(int fd, const unsigned char *sha1)
111 unsigned long size;
112 enum object_type type;
113 char *buf = read_sha1_file(sha1, &type, &size);
114 if (buf) {
115 if (size)
116 write_or_die(fd, buf, size);
117 free(buf);
121 static void write_commented_object(int fd, const unsigned char *object)
123 const char *show_args[5] =
124 {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
125 struct child_process show;
126 struct strbuf buf = STRBUF_INIT;
127 struct strbuf cbuf = STRBUF_INIT;
129 /* Invoke "git show --stat --no-notes $object" */
130 memset(&show, 0, sizeof(show));
131 show.argv = show_args;
132 show.no_stdin = 1;
133 show.out = -1;
134 show.err = 0;
135 show.git_cmd = 1;
136 if (start_command(&show))
137 die(_("unable to start 'show' for object '%s'"),
138 sha1_to_hex(object));
140 if (strbuf_read(&buf, show.out, 0) < 0)
141 die_errno(_("could not read 'show' output"));
142 strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
143 write_or_die(fd, cbuf.buf, cbuf.len);
145 strbuf_release(&cbuf);
146 strbuf_release(&buf);
148 if (finish_command(&show))
149 die(_("failed to finish 'show' for object '%s'"),
150 sha1_to_hex(object));
153 static void create_note(const unsigned char *object, struct msg_arg *msg,
154 int append_only, const unsigned char *prev,
155 unsigned char *result)
157 char *path = NULL;
159 if (msg->use_editor || !msg->given) {
160 int fd;
161 struct strbuf buf = STRBUF_INIT;
163 /* write the template message before editing: */
164 path = git_pathdup("NOTES_EDITMSG");
165 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
166 if (fd < 0)
167 die_errno(_("could not create file '%s'"), path);
169 if (msg->given)
170 write_or_die(fd, msg->buf.buf, msg->buf.len);
171 else if (prev && !append_only)
172 write_note_data(fd, prev);
174 strbuf_addch(&buf, '\n');
175 strbuf_add_commented_lines(&buf, note_template, strlen(note_template));
176 strbuf_addch(&buf, '\n');
177 write_or_die(fd, buf.buf, buf.len);
179 write_commented_object(fd, object);
181 close(fd);
182 strbuf_release(&buf);
183 strbuf_reset(&(msg->buf));
185 if (launch_editor(path, &(msg->buf), NULL)) {
186 die(_("Please supply the note contents using either -m" \
187 " or -F option"));
189 stripspace(&(msg->buf), 1);
192 if (prev && append_only) {
193 /* Append buf to previous note contents */
194 unsigned long size;
195 enum object_type type;
196 char *prev_buf = read_sha1_file(prev, &type, &size);
198 strbuf_grow(&(msg->buf), size + 1);
199 if (msg->buf.len && prev_buf && size)
200 strbuf_insert(&(msg->buf), 0, "\n", 1);
201 if (prev_buf && size)
202 strbuf_insert(&(msg->buf), 0, prev_buf, size);
203 free(prev_buf);
206 if (!msg->buf.len) {
207 fprintf(stderr, _("Removing note for object %s\n"),
208 sha1_to_hex(object));
209 hashclr(result);
210 } else {
211 if (write_sha1_file(msg->buf.buf, msg->buf.len, blob_type, result)) {
212 error(_("unable to write note object"));
213 if (path)
214 error(_("The note contents has been left in %s"),
215 path);
216 exit(128);
220 if (path) {
221 unlink_or_warn(path);
222 free(path);
226 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
228 struct msg_arg *msg = opt->value;
230 strbuf_grow(&(msg->buf), strlen(arg) + 2);
231 if (msg->buf.len)
232 strbuf_addch(&(msg->buf), '\n');
233 strbuf_addstr(&(msg->buf), arg);
234 stripspace(&(msg->buf), 0);
236 msg->given = 1;
237 return 0;
240 static int parse_file_arg(const struct option *opt, const char *arg, int unset)
242 struct msg_arg *msg = opt->value;
244 if (msg->buf.len)
245 strbuf_addch(&(msg->buf), '\n');
246 if (!strcmp(arg, "-")) {
247 if (strbuf_read(&(msg->buf), 0, 1024) < 0)
248 die_errno(_("cannot read '%s'"), arg);
249 } else if (strbuf_read_file(&(msg->buf), arg, 1024) < 0)
250 die_errno(_("could not open or read '%s'"), arg);
251 stripspace(&(msg->buf), 0);
253 msg->given = 1;
254 return 0;
257 static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
259 struct msg_arg *msg = opt->value;
260 char *buf;
261 unsigned char object[20];
262 enum object_type type;
263 unsigned long len;
265 if (msg->buf.len)
266 strbuf_addch(&(msg->buf), '\n');
268 if (get_sha1(arg, object))
269 die(_("Failed to resolve '%s' as a valid ref."), arg);
270 if (!(buf = read_sha1_file(object, &type, &len)) || !len) {
271 free(buf);
272 die(_("Failed to read object '%s'."), arg);
274 if (type != OBJ_BLOB) {
275 free(buf);
276 die(_("Cannot read note data from non-blob object '%s'."), arg);
278 strbuf_add(&(msg->buf), buf, len);
279 free(buf);
281 msg->given = 1;
282 return 0;
285 static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
287 struct msg_arg *msg = opt->value;
288 msg->use_editor = 1;
289 return parse_reuse_arg(opt, arg, unset);
292 static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
294 struct strbuf buf = STRBUF_INIT;
295 struct notes_rewrite_cfg *c = NULL;
296 struct notes_tree *t = NULL;
297 int ret = 0;
298 const char *msg = "Notes added by 'git notes copy'";
300 if (rewrite_cmd) {
301 c = init_copy_notes_for_rewrite(rewrite_cmd);
302 if (!c)
303 return 0;
304 } else {
305 init_notes(NULL, NULL, NULL, 0);
306 t = &default_notes_tree;
309 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
310 unsigned char from_obj[20], to_obj[20];
311 struct strbuf **split;
312 int err;
314 split = strbuf_split(&buf, ' ');
315 if (!split[0] || !split[1])
316 die(_("Malformed input line: '%s'."), buf.buf);
317 strbuf_rtrim(split[0]);
318 strbuf_rtrim(split[1]);
319 if (get_sha1(split[0]->buf, from_obj))
320 die(_("Failed to resolve '%s' as a valid ref."), split[0]->buf);
321 if (get_sha1(split[1]->buf, to_obj))
322 die(_("Failed to resolve '%s' as a valid ref."), split[1]->buf);
324 if (rewrite_cmd)
325 err = copy_note_for_rewrite(c, from_obj, to_obj);
326 else
327 err = copy_note(t, from_obj, to_obj, force,
328 combine_notes_overwrite);
330 if (err) {
331 error(_("Failed to copy notes from '%s' to '%s'"),
332 split[0]->buf, split[1]->buf);
333 ret = 1;
336 strbuf_list_free(split);
339 if (!rewrite_cmd) {
340 commit_notes(t, msg);
341 free_notes(t);
342 } else {
343 finish_copy_notes_for_rewrite(c, msg);
345 return ret;
348 static struct notes_tree *init_notes_check(const char *subcommand)
350 struct notes_tree *t;
351 init_notes(NULL, NULL, NULL, 0);
352 t = &default_notes_tree;
354 if (!starts_with(t->ref, "refs/notes/"))
355 die("Refusing to %s notes in %s (outside of refs/notes/)",
356 subcommand, t->ref);
357 return t;
360 static int list(int argc, const char **argv, const char *prefix)
362 struct notes_tree *t;
363 unsigned char object[20];
364 const unsigned char *note;
365 int retval = -1;
366 struct option options[] = {
367 OPT_END()
370 if (argc)
371 argc = parse_options(argc, argv, prefix, options,
372 git_notes_list_usage, 0);
374 if (1 < argc) {
375 error(_("too many parameters"));
376 usage_with_options(git_notes_list_usage, options);
379 t = init_notes_check("list");
380 if (argc) {
381 if (get_sha1(argv[0], object))
382 die(_("Failed to resolve '%s' as a valid ref."), argv[0]);
383 note = get_note(t, object);
384 if (note) {
385 puts(sha1_to_hex(note));
386 retval = 0;
387 } else
388 retval = error(_("No note found for object %s."),
389 sha1_to_hex(object));
390 } else
391 retval = for_each_note(t, 0, list_each_note, NULL);
393 free_notes(t);
394 return retval;
397 static int append_edit(int argc, const char **argv, const char *prefix);
399 static int add(int argc, const char **argv, const char *prefix)
401 int retval = 0, force = 0;
402 const char *object_ref;
403 struct notes_tree *t;
404 unsigned char object[20], new_note[20];
405 char logmsg[100];
406 const unsigned char *note;
407 struct msg_arg msg = { 0, 0, STRBUF_INIT };
408 struct option options[] = {
409 { OPTION_CALLBACK, 'm', "message", &msg, N_("message"),
410 N_("note contents as a string"), PARSE_OPT_NONEG,
411 parse_msg_arg},
412 { OPTION_CALLBACK, 'F', "file", &msg, N_("file"),
413 N_("note contents in a file"), PARSE_OPT_NONEG,
414 parse_file_arg},
415 { OPTION_CALLBACK, 'c', "reedit-message", &msg, N_("object"),
416 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
417 parse_reedit_arg},
418 { OPTION_CALLBACK, 'C', "reuse-message", &msg, N_("object"),
419 N_("reuse specified note object"), PARSE_OPT_NONEG,
420 parse_reuse_arg},
421 OPT__FORCE(&force, N_("replace existing notes")),
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 parameters"));
430 usage_with_options(git_notes_add_usage, options);
433 object_ref = argc > 1 ? argv[1] : "HEAD";
435 if (get_sha1(object_ref, object))
436 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
438 t = init_notes_check("add");
439 note = get_note(t, object);
441 if (note) {
442 if (!force) {
443 if (!msg.given) {
445 * Redirect to "edit" subcommand.
447 * We only end up here if none of -m/-F/-c/-C
448 * or -f are given. The original args are
449 * therefore still in argv[0-1].
451 argv[0] = "edit";
452 free_notes(t);
453 return append_edit(argc, argv, prefix);
455 retval = error(_("Cannot add notes. Found existing notes "
456 "for object %s. Use '-f' to overwrite "
457 "existing notes"), sha1_to_hex(object));
458 goto out;
460 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
461 sha1_to_hex(object));
464 create_note(object, &msg, 0, note, new_note);
466 if (is_null_sha1(new_note))
467 remove_note(t, object);
468 else if (add_note(t, object, new_note, combine_notes_overwrite))
469 die("BUG: combine_notes_overwrite failed");
471 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
472 is_null_sha1(new_note) ? "removed" : "added", "add");
473 commit_notes(t, logmsg);
474 out:
475 free_notes(t);
476 strbuf_release(&(msg.buf));
477 return retval;
480 static int copy(int argc, const char **argv, const char *prefix)
482 int retval = 0, force = 0, from_stdin = 0;
483 const unsigned char *from_note, *note;
484 const char *object_ref;
485 unsigned char object[20], from_obj[20];
486 struct notes_tree *t;
487 const char *rewrite_cmd = NULL;
488 struct option options[] = {
489 OPT__FORCE(&force, N_("replace existing notes")),
490 OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")),
491 OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"),
492 N_("load rewriting config for <command> (implies "
493 "--stdin)")),
494 OPT_END()
497 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
500 if (from_stdin || rewrite_cmd) {
501 if (argc) {
502 error(_("too many parameters"));
503 usage_with_options(git_notes_copy_usage, options);
504 } else {
505 return notes_copy_from_stdin(force, rewrite_cmd);
509 if (argc < 2) {
510 error(_("too few parameters"));
511 usage_with_options(git_notes_copy_usage, options);
513 if (2 < argc) {
514 error(_("too many parameters"));
515 usage_with_options(git_notes_copy_usage, options);
518 if (get_sha1(argv[0], from_obj))
519 die(_("Failed to resolve '%s' as a valid ref."), argv[0]);
521 object_ref = 1 < argc ? argv[1] : "HEAD";
523 if (get_sha1(object_ref, object))
524 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
526 t = init_notes_check("copy");
527 note = get_note(t, object);
529 if (note) {
530 if (!force) {
531 retval = error(_("Cannot copy notes. Found existing "
532 "notes for object %s. Use '-f' to "
533 "overwrite existing notes"),
534 sha1_to_hex(object));
535 goto out;
537 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
538 sha1_to_hex(object));
541 from_note = get_note(t, from_obj);
542 if (!from_note) {
543 retval = error(_("Missing notes on source object %s. Cannot "
544 "copy."), sha1_to_hex(from_obj));
545 goto out;
548 if (add_note(t, object, from_note, combine_notes_overwrite))
549 die("BUG: combine_notes_overwrite failed");
550 commit_notes(t, "Notes added by 'git notes copy'");
551 out:
552 free_notes(t);
553 return retval;
556 static int append_edit(int argc, const char **argv, const char *prefix)
558 const char *object_ref;
559 struct notes_tree *t;
560 unsigned char object[20], new_note[20];
561 const unsigned char *note;
562 char logmsg[100];
563 const char * const *usage;
564 struct msg_arg msg = { 0, 0, STRBUF_INIT };
565 struct option options[] = {
566 { OPTION_CALLBACK, 'm', "message", &msg, N_("message"),
567 N_("note contents as a string"), PARSE_OPT_NONEG,
568 parse_msg_arg},
569 { OPTION_CALLBACK, 'F', "file", &msg, N_("file"),
570 N_("note contents in a file"), PARSE_OPT_NONEG,
571 parse_file_arg},
572 { OPTION_CALLBACK, 'c', "reedit-message", &msg, N_("object"),
573 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
574 parse_reedit_arg},
575 { OPTION_CALLBACK, 'C', "reuse-message", &msg, N_("object"),
576 N_("reuse specified note object"), PARSE_OPT_NONEG,
577 parse_reuse_arg},
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 (msg.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_sha1(object_ref, object))
599 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
601 t = init_notes_check(argv[0]);
602 note = get_note(t, object);
604 create_note(object, &msg, !edit, note, new_note);
606 if (is_null_sha1(new_note))
607 remove_note(t, object);
608 else if (add_note(t, object, new_note, combine_notes_overwrite))
609 die("BUG: combine_notes_overwrite failed");
611 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
612 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
613 commit_notes(t, logmsg);
614 free_notes(t);
615 strbuf_release(&(msg.buf));
616 return 0;
619 static int show(int argc, const char **argv, const char *prefix)
621 const char *object_ref;
622 struct notes_tree *t;
623 unsigned char object[20];
624 const unsigned char *note;
625 int retval;
626 struct option options[] = {
627 OPT_END()
630 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
633 if (1 < argc) {
634 error(_("too many parameters"));
635 usage_with_options(git_notes_show_usage, options);
638 object_ref = argc ? argv[0] : "HEAD";
640 if (get_sha1(object_ref, object))
641 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
643 t = init_notes_check("show");
644 note = get_note(t, object);
646 if (!note)
647 retval = error(_("No note found for object %s."),
648 sha1_to_hex(object));
649 else {
650 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
651 retval = execv_git_cmd(show_args);
653 free_notes(t);
654 return retval;
657 static int merge_abort(struct notes_merge_options *o)
659 int ret = 0;
662 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
663 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
666 if (delete_ref("NOTES_MERGE_PARTIAL", NULL, 0))
667 ret += error("Failed to delete ref NOTES_MERGE_PARTIAL");
668 if (delete_ref("NOTES_MERGE_REF", NULL, REF_NODEREF))
669 ret += error("Failed to delete ref NOTES_MERGE_REF");
670 if (notes_merge_abort(o))
671 ret += error("Failed to remove 'git notes merge' worktree");
672 return ret;
675 static int merge_commit(struct notes_merge_options *o)
677 struct strbuf msg = STRBUF_INIT;
678 unsigned char sha1[20], parent_sha1[20];
679 struct notes_tree *t;
680 struct commit *partial;
681 struct pretty_print_context pretty_ctx;
682 void *local_ref_to_free;
683 int ret;
686 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
687 * and target notes ref from .git/NOTES_MERGE_REF.
690 if (get_sha1("NOTES_MERGE_PARTIAL", sha1))
691 die("Failed to read ref NOTES_MERGE_PARTIAL");
692 else if (!(partial = lookup_commit_reference(sha1)))
693 die("Could not find commit from NOTES_MERGE_PARTIAL.");
694 else if (parse_commit(partial))
695 die("Could not parse commit from NOTES_MERGE_PARTIAL.");
697 if (partial->parents)
698 hashcpy(parent_sha1, partial->parents->item->object.sha1);
699 else
700 hashclr(parent_sha1);
702 t = xcalloc(1, sizeof(struct notes_tree));
703 init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
705 o->local_ref = local_ref_to_free =
706 resolve_refdup("NOTES_MERGE_REF", sha1, 0, NULL);
707 if (!o->local_ref)
708 die("Failed to resolve NOTES_MERGE_REF");
710 if (notes_merge_commit(o, t, partial, sha1))
711 die("Failed to finalize notes merge");
713 /* Reuse existing commit message in reflog message */
714 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
715 format_commit_message(partial, "%s", &msg, &pretty_ctx);
716 strbuf_trim(&msg);
717 strbuf_insert(&msg, 0, "notes: ", 7);
718 update_ref(msg.buf, o->local_ref, sha1,
719 is_null_sha1(parent_sha1) ? NULL : parent_sha1,
720 0, UPDATE_REFS_DIE_ON_ERR);
722 free_notes(t);
723 strbuf_release(&msg);
724 ret = merge_abort(o);
725 free(local_ref_to_free);
726 return ret;
729 static int merge(int argc, const char **argv, const char *prefix)
731 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
732 unsigned char result_sha1[20];
733 struct notes_tree *t;
734 struct notes_merge_options o;
735 int do_merge = 0, do_commit = 0, do_abort = 0;
736 int verbosity = 0, result;
737 const char *strategy = NULL;
738 struct option options[] = {
739 OPT_GROUP(N_("General options")),
740 OPT__VERBOSITY(&verbosity),
741 OPT_GROUP(N_("Merge options")),
742 OPT_STRING('s', "strategy", &strategy, N_("strategy"),
743 N_("resolve notes conflicts using the given strategy "
744 "(manual/ours/theirs/union/cat_sort_uniq)")),
745 OPT_GROUP(N_("Committing unmerged notes")),
746 { OPTION_SET_INT, 0, "commit", &do_commit, NULL,
747 N_("finalize notes merge by committing unmerged notes"),
748 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
749 OPT_GROUP(N_("Aborting notes merge resolution")),
750 { OPTION_SET_INT, 0, "abort", &do_abort, NULL,
751 N_("abort notes merge"),
752 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
753 OPT_END()
756 argc = parse_options(argc, argv, prefix, options,
757 git_notes_merge_usage, 0);
759 if (strategy || do_commit + do_abort == 0)
760 do_merge = 1;
761 if (do_merge + do_commit + do_abort != 1) {
762 error("cannot mix --commit, --abort or -s/--strategy");
763 usage_with_options(git_notes_merge_usage, options);
766 if (do_merge && argc != 1) {
767 error("Must specify a notes ref to merge");
768 usage_with_options(git_notes_merge_usage, options);
769 } else if (!do_merge && argc) {
770 error("too many parameters");
771 usage_with_options(git_notes_merge_usage, options);
774 init_notes_merge_options(&o);
775 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
777 if (do_abort)
778 return merge_abort(&o);
779 if (do_commit)
780 return merge_commit(&o);
782 o.local_ref = default_notes_ref();
783 strbuf_addstr(&remote_ref, argv[0]);
784 expand_notes_ref(&remote_ref);
785 o.remote_ref = remote_ref.buf;
787 if (strategy) {
788 if (!strcmp(strategy, "manual"))
789 o.strategy = NOTES_MERGE_RESOLVE_MANUAL;
790 else if (!strcmp(strategy, "ours"))
791 o.strategy = NOTES_MERGE_RESOLVE_OURS;
792 else if (!strcmp(strategy, "theirs"))
793 o.strategy = NOTES_MERGE_RESOLVE_THEIRS;
794 else if (!strcmp(strategy, "union"))
795 o.strategy = NOTES_MERGE_RESOLVE_UNION;
796 else if (!strcmp(strategy, "cat_sort_uniq"))
797 o.strategy = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ;
798 else {
799 error("Unknown -s/--strategy: %s", strategy);
800 usage_with_options(git_notes_merge_usage, options);
804 t = init_notes_check("merge");
806 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
807 remote_ref.buf, default_notes_ref());
808 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
810 result = notes_merge(&o, t, result_sha1);
812 if (result >= 0) /* Merge resulted (trivially) in result_sha1 */
813 /* Update default notes ref with new commit */
814 update_ref(msg.buf, default_notes_ref(), result_sha1, NULL,
815 0, UPDATE_REFS_DIE_ON_ERR);
816 else { /* Merge has unresolved conflicts */
817 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
818 update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_sha1, NULL,
819 0, UPDATE_REFS_DIE_ON_ERR);
820 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
821 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
822 die("Failed to store link to current notes ref (%s)",
823 default_notes_ref());
824 printf("Automatic notes merge failed. Fix conflicts in %s and "
825 "commit the result with 'git notes merge --commit', or "
826 "abort the merge with 'git notes merge --abort'.\n",
827 git_path(NOTES_MERGE_WORKTREE));
830 free_notes(t);
831 strbuf_release(&remote_ref);
832 strbuf_release(&msg);
833 return result < 0; /* return non-zero on conflicts */
836 #define IGNORE_MISSING 1
838 static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
840 int status;
841 unsigned char sha1[20];
842 if (get_sha1(name, sha1))
843 return error(_("Failed to resolve '%s' as a valid ref."), name);
844 status = remove_note(t, sha1);
845 if (status)
846 fprintf(stderr, _("Object %s has no note\n"), name);
847 else
848 fprintf(stderr, _("Removing note for object %s\n"), name);
849 return (flag & IGNORE_MISSING) ? 0 : status;
852 static int remove_cmd(int argc, const char **argv, const char *prefix)
854 unsigned flag = 0;
855 int from_stdin = 0;
856 struct option options[] = {
857 OPT_BIT(0, "ignore-missing", &flag,
858 N_("attempt to remove non-existent note is not an error"),
859 IGNORE_MISSING),
860 OPT_BOOL(0, "stdin", &from_stdin,
861 N_("read object names from the standard input")),
862 OPT_END()
864 struct notes_tree *t;
865 int retval = 0;
867 argc = parse_options(argc, argv, prefix, options,
868 git_notes_remove_usage, 0);
870 t = init_notes_check("remove");
872 if (!argc && !from_stdin) {
873 retval = remove_one_note(t, "HEAD", flag);
874 } else {
875 while (*argv) {
876 retval |= remove_one_note(t, *argv, flag);
877 argv++;
880 if (from_stdin) {
881 struct strbuf sb = STRBUF_INIT;
882 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
883 strbuf_rtrim(&sb);
884 retval |= remove_one_note(t, sb.buf, flag);
886 strbuf_release(&sb);
888 if (!retval)
889 commit_notes(t, "Notes removed by 'git notes remove'");
890 free_notes(t);
891 return retval;
894 static int prune(int argc, const char **argv, const char *prefix)
896 struct notes_tree *t;
897 int show_only = 0, verbose = 0;
898 struct option options[] = {
899 OPT__DRY_RUN(&show_only, "do not remove, show only"),
900 OPT__VERBOSE(&verbose, "report pruned notes"),
901 OPT_END()
904 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
907 if (argc) {
908 error(_("too many parameters"));
909 usage_with_options(git_notes_prune_usage, options);
912 t = init_notes_check("prune");
914 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
915 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
916 if (!show_only)
917 commit_notes(t, "Notes removed by 'git notes prune'");
918 free_notes(t);
919 return 0;
922 static int get_ref(int argc, const char **argv, const char *prefix)
924 struct option options[] = { OPT_END() };
925 argc = parse_options(argc, argv, prefix, options,
926 git_notes_get_ref_usage, 0);
928 if (argc) {
929 error("too many parameters");
930 usage_with_options(git_notes_get_ref_usage, options);
933 puts(default_notes_ref());
934 return 0;
937 int cmd_notes(int argc, const char **argv, const char *prefix)
939 int result;
940 const char *override_notes_ref = NULL;
941 struct option options[] = {
942 OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
943 N_("use notes from <notes_ref>")),
944 OPT_END()
947 git_config(git_default_config, NULL);
948 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
949 PARSE_OPT_STOP_AT_NON_OPTION);
951 if (override_notes_ref) {
952 struct strbuf sb = STRBUF_INIT;
953 strbuf_addstr(&sb, override_notes_ref);
954 expand_notes_ref(&sb);
955 setenv("GIT_NOTES_REF", sb.buf, 1);
956 strbuf_release(&sb);
959 if (argc < 1 || !strcmp(argv[0], "list"))
960 result = list(argc, argv, prefix);
961 else if (!strcmp(argv[0], "add"))
962 result = add(argc, argv, prefix);
963 else if (!strcmp(argv[0], "copy"))
964 result = copy(argc, argv, prefix);
965 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
966 result = append_edit(argc, argv, prefix);
967 else if (!strcmp(argv[0], "show"))
968 result = show(argc, argv, prefix);
969 else if (!strcmp(argv[0], "merge"))
970 result = merge(argc, argv, prefix);
971 else if (!strcmp(argv[0], "remove"))
972 result = remove_cmd(argc, argv, prefix);
973 else if (!strcmp(argv[0], "prune"))
974 result = prune(argc, argv, prefix);
975 else if (!strcmp(argv[0], "get-ref"))
976 result = get_ref(argc, argv, prefix);
977 else {
978 result = error(_("Unknown subcommand: %s"), argv[0]);
979 usage_with_options(git_notes_usage, options);
982 return result ? 1 : 0;