doc: describe git svn init --ignore-refs
[git/git-svn.git] / builtin / notes.c
blob7196bff0eb2454eee86f87241706523c0266e66e
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/"))
344 * TRANSLATORS: the first %s will be replaced by a git
345 * notes command: 'add', 'merge', 'remove', etc.
347 die(_("refusing to %s notes in %s (outside of refs/notes/)"),
348 subcommand, ref);
349 return t;
352 static int list(int argc, const char **argv, const char *prefix)
354 struct notes_tree *t;
355 unsigned char object[20];
356 const unsigned char *note;
357 int retval = -1;
358 struct option options[] = {
359 OPT_END()
362 if (argc)
363 argc = parse_options(argc, argv, prefix, options,
364 git_notes_list_usage, 0);
366 if (1 < argc) {
367 error(_("too many parameters"));
368 usage_with_options(git_notes_list_usage, options);
371 t = init_notes_check("list", 0);
372 if (argc) {
373 if (get_sha1(argv[0], object))
374 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
375 note = get_note(t, object);
376 if (note) {
377 puts(sha1_to_hex(note));
378 retval = 0;
379 } else
380 retval = error(_("no note found for object %s."),
381 sha1_to_hex(object));
382 } else
383 retval = for_each_note(t, 0, list_each_note, NULL);
385 free_notes(t);
386 return retval;
389 static int append_edit(int argc, const char **argv, const char *prefix);
391 static int add(int argc, const char **argv, const char *prefix)
393 int force = 0, allow_empty = 0;
394 const char *object_ref;
395 struct notes_tree *t;
396 unsigned char object[20], new_note[20];
397 const unsigned char *note;
398 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
399 struct option options[] = {
400 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
401 N_("note contents as a string"), PARSE_OPT_NONEG,
402 parse_msg_arg},
403 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
404 N_("note contents in a file"), PARSE_OPT_NONEG,
405 parse_file_arg},
406 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
407 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
408 parse_reedit_arg},
409 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
410 N_("reuse specified note object"), PARSE_OPT_NONEG,
411 parse_reuse_arg},
412 OPT_BOOL(0, "allow-empty", &allow_empty,
413 N_("allow storing empty note")),
414 OPT__FORCE(&force, N_("replace existing notes")),
415 OPT_END()
418 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
419 PARSE_OPT_KEEP_ARGV0);
421 if (2 < argc) {
422 error(_("too many parameters"));
423 usage_with_options(git_notes_add_usage, options);
426 object_ref = argc > 1 ? argv[1] : "HEAD";
428 if (get_sha1(object_ref, object))
429 die(_("failed to resolve '%s' as a valid ref."), object_ref);
431 t = init_notes_check("add", NOTES_INIT_WRITABLE);
432 note = get_note(t, object);
434 if (note) {
435 if (!force) {
436 free_notes(t);
437 if (d.given) {
438 free_note_data(&d);
439 return error(_("Cannot add notes. "
440 "Found existing notes for object %s. "
441 "Use '-f' to overwrite existing notes"),
442 sha1_to_hex(object));
445 * Redirect to "edit" subcommand.
447 * We only end up here if none of -m/-F/-c/-C or -f are
448 * given. The original args are therefore still in
449 * argv[0-1].
451 argv[0] = "edit";
452 return append_edit(argc, argv, prefix);
454 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
455 sha1_to_hex(object));
458 prepare_note_data(object, &d, note);
459 if (d.buf.len || allow_empty) {
460 write_note_data(&d, new_note);
461 if (add_note(t, object, new_note, combine_notes_overwrite))
462 die("BUG: combine_notes_overwrite failed");
463 commit_notes(t, "Notes added by 'git notes add'");
464 } else {
465 fprintf(stderr, _("Removing note for object %s\n"),
466 sha1_to_hex(object));
467 remove_note(t, object);
468 commit_notes(t, "Notes removed by 'git notes add'");
471 free_note_data(&d);
472 free_notes(t);
473 return 0;
476 static int copy(int argc, const char **argv, const char *prefix)
478 int retval = 0, force = 0, from_stdin = 0;
479 const unsigned char *from_note, *note;
480 const char *object_ref;
481 unsigned char object[20], from_obj[20];
482 struct notes_tree *t;
483 const char *rewrite_cmd = NULL;
484 struct option options[] = {
485 OPT__FORCE(&force, N_("replace existing notes")),
486 OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")),
487 OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"),
488 N_("load rewriting config for <command> (implies "
489 "--stdin)")),
490 OPT_END()
493 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
496 if (from_stdin || rewrite_cmd) {
497 if (argc) {
498 error(_("too many parameters"));
499 usage_with_options(git_notes_copy_usage, options);
500 } else {
501 return notes_copy_from_stdin(force, rewrite_cmd);
505 if (argc < 2) {
506 error(_("too few parameters"));
507 usage_with_options(git_notes_copy_usage, options);
509 if (2 < argc) {
510 error(_("too many parameters"));
511 usage_with_options(git_notes_copy_usage, options);
514 if (get_sha1(argv[0], from_obj))
515 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
517 object_ref = 1 < argc ? argv[1] : "HEAD";
519 if (get_sha1(object_ref, object))
520 die(_("failed to resolve '%s' as a valid ref."), object_ref);
522 t = init_notes_check("copy", NOTES_INIT_WRITABLE);
523 note = get_note(t, object);
525 if (note) {
526 if (!force) {
527 retval = error(_("Cannot copy notes. Found existing "
528 "notes for object %s. Use '-f' to "
529 "overwrite existing notes"),
530 sha1_to_hex(object));
531 goto out;
533 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
534 sha1_to_hex(object));
537 from_note = get_note(t, from_obj);
538 if (!from_note) {
539 retval = error(_("missing notes on source object %s. Cannot "
540 "copy."), sha1_to_hex(from_obj));
541 goto out;
544 if (add_note(t, object, from_note, combine_notes_overwrite))
545 die("BUG: combine_notes_overwrite failed");
546 commit_notes(t, "Notes added by 'git notes copy'");
547 out:
548 free_notes(t);
549 return retval;
552 static int append_edit(int argc, const char **argv, const char *prefix)
554 int allow_empty = 0;
555 const char *object_ref;
556 struct notes_tree *t;
557 unsigned char object[20], new_note[20];
558 const unsigned char *note;
559 char *logmsg;
560 const char * const *usage;
561 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
562 struct option options[] = {
563 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
564 N_("note contents as a string"), PARSE_OPT_NONEG,
565 parse_msg_arg},
566 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
567 N_("note contents in a file"), PARSE_OPT_NONEG,
568 parse_file_arg},
569 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
570 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
571 parse_reedit_arg},
572 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
573 N_("reuse specified note object"), PARSE_OPT_NONEG,
574 parse_reuse_arg},
575 OPT_BOOL(0, "allow-empty", &allow_empty,
576 N_("allow storing empty note")),
577 OPT_END()
579 int edit = !strcmp(argv[0], "edit");
581 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
582 argc = parse_options(argc, argv, prefix, options, usage,
583 PARSE_OPT_KEEP_ARGV0);
585 if (2 < argc) {
586 error(_("too many parameters"));
587 usage_with_options(usage, options);
590 if (d.given && edit)
591 fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
592 "for the 'edit' subcommand.\n"
593 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
595 object_ref = 1 < argc ? argv[1] : "HEAD";
597 if (get_sha1(object_ref, object))
598 die(_("failed to resolve '%s' as a valid ref."), object_ref);
600 t = init_notes_check(argv[0], NOTES_INIT_WRITABLE);
601 note = get_note(t, object);
603 prepare_note_data(object, &d, edit ? note : NULL);
605 if (note && !edit) {
606 /* Append buf to previous note contents */
607 unsigned long size;
608 enum object_type type;
609 char *prev_buf = read_sha1_file(note, &type, &size);
611 strbuf_grow(&d.buf, size + 1);
612 if (d.buf.len && prev_buf && size)
613 strbuf_insert(&d.buf, 0, "\n", 1);
614 if (prev_buf && size)
615 strbuf_insert(&d.buf, 0, prev_buf, size);
616 free(prev_buf);
619 if (d.buf.len || allow_empty) {
620 write_note_data(&d, new_note);
621 if (add_note(t, object, new_note, combine_notes_overwrite))
622 die("BUG: combine_notes_overwrite failed");
623 logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]);
624 } else {
625 fprintf(stderr, _("Removing note for object %s\n"),
626 sha1_to_hex(object));
627 remove_note(t, object);
628 logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]);
630 commit_notes(t, logmsg);
632 free(logmsg);
633 free_note_data(&d);
634 free_notes(t);
635 return 0;
638 static int show(int argc, const char **argv, const char *prefix)
640 const char *object_ref;
641 struct notes_tree *t;
642 unsigned char object[20];
643 const unsigned char *note;
644 int retval;
645 struct option options[] = {
646 OPT_END()
649 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
652 if (1 < argc) {
653 error(_("too many parameters"));
654 usage_with_options(git_notes_show_usage, options);
657 object_ref = argc ? argv[0] : "HEAD";
659 if (get_sha1(object_ref, object))
660 die(_("failed to resolve '%s' as a valid ref."), object_ref);
662 t = init_notes_check("show", 0);
663 note = get_note(t, object);
665 if (!note)
666 retval = error(_("no note found for object %s."),
667 sha1_to_hex(object));
668 else {
669 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
670 retval = execv_git_cmd(show_args);
672 free_notes(t);
673 return retval;
676 static int merge_abort(struct notes_merge_options *o)
678 int ret = 0;
681 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
682 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
685 if (delete_ref(NULL, "NOTES_MERGE_PARTIAL", NULL, 0))
686 ret += error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
687 if (delete_ref(NULL, "NOTES_MERGE_REF", NULL, REF_NODEREF))
688 ret += error(_("failed to delete ref NOTES_MERGE_REF"));
689 if (notes_merge_abort(o))
690 ret += error(_("failed to remove 'git notes merge' worktree"));
691 return ret;
694 static int merge_commit(struct notes_merge_options *o)
696 struct strbuf msg = STRBUF_INIT;
697 struct object_id oid, parent_oid;
698 struct notes_tree *t;
699 struct commit *partial;
700 struct pretty_print_context pretty_ctx;
701 void *local_ref_to_free;
702 int ret;
705 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
706 * and target notes ref from .git/NOTES_MERGE_REF.
709 if (get_oid("NOTES_MERGE_PARTIAL", &oid))
710 die(_("failed to read ref NOTES_MERGE_PARTIAL"));
711 else if (!(partial = lookup_commit_reference(&oid)))
712 die(_("could not find commit from NOTES_MERGE_PARTIAL."));
713 else if (parse_commit(partial))
714 die(_("could not parse commit from NOTES_MERGE_PARTIAL."));
716 if (partial->parents)
717 oidcpy(&parent_oid, &partial->parents->item->object.oid);
718 else
719 oidclr(&parent_oid);
721 t = xcalloc(1, sizeof(struct notes_tree));
722 init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
724 o->local_ref = local_ref_to_free =
725 resolve_refdup("NOTES_MERGE_REF", 0, oid.hash, NULL);
726 if (!o->local_ref)
727 die(_("failed to resolve NOTES_MERGE_REF"));
729 if (notes_merge_commit(o, t, partial, oid.hash))
730 die(_("failed to finalize notes merge"));
732 /* Reuse existing commit message in reflog message */
733 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
734 format_commit_message(partial, "%s", &msg, &pretty_ctx);
735 strbuf_trim(&msg);
736 strbuf_insert(&msg, 0, "notes: ", 7);
737 update_ref(msg.buf, o->local_ref, oid.hash,
738 is_null_oid(&parent_oid) ? NULL : parent_oid.hash,
739 0, UPDATE_REFS_DIE_ON_ERR);
741 free_notes(t);
742 strbuf_release(&msg);
743 ret = merge_abort(o);
744 free(local_ref_to_free);
745 return ret;
748 static int git_config_get_notes_strategy(const char *key,
749 enum notes_merge_strategy *strategy)
751 char *value;
753 if (git_config_get_string(key, &value))
754 return 1;
755 if (parse_notes_merge_strategy(value, strategy))
756 git_die_config(key, _("unknown notes merge strategy %s"), value);
758 free(value);
759 return 0;
762 static int merge(int argc, const char **argv, const char *prefix)
764 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
765 unsigned char result_sha1[20];
766 struct notes_tree *t;
767 struct notes_merge_options o;
768 int do_merge = 0, do_commit = 0, do_abort = 0;
769 int verbosity = 0, result;
770 const char *strategy = NULL;
771 struct option options[] = {
772 OPT_GROUP(N_("General options")),
773 OPT__VERBOSITY(&verbosity),
774 OPT_GROUP(N_("Merge options")),
775 OPT_STRING('s', "strategy", &strategy, N_("strategy"),
776 N_("resolve notes conflicts using the given strategy "
777 "(manual/ours/theirs/union/cat_sort_uniq)")),
778 OPT_GROUP(N_("Committing unmerged notes")),
779 { OPTION_SET_INT, 0, "commit", &do_commit, NULL,
780 N_("finalize notes merge by committing unmerged notes"),
781 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
782 OPT_GROUP(N_("Aborting notes merge resolution")),
783 { OPTION_SET_INT, 0, "abort", &do_abort, NULL,
784 N_("abort notes merge"),
785 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
786 OPT_END()
789 argc = parse_options(argc, argv, prefix, options,
790 git_notes_merge_usage, 0);
792 if (strategy || do_commit + do_abort == 0)
793 do_merge = 1;
794 if (do_merge + do_commit + do_abort != 1) {
795 error(_("cannot mix --commit, --abort or -s/--strategy"));
796 usage_with_options(git_notes_merge_usage, options);
799 if (do_merge && argc != 1) {
800 error(_("must specify a notes ref to merge"));
801 usage_with_options(git_notes_merge_usage, options);
802 } else if (!do_merge && argc) {
803 error(_("too many parameters"));
804 usage_with_options(git_notes_merge_usage, options);
807 init_notes_merge_options(&o);
808 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
810 if (do_abort)
811 return merge_abort(&o);
812 if (do_commit)
813 return merge_commit(&o);
815 o.local_ref = default_notes_ref();
816 strbuf_addstr(&remote_ref, argv[0]);
817 expand_loose_notes_ref(&remote_ref);
818 o.remote_ref = remote_ref.buf;
820 t = init_notes_check("merge", NOTES_INIT_WRITABLE);
822 if (strategy) {
823 if (parse_notes_merge_strategy(strategy, &o.strategy)) {
824 error(_("unknown -s/--strategy: %s"), strategy);
825 usage_with_options(git_notes_merge_usage, options);
827 } else {
828 struct strbuf merge_key = STRBUF_INIT;
829 const char *short_ref = NULL;
831 if (!skip_prefix(o.local_ref, "refs/notes/", &short_ref))
832 die("BUG: local ref %s is outside of refs/notes/",
833 o.local_ref);
835 strbuf_addf(&merge_key, "notes.%s.mergeStrategy", short_ref);
837 if (git_config_get_notes_strategy(merge_key.buf, &o.strategy))
838 git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy);
840 strbuf_release(&merge_key);
843 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
844 remote_ref.buf, default_notes_ref());
845 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
847 result = notes_merge(&o, t, result_sha1);
849 if (result >= 0) /* Merge resulted (trivially) in result_sha1 */
850 /* Update default notes ref with new commit */
851 update_ref(msg.buf, default_notes_ref(), result_sha1, NULL,
852 0, UPDATE_REFS_DIE_ON_ERR);
853 else { /* Merge has unresolved conflicts */
854 const struct worktree *wt;
855 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
856 update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_sha1, NULL,
857 0, UPDATE_REFS_DIE_ON_ERR);
858 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
859 wt = find_shared_symref("NOTES_MERGE_REF", default_notes_ref());
860 if (wt)
861 die(_("a notes merge into %s is already in-progress at %s"),
862 default_notes_ref(), wt->path);
863 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
864 die(_("failed to store link to current notes ref (%s)"),
865 default_notes_ref());
866 printf(_("Automatic notes merge failed. Fix conflicts in %s and "
867 "commit the result with 'git notes merge --commit', or "
868 "abort the merge with 'git notes merge --abort'.\n"),
869 git_path(NOTES_MERGE_WORKTREE));
872 free_notes(t);
873 strbuf_release(&remote_ref);
874 strbuf_release(&msg);
875 return result < 0; /* return non-zero on conflicts */
878 #define IGNORE_MISSING 1
880 static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
882 int status;
883 unsigned char sha1[20];
884 if (get_sha1(name, sha1))
885 return error(_("Failed to resolve '%s' as a valid ref."), name);
886 status = remove_note(t, sha1);
887 if (status)
888 fprintf(stderr, _("Object %s has no note\n"), name);
889 else
890 fprintf(stderr, _("Removing note for object %s\n"), name);
891 return (flag & IGNORE_MISSING) ? 0 : status;
894 static int remove_cmd(int argc, const char **argv, const char *prefix)
896 unsigned flag = 0;
897 int from_stdin = 0;
898 struct option options[] = {
899 OPT_BIT(0, "ignore-missing", &flag,
900 N_("attempt to remove non-existent note is not an error"),
901 IGNORE_MISSING),
902 OPT_BOOL(0, "stdin", &from_stdin,
903 N_("read object names from the standard input")),
904 OPT_END()
906 struct notes_tree *t;
907 int retval = 0;
909 argc = parse_options(argc, argv, prefix, options,
910 git_notes_remove_usage, 0);
912 t = init_notes_check("remove", NOTES_INIT_WRITABLE);
914 if (!argc && !from_stdin) {
915 retval = remove_one_note(t, "HEAD", flag);
916 } else {
917 while (*argv) {
918 retval |= remove_one_note(t, *argv, flag);
919 argv++;
922 if (from_stdin) {
923 struct strbuf sb = STRBUF_INIT;
924 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
925 strbuf_rtrim(&sb);
926 retval |= remove_one_note(t, sb.buf, flag);
928 strbuf_release(&sb);
930 if (!retval)
931 commit_notes(t, "Notes removed by 'git notes remove'");
932 free_notes(t);
933 return retval;
936 static int prune(int argc, const char **argv, const char *prefix)
938 struct notes_tree *t;
939 int show_only = 0, verbose = 0;
940 struct option options[] = {
941 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
942 OPT__VERBOSE(&verbose, N_("report pruned notes")),
943 OPT_END()
946 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
949 if (argc) {
950 error(_("too many parameters"));
951 usage_with_options(git_notes_prune_usage, options);
954 t = init_notes_check("prune", NOTES_INIT_WRITABLE);
956 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
957 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
958 if (!show_only)
959 commit_notes(t, "Notes removed by 'git notes prune'");
960 free_notes(t);
961 return 0;
964 static int get_ref(int argc, const char **argv, const char *prefix)
966 struct option options[] = { OPT_END() };
967 argc = parse_options(argc, argv, prefix, options,
968 git_notes_get_ref_usage, 0);
970 if (argc) {
971 error(_("too many parameters"));
972 usage_with_options(git_notes_get_ref_usage, options);
975 puts(default_notes_ref());
976 return 0;
979 int cmd_notes(int argc, const char **argv, const char *prefix)
981 int result;
982 const char *override_notes_ref = NULL;
983 struct option options[] = {
984 OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
985 N_("use notes from <notes-ref>")),
986 OPT_END()
989 git_config(git_default_config, NULL);
990 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
991 PARSE_OPT_STOP_AT_NON_OPTION);
993 if (override_notes_ref) {
994 struct strbuf sb = STRBUF_INIT;
995 strbuf_addstr(&sb, override_notes_ref);
996 expand_notes_ref(&sb);
997 setenv("GIT_NOTES_REF", sb.buf, 1);
998 strbuf_release(&sb);
1001 if (argc < 1 || !strcmp(argv[0], "list"))
1002 result = list(argc, argv, prefix);
1003 else if (!strcmp(argv[0], "add"))
1004 result = add(argc, argv, prefix);
1005 else if (!strcmp(argv[0], "copy"))
1006 result = copy(argc, argv, prefix);
1007 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
1008 result = append_edit(argc, argv, prefix);
1009 else if (!strcmp(argv[0], "show"))
1010 result = show(argc, argv, prefix);
1011 else if (!strcmp(argv[0], "merge"))
1012 result = merge(argc, argv, prefix);
1013 else if (!strcmp(argv[0], "remove"))
1014 result = remove_cmd(argc, argv, prefix);
1015 else if (!strcmp(argv[0], "prune"))
1016 result = prune(argc, argv, prefix);
1017 else if (!strcmp(argv[0], "get-ref"))
1018 result = get_ref(argc, argv, prefix);
1019 else {
1020 result = error(_("unknown subcommand: %s"), argv[0]);
1021 usage_with_options(git_notes_usage, options);
1024 return result ? 1 : 0;