1/*
2 * Builtin "git notes"
3 *
4 * Copyright (c) 2010 Johan Herland <johan@herland.net>
5 *
6 * Based on git-notes.sh by Johannes Schindelin,
7 * and builtin-tag.c by Kristian Høgsberg and Carlos Rica.
8 */
9
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
22static 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
36};
37
38static const char * const git_notes_list_usage[] = {
39 "git notes [list [<object>]]",
40 NULL
41};
42
43static const char * const git_notes_add_usage[] = {
44 "git notes add [<options>] [<object>]",
45 NULL
46};
47
48static 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
52};
53
54static const char * const git_notes_append_usage[] = {
55 "git notes append [<options>] [<object>]",
56 NULL
57};
58
59static const char * const git_notes_edit_usage[] = {
60 "git notes edit [<object>]",
61 NULL
62};
63
64static const char * const git_notes_show_usage[] = {
65 "git notes show [<object>]",
66 NULL
67};
68
69static 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
74};
75
76static const char * const git_notes_remove_usage[] = {
77 "git notes remove [<object>]",
78 NULL
79};
80
81static const char * const git_notes_prune_usage[] = {
82 "git notes prune [<options>]",
83 NULL
84};
85
86static const char * const git_notes_get_ref_usage[] = {
87 "git notes get-ref",
88 NULL
89};
90
91static const char note_template[] =
92 "\n"
93 "#\n"
94 "# Write/edit the notes for the following object:\n"
95 "#\n";
96
97struct msg_arg {
98 int given;
99 int use_editor;
100 struct strbuf buf;
101};
102
103static void expand_notes_ref(struct strbuf *sb)
104{
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);
111}
112
113static int list_each_note(const unsigned char *object_sha1,
114 const unsigned char *note_sha1, char *note_path,
115 void *cb_data)
116{
117 printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
118 return 0;
119}
120
121static void write_note_data(int fd, const unsigned char *sha1)
122{
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);
130 }
131}
132
133static void write_commented_object(int fd, const unsigned char *object)
134{
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;
140
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));
151
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"));
156
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);
162 }
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));
170}
171
172static void create_note(const unsigned char *object, struct msg_arg *msg,
173 int append_only, const unsigned char *prev,
174 unsigned char *result)
175{
176 char *path = NULL;
177
178 if (msg->use_editor || !msg->given) {
179 int fd;
180
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);
186
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));
192
193 write_commented_object(fd, object);
194
195 close(fd);
196 strbuf_reset(&(msg->buf));
197
198 if (launch_editor(path, &(msg->buf), NULL)) {
199 die(_("Please supply the note contents using either -m" \
200 " or -F option"));
201 }
202 stripspace(&(msg->buf), 1);
203 }
204
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);
210
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);
217 }
218
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);
230 }
231 }
232
233 if (path) {
234 unlink_or_warn(path);
235 free(path);
236 }
237}
238
239static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
240{
241 struct msg_arg *msg = opt->value;
242
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);
248
249 msg->given = 1;
250 return 0;
251}
252
253static int parse_file_arg(const struct option *opt, const char *arg, int unset)
254{
255 struct msg_arg *msg = opt->value;
256
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);
265
266 msg->given = 1;
267 return 0;
268}
269
270static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
271{
272 struct msg_arg *msg = opt->value;
273 char *buf;
274 unsigned char object[20];
275 enum object_type type;
276 unsigned long len;
277
278 if (msg->buf.len)
279 strbuf_addch(&(msg->buf), '\n');
280
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);;
286 }
287 strbuf_add(&(msg->buf), buf, len);
288 free(buf);
289
290 msg->given = 1;
291 return 0;
292}
293
294static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
295{
296 struct msg_arg *msg = opt->value;
297 msg->use_editor = 1;
298 return parse_reuse_arg(opt, arg, unset);
299}
300
301void commit_notes(struct notes_tree *t, const char *msg)
302{
303 struct strbuf buf = STRBUF_INIT;
304 unsigned char commit_sha1[20];
305
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 */
312
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 */
318
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);
321
322 strbuf_release(&buf);
323}
324
325combine_notes_fn parse_combine_notes_fn(const char *v)
326{
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;
337}
338
339static int notes_rewrite_config(const char *k, const char *v, void *cb)
340{
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;
352 }
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;
363 }
364
365 return 0;
366}
367
368
369struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
370{
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);
389 }
390 if (rewrite_refs_env) {
391 c->refs_from_env = 1;
392 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
393 }
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;
400 }
401 c->trees = load_notes_trees(c->refs);
402 string_list_clear(c->refs, 0);
403 free(c->refs);
404 return c;
405}
406
407int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
408 const unsigned char *from_obj, const unsigned char *to_obj)
409{
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;
415}
416
417void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c)
418{
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]);
423 }
424 free(c->trees);
425 free(c);
426}
427
428static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
429{
430 struct strbuf buf = STRBUF_INIT;
431 struct notes_rewrite_cfg *c = NULL;
432 struct notes_tree *t = NULL;
433 int ret = 0;
434
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;
442 }
443
444 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
445 unsigned char from_obj[20], to_obj[20];
446 struct strbuf **split;
447 int err;
448
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);
458
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);
464
465 if (err) {
466 error(_("Failed to copy notes from '%s' to '%s'"),
467 split[0]->buf, split[1]->buf);
468 ret = 1;
469 }
470
471 strbuf_list_free(split);
472 }
473
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);
479 }
480 return ret;
481}
482
483static struct notes_tree *init_notes_check(const char *subcommand)
484{
485 struct notes_tree *t;
486 init_notes(NULL, NULL, NULL, 0);
487 t = &default_notes_tree;
488
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;
493}
494
495static int list(int argc, const char **argv, const char *prefix)
496{
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()
503 };
504
505 if (argc)
506 argc = parse_options(argc, argv, prefix, options,
507 git_notes_list_usage, 0);
508
509 if (1 < argc) {
510 error(_("too many parameters"));
511 usage_with_options(git_notes_list_usage, options);
512 }
513
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);
527
528 free_notes(t);
529 return retval;
530}
531
532static int append_edit(int argc, const char **argv, const char *prefix);
533
534static int add(int argc, const char **argv, const char *prefix)
535{
536 int retval = 0, force = 0;
537 const char *object_ref;
538 struct notes_tree *t;
539 unsigned char object[20], new_note[20];
540 char logmsg[100];
541 const unsigned char *note;
542 struct msg_arg msg = { 0, 0, STRBUF_INIT };
543 struct option options[] = {
544 { OPTION_CALLBACK, 'm', "message", &msg, "msg",
545 "note contents as a string", PARSE_OPT_NONEG,
546 parse_msg_arg},
547 { OPTION_CALLBACK, 'F', "file", &msg, "file",
548 "note contents in a file", PARSE_OPT_NONEG,
549 parse_file_arg},
550 { OPTION_CALLBACK, 'c', "reedit-message", &msg, "object",
551 "reuse and edit specified note object", PARSE_OPT_NONEG,
552 parse_reedit_arg},
553 { OPTION_CALLBACK, 'C', "reuse-message", &msg, "object",
554 "reuse specified note object", PARSE_OPT_NONEG,
555 parse_reuse_arg},
556 OPT__FORCE(&force, "replace existing notes"),
557 OPT_END()
558 };
559
560 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
561 PARSE_OPT_KEEP_ARGV0);
562
563 if (2 < argc) {
564 error(_("too many parameters"));
565 usage_with_options(git_notes_add_usage, options);
566 }
567
568 object_ref = argc > 1 ? argv[1] : "HEAD";
569
570 if (get_sha1(object_ref, object))
571 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
572
573 t = init_notes_check("add");
574 note = get_note(t, object);
575
576 if (note) {
577 if (!force) {
578 if (!msg.given) {
579 /*
580 * Redirect to "edit" subcommand.
581 *
582 * We only end up here if none of -m/-F/-c/-C
583 * or -f are given. The original args are
584 * therefore still in argv[0-1].
585 */
586 argv[0] = "edit";
587 free_notes(t);
588 return append_edit(argc, argv, prefix);
589 }
590 retval = error(_("Cannot add notes. Found existing notes "
591 "for object %s. Use '-f' to overwrite "
592 "existing notes"), sha1_to_hex(object));
593 goto out;
594 }
595 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
596 sha1_to_hex(object));
597 }
598
599 create_note(object, &msg, 0, note, new_note);
600
601 if (is_null_sha1(new_note))
602 remove_note(t, object);
603 else if (add_note(t, object, new_note, combine_notes_overwrite))
604 die("BUG: combine_notes_overwrite failed");
605
606 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
607 is_null_sha1(new_note) ? "removed" : "added", "add");
608 commit_notes(t, logmsg);
609out:
610 free_notes(t);
611 strbuf_release(&(msg.buf));
612 return retval;
613}
614
615static int copy(int argc, const char **argv, const char *prefix)
616{
617 int retval = 0, force = 0, from_stdin = 0;
618 const unsigned char *from_note, *note;
619 const char *object_ref;
620 unsigned char object[20], from_obj[20];
621 struct notes_tree *t;
622 const char *rewrite_cmd = NULL;
623 struct option options[] = {
624 OPT__FORCE(&force, "replace existing notes"),
625 OPT_BOOLEAN(0, "stdin", &from_stdin, "read objects from stdin"),
626 OPT_STRING(0, "for-rewrite", &rewrite_cmd, "command",
627 "load rewriting config for <command> (implies "
628 "--stdin)"),
629 OPT_END()
630 };
631
632 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
633 0);
634
635 if (from_stdin || rewrite_cmd) {
636 if (argc) {
637 error(_("too many parameters"));
638 usage_with_options(git_notes_copy_usage, options);
639 } else {
640 return notes_copy_from_stdin(force, rewrite_cmd);
641 }
642 }
643
644 if (argc < 2) {
645 error(_("too few parameters"));
646 usage_with_options(git_notes_copy_usage, options);
647 }
648 if (2 < argc) {
649 error(_("too many parameters"));
650 usage_with_options(git_notes_copy_usage, options);
651 }
652
653 if (get_sha1(argv[0], from_obj))
654 die(_("Failed to resolve '%s' as a valid ref."), argv[0]);
655
656 object_ref = 1 < argc ? argv[1] : "HEAD";
657
658 if (get_sha1(object_ref, object))
659 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
660
661 t = init_notes_check("copy");
662 note = get_note(t, object);
663
664 if (note) {
665 if (!force) {
666 retval = error(_("Cannot copy notes. Found existing "
667 "notes for object %s. Use '-f' to "
668 "overwrite existing notes"),
669 sha1_to_hex(object));
670 goto out;
671 }
672 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
673 sha1_to_hex(object));
674 }
675
676 from_note = get_note(t, from_obj);
677 if (!from_note) {
678 retval = error(_("Missing notes on source object %s. Cannot "
679 "copy."), sha1_to_hex(from_obj));
680 goto out;
681 }
682
683 if (add_note(t, object, from_note, combine_notes_overwrite))
684 die("BUG: combine_notes_overwrite failed");
685 commit_notes(t, "Notes added by 'git notes copy'");
686out:
687 free_notes(t);
688 return retval;
689}
690
691static int append_edit(int argc, const char **argv, const char *prefix)
692{
693 const char *object_ref;
694 struct notes_tree *t;
695 unsigned char object[20], new_note[20];
696 const unsigned char *note;
697 char logmsg[100];
698 const char * const *usage;
699 struct msg_arg msg = { 0, 0, STRBUF_INIT };
700 struct option options[] = {
701 { OPTION_CALLBACK, 'm', "message", &msg, "msg",
702 "note contents as a string", PARSE_OPT_NONEG,
703 parse_msg_arg},
704 { OPTION_CALLBACK, 'F', "file", &msg, "file",
705 "note contents in a file", PARSE_OPT_NONEG,
706 parse_file_arg},
707 { OPTION_CALLBACK, 'c', "reedit-message", &msg, "object",
708 "reuse and edit specified note object", PARSE_OPT_NONEG,
709 parse_reedit_arg},
710 { OPTION_CALLBACK, 'C', "reuse-message", &msg, "object",
711 "reuse specified note object", PARSE_OPT_NONEG,
712 parse_reuse_arg},
713 OPT_END()
714 };
715 int edit = !strcmp(argv[0], "edit");
716
717 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
718 argc = parse_options(argc, argv, prefix, options, usage,
719 PARSE_OPT_KEEP_ARGV0);
720
721 if (2 < argc) {
722 error(_("too many parameters"));
723 usage_with_options(usage, options);
724 }
725
726 if (msg.given && edit)
727 fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
728 "for the 'edit' subcommand.\n"
729 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
730
731 object_ref = 1 < argc ? argv[1] : "HEAD";
732
733 if (get_sha1(object_ref, object))
734 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
735
736 t = init_notes_check(argv[0]);
737 note = get_note(t, object);
738
739 create_note(object, &msg, !edit, note, new_note);
740
741 if (is_null_sha1(new_note))
742 remove_note(t, object);
743 else if (add_note(t, object, new_note, combine_notes_overwrite))
744 die("BUG: combine_notes_overwrite failed");
745
746 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
747 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
748 commit_notes(t, logmsg);
749 free_notes(t);
750 strbuf_release(&(msg.buf));
751 return 0;
752}
753
754static int show(int argc, const char **argv, const char *prefix)
755{
756 const char *object_ref;
757 struct notes_tree *t;
758 unsigned char object[20];
759 const unsigned char *note;
760 int retval;
761 struct option options[] = {
762 OPT_END()
763 };
764
765 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
766 0);
767
768 if (1 < argc) {
769 error(_("too many parameters"));
770 usage_with_options(git_notes_show_usage, options);
771 }
772
773 object_ref = argc ? argv[0] : "HEAD";
774
775 if (get_sha1(object_ref, object))
776 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
777
778 t = init_notes_check("show");
779 note = get_note(t, object);
780
781 if (!note)
782 retval = error(_("No note found for object %s."),
783 sha1_to_hex(object));
784 else {
785 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
786 retval = execv_git_cmd(show_args);
787 }
788 free_notes(t);
789 return retval;
790}
791
792static int merge_abort(struct notes_merge_options *o)
793{
794 int ret = 0;
795
796 /*
797 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
798 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
799 */
800
801 if (delete_ref("NOTES_MERGE_PARTIAL", NULL, 0))
802 ret += error("Failed to delete ref NOTES_MERGE_PARTIAL");
803 if (delete_ref("NOTES_MERGE_REF", NULL, REF_NODEREF))
804 ret += error("Failed to delete ref NOTES_MERGE_REF");
805 if (notes_merge_abort(o))
806 ret += error("Failed to remove 'git notes merge' worktree");
807 return ret;
808}
809
810static int merge_commit(struct notes_merge_options *o)
811{
812 struct strbuf msg = STRBUF_INIT;
813 unsigned char sha1[20], parent_sha1[20];
814 struct notes_tree *t;
815 struct commit *partial;
816 struct pretty_print_context pretty_ctx;
817
818 /*
819 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
820 * and target notes ref from .git/NOTES_MERGE_REF.
821 */
822
823 if (get_sha1("NOTES_MERGE_PARTIAL", sha1))
824 die("Failed to read ref NOTES_MERGE_PARTIAL");
825 else if (!(partial = lookup_commit_reference(sha1)))
826 die("Could not find commit from NOTES_MERGE_PARTIAL.");
827 else if (parse_commit(partial))
828 die("Could not parse commit from NOTES_MERGE_PARTIAL.");
829
830 if (partial->parents)
831 hashcpy(parent_sha1, partial->parents->item->object.sha1);
832 else
833 hashclr(parent_sha1);
834
835 t = xcalloc(1, sizeof(struct notes_tree));
836 init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
837
838 o->local_ref = resolve_ref("NOTES_MERGE_REF", sha1, 0, NULL);
839 if (!o->local_ref)
840 die("Failed to resolve NOTES_MERGE_REF");
841
842 if (notes_merge_commit(o, t, partial, sha1))
843 die("Failed to finalize notes merge");
844
845 /* Reuse existing commit message in reflog message */
846 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
847 format_commit_message(partial, "%s", &msg, &pretty_ctx);
848 strbuf_trim(&msg);
849 strbuf_insert(&msg, 0, "notes: ", 7);
850 update_ref(msg.buf, o->local_ref, sha1,
851 is_null_sha1(parent_sha1) ? NULL : parent_sha1,
852 0, DIE_ON_ERR);
853
854 free_notes(t);
855 strbuf_release(&msg);
856 return merge_abort(o);
857}
858
859static int merge(int argc, const char **argv, const char *prefix)
860{
861 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
862 unsigned char result_sha1[20];
863 struct notes_tree *t;
864 struct notes_merge_options o;
865 int do_merge = 0, do_commit = 0, do_abort = 0;
866 int verbosity = 0, result;
867 const char *strategy = NULL;
868 struct option options[] = {
869 OPT_GROUP("General options"),
870 OPT__VERBOSITY(&verbosity),
871 OPT_GROUP("Merge options"),
872 OPT_STRING('s', "strategy", &strategy, "strategy",
873 "resolve notes conflicts using the given strategy "
874 "(manual/ours/theirs/union/cat_sort_uniq)"),
875 OPT_GROUP("Committing unmerged notes"),
876 { OPTION_BOOLEAN, 0, "commit", &do_commit, NULL,
877 "finalize notes merge by committing unmerged notes",
878 PARSE_OPT_NOARG | PARSE_OPT_NONEG },
879 OPT_GROUP("Aborting notes merge resolution"),
880 { OPTION_BOOLEAN, 0, "abort", &do_abort, NULL,
881 "abort notes merge",
882 PARSE_OPT_NOARG | PARSE_OPT_NONEG },
883 OPT_END()
884 };
885
886 argc = parse_options(argc, argv, prefix, options,
887 git_notes_merge_usage, 0);
888
889 if (strategy || do_commit + do_abort == 0)
890 do_merge = 1;
891 if (do_merge + do_commit + do_abort != 1) {
892 error("cannot mix --commit, --abort or -s/--strategy");
893 usage_with_options(git_notes_merge_usage, options);
894 }
895
896 if (do_merge && argc != 1) {
897 error("Must specify a notes ref to merge");
898 usage_with_options(git_notes_merge_usage, options);
899 } else if (!do_merge && argc) {
900 error("too many parameters");
901 usage_with_options(git_notes_merge_usage, options);
902 }
903
904 init_notes_merge_options(&o);
905 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
906
907 if (do_abort)
908 return merge_abort(&o);
909 if (do_commit)
910 return merge_commit(&o);
911
912 o.local_ref = default_notes_ref();
913 strbuf_addstr(&remote_ref, argv[0]);
914 expand_notes_ref(&remote_ref);
915 o.remote_ref = remote_ref.buf;
916
917 if (strategy) {
918 if (!strcmp(strategy, "manual"))
919 o.strategy = NOTES_MERGE_RESOLVE_MANUAL;
920 else if (!strcmp(strategy, "ours"))
921 o.strategy = NOTES_MERGE_RESOLVE_OURS;
922 else if (!strcmp(strategy, "theirs"))
923 o.strategy = NOTES_MERGE_RESOLVE_THEIRS;
924 else if (!strcmp(strategy, "union"))
925 o.strategy = NOTES_MERGE_RESOLVE_UNION;
926 else if (!strcmp(strategy, "cat_sort_uniq"))
927 o.strategy = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ;
928 else {
929 error("Unknown -s/--strategy: %s", strategy);
930 usage_with_options(git_notes_merge_usage, options);
931 }
932 }
933
934 t = init_notes_check("merge");
935
936 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
937 remote_ref.buf, default_notes_ref());
938 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
939
940 result = notes_merge(&o, t, result_sha1);
941
942 if (result >= 0) /* Merge resulted (trivially) in result_sha1 */
943 /* Update default notes ref with new commit */
944 update_ref(msg.buf, default_notes_ref(), result_sha1, NULL,
945 0, DIE_ON_ERR);
946 else { /* Merge has unresolved conflicts */
947 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
948 update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_sha1, NULL,
949 0, DIE_ON_ERR);
950 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
951 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
952 die("Failed to store link to current notes ref (%s)",
953 default_notes_ref());
954 printf("Automatic notes merge failed. Fix conflicts in %s and "
955 "commit the result with 'git notes merge --commit', or "
956 "abort the merge with 'git notes merge --abort'.\n",
957 git_path(NOTES_MERGE_WORKTREE));
958 }
959
960 free_notes(t);
961 strbuf_release(&remote_ref);
962 strbuf_release(&msg);
963 return result < 0; /* return non-zero on conflicts */
964}
965
966static int remove_cmd(int argc, const char **argv, const char *prefix)
967{
968 struct option options[] = {
969 OPT_END()
970 };
971 const char *object_ref;
972 struct notes_tree *t;
973 unsigned char object[20];
974 int retval;
975
976 argc = parse_options(argc, argv, prefix, options,
977 git_notes_remove_usage, 0);
978
979 if (1 < argc) {
980 error(_("too many parameters"));
981 usage_with_options(git_notes_remove_usage, options);
982 }
983
984 object_ref = argc ? argv[0] : "HEAD";
985
986 if (get_sha1(object_ref, object))
987 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
988
989 t = init_notes_check("remove");
990
991 retval = remove_note(t, object);
992 if (retval)
993 fprintf(stderr, _("Object %s has no note\n"), sha1_to_hex(object));
994 else {
995 fprintf(stderr, _("Removing note for object %s\n"),
996 sha1_to_hex(object));
997
998 commit_notes(t, "Notes removed by 'git notes remove'");
999 }
1000 free_notes(t);
1001 return retval;
1002}
1003
1004static int prune(int argc, const char **argv, const char *prefix)
1005{
1006 struct notes_tree *t;
1007 int show_only = 0, verbose = 0;
1008 struct option options[] = {
1009 OPT__DRY_RUN(&show_only, "do not remove, show only"),
1010 OPT__VERBOSE(&verbose, "report pruned notes"),
1011 OPT_END()
1012 };
1013
1014 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
1015 0);
1016
1017 if (argc) {
1018 error(_("too many parameters"));
1019 usage_with_options(git_notes_prune_usage, options);
1020 }
1021
1022 t = init_notes_check("prune");
1023
1024 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
1025 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
1026 if (!show_only)
1027 commit_notes(t, "Notes removed by 'git notes prune'");
1028 free_notes(t);
1029 return 0;
1030}
1031
1032static int get_ref(int argc, const char **argv, const char *prefix)
1033{
1034 struct option options[] = { OPT_END() };
1035 argc = parse_options(argc, argv, prefix, options,
1036 git_notes_get_ref_usage, 0);
1037
1038 if (argc) {
1039 error("too many parameters");
1040 usage_with_options(git_notes_get_ref_usage, options);
1041 }
1042
1043 puts(default_notes_ref());
1044 return 0;
1045}
1046
1047int cmd_notes(int argc, const char **argv, const char *prefix)
1048{
1049 int result;
1050 const char *override_notes_ref = NULL;
1051 struct option options[] = {
1052 OPT_STRING(0, "ref", &override_notes_ref, "notes_ref",
1053 "use notes from <notes_ref>"),
1054 OPT_END()
1055 };
1056
1057 git_config(git_default_config, NULL);
1058 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
1059 PARSE_OPT_STOP_AT_NON_OPTION);
1060
1061 if (override_notes_ref) {
1062 struct strbuf sb = STRBUF_INIT;
1063 strbuf_addstr(&sb, override_notes_ref);
1064 expand_notes_ref(&sb);
1065 setenv("GIT_NOTES_REF", sb.buf, 1);
1066 strbuf_release(&sb);
1067 }
1068
1069 if (argc < 1 || !strcmp(argv[0], "list"))
1070 result = list(argc, argv, prefix);
1071 else if (!strcmp(argv[0], "add"))
1072 result = add(argc, argv, prefix);
1073 else if (!strcmp(argv[0], "copy"))
1074 result = copy(argc, argv, prefix);
1075 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
1076 result = append_edit(argc, argv, prefix);
1077 else if (!strcmp(argv[0], "show"))
1078 result = show(argc, argv, prefix);
1079 else if (!strcmp(argv[0], "merge"))
1080 result = merge(argc, argv, prefix);
1081 else if (!strcmp(argv[0], "remove"))
1082 result = remove_cmd(argc, argv, prefix);
1083 else if (!strcmp(argv[0], "prune"))
1084 result = prune(argc, argv, prefix);
1085 else if (!strcmp(argv[0], "get-ref"))
1086 result = get_ref(argc, argv, prefix);
1087 else {
1088 result = error(_("Unknown subcommand: %s"), argv[0]);
1089 usage_with_options(git_notes_usage, options);
1090 }
1091
1092 return result ? 1 : 0;
1093}