Interested in helping open source friends on HP-UX?
[git/mjg.git] / builtin / notes.c
blob63f95fc55439060670879fd987033000b8ba3401
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] [--allow-empty] [-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 [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
28 N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<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 note_data {
96 int given;
97 int use_editor;
98 char *edit_path;
99 struct strbuf buf;
102 static void free_note_data(struct note_data *d)
104 if (d->edit_path) {
105 unlink_or_warn(d->edit_path);
106 free(d->edit_path);
108 strbuf_release(&d->buf);
111 static int list_each_note(const unsigned char *object_sha1,
112 const unsigned char *note_sha1, char *note_path,
113 void *cb_data)
115 printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
116 return 0;
119 static void copy_obj_to_fd(int fd, const unsigned char *sha1)
121 unsigned long size;
122 enum object_type type;
123 char *buf = read_sha1_file(sha1, &type, &size);
124 if (buf) {
125 if (size)
126 write_or_die(fd, buf, size);
127 free(buf);
131 static void write_commented_object(int fd, const unsigned char *object)
133 const char *show_args[5] =
134 {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
135 struct child_process show = CHILD_PROCESS_INIT;
136 struct strbuf buf = STRBUF_INIT;
137 struct strbuf cbuf = STRBUF_INIT;
139 /* Invoke "git show --stat --no-notes $object" */
140 show.argv = show_args;
141 show.no_stdin = 1;
142 show.out = -1;
143 show.err = 0;
144 show.git_cmd = 1;
145 if (start_command(&show))
146 die(_("unable to start 'show' for object '%s'"),
147 sha1_to_hex(object));
149 if (strbuf_read(&buf, show.out, 0) < 0)
150 die_errno(_("could not read 'show' output"));
151 strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
152 write_or_die(fd, cbuf.buf, cbuf.len);
154 strbuf_release(&cbuf);
155 strbuf_release(&buf);
157 if (finish_command(&show))
158 die(_("failed to finish 'show' for object '%s'"),
159 sha1_to_hex(object));
162 static void prepare_note_data(const unsigned char *object, struct note_data *d,
163 const unsigned char *old_note)
165 if (d->use_editor || !d->given) {
166 int fd;
167 struct strbuf buf = STRBUF_INIT;
169 /* write the template message before editing: */
170 d->edit_path = git_pathdup("NOTES_EDITMSG");
171 fd = open(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
172 if (fd < 0)
173 die_errno(_("could not create file '%s'"), d->edit_path);
175 if (d->given)
176 write_or_die(fd, d->buf.buf, d->buf.len);
177 else if (old_note)
178 copy_obj_to_fd(fd, old_note);
180 strbuf_addch(&buf, '\n');
181 strbuf_add_commented_lines(&buf, note_template, strlen(note_template));
182 strbuf_addch(&buf, '\n');
183 write_or_die(fd, buf.buf, buf.len);
185 write_commented_object(fd, object);
187 close(fd);
188 strbuf_release(&buf);
189 strbuf_reset(&d->buf);
191 if (launch_editor(d->edit_path, &d->buf, NULL)) {
192 die(_("Please supply the note contents using either -m or -F option"));
194 stripspace(&d->buf, 1);
198 static void write_note_data(struct note_data *d, unsigned char *sha1)
200 if (write_sha1_file(d->buf.buf, d->buf.len, blob_type, sha1)) {
201 error(_("unable to write note object"));
202 if (d->edit_path)
203 error(_("The note contents have been left in %s"),
204 d->edit_path);
205 exit(128);
209 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
211 struct note_data *d = opt->value;
213 strbuf_grow(&d->buf, strlen(arg) + 2);
214 if (d->buf.len)
215 strbuf_addch(&d->buf, '\n');
216 strbuf_addstr(&d->buf, arg);
217 stripspace(&d->buf, 0);
219 d->given = 1;
220 return 0;
223 static int parse_file_arg(const struct option *opt, const char *arg, int unset)
225 struct note_data *d = opt->value;
227 if (d->buf.len)
228 strbuf_addch(&d->buf, '\n');
229 if (!strcmp(arg, "-")) {
230 if (strbuf_read(&d->buf, 0, 1024) < 0)
231 die_errno(_("cannot read '%s'"), arg);
232 } else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
233 die_errno(_("could not open or read '%s'"), arg);
234 stripspace(&d->buf, 0);
236 d->given = 1;
237 return 0;
240 static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
242 struct note_data *d = opt->value;
243 char *buf;
244 unsigned char object[20];
245 enum object_type type;
246 unsigned long len;
248 if (d->buf.len)
249 strbuf_addch(&d->buf, '\n');
251 if (get_sha1(arg, object))
252 die(_("Failed to resolve '%s' as a valid ref."), arg);
253 if (!(buf = read_sha1_file(object, &type, &len))) {
254 free(buf);
255 die(_("Failed to read object '%s'."), arg);
257 if (type != OBJ_BLOB) {
258 free(buf);
259 die(_("Cannot read note data from non-blob object '%s'."), arg);
261 strbuf_add(&d->buf, buf, len);
262 free(buf);
264 d->given = 1;
265 return 0;
268 static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
270 struct note_data *d = opt->value;
271 d->use_editor = 1;
272 return parse_reuse_arg(opt, arg, unset);
275 static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
277 struct strbuf buf = STRBUF_INIT;
278 struct notes_rewrite_cfg *c = NULL;
279 struct notes_tree *t = NULL;
280 int ret = 0;
281 const char *msg = "Notes added by 'git notes copy'";
283 if (rewrite_cmd) {
284 c = init_copy_notes_for_rewrite(rewrite_cmd);
285 if (!c)
286 return 0;
287 } else {
288 init_notes(NULL, NULL, NULL, 0);
289 t = &default_notes_tree;
292 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
293 unsigned char from_obj[20], to_obj[20];
294 struct strbuf **split;
295 int err;
297 split = strbuf_split(&buf, ' ');
298 if (!split[0] || !split[1])
299 die(_("Malformed input line: '%s'."), buf.buf);
300 strbuf_rtrim(split[0]);
301 strbuf_rtrim(split[1]);
302 if (get_sha1(split[0]->buf, from_obj))
303 die(_("Failed to resolve '%s' as a valid ref."), split[0]->buf);
304 if (get_sha1(split[1]->buf, to_obj))
305 die(_("Failed to resolve '%s' as a valid ref."), split[1]->buf);
307 if (rewrite_cmd)
308 err = copy_note_for_rewrite(c, from_obj, to_obj);
309 else
310 err = copy_note(t, from_obj, to_obj, force,
311 combine_notes_overwrite);
313 if (err) {
314 error(_("Failed to copy notes from '%s' to '%s'"),
315 split[0]->buf, split[1]->buf);
316 ret = 1;
319 strbuf_list_free(split);
322 if (!rewrite_cmd) {
323 commit_notes(t, msg);
324 free_notes(t);
325 } else {
326 finish_copy_notes_for_rewrite(c, msg);
328 return ret;
331 static struct notes_tree *init_notes_check(const char *subcommand)
333 struct notes_tree *t;
334 init_notes(NULL, NULL, NULL, 0);
335 t = &default_notes_tree;
337 if (!starts_with(t->ref, "refs/notes/"))
338 die("Refusing to %s notes in %s (outside of refs/notes/)",
339 subcommand, t->ref);
340 return t;
343 static int list(int argc, const char **argv, const char *prefix)
345 struct notes_tree *t;
346 unsigned char object[20];
347 const unsigned char *note;
348 int retval = -1;
349 struct option options[] = {
350 OPT_END()
353 if (argc)
354 argc = parse_options(argc, argv, prefix, options,
355 git_notes_list_usage, 0);
357 if (1 < argc) {
358 error(_("too many parameters"));
359 usage_with_options(git_notes_list_usage, options);
362 t = init_notes_check("list");
363 if (argc) {
364 if (get_sha1(argv[0], object))
365 die(_("Failed to resolve '%s' as a valid ref."), argv[0]);
366 note = get_note(t, object);
367 if (note) {
368 puts(sha1_to_hex(note));
369 retval = 0;
370 } else
371 retval = error(_("No note found for object %s."),
372 sha1_to_hex(object));
373 } else
374 retval = for_each_note(t, 0, list_each_note, NULL);
376 free_notes(t);
377 return retval;
380 static int append_edit(int argc, const char **argv, const char *prefix);
382 static int add(int argc, const char **argv, const char *prefix)
384 int force = 0, allow_empty = 0;
385 const char *object_ref;
386 struct notes_tree *t;
387 unsigned char object[20], new_note[20];
388 const unsigned char *note;
389 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
390 struct option options[] = {
391 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
392 N_("note contents as a string"), PARSE_OPT_NONEG,
393 parse_msg_arg},
394 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
395 N_("note contents in a file"), PARSE_OPT_NONEG,
396 parse_file_arg},
397 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
398 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
399 parse_reedit_arg},
400 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
401 N_("reuse specified note object"), PARSE_OPT_NONEG,
402 parse_reuse_arg},
403 OPT_BOOL(0, "allow-empty", &allow_empty,
404 N_("allow storing empty note")),
405 OPT__FORCE(&force, N_("replace existing notes")),
406 OPT_END()
409 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
410 PARSE_OPT_KEEP_ARGV0);
412 if (2 < argc) {
413 error(_("too many parameters"));
414 usage_with_options(git_notes_add_usage, options);
417 object_ref = argc > 1 ? argv[1] : "HEAD";
419 if (get_sha1(object_ref, object))
420 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
422 t = init_notes_check("add");
423 note = get_note(t, object);
425 if (note) {
426 if (!force) {
427 free_notes(t);
428 if (d.given) {
429 free_note_data(&d);
430 return error(_("Cannot add notes. "
431 "Found existing notes for object %s. "
432 "Use '-f' to overwrite existing notes"),
433 sha1_to_hex(object));
436 * Redirect to "edit" subcommand.
438 * We only end up here if none of -m/-F/-c/-C or -f are
439 * given. The original args are therefore still in
440 * argv[0-1].
442 argv[0] = "edit";
443 return append_edit(argc, argv, prefix);
445 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
446 sha1_to_hex(object));
449 prepare_note_data(object, &d, note);
450 if (d.buf.len || allow_empty) {
451 write_note_data(&d, new_note);
452 if (add_note(t, object, new_note, combine_notes_overwrite))
453 die("BUG: combine_notes_overwrite failed");
454 commit_notes(t, "Notes added by 'git notes add'");
455 } else {
456 fprintf(stderr, _("Removing note for object %s\n"),
457 sha1_to_hex(object));
458 remove_note(t, object);
459 commit_notes(t, "Notes removed by 'git notes add'");
462 free_note_data(&d);
463 free_notes(t);
464 return 0;
467 static int copy(int argc, const char **argv, const char *prefix)
469 int retval = 0, force = 0, from_stdin = 0;
470 const unsigned char *from_note, *note;
471 const char *object_ref;
472 unsigned char object[20], from_obj[20];
473 struct notes_tree *t;
474 const char *rewrite_cmd = NULL;
475 struct option options[] = {
476 OPT__FORCE(&force, N_("replace existing notes")),
477 OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")),
478 OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"),
479 N_("load rewriting config for <command> (implies "
480 "--stdin)")),
481 OPT_END()
484 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
487 if (from_stdin || rewrite_cmd) {
488 if (argc) {
489 error(_("too many parameters"));
490 usage_with_options(git_notes_copy_usage, options);
491 } else {
492 return notes_copy_from_stdin(force, rewrite_cmd);
496 if (argc < 2) {
497 error(_("too few parameters"));
498 usage_with_options(git_notes_copy_usage, options);
500 if (2 < argc) {
501 error(_("too many parameters"));
502 usage_with_options(git_notes_copy_usage, options);
505 if (get_sha1(argv[0], from_obj))
506 die(_("Failed to resolve '%s' as a valid ref."), argv[0]);
508 object_ref = 1 < argc ? argv[1] : "HEAD";
510 if (get_sha1(object_ref, object))
511 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
513 t = init_notes_check("copy");
514 note = get_note(t, object);
516 if (note) {
517 if (!force) {
518 retval = error(_("Cannot copy notes. Found existing "
519 "notes for object %s. Use '-f' to "
520 "overwrite existing notes"),
521 sha1_to_hex(object));
522 goto out;
524 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
525 sha1_to_hex(object));
528 from_note = get_note(t, from_obj);
529 if (!from_note) {
530 retval = error(_("Missing notes on source object %s. Cannot "
531 "copy."), sha1_to_hex(from_obj));
532 goto out;
535 if (add_note(t, object, from_note, combine_notes_overwrite))
536 die("BUG: combine_notes_overwrite failed");
537 commit_notes(t, "Notes added by 'git notes copy'");
538 out:
539 free_notes(t);
540 return retval;
543 static int append_edit(int argc, const char **argv, const char *prefix)
545 int allow_empty = 0;
546 const char *object_ref;
547 struct notes_tree *t;
548 unsigned char object[20], new_note[20];
549 const unsigned char *note;
550 char logmsg[100];
551 const char * const *usage;
552 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
553 struct option options[] = {
554 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
555 N_("note contents as a string"), PARSE_OPT_NONEG,
556 parse_msg_arg},
557 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
558 N_("note contents in a file"), PARSE_OPT_NONEG,
559 parse_file_arg},
560 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
561 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
562 parse_reedit_arg},
563 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
564 N_("reuse specified note object"), PARSE_OPT_NONEG,
565 parse_reuse_arg},
566 OPT_BOOL(0, "allow-empty", &allow_empty,
567 N_("allow storing empty note")),
568 OPT_END()
570 int edit = !strcmp(argv[0], "edit");
572 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
573 argc = parse_options(argc, argv, prefix, options, usage,
574 PARSE_OPT_KEEP_ARGV0);
576 if (2 < argc) {
577 error(_("too many parameters"));
578 usage_with_options(usage, options);
581 if (d.given && edit)
582 fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
583 "for the 'edit' subcommand.\n"
584 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
586 object_ref = 1 < argc ? argv[1] : "HEAD";
588 if (get_sha1(object_ref, object))
589 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
591 t = init_notes_check(argv[0]);
592 note = get_note(t, object);
594 prepare_note_data(object, &d, edit ? note : NULL);
596 if (note && !edit) {
597 /* Append buf to previous note contents */
598 unsigned long size;
599 enum object_type type;
600 char *prev_buf = read_sha1_file(note, &type, &size);
602 strbuf_grow(&d.buf, size + 1);
603 if (d.buf.len && prev_buf && size)
604 strbuf_insert(&d.buf, 0, "\n", 1);
605 if (prev_buf && size)
606 strbuf_insert(&d.buf, 0, prev_buf, size);
607 free(prev_buf);
610 if (d.buf.len || allow_empty) {
611 write_note_data(&d, new_note);
612 if (add_note(t, object, new_note, combine_notes_overwrite))
613 die("BUG: combine_notes_overwrite failed");
614 snprintf(logmsg, sizeof(logmsg), "Notes added by 'git notes %s'",
615 argv[0]);
616 } else {
617 fprintf(stderr, _("Removing note for object %s\n"),
618 sha1_to_hex(object));
619 remove_note(t, object);
620 snprintf(logmsg, sizeof(logmsg), "Notes removed by 'git notes %s'",
621 argv[0]);
623 commit_notes(t, logmsg);
625 free_note_data(&d);
626 free_notes(t);
627 return 0;
630 static int show(int argc, const char **argv, const char *prefix)
632 const char *object_ref;
633 struct notes_tree *t;
634 unsigned char object[20];
635 const unsigned char *note;
636 int retval;
637 struct option options[] = {
638 OPT_END()
641 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
644 if (1 < argc) {
645 error(_("too many parameters"));
646 usage_with_options(git_notes_show_usage, options);
649 object_ref = argc ? argv[0] : "HEAD";
651 if (get_sha1(object_ref, object))
652 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
654 t = init_notes_check("show");
655 note = get_note(t, object);
657 if (!note)
658 retval = error(_("No note found for object %s."),
659 sha1_to_hex(object));
660 else {
661 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
662 retval = execv_git_cmd(show_args);
664 free_notes(t);
665 return retval;
668 static int merge_abort(struct notes_merge_options *o)
670 int ret = 0;
673 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
674 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
677 if (delete_ref("NOTES_MERGE_PARTIAL", NULL, 0))
678 ret += error("Failed to delete ref NOTES_MERGE_PARTIAL");
679 if (delete_ref("NOTES_MERGE_REF", NULL, REF_NODEREF))
680 ret += error("Failed to delete ref NOTES_MERGE_REF");
681 if (notes_merge_abort(o))
682 ret += error("Failed to remove 'git notes merge' worktree");
683 return ret;
686 static int merge_commit(struct notes_merge_options *o)
688 struct strbuf msg = STRBUF_INIT;
689 unsigned char sha1[20], parent_sha1[20];
690 struct notes_tree *t;
691 struct commit *partial;
692 struct pretty_print_context pretty_ctx;
693 void *local_ref_to_free;
694 int ret;
697 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
698 * and target notes ref from .git/NOTES_MERGE_REF.
701 if (get_sha1("NOTES_MERGE_PARTIAL", sha1))
702 die("Failed to read ref NOTES_MERGE_PARTIAL");
703 else if (!(partial = lookup_commit_reference(sha1)))
704 die("Could not find commit from NOTES_MERGE_PARTIAL.");
705 else if (parse_commit(partial))
706 die("Could not parse commit from NOTES_MERGE_PARTIAL.");
708 if (partial->parents)
709 hashcpy(parent_sha1, partial->parents->item->object.sha1);
710 else
711 hashclr(parent_sha1);
713 t = xcalloc(1, sizeof(struct notes_tree));
714 init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
716 o->local_ref = local_ref_to_free =
717 resolve_refdup("NOTES_MERGE_REF", 0, sha1, NULL);
718 if (!o->local_ref)
719 die("Failed to resolve NOTES_MERGE_REF");
721 if (notes_merge_commit(o, t, partial, sha1))
722 die("Failed to finalize notes merge");
724 /* Reuse existing commit message in reflog message */
725 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
726 format_commit_message(partial, "%s", &msg, &pretty_ctx);
727 strbuf_trim(&msg);
728 strbuf_insert(&msg, 0, "notes: ", 7);
729 update_ref(msg.buf, o->local_ref, sha1,
730 is_null_sha1(parent_sha1) ? NULL : parent_sha1,
731 0, UPDATE_REFS_DIE_ON_ERR);
733 free_notes(t);
734 strbuf_release(&msg);
735 ret = merge_abort(o);
736 free(local_ref_to_free);
737 return ret;
740 static int merge(int argc, const char **argv, const char *prefix)
742 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
743 unsigned char result_sha1[20];
744 struct notes_tree *t;
745 struct notes_merge_options o;
746 int do_merge = 0, do_commit = 0, do_abort = 0;
747 int verbosity = 0, result;
748 const char *strategy = NULL;
749 struct option options[] = {
750 OPT_GROUP(N_("General options")),
751 OPT__VERBOSITY(&verbosity),
752 OPT_GROUP(N_("Merge options")),
753 OPT_STRING('s', "strategy", &strategy, N_("strategy"),
754 N_("resolve notes conflicts using the given strategy "
755 "(manual/ours/theirs/union/cat_sort_uniq)")),
756 OPT_GROUP(N_("Committing unmerged notes")),
757 { OPTION_SET_INT, 0, "commit", &do_commit, NULL,
758 N_("finalize notes merge by committing unmerged notes"),
759 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
760 OPT_GROUP(N_("Aborting notes merge resolution")),
761 { OPTION_SET_INT, 0, "abort", &do_abort, NULL,
762 N_("abort notes merge"),
763 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
764 OPT_END()
767 argc = parse_options(argc, argv, prefix, options,
768 git_notes_merge_usage, 0);
770 if (strategy || do_commit + do_abort == 0)
771 do_merge = 1;
772 if (do_merge + do_commit + do_abort != 1) {
773 error("cannot mix --commit, --abort or -s/--strategy");
774 usage_with_options(git_notes_merge_usage, options);
777 if (do_merge && argc != 1) {
778 error("Must specify a notes ref to merge");
779 usage_with_options(git_notes_merge_usage, options);
780 } else if (!do_merge && argc) {
781 error("too many parameters");
782 usage_with_options(git_notes_merge_usage, options);
785 init_notes_merge_options(&o);
786 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
788 if (do_abort)
789 return merge_abort(&o);
790 if (do_commit)
791 return merge_commit(&o);
793 o.local_ref = default_notes_ref();
794 strbuf_addstr(&remote_ref, argv[0]);
795 expand_notes_ref(&remote_ref);
796 o.remote_ref = remote_ref.buf;
798 if (strategy) {
799 if (!strcmp(strategy, "manual"))
800 o.strategy = NOTES_MERGE_RESOLVE_MANUAL;
801 else if (!strcmp(strategy, "ours"))
802 o.strategy = NOTES_MERGE_RESOLVE_OURS;
803 else if (!strcmp(strategy, "theirs"))
804 o.strategy = NOTES_MERGE_RESOLVE_THEIRS;
805 else if (!strcmp(strategy, "union"))
806 o.strategy = NOTES_MERGE_RESOLVE_UNION;
807 else if (!strcmp(strategy, "cat_sort_uniq"))
808 o.strategy = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ;
809 else {
810 error("Unknown -s/--strategy: %s", strategy);
811 usage_with_options(git_notes_merge_usage, options);
815 t = init_notes_check("merge");
817 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
818 remote_ref.buf, default_notes_ref());
819 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
821 result = notes_merge(&o, t, result_sha1);
823 if (result >= 0) /* Merge resulted (trivially) in result_sha1 */
824 /* Update default notes ref with new commit */
825 update_ref(msg.buf, default_notes_ref(), result_sha1, NULL,
826 0, UPDATE_REFS_DIE_ON_ERR);
827 else { /* Merge has unresolved conflicts */
828 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
829 update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_sha1, NULL,
830 0, UPDATE_REFS_DIE_ON_ERR);
831 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
832 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
833 die("Failed to store link to current notes ref (%s)",
834 default_notes_ref());
835 printf("Automatic notes merge failed. Fix conflicts in %s and "
836 "commit the result with 'git notes merge --commit', or "
837 "abort the merge with 'git notes merge --abort'.\n",
838 git_path(NOTES_MERGE_WORKTREE));
841 free_notes(t);
842 strbuf_release(&remote_ref);
843 strbuf_release(&msg);
844 return result < 0; /* return non-zero on conflicts */
847 #define IGNORE_MISSING 1
849 static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
851 int status;
852 unsigned char sha1[20];
853 if (get_sha1(name, sha1))
854 return error(_("Failed to resolve '%s' as a valid ref."), name);
855 status = remove_note(t, sha1);
856 if (status)
857 fprintf(stderr, _("Object %s has no note\n"), name);
858 else
859 fprintf(stderr, _("Removing note for object %s\n"), name);
860 return (flag & IGNORE_MISSING) ? 0 : status;
863 static int remove_cmd(int argc, const char **argv, const char *prefix)
865 unsigned flag = 0;
866 int from_stdin = 0;
867 struct option options[] = {
868 OPT_BIT(0, "ignore-missing", &flag,
869 N_("attempt to remove non-existent note is not an error"),
870 IGNORE_MISSING),
871 OPT_BOOL(0, "stdin", &from_stdin,
872 N_("read object names from the standard input")),
873 OPT_END()
875 struct notes_tree *t;
876 int retval = 0;
878 argc = parse_options(argc, argv, prefix, options,
879 git_notes_remove_usage, 0);
881 t = init_notes_check("remove");
883 if (!argc && !from_stdin) {
884 retval = remove_one_note(t, "HEAD", flag);
885 } else {
886 while (*argv) {
887 retval |= remove_one_note(t, *argv, flag);
888 argv++;
891 if (from_stdin) {
892 struct strbuf sb = STRBUF_INIT;
893 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
894 strbuf_rtrim(&sb);
895 retval |= remove_one_note(t, sb.buf, flag);
897 strbuf_release(&sb);
899 if (!retval)
900 commit_notes(t, "Notes removed by 'git notes remove'");
901 free_notes(t);
902 return retval;
905 static int prune(int argc, const char **argv, const char *prefix)
907 struct notes_tree *t;
908 int show_only = 0, verbose = 0;
909 struct option options[] = {
910 OPT__DRY_RUN(&show_only, "do not remove, show only"),
911 OPT__VERBOSE(&verbose, "report pruned notes"),
912 OPT_END()
915 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
918 if (argc) {
919 error(_("too many parameters"));
920 usage_with_options(git_notes_prune_usage, options);
923 t = init_notes_check("prune");
925 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
926 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
927 if (!show_only)
928 commit_notes(t, "Notes removed by 'git notes prune'");
929 free_notes(t);
930 return 0;
933 static int get_ref(int argc, const char **argv, const char *prefix)
935 struct option options[] = { OPT_END() };
936 argc = parse_options(argc, argv, prefix, options,
937 git_notes_get_ref_usage, 0);
939 if (argc) {
940 error("too many parameters");
941 usage_with_options(git_notes_get_ref_usage, options);
944 puts(default_notes_ref());
945 return 0;
948 int cmd_notes(int argc, const char **argv, const char *prefix)
950 int result;
951 const char *override_notes_ref = NULL;
952 struct option options[] = {
953 OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
954 N_("use notes from <notes-ref>")),
955 OPT_END()
958 git_config(git_default_config, NULL);
959 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
960 PARSE_OPT_STOP_AT_NON_OPTION);
962 if (override_notes_ref) {
963 struct strbuf sb = STRBUF_INIT;
964 strbuf_addstr(&sb, override_notes_ref);
965 expand_notes_ref(&sb);
966 setenv("GIT_NOTES_REF", sb.buf, 1);
967 strbuf_release(&sb);
970 if (argc < 1 || !strcmp(argv[0], "list"))
971 result = list(argc, argv, prefix);
972 else if (!strcmp(argv[0], "add"))
973 result = add(argc, argv, prefix);
974 else if (!strcmp(argv[0], "copy"))
975 result = copy(argc, argv, prefix);
976 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
977 result = append_edit(argc, argv, prefix);
978 else if (!strcmp(argv[0], "show"))
979 result = show(argc, argv, prefix);
980 else if (!strcmp(argv[0], "merge"))
981 result = merge(argc, argv, prefix);
982 else if (!strcmp(argv[0], "remove"))
983 result = remove_cmd(argc, argv, prefix);
984 else if (!strcmp(argv[0], "prune"))
985 result = prune(argc, argv, prefix);
986 else if (!strcmp(argv[0], "get-ref"))
987 result = get_ref(argc, argv, prefix);
988 else {
989 result = error(_("Unknown subcommand: %s"), argv[0]);
990 usage_with_options(git_notes_usage, options);
993 return result ? 1 : 0;