Merge branch 'jh/dirstat'
[git/mingw/j6t.git] / builtin / notes.c
blobd6dcfcb0149ab6dc45f4a71e21cf77d13b494e18
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 const char * const git_notes_usage[] = {
23 "git notes [--ref <notes_ref>] [list [<object>]]",
24 "git notes [--ref <notes_ref>] add [-f] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
25 "git notes [--ref <notes_ref>] copy [-f] <from-object> <to-object>",
26 "git notes [--ref <notes_ref>] append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
27 "git notes [--ref <notes_ref>] edit [<object>]",
28 "git notes [--ref <notes_ref>] show [<object>]",
29 "git notes [--ref <notes_ref>] merge [-v | -q] [-s <strategy> ] <notes_ref>",
30 "git notes merge --commit [-v | -q]",
31 "git notes merge --abort [-v | -q]",
32 "git notes [--ref <notes_ref>] remove [<object>]",
33 "git notes [--ref <notes_ref>] prune [-n | -v]",
34 "git notes [--ref <notes_ref>] get-ref",
35 NULL
38 static const char * const git_notes_list_usage[] = {
39 "git notes [list [<object>]]",
40 NULL
43 static const char * const git_notes_add_usage[] = {
44 "git notes add [<options>] [<object>]",
45 NULL
48 static const char * const git_notes_copy_usage[] = {
49 "git notes copy [<options>] <from-object> <to-object>",
50 "git notes copy --stdin [<from-object> <to-object>]...",
51 NULL
54 static const char * const git_notes_append_usage[] = {
55 "git notes append [<options>] [<object>]",
56 NULL
59 static const char * const git_notes_edit_usage[] = {
60 "git notes edit [<object>]",
61 NULL
64 static const char * const git_notes_show_usage[] = {
65 "git notes show [<object>]",
66 NULL
69 static const char * const git_notes_merge_usage[] = {
70 "git notes merge [<options>] <notes_ref>",
71 "git notes merge --commit [<options>]",
72 "git notes merge --abort [<options>]",
73 NULL
76 static const char * const git_notes_remove_usage[] = {
77 "git notes remove [<object>]",
78 NULL
81 static const char * const git_notes_prune_usage[] = {
82 "git notes prune [<options>]",
83 NULL
86 static const char * const git_notes_get_ref_usage[] = {
87 "git notes get-ref",
88 NULL
91 static const char note_template[] =
92 "\n"
93 "#\n"
94 "# Write/edit the notes for the following object:\n"
95 "#\n";
97 struct msg_arg {
98 int given;
99 int use_editor;
100 struct strbuf buf;
103 static void expand_notes_ref(struct strbuf *sb)
105 if (!prefixcmp(sb->buf, "refs/notes/"))
106 return; /* we're happy */
107 else if (!prefixcmp(sb->buf, "notes/"))
108 strbuf_insert(sb, 0, "refs/", 5);
109 else
110 strbuf_insert(sb, 0, "refs/notes/", 11);
113 static int list_each_note(const unsigned char *object_sha1,
114 const unsigned char *note_sha1, char *note_path,
115 void *cb_data)
117 printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
118 return 0;
121 static void write_note_data(int fd, const unsigned char *sha1)
123 unsigned long size;
124 enum object_type type;
125 char *buf = read_sha1_file(sha1, &type, &size);
126 if (buf) {
127 if (size)
128 write_or_die(fd, buf, size);
129 free(buf);
133 static void write_commented_object(int fd, const unsigned char *object)
135 const char *show_args[5] =
136 {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
137 struct child_process show;
138 struct strbuf buf = STRBUF_INIT;
139 FILE *show_out;
141 /* Invoke "git show --stat --no-notes $object" */
142 memset(&show, 0, sizeof(show));
143 show.argv = show_args;
144 show.no_stdin = 1;
145 show.out = -1;
146 show.err = 0;
147 show.git_cmd = 1;
148 if (start_command(&show))
149 die(_("unable to start 'show' for object '%s'"),
150 sha1_to_hex(object));
152 /* Open the output as FILE* so strbuf_getline() can be used. */
153 show_out = xfdopen(show.out, "r");
154 if (show_out == NULL)
155 die_errno(_("can't fdopen 'show' output fd"));
157 /* Prepend "# " to each output line and write result to 'fd' */
158 while (strbuf_getline(&buf, show_out, '\n') != EOF) {
159 write_or_die(fd, "# ", 2);
160 write_or_die(fd, buf.buf, buf.len);
161 write_or_die(fd, "\n", 1);
163 strbuf_release(&buf);
164 if (fclose(show_out))
165 die_errno(_("failed to close pipe to 'show' for object '%s'"),
166 sha1_to_hex(object));
167 if (finish_command(&show))
168 die(_("failed to finish 'show' for object '%s'"),
169 sha1_to_hex(object));
172 static void create_note(const unsigned char *object, struct msg_arg *msg,
173 int append_only, const unsigned char *prev,
174 unsigned char *result)
176 char *path = NULL;
178 if (msg->use_editor || !msg->given) {
179 int fd;
181 /* write the template message before editing: */
182 path = git_pathdup("NOTES_EDITMSG");
183 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
184 if (fd < 0)
185 die_errno(_("could not create file '%s'"), path);
187 if (msg->given)
188 write_or_die(fd, msg->buf.buf, msg->buf.len);
189 else if (prev && !append_only)
190 write_note_data(fd, prev);
191 write_or_die(fd, note_template, strlen(note_template));
193 write_commented_object(fd, object);
195 close(fd);
196 strbuf_reset(&(msg->buf));
198 if (launch_editor(path, &(msg->buf), NULL)) {
199 die(_("Please supply the note contents using either -m" \
200 " or -F option"));
202 stripspace(&(msg->buf), 1);
205 if (prev && append_only) {
206 /* Append buf to previous note contents */
207 unsigned long size;
208 enum object_type type;
209 char *prev_buf = read_sha1_file(prev, &type, &size);
211 strbuf_grow(&(msg->buf), size + 1);
212 if (msg->buf.len && prev_buf && size)
213 strbuf_insert(&(msg->buf), 0, "\n", 1);
214 if (prev_buf && size)
215 strbuf_insert(&(msg->buf), 0, prev_buf, size);
216 free(prev_buf);
219 if (!msg->buf.len) {
220 fprintf(stderr, _("Removing note for object %s\n"),
221 sha1_to_hex(object));
222 hashclr(result);
223 } else {
224 if (write_sha1_file(msg->buf.buf, msg->buf.len, blob_type, result)) {
225 error(_("unable to write note object"));
226 if (path)
227 error(_("The note contents has been left in %s"),
228 path);
229 exit(128);
233 if (path) {
234 unlink_or_warn(path);
235 free(path);
239 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
241 struct msg_arg *msg = opt->value;
243 strbuf_grow(&(msg->buf), strlen(arg) + 2);
244 if (msg->buf.len)
245 strbuf_addch(&(msg->buf), '\n');
246 strbuf_addstr(&(msg->buf), arg);
247 stripspace(&(msg->buf), 0);
249 msg->given = 1;
250 return 0;
253 static int parse_file_arg(const struct option *opt, const char *arg, int unset)
255 struct msg_arg *msg = opt->value;
257 if (msg->buf.len)
258 strbuf_addch(&(msg->buf), '\n');
259 if (!strcmp(arg, "-")) {
260 if (strbuf_read(&(msg->buf), 0, 1024) < 0)
261 die_errno(_("cannot read '%s'"), arg);
262 } else if (strbuf_read_file(&(msg->buf), arg, 1024) < 0)
263 die_errno(_("could not open or read '%s'"), arg);
264 stripspace(&(msg->buf), 0);
266 msg->given = 1;
267 return 0;
270 static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
272 struct msg_arg *msg = opt->value;
273 char *buf;
274 unsigned char object[20];
275 enum object_type type;
276 unsigned long len;
278 if (msg->buf.len)
279 strbuf_addch(&(msg->buf), '\n');
281 if (get_sha1(arg, object))
282 die(_("Failed to resolve '%s' as a valid ref."), arg);
283 if (!(buf = read_sha1_file(object, &type, &len)) || !len) {
284 free(buf);
285 die(_("Failed to read object '%s'."), arg);;
287 strbuf_add(&(msg->buf), buf, len);
288 free(buf);
290 msg->given = 1;
291 return 0;
294 static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
296 struct msg_arg *msg = opt->value;
297 msg->use_editor = 1;
298 return parse_reuse_arg(opt, arg, unset);
301 void commit_notes(struct notes_tree *t, const char *msg)
303 struct strbuf buf = STRBUF_INIT;
304 unsigned char commit_sha1[20];
306 if (!t)
307 t = &default_notes_tree;
308 if (!t->initialized || !t->ref || !*t->ref)
309 die(_("Cannot commit uninitialized/unreferenced notes tree"));
310 if (!t->dirty)
311 return; /* don't have to commit an unchanged tree */
313 /* Prepare commit message and reflog message */
314 strbuf_addstr(&buf, "notes: "); /* commit message starts at index 7 */
315 strbuf_addstr(&buf, msg);
316 if (buf.buf[buf.len - 1] != '\n')
317 strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
319 create_notes_commit(t, NULL, buf.buf + 7, commit_sha1);
320 update_ref(buf.buf, t->ref, commit_sha1, NULL, 0, DIE_ON_ERR);
322 strbuf_release(&buf);
325 combine_notes_fn parse_combine_notes_fn(const char *v)
327 if (!strcasecmp(v, "overwrite"))
328 return combine_notes_overwrite;
329 else if (!strcasecmp(v, "ignore"))
330 return combine_notes_ignore;
331 else if (!strcasecmp(v, "concatenate"))
332 return combine_notes_concatenate;
333 else if (!strcasecmp(v, "cat_sort_uniq"))
334 return combine_notes_cat_sort_uniq;
335 else
336 return NULL;
339 static int notes_rewrite_config(const char *k, const char *v, void *cb)
341 struct notes_rewrite_cfg *c = cb;
342 if (!prefixcmp(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
343 c->enabled = git_config_bool(k, v);
344 return 0;
345 } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
346 if (!v)
347 config_error_nonbool(k);
348 c->combine = parse_combine_notes_fn(v);
349 if (!c->combine) {
350 error(_("Bad notes.rewriteMode value: '%s'"), v);
351 return 1;
353 return 0;
354 } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
355 /* note that a refs/ prefix is implied in the
356 * underlying for_each_glob_ref */
357 if (!prefixcmp(v, "refs/notes/"))
358 string_list_add_refs_by_glob(c->refs, v);
359 else
360 warning(_("Refusing to rewrite notes in %s"
361 " (outside of refs/notes/)"), v);
362 return 0;
365 return 0;
369 struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
371 struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
372 const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
373 const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
374 c->cmd = cmd;
375 c->enabled = 1;
376 c->combine = combine_notes_concatenate;
377 c->refs = xcalloc(1, sizeof(struct string_list));
378 c->refs->strdup_strings = 1;
379 c->refs_from_env = 0;
380 c->mode_from_env = 0;
381 if (rewrite_mode_env) {
382 c->mode_from_env = 1;
383 c->combine = parse_combine_notes_fn(rewrite_mode_env);
384 if (!c->combine)
385 /* TRANSLATORS: The first %s is the name of the
386 environment variable, the second %s is its value */
387 error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT,
388 rewrite_mode_env);
390 if (rewrite_refs_env) {
391 c->refs_from_env = 1;
392 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
394 git_config(notes_rewrite_config, c);
395 if (!c->enabled || !c->refs->nr) {
396 string_list_clear(c->refs, 0);
397 free(c->refs);
398 free(c);
399 return NULL;
401 c->trees = load_notes_trees(c->refs);
402 string_list_clear(c->refs, 0);
403 free(c->refs);
404 return c;
407 int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
408 const unsigned char *from_obj, const unsigned char *to_obj)
410 int ret = 0;
411 int i;
412 for (i = 0; c->trees[i]; i++)
413 ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
414 return ret;
417 void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c)
419 int i;
420 for (i = 0; c->trees[i]; i++) {
421 commit_notes(c->trees[i], "Notes added by 'git notes copy'");
422 free_notes(c->trees[i]);
424 free(c->trees);
425 free(c);
428 static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
430 struct strbuf buf = STRBUF_INIT;
431 struct notes_rewrite_cfg *c = NULL;
432 struct notes_tree *t = NULL;
433 int ret = 0;
435 if (rewrite_cmd) {
436 c = init_copy_notes_for_rewrite(rewrite_cmd);
437 if (!c)
438 return 0;
439 } else {
440 init_notes(NULL, NULL, NULL, 0);
441 t = &default_notes_tree;
444 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
445 unsigned char from_obj[20], to_obj[20];
446 struct strbuf **split;
447 int err;
449 split = strbuf_split(&buf, ' ');
450 if (!split[0] || !split[1])
451 die(_("Malformed input line: '%s'."), buf.buf);
452 strbuf_rtrim(split[0]);
453 strbuf_rtrim(split[1]);
454 if (get_sha1(split[0]->buf, from_obj))
455 die(_("Failed to resolve '%s' as a valid ref."), split[0]->buf);
456 if (get_sha1(split[1]->buf, to_obj))
457 die(_("Failed to resolve '%s' as a valid ref."), split[1]->buf);
459 if (rewrite_cmd)
460 err = copy_note_for_rewrite(c, from_obj, to_obj);
461 else
462 err = copy_note(t, from_obj, to_obj, force,
463 combine_notes_overwrite);
465 if (err) {
466 error(_("Failed to copy notes from '%s' to '%s'"),
467 split[0]->buf, split[1]->buf);
468 ret = 1;
471 strbuf_list_free(split);
474 if (!rewrite_cmd) {
475 commit_notes(t, "Notes added by 'git notes copy'");
476 free_notes(t);
477 } else {
478 finish_copy_notes_for_rewrite(c);
480 return ret;
483 static struct notes_tree *init_notes_check(const char *subcommand)
485 struct notes_tree *t;
486 init_notes(NULL, NULL, NULL, 0);
487 t = &default_notes_tree;
489 if (prefixcmp(t->ref, "refs/notes/"))
490 die("Refusing to %s notes in %s (outside of refs/notes/)",
491 subcommand, t->ref);
492 return t;
495 static int list(int argc, const char **argv, const char *prefix)
497 struct notes_tree *t;
498 unsigned char object[20];
499 const unsigned char *note;
500 int retval = -1;
501 struct option options[] = {
502 OPT_END()
505 if (argc)
506 argc = parse_options(argc, argv, prefix, options,
507 git_notes_list_usage, 0);
509 if (1 < argc) {
510 error(_("too many parameters"));
511 usage_with_options(git_notes_list_usage, options);
514 t = init_notes_check("list");
515 if (argc) {
516 if (get_sha1(argv[0], object))
517 die(_("Failed to resolve '%s' as a valid ref."), argv[0]);
518 note = get_note(t, object);
519 if (note) {
520 puts(sha1_to_hex(note));
521 retval = 0;
522 } else
523 retval = error(_("No note found for object %s."),
524 sha1_to_hex(object));
525 } else
526 retval = for_each_note(t, 0, list_each_note, NULL);
528 free_notes(t);
529 return retval;
532 static int add(int argc, const char **argv, const char *prefix)
534 int retval = 0, force = 0;
535 const char *object_ref;
536 struct notes_tree *t;
537 unsigned char object[20], new_note[20];
538 char logmsg[100];
539 const unsigned char *note;
540 struct msg_arg msg = { 0, 0, STRBUF_INIT };
541 struct option options[] = {
542 { OPTION_CALLBACK, 'm', "message", &msg, "msg",
543 "note contents as a string", PARSE_OPT_NONEG,
544 parse_msg_arg},
545 { OPTION_CALLBACK, 'F', "file", &msg, "file",
546 "note contents in a file", PARSE_OPT_NONEG,
547 parse_file_arg},
548 { OPTION_CALLBACK, 'c', "reedit-message", &msg, "object",
549 "reuse and edit specified note object", PARSE_OPT_NONEG,
550 parse_reedit_arg},
551 { OPTION_CALLBACK, 'C', "reuse-message", &msg, "object",
552 "reuse specified note object", PARSE_OPT_NONEG,
553 parse_reuse_arg},
554 OPT__FORCE(&force, "replace existing notes"),
555 OPT_END()
558 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
561 if (1 < argc) {
562 error(_("too many parameters"));
563 usage_with_options(git_notes_add_usage, options);
566 object_ref = argc ? argv[0] : "HEAD";
568 if (get_sha1(object_ref, object))
569 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
571 t = init_notes_check("add");
572 note = get_note(t, object);
574 if (note) {
575 if (!force) {
576 retval = error(_("Cannot add notes. Found existing notes "
577 "for object %s. Use '-f' to overwrite "
578 "existing notes"), sha1_to_hex(object));
579 goto out;
581 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
582 sha1_to_hex(object));
585 create_note(object, &msg, 0, note, new_note);
587 if (is_null_sha1(new_note))
588 remove_note(t, object);
589 else if (add_note(t, object, new_note, combine_notes_overwrite))
590 die("BUG: combine_notes_overwrite failed");
592 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
593 is_null_sha1(new_note) ? "removed" : "added", "add");
594 commit_notes(t, logmsg);
595 out:
596 free_notes(t);
597 strbuf_release(&(msg.buf));
598 return retval;
601 static int copy(int argc, const char **argv, const char *prefix)
603 int retval = 0, force = 0, from_stdin = 0;
604 const unsigned char *from_note, *note;
605 const char *object_ref;
606 unsigned char object[20], from_obj[20];
607 struct notes_tree *t;
608 const char *rewrite_cmd = NULL;
609 struct option options[] = {
610 OPT__FORCE(&force, "replace existing notes"),
611 OPT_BOOLEAN(0, "stdin", &from_stdin, "read objects from stdin"),
612 OPT_STRING(0, "for-rewrite", &rewrite_cmd, "command",
613 "load rewriting config for <command> (implies "
614 "--stdin)"),
615 OPT_END()
618 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
621 if (from_stdin || rewrite_cmd) {
622 if (argc) {
623 error(_("too many parameters"));
624 usage_with_options(git_notes_copy_usage, options);
625 } else {
626 return notes_copy_from_stdin(force, rewrite_cmd);
630 if (argc < 2) {
631 error(_("too few parameters"));
632 usage_with_options(git_notes_copy_usage, options);
634 if (2 < argc) {
635 error(_("too many parameters"));
636 usage_with_options(git_notes_copy_usage, options);
639 if (get_sha1(argv[0], from_obj))
640 die(_("Failed to resolve '%s' as a valid ref."), argv[0]);
642 object_ref = 1 < argc ? argv[1] : "HEAD";
644 if (get_sha1(object_ref, object))
645 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
647 t = init_notes_check("copy");
648 note = get_note(t, object);
650 if (note) {
651 if (!force) {
652 retval = error(_("Cannot copy notes. Found existing "
653 "notes for object %s. Use '-f' to "
654 "overwrite existing notes"),
655 sha1_to_hex(object));
656 goto out;
658 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
659 sha1_to_hex(object));
662 from_note = get_note(t, from_obj);
663 if (!from_note) {
664 retval = error(_("Missing notes on source object %s. Cannot "
665 "copy."), sha1_to_hex(from_obj));
666 goto out;
669 if (add_note(t, object, from_note, combine_notes_overwrite))
670 die("BUG: combine_notes_overwrite failed");
671 commit_notes(t, "Notes added by 'git notes copy'");
672 out:
673 free_notes(t);
674 return retval;
677 static int append_edit(int argc, const char **argv, const char *prefix)
679 const char *object_ref;
680 struct notes_tree *t;
681 unsigned char object[20], new_note[20];
682 const unsigned char *note;
683 char logmsg[100];
684 const char * const *usage;
685 struct msg_arg msg = { 0, 0, STRBUF_INIT };
686 struct option options[] = {
687 { OPTION_CALLBACK, 'm', "message", &msg, "msg",
688 "note contents as a string", PARSE_OPT_NONEG,
689 parse_msg_arg},
690 { OPTION_CALLBACK, 'F', "file", &msg, "file",
691 "note contents in a file", PARSE_OPT_NONEG,
692 parse_file_arg},
693 { OPTION_CALLBACK, 'c', "reedit-message", &msg, "object",
694 "reuse and edit specified note object", PARSE_OPT_NONEG,
695 parse_reedit_arg},
696 { OPTION_CALLBACK, 'C', "reuse-message", &msg, "object",
697 "reuse specified note object", PARSE_OPT_NONEG,
698 parse_reuse_arg},
699 OPT_END()
701 int edit = !strcmp(argv[0], "edit");
703 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
704 argc = parse_options(argc, argv, prefix, options, usage,
705 PARSE_OPT_KEEP_ARGV0);
707 if (2 < argc) {
708 error(_("too many parameters"));
709 usage_with_options(usage, options);
712 if (msg.given && edit)
713 fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
714 "for the 'edit' subcommand.\n"
715 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
717 object_ref = 1 < argc ? argv[1] : "HEAD";
719 if (get_sha1(object_ref, object))
720 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
722 t = init_notes_check(argv[0]);
723 note = get_note(t, object);
725 create_note(object, &msg, !edit, note, new_note);
727 if (is_null_sha1(new_note))
728 remove_note(t, object);
729 else if (add_note(t, object, new_note, combine_notes_overwrite))
730 die("BUG: combine_notes_overwrite failed");
732 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
733 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
734 commit_notes(t, logmsg);
735 free_notes(t);
736 strbuf_release(&(msg.buf));
737 return 0;
740 static int show(int argc, const char **argv, const char *prefix)
742 const char *object_ref;
743 struct notes_tree *t;
744 unsigned char object[20];
745 const unsigned char *note;
746 int retval;
747 struct option options[] = {
748 OPT_END()
751 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
754 if (1 < argc) {
755 error(_("too many parameters"));
756 usage_with_options(git_notes_show_usage, options);
759 object_ref = argc ? argv[0] : "HEAD";
761 if (get_sha1(object_ref, object))
762 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
764 t = init_notes_check("show");
765 note = get_note(t, object);
767 if (!note)
768 retval = error(_("No note found for object %s."),
769 sha1_to_hex(object));
770 else {
771 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
772 retval = execv_git_cmd(show_args);
774 free_notes(t);
775 return retval;
778 static int merge_abort(struct notes_merge_options *o)
780 int ret = 0;
783 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
784 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
787 if (delete_ref("NOTES_MERGE_PARTIAL", NULL, 0))
788 ret += error("Failed to delete ref NOTES_MERGE_PARTIAL");
789 if (delete_ref("NOTES_MERGE_REF", NULL, REF_NODEREF))
790 ret += error("Failed to delete ref NOTES_MERGE_REF");
791 if (notes_merge_abort(o))
792 ret += error("Failed to remove 'git notes merge' worktree");
793 return ret;
796 static int merge_commit(struct notes_merge_options *o)
798 struct strbuf msg = STRBUF_INIT;
799 unsigned char sha1[20], parent_sha1[20];
800 struct notes_tree *t;
801 struct commit *partial;
802 struct pretty_print_context pretty_ctx;
805 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
806 * and target notes ref from .git/NOTES_MERGE_REF.
809 if (get_sha1("NOTES_MERGE_PARTIAL", sha1))
810 die("Failed to read ref NOTES_MERGE_PARTIAL");
811 else if (!(partial = lookup_commit_reference(sha1)))
812 die("Could not find commit from NOTES_MERGE_PARTIAL.");
813 else if (parse_commit(partial))
814 die("Could not parse commit from NOTES_MERGE_PARTIAL.");
816 if (partial->parents)
817 hashcpy(parent_sha1, partial->parents->item->object.sha1);
818 else
819 hashclr(parent_sha1);
821 t = xcalloc(1, sizeof(struct notes_tree));
822 init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
824 o->local_ref = resolve_ref("NOTES_MERGE_REF", sha1, 0, NULL);
825 if (!o->local_ref)
826 die("Failed to resolve NOTES_MERGE_REF");
828 if (notes_merge_commit(o, t, partial, sha1))
829 die("Failed to finalize notes merge");
831 /* Reuse existing commit message in reflog message */
832 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
833 format_commit_message(partial, "%s", &msg, &pretty_ctx);
834 strbuf_trim(&msg);
835 strbuf_insert(&msg, 0, "notes: ", 7);
836 update_ref(msg.buf, o->local_ref, sha1,
837 is_null_sha1(parent_sha1) ? NULL : parent_sha1,
838 0, DIE_ON_ERR);
840 free_notes(t);
841 strbuf_release(&msg);
842 return merge_abort(o);
845 static int merge(int argc, const char **argv, const char *prefix)
847 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
848 unsigned char result_sha1[20];
849 struct notes_tree *t;
850 struct notes_merge_options o;
851 int do_merge = 0, do_commit = 0, do_abort = 0;
852 int verbosity = 0, result;
853 const char *strategy = NULL;
854 struct option options[] = {
855 OPT_GROUP("General options"),
856 OPT__VERBOSITY(&verbosity),
857 OPT_GROUP("Merge options"),
858 OPT_STRING('s', "strategy", &strategy, "strategy",
859 "resolve notes conflicts using the given strategy "
860 "(manual/ours/theirs/union/cat_sort_uniq)"),
861 OPT_GROUP("Committing unmerged notes"),
862 { OPTION_BOOLEAN, 0, "commit", &do_commit, NULL,
863 "finalize notes merge by committing unmerged notes",
864 PARSE_OPT_NOARG | PARSE_OPT_NONEG },
865 OPT_GROUP("Aborting notes merge resolution"),
866 { OPTION_BOOLEAN, 0, "abort", &do_abort, NULL,
867 "abort notes merge",
868 PARSE_OPT_NOARG | PARSE_OPT_NONEG },
869 OPT_END()
872 argc = parse_options(argc, argv, prefix, options,
873 git_notes_merge_usage, 0);
875 if (strategy || do_commit + do_abort == 0)
876 do_merge = 1;
877 if (do_merge + do_commit + do_abort != 1) {
878 error("cannot mix --commit, --abort or -s/--strategy");
879 usage_with_options(git_notes_merge_usage, options);
882 if (do_merge && argc != 1) {
883 error("Must specify a notes ref to merge");
884 usage_with_options(git_notes_merge_usage, options);
885 } else if (!do_merge && argc) {
886 error("too many parameters");
887 usage_with_options(git_notes_merge_usage, options);
890 init_notes_merge_options(&o);
891 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
893 if (do_abort)
894 return merge_abort(&o);
895 if (do_commit)
896 return merge_commit(&o);
898 o.local_ref = default_notes_ref();
899 strbuf_addstr(&remote_ref, argv[0]);
900 expand_notes_ref(&remote_ref);
901 o.remote_ref = remote_ref.buf;
903 if (strategy) {
904 if (!strcmp(strategy, "manual"))
905 o.strategy = NOTES_MERGE_RESOLVE_MANUAL;
906 else if (!strcmp(strategy, "ours"))
907 o.strategy = NOTES_MERGE_RESOLVE_OURS;
908 else if (!strcmp(strategy, "theirs"))
909 o.strategy = NOTES_MERGE_RESOLVE_THEIRS;
910 else if (!strcmp(strategy, "union"))
911 o.strategy = NOTES_MERGE_RESOLVE_UNION;
912 else if (!strcmp(strategy, "cat_sort_uniq"))
913 o.strategy = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ;
914 else {
915 error("Unknown -s/--strategy: %s", strategy);
916 usage_with_options(git_notes_merge_usage, options);
920 t = init_notes_check("merge");
922 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
923 remote_ref.buf, default_notes_ref());
924 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
926 result = notes_merge(&o, t, result_sha1);
928 if (result >= 0) /* Merge resulted (trivially) in result_sha1 */
929 /* Update default notes ref with new commit */
930 update_ref(msg.buf, default_notes_ref(), result_sha1, NULL,
931 0, DIE_ON_ERR);
932 else { /* Merge has unresolved conflicts */
933 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
934 update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_sha1, NULL,
935 0, DIE_ON_ERR);
936 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
937 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
938 die("Failed to store link to current notes ref (%s)",
939 default_notes_ref());
940 printf("Automatic notes merge failed. Fix conflicts in %s and "
941 "commit the result with 'git notes merge --commit', or "
942 "abort the merge with 'git notes merge --abort'.\n",
943 git_path(NOTES_MERGE_WORKTREE));
946 free_notes(t);
947 strbuf_release(&remote_ref);
948 strbuf_release(&msg);
949 return result < 0; /* return non-zero on conflicts */
952 static int remove_cmd(int argc, const char **argv, const char *prefix)
954 struct option options[] = {
955 OPT_END()
957 const char *object_ref;
958 struct notes_tree *t;
959 unsigned char object[20];
960 int retval;
962 argc = parse_options(argc, argv, prefix, options,
963 git_notes_remove_usage, 0);
965 if (1 < argc) {
966 error(_("too many parameters"));
967 usage_with_options(git_notes_remove_usage, options);
970 object_ref = argc ? argv[0] : "HEAD";
972 if (get_sha1(object_ref, object))
973 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
975 t = init_notes_check("remove");
977 retval = remove_note(t, object);
978 if (retval)
979 fprintf(stderr, _("Object %s has no note\n"), sha1_to_hex(object));
980 else {
981 fprintf(stderr, _("Removing note for object %s\n"),
982 sha1_to_hex(object));
984 commit_notes(t, "Notes removed by 'git notes remove'");
986 free_notes(t);
987 return retval;
990 static int prune(int argc, const char **argv, const char *prefix)
992 struct notes_tree *t;
993 int show_only = 0, verbose = 0;
994 struct option options[] = {
995 OPT__DRY_RUN(&show_only, "do not remove, show only"),
996 OPT__VERBOSE(&verbose, "report pruned notes"),
997 OPT_END()
1000 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
1003 if (argc) {
1004 error(_("too many parameters"));
1005 usage_with_options(git_notes_prune_usage, options);
1008 t = init_notes_check("prune");
1010 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
1011 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
1012 if (!show_only)
1013 commit_notes(t, "Notes removed by 'git notes prune'");
1014 free_notes(t);
1015 return 0;
1018 static int get_ref(int argc, const char **argv, const char *prefix)
1020 struct option options[] = { OPT_END() };
1021 argc = parse_options(argc, argv, prefix, options,
1022 git_notes_get_ref_usage, 0);
1024 if (argc) {
1025 error("too many parameters");
1026 usage_with_options(git_notes_get_ref_usage, options);
1029 puts(default_notes_ref());
1030 return 0;
1033 int cmd_notes(int argc, const char **argv, const char *prefix)
1035 int result;
1036 const char *override_notes_ref = NULL;
1037 struct option options[] = {
1038 OPT_STRING(0, "ref", &override_notes_ref, "notes_ref",
1039 "use notes from <notes_ref>"),
1040 OPT_END()
1043 git_config(git_default_config, NULL);
1044 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
1045 PARSE_OPT_STOP_AT_NON_OPTION);
1047 if (override_notes_ref) {
1048 struct strbuf sb = STRBUF_INIT;
1049 strbuf_addstr(&sb, override_notes_ref);
1050 expand_notes_ref(&sb);
1051 setenv("GIT_NOTES_REF", sb.buf, 1);
1052 strbuf_release(&sb);
1055 if (argc < 1 || !strcmp(argv[0], "list"))
1056 result = list(argc, argv, prefix);
1057 else if (!strcmp(argv[0], "add"))
1058 result = add(argc, argv, prefix);
1059 else if (!strcmp(argv[0], "copy"))
1060 result = copy(argc, argv, prefix);
1061 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
1062 result = append_edit(argc, argv, prefix);
1063 else if (!strcmp(argv[0], "show"))
1064 result = show(argc, argv, prefix);
1065 else if (!strcmp(argv[0], "merge"))
1066 result = merge(argc, argv, prefix);
1067 else if (!strcmp(argv[0], "remove"))
1068 result = remove_cmd(argc, argv, prefix);
1069 else if (!strcmp(argv[0], "prune"))
1070 result = prune(argc, argv, prefix);
1071 else if (!strcmp(argv[0], "get-ref"))
1072 result = get_ref(argc, argv, prefix);
1073 else {
1074 result = error(_("Unknown subcommand: %s"), argv[0]);
1075 usage_with_options(git_notes_usage, options);
1078 return result ? 1 : 0;