Merge branch 'dt/notes-multiple'
[git.git] / builtin / notes.c
blob04234808270908ecc5a8b6e015749ceaa29bb081
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 "branch.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 "\nWrite/edit the notes for the following object:\n";
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, note_template, strlen(note_template));
183 strbuf_addch(&buf, '\n');
184 write_or_die(fd, buf.buf, buf.len);
186 write_commented_object(fd, object);
188 close(fd);
189 strbuf_release(&buf);
190 strbuf_reset(&d->buf);
192 if (launch_editor(d->edit_path, &d->buf, NULL)) {
193 die(_("Please supply the note contents using either -m or -F option"));
195 stripspace(&d->buf, 1);
199 static void write_note_data(struct note_data *d, unsigned char *sha1)
201 if (write_sha1_file(d->buf.buf, d->buf.len, blob_type, sha1)) {
202 error(_("unable to write note object"));
203 if (d->edit_path)
204 error(_("The note contents have been left in %s"),
205 d->edit_path);
206 exit(128);
210 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
212 struct note_data *d = opt->value;
214 strbuf_grow(&d->buf, strlen(arg) + 2);
215 if (d->buf.len)
216 strbuf_addch(&d->buf, '\n');
217 strbuf_addstr(&d->buf, arg);
218 stripspace(&d->buf, 0);
220 d->given = 1;
221 return 0;
224 static int parse_file_arg(const struct option *opt, const char *arg, int unset)
226 struct note_data *d = opt->value;
228 if (d->buf.len)
229 strbuf_addch(&d->buf, '\n');
230 if (!strcmp(arg, "-")) {
231 if (strbuf_read(&d->buf, 0, 1024) < 0)
232 die_errno(_("cannot read '%s'"), arg);
233 } else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
234 die_errno(_("could not open or read '%s'"), arg);
235 stripspace(&d->buf, 0);
237 d->given = 1;
238 return 0;
241 static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
243 struct note_data *d = opt->value;
244 char *buf;
245 unsigned char object[20];
246 enum object_type type;
247 unsigned long len;
249 if (d->buf.len)
250 strbuf_addch(&d->buf, '\n');
252 if (get_sha1(arg, object))
253 die(_("Failed to resolve '%s' as a valid ref."), arg);
254 if (!(buf = read_sha1_file(object, &type, &len))) {
255 free(buf);
256 die(_("Failed to read object '%s'."), arg);
258 if (type != OBJ_BLOB) {
259 free(buf);
260 die(_("Cannot read note data from non-blob object '%s'."), arg);
262 strbuf_add(&d->buf, buf, len);
263 free(buf);
265 d->given = 1;
266 return 0;
269 static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
271 struct note_data *d = opt->value;
272 d->use_editor = 1;
273 return parse_reuse_arg(opt, arg, unset);
276 static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
278 struct strbuf buf = STRBUF_INIT;
279 struct notes_rewrite_cfg *c = NULL;
280 struct notes_tree *t = NULL;
281 int ret = 0;
282 const char *msg = "Notes added by 'git notes copy'";
284 if (rewrite_cmd) {
285 c = init_copy_notes_for_rewrite(rewrite_cmd);
286 if (!c)
287 return 0;
288 } else {
289 init_notes(NULL, NULL, NULL, 0);
290 t = &default_notes_tree;
293 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
294 unsigned char from_obj[20], to_obj[20];
295 struct strbuf **split;
296 int err;
298 split = strbuf_split(&buf, ' ');
299 if (!split[0] || !split[1])
300 die(_("Malformed input line: '%s'."), buf.buf);
301 strbuf_rtrim(split[0]);
302 strbuf_rtrim(split[1]);
303 if (get_sha1(split[0]->buf, from_obj))
304 die(_("Failed to resolve '%s' as a valid ref."), split[0]->buf);
305 if (get_sha1(split[1]->buf, to_obj))
306 die(_("Failed to resolve '%s' as a valid ref."), split[1]->buf);
308 if (rewrite_cmd)
309 err = copy_note_for_rewrite(c, from_obj, to_obj);
310 else
311 err = copy_note(t, from_obj, to_obj, force,
312 combine_notes_overwrite);
314 if (err) {
315 error(_("Failed to copy notes from '%s' to '%s'"),
316 split[0]->buf, split[1]->buf);
317 ret = 1;
320 strbuf_list_free(split);
323 if (!rewrite_cmd) {
324 commit_notes(t, msg);
325 free_notes(t);
326 } else {
327 finish_copy_notes_for_rewrite(c, msg);
329 return ret;
332 static struct notes_tree *init_notes_check(const char *subcommand)
334 struct notes_tree *t;
335 init_notes(NULL, NULL, NULL, 0);
336 t = &default_notes_tree;
338 if (!starts_with(t->ref, "refs/notes/"))
339 die("Refusing to %s notes in %s (outside of refs/notes/)",
340 subcommand, t->ref);
341 return t;
344 static int list(int argc, const char **argv, const char *prefix)
346 struct notes_tree *t;
347 unsigned char object[20];
348 const unsigned char *note;
349 int retval = -1;
350 struct option options[] = {
351 OPT_END()
354 if (argc)
355 argc = parse_options(argc, argv, prefix, options,
356 git_notes_list_usage, 0);
358 if (1 < argc) {
359 error(_("too many parameters"));
360 usage_with_options(git_notes_list_usage, options);
363 t = init_notes_check("list");
364 if (argc) {
365 if (get_sha1(argv[0], object))
366 die(_("Failed to resolve '%s' as a valid ref."), argv[0]);
367 note = get_note(t, object);
368 if (note) {
369 puts(sha1_to_hex(note));
370 retval = 0;
371 } else
372 retval = error(_("No note found for object %s."),
373 sha1_to_hex(object));
374 } else
375 retval = for_each_note(t, 0, list_each_note, NULL);
377 free_notes(t);
378 return retval;
381 static int append_edit(int argc, const char **argv, const char *prefix);
383 static int add(int argc, const char **argv, const char *prefix)
385 int force = 0, allow_empty = 0;
386 const char *object_ref;
387 struct notes_tree *t;
388 unsigned char object[20], new_note[20];
389 const unsigned char *note;
390 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
391 struct option options[] = {
392 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
393 N_("note contents as a string"), PARSE_OPT_NONEG,
394 parse_msg_arg},
395 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
396 N_("note contents in a file"), PARSE_OPT_NONEG,
397 parse_file_arg},
398 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
399 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
400 parse_reedit_arg},
401 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
402 N_("reuse specified note object"), PARSE_OPT_NONEG,
403 parse_reuse_arg},
404 OPT_BOOL(0, "allow-empty", &allow_empty,
405 N_("allow storing empty note")),
406 OPT__FORCE(&force, N_("replace existing notes")),
407 OPT_END()
410 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
411 PARSE_OPT_KEEP_ARGV0);
413 if (2 < argc) {
414 error(_("too many parameters"));
415 usage_with_options(git_notes_add_usage, options);
418 object_ref = argc > 1 ? argv[1] : "HEAD";
420 if (get_sha1(object_ref, object))
421 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
423 t = init_notes_check("add");
424 note = get_note(t, object);
426 if (note) {
427 if (!force) {
428 free_notes(t);
429 if (d.given) {
430 free_note_data(&d);
431 return error(_("Cannot add notes. "
432 "Found existing notes for object %s. "
433 "Use '-f' to overwrite existing notes"),
434 sha1_to_hex(object));
437 * Redirect to "edit" subcommand.
439 * We only end up here if none of -m/-F/-c/-C or -f are
440 * given. The original args are therefore still in
441 * argv[0-1].
443 argv[0] = "edit";
444 return append_edit(argc, argv, prefix);
446 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
447 sha1_to_hex(object));
450 prepare_note_data(object, &d, note);
451 if (d.buf.len || allow_empty) {
452 write_note_data(&d, new_note);
453 if (add_note(t, object, new_note, combine_notes_overwrite))
454 die("BUG: combine_notes_overwrite failed");
455 commit_notes(t, "Notes added by 'git notes add'");
456 } else {
457 fprintf(stderr, _("Removing note for object %s\n"),
458 sha1_to_hex(object));
459 remove_note(t, object);
460 commit_notes(t, "Notes removed by 'git notes add'");
463 free_note_data(&d);
464 free_notes(t);
465 return 0;
468 static int copy(int argc, const char **argv, const char *prefix)
470 int retval = 0, force = 0, from_stdin = 0;
471 const unsigned char *from_note, *note;
472 const char *object_ref;
473 unsigned char object[20], from_obj[20];
474 struct notes_tree *t;
475 const char *rewrite_cmd = NULL;
476 struct option options[] = {
477 OPT__FORCE(&force, N_("replace existing notes")),
478 OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")),
479 OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"),
480 N_("load rewriting config for <command> (implies "
481 "--stdin)")),
482 OPT_END()
485 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
488 if (from_stdin || rewrite_cmd) {
489 if (argc) {
490 error(_("too many parameters"));
491 usage_with_options(git_notes_copy_usage, options);
492 } else {
493 return notes_copy_from_stdin(force, rewrite_cmd);
497 if (argc < 2) {
498 error(_("too few parameters"));
499 usage_with_options(git_notes_copy_usage, options);
501 if (2 < argc) {
502 error(_("too many parameters"));
503 usage_with_options(git_notes_copy_usage, options);
506 if (get_sha1(argv[0], from_obj))
507 die(_("Failed to resolve '%s' as a valid ref."), argv[0]);
509 object_ref = 1 < argc ? argv[1] : "HEAD";
511 if (get_sha1(object_ref, object))
512 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
514 t = init_notes_check("copy");
515 note = get_note(t, object);
517 if (note) {
518 if (!force) {
519 retval = error(_("Cannot copy notes. Found existing "
520 "notes for object %s. Use '-f' to "
521 "overwrite existing notes"),
522 sha1_to_hex(object));
523 goto out;
525 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
526 sha1_to_hex(object));
529 from_note = get_note(t, from_obj);
530 if (!from_note) {
531 retval = error(_("Missing notes on source object %s. Cannot "
532 "copy."), sha1_to_hex(from_obj));
533 goto out;
536 if (add_note(t, object, from_note, combine_notes_overwrite))
537 die("BUG: combine_notes_overwrite failed");
538 commit_notes(t, "Notes added by 'git notes copy'");
539 out:
540 free_notes(t);
541 return retval;
544 static int append_edit(int argc, const char **argv, const char *prefix)
546 int allow_empty = 0;
547 const char *object_ref;
548 struct notes_tree *t;
549 unsigned char object[20], new_note[20];
550 const unsigned char *note;
551 char logmsg[100];
552 const char * const *usage;
553 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
554 struct option options[] = {
555 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
556 N_("note contents as a string"), PARSE_OPT_NONEG,
557 parse_msg_arg},
558 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
559 N_("note contents in a file"), PARSE_OPT_NONEG,
560 parse_file_arg},
561 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
562 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
563 parse_reedit_arg},
564 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
565 N_("reuse specified note object"), PARSE_OPT_NONEG,
566 parse_reuse_arg},
567 OPT_BOOL(0, "allow-empty", &allow_empty,
568 N_("allow storing empty note")),
569 OPT_END()
571 int edit = !strcmp(argv[0], "edit");
573 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
574 argc = parse_options(argc, argv, prefix, options, usage,
575 PARSE_OPT_KEEP_ARGV0);
577 if (2 < argc) {
578 error(_("too many parameters"));
579 usage_with_options(usage, options);
582 if (d.given && edit)
583 fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
584 "for the 'edit' subcommand.\n"
585 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
587 object_ref = 1 < argc ? argv[1] : "HEAD";
589 if (get_sha1(object_ref, object))
590 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
592 t = init_notes_check(argv[0]);
593 note = get_note(t, object);
595 prepare_note_data(object, &d, edit ? note : NULL);
597 if (note && !edit) {
598 /* Append buf to previous note contents */
599 unsigned long size;
600 enum object_type type;
601 char *prev_buf = read_sha1_file(note, &type, &size);
603 strbuf_grow(&d.buf, size + 1);
604 if (d.buf.len && prev_buf && size)
605 strbuf_insert(&d.buf, 0, "\n", 1);
606 if (prev_buf && size)
607 strbuf_insert(&d.buf, 0, prev_buf, size);
608 free(prev_buf);
611 if (d.buf.len || allow_empty) {
612 write_note_data(&d, new_note);
613 if (add_note(t, object, new_note, combine_notes_overwrite))
614 die("BUG: combine_notes_overwrite failed");
615 snprintf(logmsg, sizeof(logmsg), "Notes added by 'git notes %s'",
616 argv[0]);
617 } else {
618 fprintf(stderr, _("Removing note for object %s\n"),
619 sha1_to_hex(object));
620 remove_note(t, object);
621 snprintf(logmsg, sizeof(logmsg), "Notes removed by 'git notes %s'",
622 argv[0]);
624 commit_notes(t, logmsg);
626 free_note_data(&d);
627 free_notes(t);
628 return 0;
631 static int show(int argc, const char **argv, const char *prefix)
633 const char *object_ref;
634 struct notes_tree *t;
635 unsigned char object[20];
636 const unsigned char *note;
637 int retval;
638 struct option options[] = {
639 OPT_END()
642 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
645 if (1 < argc) {
646 error(_("too many parameters"));
647 usage_with_options(git_notes_show_usage, options);
650 object_ref = argc ? argv[0] : "HEAD";
652 if (get_sha1(object_ref, object))
653 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
655 t = init_notes_check("show");
656 note = get_note(t, object);
658 if (!note)
659 retval = error(_("No note found for object %s."),
660 sha1_to_hex(object));
661 else {
662 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
663 retval = execv_git_cmd(show_args);
665 free_notes(t);
666 return retval;
669 static int merge_abort(struct notes_merge_options *o)
671 int ret = 0;
674 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
675 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
678 if (delete_ref("NOTES_MERGE_PARTIAL", NULL, 0))
679 ret += error("Failed to delete ref NOTES_MERGE_PARTIAL");
680 if (delete_ref("NOTES_MERGE_REF", NULL, REF_NODEREF))
681 ret += error("Failed to delete ref NOTES_MERGE_REF");
682 if (notes_merge_abort(o))
683 ret += error("Failed to remove 'git notes merge' worktree");
684 return ret;
687 static int merge_commit(struct notes_merge_options *o)
689 struct strbuf msg = STRBUF_INIT;
690 unsigned char sha1[20], parent_sha1[20];
691 struct notes_tree *t;
692 struct commit *partial;
693 struct pretty_print_context pretty_ctx;
694 void *local_ref_to_free;
695 int ret;
698 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
699 * and target notes ref from .git/NOTES_MERGE_REF.
702 if (get_sha1("NOTES_MERGE_PARTIAL", sha1))
703 die("Failed to read ref NOTES_MERGE_PARTIAL");
704 else if (!(partial = lookup_commit_reference(sha1)))
705 die("Could not find commit from NOTES_MERGE_PARTIAL.");
706 else if (parse_commit(partial))
707 die("Could not parse commit from NOTES_MERGE_PARTIAL.");
709 if (partial->parents)
710 hashcpy(parent_sha1, partial->parents->item->object.sha1);
711 else
712 hashclr(parent_sha1);
714 t = xcalloc(1, sizeof(struct notes_tree));
715 init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
717 o->local_ref = local_ref_to_free =
718 resolve_refdup("NOTES_MERGE_REF", 0, sha1, NULL);
719 if (!o->local_ref)
720 die("Failed to resolve NOTES_MERGE_REF");
722 if (notes_merge_commit(o, t, partial, sha1))
723 die("Failed to finalize notes merge");
725 /* Reuse existing commit message in reflog message */
726 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
727 format_commit_message(partial, "%s", &msg, &pretty_ctx);
728 strbuf_trim(&msg);
729 strbuf_insert(&msg, 0, "notes: ", 7);
730 update_ref(msg.buf, o->local_ref, sha1,
731 is_null_sha1(parent_sha1) ? NULL : parent_sha1,
732 0, UPDATE_REFS_DIE_ON_ERR);
734 free_notes(t);
735 strbuf_release(&msg);
736 ret = merge_abort(o);
737 free(local_ref_to_free);
738 return ret;
741 static int merge(int argc, const char **argv, const char *prefix)
743 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
744 unsigned char result_sha1[20];
745 struct notes_tree *t;
746 struct notes_merge_options o;
747 int do_merge = 0, do_commit = 0, do_abort = 0;
748 int verbosity = 0, result;
749 const char *strategy = NULL;
750 struct option options[] = {
751 OPT_GROUP(N_("General options")),
752 OPT__VERBOSITY(&verbosity),
753 OPT_GROUP(N_("Merge options")),
754 OPT_STRING('s', "strategy", &strategy, N_("strategy"),
755 N_("resolve notes conflicts using the given strategy "
756 "(manual/ours/theirs/union/cat_sort_uniq)")),
757 OPT_GROUP(N_("Committing unmerged notes")),
758 { OPTION_SET_INT, 0, "commit", &do_commit, NULL,
759 N_("finalize notes merge by committing unmerged notes"),
760 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
761 OPT_GROUP(N_("Aborting notes merge resolution")),
762 { OPTION_SET_INT, 0, "abort", &do_abort, NULL,
763 N_("abort notes merge"),
764 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
765 OPT_END()
768 argc = parse_options(argc, argv, prefix, options,
769 git_notes_merge_usage, 0);
771 if (strategy || do_commit + do_abort == 0)
772 do_merge = 1;
773 if (do_merge + do_commit + do_abort != 1) {
774 error("cannot mix --commit, --abort or -s/--strategy");
775 usage_with_options(git_notes_merge_usage, options);
778 if (do_merge && argc != 1) {
779 error("Must specify a notes ref to merge");
780 usage_with_options(git_notes_merge_usage, options);
781 } else if (!do_merge && argc) {
782 error("too many parameters");
783 usage_with_options(git_notes_merge_usage, options);
786 init_notes_merge_options(&o);
787 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
789 if (do_abort)
790 return merge_abort(&o);
791 if (do_commit)
792 return merge_commit(&o);
794 o.local_ref = default_notes_ref();
795 strbuf_addstr(&remote_ref, argv[0]);
796 expand_notes_ref(&remote_ref);
797 o.remote_ref = remote_ref.buf;
799 if (strategy) {
800 if (!strcmp(strategy, "manual"))
801 o.strategy = NOTES_MERGE_RESOLVE_MANUAL;
802 else if (!strcmp(strategy, "ours"))
803 o.strategy = NOTES_MERGE_RESOLVE_OURS;
804 else if (!strcmp(strategy, "theirs"))
805 o.strategy = NOTES_MERGE_RESOLVE_THEIRS;
806 else if (!strcmp(strategy, "union"))
807 o.strategy = NOTES_MERGE_RESOLVE_UNION;
808 else if (!strcmp(strategy, "cat_sort_uniq"))
809 o.strategy = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ;
810 else {
811 error("Unknown -s/--strategy: %s", strategy);
812 usage_with_options(git_notes_merge_usage, options);
816 t = init_notes_check("merge");
818 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
819 remote_ref.buf, default_notes_ref());
820 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
822 result = notes_merge(&o, t, result_sha1);
824 if (result >= 0) /* Merge resulted (trivially) in result_sha1 */
825 /* Update default notes ref with new commit */
826 update_ref(msg.buf, default_notes_ref(), result_sha1, NULL,
827 0, UPDATE_REFS_DIE_ON_ERR);
828 else { /* Merge has unresolved conflicts */
829 char *existing;
830 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
831 update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_sha1, NULL,
832 0, UPDATE_REFS_DIE_ON_ERR);
833 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
834 existing = find_shared_symref("NOTES_MERGE_REF", default_notes_ref());
835 if (existing)
836 die(_("A notes merge into %s is already in-progress at %s"),
837 default_notes_ref(), existing);
838 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
839 die("Failed to store link to current notes ref (%s)",
840 default_notes_ref());
841 printf("Automatic notes merge failed. Fix conflicts in %s and "
842 "commit the result with 'git notes merge --commit', or "
843 "abort the merge with 'git notes merge --abort'.\n",
844 git_path(NOTES_MERGE_WORKTREE));
847 free_notes(t);
848 strbuf_release(&remote_ref);
849 strbuf_release(&msg);
850 return result < 0; /* return non-zero on conflicts */
853 #define IGNORE_MISSING 1
855 static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
857 int status;
858 unsigned char sha1[20];
859 if (get_sha1(name, sha1))
860 return error(_("Failed to resolve '%s' as a valid ref."), name);
861 status = remove_note(t, sha1);
862 if (status)
863 fprintf(stderr, _("Object %s has no note\n"), name);
864 else
865 fprintf(stderr, _("Removing note for object %s\n"), name);
866 return (flag & IGNORE_MISSING) ? 0 : status;
869 static int remove_cmd(int argc, const char **argv, const char *prefix)
871 unsigned flag = 0;
872 int from_stdin = 0;
873 struct option options[] = {
874 OPT_BIT(0, "ignore-missing", &flag,
875 N_("attempt to remove non-existent note is not an error"),
876 IGNORE_MISSING),
877 OPT_BOOL(0, "stdin", &from_stdin,
878 N_("read object names from the standard input")),
879 OPT_END()
881 struct notes_tree *t;
882 int retval = 0;
884 argc = parse_options(argc, argv, prefix, options,
885 git_notes_remove_usage, 0);
887 t = init_notes_check("remove");
889 if (!argc && !from_stdin) {
890 retval = remove_one_note(t, "HEAD", flag);
891 } else {
892 while (*argv) {
893 retval |= remove_one_note(t, *argv, flag);
894 argv++;
897 if (from_stdin) {
898 struct strbuf sb = STRBUF_INIT;
899 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
900 strbuf_rtrim(&sb);
901 retval |= remove_one_note(t, sb.buf, flag);
903 strbuf_release(&sb);
905 if (!retval)
906 commit_notes(t, "Notes removed by 'git notes remove'");
907 free_notes(t);
908 return retval;
911 static int prune(int argc, const char **argv, const char *prefix)
913 struct notes_tree *t;
914 int show_only = 0, verbose = 0;
915 struct option options[] = {
916 OPT__DRY_RUN(&show_only, "do not remove, show only"),
917 OPT__VERBOSE(&verbose, "report pruned notes"),
918 OPT_END()
921 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
924 if (argc) {
925 error(_("too many parameters"));
926 usage_with_options(git_notes_prune_usage, options);
929 t = init_notes_check("prune");
931 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
932 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
933 if (!show_only)
934 commit_notes(t, "Notes removed by 'git notes prune'");
935 free_notes(t);
936 return 0;
939 static int get_ref(int argc, const char **argv, const char *prefix)
941 struct option options[] = { OPT_END() };
942 argc = parse_options(argc, argv, prefix, options,
943 git_notes_get_ref_usage, 0);
945 if (argc) {
946 error("too many parameters");
947 usage_with_options(git_notes_get_ref_usage, options);
950 puts(default_notes_ref());
951 return 0;
954 int cmd_notes(int argc, const char **argv, const char *prefix)
956 int result;
957 const char *override_notes_ref = NULL;
958 struct option options[] = {
959 OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
960 N_("use notes from <notes-ref>")),
961 OPT_END()
964 git_config(git_default_config, NULL);
965 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
966 PARSE_OPT_STOP_AT_NON_OPTION);
968 if (override_notes_ref) {
969 struct strbuf sb = STRBUF_INIT;
970 strbuf_addstr(&sb, override_notes_ref);
971 expand_notes_ref(&sb);
972 setenv("GIT_NOTES_REF", sb.buf, 1);
973 strbuf_release(&sb);
976 if (argc < 1 || !strcmp(argv[0], "list"))
977 result = list(argc, argv, prefix);
978 else if (!strcmp(argv[0], "add"))
979 result = add(argc, argv, prefix);
980 else if (!strcmp(argv[0], "copy"))
981 result = copy(argc, argv, prefix);
982 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
983 result = append_edit(argc, argv, prefix);
984 else if (!strcmp(argv[0], "show"))
985 result = show(argc, argv, prefix);
986 else if (!strcmp(argv[0], "merge"))
987 result = merge(argc, argv, prefix);
988 else if (!strcmp(argv[0], "remove"))
989 result = remove_cmd(argc, argv, prefix);
990 else if (!strcmp(argv[0], "prune"))
991 result = prune(argc, argv, prefix);
992 else if (!strcmp(argv[0], "get-ref"))
993 result = get_ref(argc, argv, prefix);
994 else {
995 result = error(_("Unknown subcommand: %s"), argv[0]);
996 usage_with_options(git_notes_usage, options);
999 return result ? 1 : 0;