Update draft release notes to 1.8.3
[git/jnareb-git.git] / builtin / notes.c
blob57748a6fb67820c15ee6a8a92b32d4a0392dd933
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"
22 static void commit_notes(struct notes_tree *t, const char *msg);
23 static combine_notes_fn parse_combine_notes_fn(const char *v);
25 static const char * const git_notes_usage[] = {
26 N_("git notes [--ref <notes_ref>] [list [<object>]]"),
27 N_("git notes [--ref <notes_ref>] add [-f] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
28 N_("git notes [--ref <notes_ref>] copy [-f] <from-object> <to-object>"),
29 N_("git notes [--ref <notes_ref>] append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
30 N_("git notes [--ref <notes_ref>] edit [<object>]"),
31 N_("git notes [--ref <notes_ref>] show [<object>]"),
32 N_("git notes [--ref <notes_ref>] merge [-v | -q] [-s <strategy> ] <notes_ref>"),
33 N_("git notes merge --commit [-v | -q]"),
34 N_("git notes merge --abort [-v | -q]"),
35 N_("git notes [--ref <notes_ref>] remove [<object>...]"),
36 N_("git notes [--ref <notes_ref>] prune [-n | -v]"),
37 N_("git notes [--ref <notes_ref>] get-ref"),
38 NULL
41 static const char * const git_notes_list_usage[] = {
42 N_("git notes [list [<object>]]"),
43 NULL
46 static const char * const git_notes_add_usage[] = {
47 N_("git notes add [<options>] [<object>]"),
48 NULL
51 static const char * const git_notes_copy_usage[] = {
52 N_("git notes copy [<options>] <from-object> <to-object>"),
53 N_("git notes copy --stdin [<from-object> <to-object>]..."),
54 NULL
57 static const char * const git_notes_append_usage[] = {
58 N_("git notes append [<options>] [<object>]"),
59 NULL
62 static const char * const git_notes_edit_usage[] = {
63 N_("git notes edit [<object>]"),
64 NULL
67 static const char * const git_notes_show_usage[] = {
68 N_("git notes show [<object>]"),
69 NULL
72 static const char * const git_notes_merge_usage[] = {
73 N_("git notes merge [<options>] <notes_ref>"),
74 N_("git notes merge --commit [<options>]"),
75 N_("git notes merge --abort [<options>]"),
76 NULL
79 static const char * const git_notes_remove_usage[] = {
80 N_("git notes remove [<object>]"),
81 NULL
84 static const char * const git_notes_prune_usage[] = {
85 N_("git notes prune [<options>]"),
86 NULL
89 static const char * const git_notes_get_ref_usage[] = {
90 N_("git notes get-ref"),
91 NULL
94 static const char note_template[] =
95 "\nWrite/edit the notes for the following object:\n";
97 struct msg_arg {
98 int given;
99 int use_editor;
100 struct strbuf buf;
103 static int list_each_note(const unsigned char *object_sha1,
104 const unsigned char *note_sha1, char *note_path,
105 void *cb_data)
107 printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
108 return 0;
111 static void write_note_data(int fd, const unsigned char *sha1)
113 unsigned long size;
114 enum object_type type;
115 char *buf = read_sha1_file(sha1, &type, &size);
116 if (buf) {
117 if (size)
118 write_or_die(fd, buf, size);
119 free(buf);
123 static void write_commented_object(int fd, const unsigned char *object)
125 const char *show_args[5] =
126 {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
127 struct child_process show;
128 struct strbuf buf = STRBUF_INIT;
129 struct strbuf cbuf = STRBUF_INIT;
131 /* Invoke "git show --stat --no-notes $object" */
132 memset(&show, 0, sizeof(show));
133 show.argv = show_args;
134 show.no_stdin = 1;
135 show.out = -1;
136 show.err = 0;
137 show.git_cmd = 1;
138 if (start_command(&show))
139 die(_("unable to start 'show' for object '%s'"),
140 sha1_to_hex(object));
142 if (strbuf_read(&buf, show.out, 0) < 0)
143 die_errno(_("could not read 'show' output"));
144 strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
145 write_or_die(fd, cbuf.buf, cbuf.len);
147 strbuf_release(&cbuf);
148 strbuf_release(&buf);
150 if (finish_command(&show))
151 die(_("failed to finish 'show' for object '%s'"),
152 sha1_to_hex(object));
155 static void create_note(const unsigned char *object, struct msg_arg *msg,
156 int append_only, const unsigned char *prev,
157 unsigned char *result)
159 char *path = NULL;
161 if (msg->use_editor || !msg->given) {
162 int fd;
163 struct strbuf buf = STRBUF_INIT;
165 /* write the template message before editing: */
166 path = git_pathdup("NOTES_EDITMSG");
167 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
168 if (fd < 0)
169 die_errno(_("could not create file '%s'"), path);
171 if (msg->given)
172 write_or_die(fd, msg->buf.buf, msg->buf.len);
173 else if (prev && !append_only)
174 write_note_data(fd, prev);
176 strbuf_addch(&buf, '\n');
177 strbuf_add_commented_lines(&buf, note_template, strlen(note_template));
178 strbuf_addch(&buf, '\n');
179 write_or_die(fd, buf.buf, buf.len);
181 write_commented_object(fd, object);
183 close(fd);
184 strbuf_release(&buf);
185 strbuf_reset(&(msg->buf));
187 if (launch_editor(path, &(msg->buf), NULL)) {
188 die(_("Please supply the note contents using either -m" \
189 " or -F option"));
191 stripspace(&(msg->buf), 1);
194 if (prev && append_only) {
195 /* Append buf to previous note contents */
196 unsigned long size;
197 enum object_type type;
198 char *prev_buf = read_sha1_file(prev, &type, &size);
200 strbuf_grow(&(msg->buf), size + 1);
201 if (msg->buf.len && prev_buf && size)
202 strbuf_insert(&(msg->buf), 0, "\n", 1);
203 if (prev_buf && size)
204 strbuf_insert(&(msg->buf), 0, prev_buf, size);
205 free(prev_buf);
208 if (!msg->buf.len) {
209 fprintf(stderr, _("Removing note for object %s\n"),
210 sha1_to_hex(object));
211 hashclr(result);
212 } else {
213 if (write_sha1_file(msg->buf.buf, msg->buf.len, blob_type, result)) {
214 error(_("unable to write note object"));
215 if (path)
216 error(_("The note contents has been left in %s"),
217 path);
218 exit(128);
222 if (path) {
223 unlink_or_warn(path);
224 free(path);
228 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
230 struct msg_arg *msg = opt->value;
232 strbuf_grow(&(msg->buf), strlen(arg) + 2);
233 if (msg->buf.len)
234 strbuf_addch(&(msg->buf), '\n');
235 strbuf_addstr(&(msg->buf), arg);
236 stripspace(&(msg->buf), 0);
238 msg->given = 1;
239 return 0;
242 static int parse_file_arg(const struct option *opt, const char *arg, int unset)
244 struct msg_arg *msg = opt->value;
246 if (msg->buf.len)
247 strbuf_addch(&(msg->buf), '\n');
248 if (!strcmp(arg, "-")) {
249 if (strbuf_read(&(msg->buf), 0, 1024) < 0)
250 die_errno(_("cannot read '%s'"), arg);
251 } else if (strbuf_read_file(&(msg->buf), arg, 1024) < 0)
252 die_errno(_("could not open or read '%s'"), arg);
253 stripspace(&(msg->buf), 0);
255 msg->given = 1;
256 return 0;
259 static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
261 struct msg_arg *msg = opt->value;
262 char *buf;
263 unsigned char object[20];
264 enum object_type type;
265 unsigned long len;
267 if (msg->buf.len)
268 strbuf_addch(&(msg->buf), '\n');
270 if (get_sha1(arg, object))
271 die(_("Failed to resolve '%s' as a valid ref."), arg);
272 if (!(buf = read_sha1_file(object, &type, &len)) || !len) {
273 free(buf);
274 die(_("Failed to read object '%s'."), arg);;
276 strbuf_add(&(msg->buf), buf, len);
277 free(buf);
279 msg->given = 1;
280 return 0;
283 static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
285 struct msg_arg *msg = opt->value;
286 msg->use_editor = 1;
287 return parse_reuse_arg(opt, arg, unset);
290 static void commit_notes(struct notes_tree *t, const char *msg)
292 struct strbuf buf = STRBUF_INIT;
293 unsigned char commit_sha1[20];
295 if (!t)
296 t = &default_notes_tree;
297 if (!t->initialized || !t->ref || !*t->ref)
298 die(_("Cannot commit uninitialized/unreferenced notes tree"));
299 if (!t->dirty)
300 return; /* don't have to commit an unchanged tree */
302 /* Prepare commit message and reflog message */
303 strbuf_addstr(&buf, msg);
304 if (buf.buf[buf.len - 1] != '\n')
305 strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
307 create_notes_commit(t, NULL, &buf, commit_sha1);
308 strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
309 update_ref(buf.buf, t->ref, commit_sha1, NULL, 0, DIE_ON_ERR);
311 strbuf_release(&buf);
314 static combine_notes_fn parse_combine_notes_fn(const char *v)
316 if (!strcasecmp(v, "overwrite"))
317 return combine_notes_overwrite;
318 else if (!strcasecmp(v, "ignore"))
319 return combine_notes_ignore;
320 else if (!strcasecmp(v, "concatenate"))
321 return combine_notes_concatenate;
322 else if (!strcasecmp(v, "cat_sort_uniq"))
323 return combine_notes_cat_sort_uniq;
324 else
325 return NULL;
328 static int notes_rewrite_config(const char *k, const char *v, void *cb)
330 struct notes_rewrite_cfg *c = cb;
331 if (!prefixcmp(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
332 c->enabled = git_config_bool(k, v);
333 return 0;
334 } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
335 if (!v)
336 config_error_nonbool(k);
337 c->combine = parse_combine_notes_fn(v);
338 if (!c->combine) {
339 error(_("Bad notes.rewriteMode value: '%s'"), v);
340 return 1;
342 return 0;
343 } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
344 /* note that a refs/ prefix is implied in the
345 * underlying for_each_glob_ref */
346 if (!prefixcmp(v, "refs/notes/"))
347 string_list_add_refs_by_glob(c->refs, v);
348 else
349 warning(_("Refusing to rewrite notes in %s"
350 " (outside of refs/notes/)"), v);
351 return 0;
354 return 0;
358 struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
360 struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
361 const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
362 const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
363 c->cmd = cmd;
364 c->enabled = 1;
365 c->combine = combine_notes_concatenate;
366 c->refs = xcalloc(1, sizeof(struct string_list));
367 c->refs->strdup_strings = 1;
368 c->refs_from_env = 0;
369 c->mode_from_env = 0;
370 if (rewrite_mode_env) {
371 c->mode_from_env = 1;
372 c->combine = parse_combine_notes_fn(rewrite_mode_env);
373 if (!c->combine)
374 /* TRANSLATORS: The first %s is the name of the
375 environment variable, the second %s is its value */
376 error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT,
377 rewrite_mode_env);
379 if (rewrite_refs_env) {
380 c->refs_from_env = 1;
381 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
383 git_config(notes_rewrite_config, c);
384 if (!c->enabled || !c->refs->nr) {
385 string_list_clear(c->refs, 0);
386 free(c->refs);
387 free(c);
388 return NULL;
390 c->trees = load_notes_trees(c->refs);
391 string_list_clear(c->refs, 0);
392 free(c->refs);
393 return c;
396 int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
397 const unsigned char *from_obj, const unsigned char *to_obj)
399 int ret = 0;
400 int i;
401 for (i = 0; c->trees[i]; i++)
402 ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
403 return ret;
406 void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c)
408 int i;
409 for (i = 0; c->trees[i]; i++) {
410 commit_notes(c->trees[i], "Notes added by 'git notes copy'");
411 free_notes(c->trees[i]);
413 free(c->trees);
414 free(c);
417 static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
419 struct strbuf buf = STRBUF_INIT;
420 struct notes_rewrite_cfg *c = NULL;
421 struct notes_tree *t = NULL;
422 int ret = 0;
424 if (rewrite_cmd) {
425 c = init_copy_notes_for_rewrite(rewrite_cmd);
426 if (!c)
427 return 0;
428 } else {
429 init_notes(NULL, NULL, NULL, 0);
430 t = &default_notes_tree;
433 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
434 unsigned char from_obj[20], to_obj[20];
435 struct strbuf **split;
436 int err;
438 split = strbuf_split(&buf, ' ');
439 if (!split[0] || !split[1])
440 die(_("Malformed input line: '%s'."), buf.buf);
441 strbuf_rtrim(split[0]);
442 strbuf_rtrim(split[1]);
443 if (get_sha1(split[0]->buf, from_obj))
444 die(_("Failed to resolve '%s' as a valid ref."), split[0]->buf);
445 if (get_sha1(split[1]->buf, to_obj))
446 die(_("Failed to resolve '%s' as a valid ref."), split[1]->buf);
448 if (rewrite_cmd)
449 err = copy_note_for_rewrite(c, from_obj, to_obj);
450 else
451 err = copy_note(t, from_obj, to_obj, force,
452 combine_notes_overwrite);
454 if (err) {
455 error(_("Failed to copy notes from '%s' to '%s'"),
456 split[0]->buf, split[1]->buf);
457 ret = 1;
460 strbuf_list_free(split);
463 if (!rewrite_cmd) {
464 commit_notes(t, "Notes added by 'git notes copy'");
465 free_notes(t);
466 } else {
467 finish_copy_notes_for_rewrite(c);
469 return ret;
472 static struct notes_tree *init_notes_check(const char *subcommand)
474 struct notes_tree *t;
475 init_notes(NULL, NULL, NULL, 0);
476 t = &default_notes_tree;
478 if (prefixcmp(t->ref, "refs/notes/"))
479 die("Refusing to %s notes in %s (outside of refs/notes/)",
480 subcommand, t->ref);
481 return t;
484 static int list(int argc, const char **argv, const char *prefix)
486 struct notes_tree *t;
487 unsigned char object[20];
488 const unsigned char *note;
489 int retval = -1;
490 struct option options[] = {
491 OPT_END()
494 if (argc)
495 argc = parse_options(argc, argv, prefix, options,
496 git_notes_list_usage, 0);
498 if (1 < argc) {
499 error(_("too many parameters"));
500 usage_with_options(git_notes_list_usage, options);
503 t = init_notes_check("list");
504 if (argc) {
505 if (get_sha1(argv[0], object))
506 die(_("Failed to resolve '%s' as a valid ref."), argv[0]);
507 note = get_note(t, object);
508 if (note) {
509 puts(sha1_to_hex(note));
510 retval = 0;
511 } else
512 retval = error(_("No note found for object %s."),
513 sha1_to_hex(object));
514 } else
515 retval = for_each_note(t, 0, list_each_note, NULL);
517 free_notes(t);
518 return retval;
521 static int append_edit(int argc, const char **argv, const char *prefix);
523 static int add(int argc, const char **argv, const char *prefix)
525 int retval = 0, force = 0;
526 const char *object_ref;
527 struct notes_tree *t;
528 unsigned char object[20], new_note[20];
529 char logmsg[100];
530 const unsigned char *note;
531 struct msg_arg msg = { 0, 0, STRBUF_INIT };
532 struct option options[] = {
533 { OPTION_CALLBACK, 'm', "message", &msg, N_("message"),
534 N_("note contents as a string"), PARSE_OPT_NONEG,
535 parse_msg_arg},
536 { OPTION_CALLBACK, 'F', "file", &msg, N_("file"),
537 N_("note contents in a file"), PARSE_OPT_NONEG,
538 parse_file_arg},
539 { OPTION_CALLBACK, 'c', "reedit-message", &msg, N_("object"),
540 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
541 parse_reedit_arg},
542 { OPTION_CALLBACK, 'C', "reuse-message", &msg, N_("object"),
543 N_("reuse specified note object"), PARSE_OPT_NONEG,
544 parse_reuse_arg},
545 OPT__FORCE(&force, N_("replace existing notes")),
546 OPT_END()
549 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
550 PARSE_OPT_KEEP_ARGV0);
552 if (2 < argc) {
553 error(_("too many parameters"));
554 usage_with_options(git_notes_add_usage, options);
557 object_ref = argc > 1 ? argv[1] : "HEAD";
559 if (get_sha1(object_ref, object))
560 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
562 t = init_notes_check("add");
563 note = get_note(t, object);
565 if (note) {
566 if (!force) {
567 if (!msg.given) {
569 * Redirect to "edit" subcommand.
571 * We only end up here if none of -m/-F/-c/-C
572 * or -f are given. The original args are
573 * therefore still in argv[0-1].
575 argv[0] = "edit";
576 free_notes(t);
577 return append_edit(argc, argv, prefix);
579 retval = error(_("Cannot add notes. Found existing notes "
580 "for object %s. Use '-f' to overwrite "
581 "existing notes"), sha1_to_hex(object));
582 goto out;
584 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
585 sha1_to_hex(object));
588 create_note(object, &msg, 0, note, new_note);
590 if (is_null_sha1(new_note))
591 remove_note(t, object);
592 else if (add_note(t, object, new_note, combine_notes_overwrite))
593 die("BUG: combine_notes_overwrite failed");
595 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
596 is_null_sha1(new_note) ? "removed" : "added", "add");
597 commit_notes(t, logmsg);
598 out:
599 free_notes(t);
600 strbuf_release(&(msg.buf));
601 return retval;
604 static int copy(int argc, const char **argv, const char *prefix)
606 int retval = 0, force = 0, from_stdin = 0;
607 const unsigned char *from_note, *note;
608 const char *object_ref;
609 unsigned char object[20], from_obj[20];
610 struct notes_tree *t;
611 const char *rewrite_cmd = NULL;
612 struct option options[] = {
613 OPT__FORCE(&force, N_("replace existing notes")),
614 OPT_BOOLEAN(0, "stdin", &from_stdin, N_("read objects from stdin")),
615 OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"),
616 N_("load rewriting config for <command> (implies "
617 "--stdin)")),
618 OPT_END()
621 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
624 if (from_stdin || rewrite_cmd) {
625 if (argc) {
626 error(_("too many parameters"));
627 usage_with_options(git_notes_copy_usage, options);
628 } else {
629 return notes_copy_from_stdin(force, rewrite_cmd);
633 if (argc < 2) {
634 error(_("too few parameters"));
635 usage_with_options(git_notes_copy_usage, options);
637 if (2 < argc) {
638 error(_("too many parameters"));
639 usage_with_options(git_notes_copy_usage, options);
642 if (get_sha1(argv[0], from_obj))
643 die(_("Failed to resolve '%s' as a valid ref."), argv[0]);
645 object_ref = 1 < argc ? argv[1] : "HEAD";
647 if (get_sha1(object_ref, object))
648 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
650 t = init_notes_check("copy");
651 note = get_note(t, object);
653 if (note) {
654 if (!force) {
655 retval = error(_("Cannot copy notes. Found existing "
656 "notes for object %s. Use '-f' to "
657 "overwrite existing notes"),
658 sha1_to_hex(object));
659 goto out;
661 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
662 sha1_to_hex(object));
665 from_note = get_note(t, from_obj);
666 if (!from_note) {
667 retval = error(_("Missing notes on source object %s. Cannot "
668 "copy."), sha1_to_hex(from_obj));
669 goto out;
672 if (add_note(t, object, from_note, combine_notes_overwrite))
673 die("BUG: combine_notes_overwrite failed");
674 commit_notes(t, "Notes added by 'git notes copy'");
675 out:
676 free_notes(t);
677 return retval;
680 static int append_edit(int argc, const char **argv, const char *prefix)
682 const char *object_ref;
683 struct notes_tree *t;
684 unsigned char object[20], new_note[20];
685 const unsigned char *note;
686 char logmsg[100];
687 const char * const *usage;
688 struct msg_arg msg = { 0, 0, STRBUF_INIT };
689 struct option options[] = {
690 { OPTION_CALLBACK, 'm', "message", &msg, N_("message"),
691 N_("note contents as a string"), PARSE_OPT_NONEG,
692 parse_msg_arg},
693 { OPTION_CALLBACK, 'F', "file", &msg, N_("file"),
694 N_("note contents in a file"), PARSE_OPT_NONEG,
695 parse_file_arg},
696 { OPTION_CALLBACK, 'c', "reedit-message", &msg, N_("object"),
697 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
698 parse_reedit_arg},
699 { OPTION_CALLBACK, 'C', "reuse-message", &msg, N_("object"),
700 N_("reuse specified note object"), PARSE_OPT_NONEG,
701 parse_reuse_arg},
702 OPT_END()
704 int edit = !strcmp(argv[0], "edit");
706 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
707 argc = parse_options(argc, argv, prefix, options, usage,
708 PARSE_OPT_KEEP_ARGV0);
710 if (2 < argc) {
711 error(_("too many parameters"));
712 usage_with_options(usage, options);
715 if (msg.given && edit)
716 fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
717 "for the 'edit' subcommand.\n"
718 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
720 object_ref = 1 < argc ? argv[1] : "HEAD";
722 if (get_sha1(object_ref, object))
723 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
725 t = init_notes_check(argv[0]);
726 note = get_note(t, object);
728 create_note(object, &msg, !edit, note, new_note);
730 if (is_null_sha1(new_note))
731 remove_note(t, object);
732 else if (add_note(t, object, new_note, combine_notes_overwrite))
733 die("BUG: combine_notes_overwrite failed");
735 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
736 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
737 commit_notes(t, logmsg);
738 free_notes(t);
739 strbuf_release(&(msg.buf));
740 return 0;
743 static int show(int argc, const char **argv, const char *prefix)
745 const char *object_ref;
746 struct notes_tree *t;
747 unsigned char object[20];
748 const unsigned char *note;
749 int retval;
750 struct option options[] = {
751 OPT_END()
754 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
757 if (1 < argc) {
758 error(_("too many parameters"));
759 usage_with_options(git_notes_show_usage, options);
762 object_ref = argc ? argv[0] : "HEAD";
764 if (get_sha1(object_ref, object))
765 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
767 t = init_notes_check("show");
768 note = get_note(t, object);
770 if (!note)
771 retval = error(_("No note found for object %s."),
772 sha1_to_hex(object));
773 else {
774 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
775 retval = execv_git_cmd(show_args);
777 free_notes(t);
778 return retval;
781 static int merge_abort(struct notes_merge_options *o)
783 int ret = 0;
786 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
787 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
790 if (delete_ref("NOTES_MERGE_PARTIAL", NULL, 0))
791 ret += error("Failed to delete ref NOTES_MERGE_PARTIAL");
792 if (delete_ref("NOTES_MERGE_REF", NULL, REF_NODEREF))
793 ret += error("Failed to delete ref NOTES_MERGE_REF");
794 if (notes_merge_abort(o))
795 ret += error("Failed to remove 'git notes merge' worktree");
796 return ret;
799 static int merge_commit(struct notes_merge_options *o)
801 struct strbuf msg = STRBUF_INIT;
802 unsigned char sha1[20], parent_sha1[20];
803 struct notes_tree *t;
804 struct commit *partial;
805 struct pretty_print_context pretty_ctx;
806 void *local_ref_to_free;
807 int ret;
810 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
811 * and target notes ref from .git/NOTES_MERGE_REF.
814 if (get_sha1("NOTES_MERGE_PARTIAL", sha1))
815 die("Failed to read ref NOTES_MERGE_PARTIAL");
816 else if (!(partial = lookup_commit_reference(sha1)))
817 die("Could not find commit from NOTES_MERGE_PARTIAL.");
818 else if (parse_commit(partial))
819 die("Could not parse commit from NOTES_MERGE_PARTIAL.");
821 if (partial->parents)
822 hashcpy(parent_sha1, partial->parents->item->object.sha1);
823 else
824 hashclr(parent_sha1);
826 t = xcalloc(1, sizeof(struct notes_tree));
827 init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
829 o->local_ref = local_ref_to_free =
830 resolve_refdup("NOTES_MERGE_REF", sha1, 0, NULL);
831 if (!o->local_ref)
832 die("Failed to resolve NOTES_MERGE_REF");
834 if (notes_merge_commit(o, t, partial, sha1))
835 die("Failed to finalize notes merge");
837 /* Reuse existing commit message in reflog message */
838 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
839 format_commit_message(partial, "%s", &msg, &pretty_ctx);
840 strbuf_trim(&msg);
841 strbuf_insert(&msg, 0, "notes: ", 7);
842 update_ref(msg.buf, o->local_ref, sha1,
843 is_null_sha1(parent_sha1) ? NULL : parent_sha1,
844 0, DIE_ON_ERR);
846 free_notes(t);
847 strbuf_release(&msg);
848 ret = merge_abort(o);
849 free(local_ref_to_free);
850 return ret;
853 static int merge(int argc, const char **argv, const char *prefix)
855 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
856 unsigned char result_sha1[20];
857 struct notes_tree *t;
858 struct notes_merge_options o;
859 int do_merge = 0, do_commit = 0, do_abort = 0;
860 int verbosity = 0, result;
861 const char *strategy = NULL;
862 struct option options[] = {
863 OPT_GROUP(N_("General options")),
864 OPT__VERBOSITY(&verbosity),
865 OPT_GROUP(N_("Merge options")),
866 OPT_STRING('s', "strategy", &strategy, N_("strategy"),
867 N_("resolve notes conflicts using the given strategy "
868 "(manual/ours/theirs/union/cat_sort_uniq)")),
869 OPT_GROUP(N_("Committing unmerged notes")),
870 { OPTION_BOOLEAN, 0, "commit", &do_commit, NULL,
871 N_("finalize notes merge by committing unmerged notes"),
872 PARSE_OPT_NOARG | PARSE_OPT_NONEG },
873 OPT_GROUP(N_("Aborting notes merge resolution")),
874 { OPTION_BOOLEAN, 0, "abort", &do_abort, NULL,
875 N_("abort notes merge"),
876 PARSE_OPT_NOARG | PARSE_OPT_NONEG },
877 OPT_END()
880 argc = parse_options(argc, argv, prefix, options,
881 git_notes_merge_usage, 0);
883 if (strategy || do_commit + do_abort == 0)
884 do_merge = 1;
885 if (do_merge + do_commit + do_abort != 1) {
886 error("cannot mix --commit, --abort or -s/--strategy");
887 usage_with_options(git_notes_merge_usage, options);
890 if (do_merge && argc != 1) {
891 error("Must specify a notes ref to merge");
892 usage_with_options(git_notes_merge_usage, options);
893 } else if (!do_merge && argc) {
894 error("too many parameters");
895 usage_with_options(git_notes_merge_usage, options);
898 init_notes_merge_options(&o);
899 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
901 if (do_abort)
902 return merge_abort(&o);
903 if (do_commit)
904 return merge_commit(&o);
906 o.local_ref = default_notes_ref();
907 strbuf_addstr(&remote_ref, argv[0]);
908 expand_notes_ref(&remote_ref);
909 o.remote_ref = remote_ref.buf;
911 if (strategy) {
912 if (!strcmp(strategy, "manual"))
913 o.strategy = NOTES_MERGE_RESOLVE_MANUAL;
914 else if (!strcmp(strategy, "ours"))
915 o.strategy = NOTES_MERGE_RESOLVE_OURS;
916 else if (!strcmp(strategy, "theirs"))
917 o.strategy = NOTES_MERGE_RESOLVE_THEIRS;
918 else if (!strcmp(strategy, "union"))
919 o.strategy = NOTES_MERGE_RESOLVE_UNION;
920 else if (!strcmp(strategy, "cat_sort_uniq"))
921 o.strategy = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ;
922 else {
923 error("Unknown -s/--strategy: %s", strategy);
924 usage_with_options(git_notes_merge_usage, options);
928 t = init_notes_check("merge");
930 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
931 remote_ref.buf, default_notes_ref());
932 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
934 result = notes_merge(&o, t, result_sha1);
936 if (result >= 0) /* Merge resulted (trivially) in result_sha1 */
937 /* Update default notes ref with new commit */
938 update_ref(msg.buf, default_notes_ref(), result_sha1, NULL,
939 0, DIE_ON_ERR);
940 else { /* Merge has unresolved conflicts */
941 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
942 update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_sha1, NULL,
943 0, DIE_ON_ERR);
944 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
945 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
946 die("Failed to store link to current notes ref (%s)",
947 default_notes_ref());
948 printf("Automatic notes merge failed. Fix conflicts in %s and "
949 "commit the result with 'git notes merge --commit', or "
950 "abort the merge with 'git notes merge --abort'.\n",
951 git_path(NOTES_MERGE_WORKTREE));
954 free_notes(t);
955 strbuf_release(&remote_ref);
956 strbuf_release(&msg);
957 return result < 0; /* return non-zero on conflicts */
960 #define IGNORE_MISSING 1
962 static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
964 int status;
965 unsigned char sha1[20];
966 if (get_sha1(name, sha1))
967 return error(_("Failed to resolve '%s' as a valid ref."), name);
968 status = remove_note(t, sha1);
969 if (status)
970 fprintf(stderr, _("Object %s has no note\n"), name);
971 else
972 fprintf(stderr, _("Removing note for object %s\n"), name);
973 return (flag & IGNORE_MISSING) ? 0 : status;
976 static int remove_cmd(int argc, const char **argv, const char *prefix)
978 unsigned flag = 0;
979 int from_stdin = 0;
980 struct option options[] = {
981 OPT_BIT(0, "ignore-missing", &flag,
982 N_("attempt to remove non-existent note is not an error"),
983 IGNORE_MISSING),
984 OPT_BOOLEAN(0, "stdin", &from_stdin,
985 N_("read object names from the standard input")),
986 OPT_END()
988 struct notes_tree *t;
989 int retval = 0;
991 argc = parse_options(argc, argv, prefix, options,
992 git_notes_remove_usage, 0);
994 t = init_notes_check("remove");
996 if (!argc && !from_stdin) {
997 retval = remove_one_note(t, "HEAD", flag);
998 } else {
999 while (*argv) {
1000 retval |= remove_one_note(t, *argv, flag);
1001 argv++;
1004 if (from_stdin) {
1005 struct strbuf sb = STRBUF_INIT;
1006 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1007 strbuf_rtrim(&sb);
1008 retval |= remove_one_note(t, sb.buf, flag);
1010 strbuf_release(&sb);
1012 if (!retval)
1013 commit_notes(t, "Notes removed by 'git notes remove'");
1014 free_notes(t);
1015 return retval;
1018 static int prune(int argc, const char **argv, const char *prefix)
1020 struct notes_tree *t;
1021 int show_only = 0, verbose = 0;
1022 struct option options[] = {
1023 OPT__DRY_RUN(&show_only, "do not remove, show only"),
1024 OPT__VERBOSE(&verbose, "report pruned notes"),
1025 OPT_END()
1028 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
1031 if (argc) {
1032 error(_("too many parameters"));
1033 usage_with_options(git_notes_prune_usage, options);
1036 t = init_notes_check("prune");
1038 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
1039 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
1040 if (!show_only)
1041 commit_notes(t, "Notes removed by 'git notes prune'");
1042 free_notes(t);
1043 return 0;
1046 static int get_ref(int argc, const char **argv, const char *prefix)
1048 struct option options[] = { OPT_END() };
1049 argc = parse_options(argc, argv, prefix, options,
1050 git_notes_get_ref_usage, 0);
1052 if (argc) {
1053 error("too many parameters");
1054 usage_with_options(git_notes_get_ref_usage, options);
1057 puts(default_notes_ref());
1058 return 0;
1061 int cmd_notes(int argc, const char **argv, const char *prefix)
1063 int result;
1064 const char *override_notes_ref = NULL;
1065 struct option options[] = {
1066 OPT_STRING(0, "ref", &override_notes_ref, N_("notes_ref"),
1067 N_("use notes from <notes_ref>")),
1068 OPT_END()
1071 git_config(git_default_config, NULL);
1072 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
1073 PARSE_OPT_STOP_AT_NON_OPTION);
1075 if (override_notes_ref) {
1076 struct strbuf sb = STRBUF_INIT;
1077 strbuf_addstr(&sb, override_notes_ref);
1078 expand_notes_ref(&sb);
1079 setenv("GIT_NOTES_REF", sb.buf, 1);
1080 strbuf_release(&sb);
1083 if (argc < 1 || !strcmp(argv[0], "list"))
1084 result = list(argc, argv, prefix);
1085 else if (!strcmp(argv[0], "add"))
1086 result = add(argc, argv, prefix);
1087 else if (!strcmp(argv[0], "copy"))
1088 result = copy(argc, argv, prefix);
1089 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
1090 result = append_edit(argc, argv, prefix);
1091 else if (!strcmp(argv[0], "show"))
1092 result = show(argc, argv, prefix);
1093 else if (!strcmp(argv[0], "merge"))
1094 result = merge(argc, argv, prefix);
1095 else if (!strcmp(argv[0], "remove"))
1096 result = remove_cmd(argc, argv, prefix);
1097 else if (!strcmp(argv[0], "prune"))
1098 result = prune(argc, argv, prefix);
1099 else if (!strcmp(argv[0], "get-ref"))
1100 result = get_ref(argc, argv, prefix);
1101 else {
1102 result = error(_("Unknown subcommand: %s"), argv[0]);
1103 usage_with_options(git_notes_usage, options);
1106 return result ? 1 : 0;