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 "config.h"
12#include "builtin.h"
13#include "notes.h"
14#include "object-store.h"
15#include "repository.h"
16#include "blob.h"
17#include "pretty.h"
18#include "refs.h"
19#include "exec-cmd.h"
20#include "run-command.h"
21#include "parse-options.h"
22#include "string-list.h"
23#include "notes-merge.h"
24#include "notes-utils.h"
25#include "worktree.h"
26
27static const char * const git_notes_usage[] = {
28 N_("git notes [--ref <notes-ref>] [list [<object>]]"),
29 N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
30 N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"),
31 N_("git notes [--ref <notes-ref>] append [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
32 N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
33 N_("git notes [--ref <notes-ref>] show [<object>]"),
34 N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"),
35 N_("git notes merge --commit [-v | -q]"),
36 N_("git notes merge --abort [-v | -q]"),
37 N_("git notes [--ref <notes-ref>] remove [<object>...]"),
38 N_("git notes [--ref <notes-ref>] prune [-n] [-v]"),
39 N_("git notes [--ref <notes-ref>] get-ref"),
40 NULL
41};
42
43static const char * const git_notes_list_usage[] = {
44 N_("git notes [list [<object>]]"),
45 NULL
46};
47
48static const char * const git_notes_add_usage[] = {
49 N_("git notes add [<options>] [<object>]"),
50 NULL
51};
52
53static const char * const git_notes_copy_usage[] = {
54 N_("git notes copy [<options>] <from-object> <to-object>"),
55 N_("git notes copy --stdin [<from-object> <to-object>]..."),
56 NULL
57};
58
59static const char * const git_notes_append_usage[] = {
60 N_("git notes append [<options>] [<object>]"),
61 NULL
62};
63
64static const char * const git_notes_edit_usage[] = {
65 N_("git notes edit [<object>]"),
66 NULL
67};
68
69static const char * const git_notes_show_usage[] = {
70 N_("git notes show [<object>]"),
71 NULL
72};
73
74static const char * const git_notes_merge_usage[] = {
75 N_("git notes merge [<options>] <notes-ref>"),
76 N_("git notes merge --commit [<options>]"),
77 N_("git notes merge --abort [<options>]"),
78 NULL
79};
80
81static const char * const git_notes_remove_usage[] = {
82 N_("git notes remove [<object>]"),
83 NULL
84};
85
86static const char * const git_notes_prune_usage[] = {
87 N_("git notes prune [<options>]"),
88 NULL
89};
90
91static const char * const git_notes_get_ref_usage[] = {
92 N_("git notes get-ref"),
93 NULL
94};
95
96static const char note_template[] =
97 N_("Write/edit the notes for the following object:");
98
99struct note_data {
100 int given;
101 int use_editor;
102 char *edit_path;
103 struct strbuf buf;
104};
105
106static void free_note_data(struct note_data *d)
107{
108 if (d->edit_path) {
109 unlink_or_warn(d->edit_path);
110 free(d->edit_path);
111 }
112 strbuf_release(&d->buf);
113}
114
115static int list_each_note(const struct object_id *object_oid,
116 const struct object_id *note_oid, char *note_path,
117 void *cb_data)
118{
119 printf("%s %s\n", oid_to_hex(note_oid), oid_to_hex(object_oid));
120 return 0;
121}
122
123static void copy_obj_to_fd(int fd, const struct object_id *oid)
124{
125 unsigned long size;
126 enum object_type type;
127 char *buf = read_object_file(oid, &type, &size);
128 if (buf) {
129 if (size)
130 write_or_die(fd, buf, size);
131 free(buf);
132 }
133}
134
135static void write_commented_object(int fd, const struct object_id *object)
136{
137 const char *show_args[5] =
138 {"show", "--stat", "--no-notes", oid_to_hex(object), NULL};
139 struct child_process show = CHILD_PROCESS_INIT;
140 struct strbuf buf = STRBUF_INIT;
141 struct strbuf cbuf = STRBUF_INIT;
142
143 /* Invoke "git show --stat --no-notes $object" */
144 show.argv = show_args;
145 show.no_stdin = 1;
146 show.out = -1;
147 show.err = 0;
148 show.git_cmd = 1;
149 if (start_command(&show))
150 die(_("unable to start 'show' for object '%s'"),
151 oid_to_hex(object));
152
153 if (strbuf_read(&buf, show.out, 0) < 0)
154 die_errno(_("could not read 'show' output"));
155 strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
156 write_or_die(fd, cbuf.buf, cbuf.len);
157
158 strbuf_release(&cbuf);
159 strbuf_release(&buf);
160
161 if (finish_command(&show))
162 die(_("failed to finish 'show' for object '%s'"),
163 oid_to_hex(object));
164}
165
166static void prepare_note_data(const struct object_id *object, struct note_data *d,
167 const struct object_id *old_note)
168{
169 if (d->use_editor || !d->given) {
170 int fd;
171 struct strbuf buf = STRBUF_INIT;
172
173 /* write the template message before editing: */
174 d->edit_path = git_pathdup("NOTES_EDITMSG");
175 fd = open(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
176 if (fd < 0)
177 die_errno(_("could not create file '%s'"), d->edit_path);
178
179 if (d->given)
180 write_or_die(fd, d->buf.buf, d->buf.len);
181 else if (old_note)
182 copy_obj_to_fd(fd, old_note);
183
184 strbuf_addch(&buf, '\n');
185 strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
186 strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)));
187 strbuf_addch(&buf, '\n');
188 write_or_die(fd, buf.buf, buf.len);
189
190 write_commented_object(fd, object);
191
192 close(fd);
193 strbuf_release(&buf);
194 strbuf_reset(&d->buf);
195
196 if (launch_editor(d->edit_path, &d->buf, NULL)) {
197 die(_("please supply the note contents using either -m or -F option"));
198 }
199 strbuf_stripspace(&d->buf, 1);
200 }
201}
202
203static void write_note_data(struct note_data *d, struct object_id *oid)
204{
205 if (write_object_file(d->buf.buf, d->buf.len, blob_type, oid)) {
206 error(_("unable to write note object"));
207 if (d->edit_path)
208 error(_("the note contents have been left in %s"),
209 d->edit_path);
210 exit(128);
211 }
212}
213
214static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
215{
216 struct note_data *d = opt->value;
217
218 BUG_ON_OPT_NEG(unset);
219
220 strbuf_grow(&d->buf, strlen(arg) + 2);
221 if (d->buf.len)
222 strbuf_addch(&d->buf, '\n');
223 strbuf_addstr(&d->buf, arg);
224 strbuf_stripspace(&d->buf, 0);
225
226 d->given = 1;
227 return 0;
228}
229
230static int parse_file_arg(const struct option *opt, const char *arg, int unset)
231{
232 struct note_data *d = opt->value;
233
234 BUG_ON_OPT_NEG(unset);
235
236 if (d->buf.len)
237 strbuf_addch(&d->buf, '\n');
238 if (!strcmp(arg, "-")) {
239 if (strbuf_read(&d->buf, 0, 1024) < 0)
240 die_errno(_("cannot read '%s'"), arg);
241 } else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
242 die_errno(_("could not open or read '%s'"), arg);
243 strbuf_stripspace(&d->buf, 0);
244
245 d->given = 1;
246 return 0;
247}
248
249static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
250{
251 struct note_data *d = opt->value;
252 char *buf;
253 struct object_id object;
254 enum object_type type;
255 unsigned long len;
256
257 BUG_ON_OPT_NEG(unset);
258
259 if (d->buf.len)
260 strbuf_addch(&d->buf, '\n');
261
262 if (get_oid(arg, &object))
263 die(_("failed to resolve '%s' as a valid ref."), arg);
264 if (!(buf = read_object_file(&object, &type, &len))) {
265 free(buf);
266 die(_("failed to read object '%s'."), arg);
267 }
268 if (type != OBJ_BLOB) {
269 free(buf);
270 die(_("cannot read note data from non-blob object '%s'."), arg);
271 }
272 strbuf_add(&d->buf, buf, len);
273 free(buf);
274
275 d->given = 1;
276 return 0;
277}
278
279static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
280{
281 struct note_data *d = opt->value;
282 BUG_ON_OPT_NEG(unset);
283 d->use_editor = 1;
284 return parse_reuse_arg(opt, arg, unset);
285}
286
287static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
288{
289 struct strbuf buf = STRBUF_INIT;
290 struct notes_rewrite_cfg *c = NULL;
291 struct notes_tree *t = NULL;
292 int ret = 0;
293 const char *msg = "Notes added by 'git notes copy'";
294
295 if (rewrite_cmd) {
296 c = init_copy_notes_for_rewrite(rewrite_cmd);
297 if (!c)
298 return 0;
299 } else {
300 init_notes(NULL, NULL, NULL, NOTES_INIT_WRITABLE);
301 t = &default_notes_tree;
302 }
303
304 while (strbuf_getline_lf(&buf, stdin) != EOF) {
305 struct object_id from_obj, to_obj;
306 struct strbuf **split;
307 int err;
308
309 split = strbuf_split(&buf, ' ');
310 if (!split[0] || !split[1])
311 die(_("malformed input line: '%s'."), buf.buf);
312 strbuf_rtrim(split[0]);
313 strbuf_rtrim(split[1]);
314 if (get_oid(split[0]->buf, &from_obj))
315 die(_("failed to resolve '%s' as a valid ref."), split[0]->buf);
316 if (get_oid(split[1]->buf, &to_obj))
317 die(_("failed to resolve '%s' as a valid ref."), split[1]->buf);
318
319 if (rewrite_cmd)
320 err = copy_note_for_rewrite(c, &from_obj, &to_obj);
321 else
322 err = copy_note(t, &from_obj, &to_obj, force,
323 combine_notes_overwrite);
324
325 if (err) {
326 error(_("failed to copy notes from '%s' to '%s'"),
327 split[0]->buf, split[1]->buf);
328 ret = 1;
329 }
330
331 strbuf_list_free(split);
332 }
333
334 if (!rewrite_cmd) {
335 commit_notes(t, msg);
336 free_notes(t);
337 } else {
338 finish_copy_notes_for_rewrite(c, msg);
339 }
340 strbuf_release(&buf);
341 return ret;
342}
343
344static struct notes_tree *init_notes_check(const char *subcommand,
345 int flags)
346{
347 struct notes_tree *t;
348 const char *ref;
349 init_notes(NULL, NULL, NULL, flags);
350 t = &default_notes_tree;
351
352 ref = (flags & NOTES_INIT_WRITABLE) ? t->update_ref : t->ref;
353 if (!starts_with(ref, "refs/notes/"))
354 /*
355 * TRANSLATORS: the first %s will be replaced by a git
356 * notes command: 'add', 'merge', 'remove', etc.
357 */
358 die(_("refusing to %s notes in %s (outside of refs/notes/)"),
359 subcommand, ref);
360 return t;
361}
362
363static int list(int argc, const char **argv, const char *prefix)
364{
365 struct notes_tree *t;
366 struct object_id object;
367 const struct object_id *note;
368 int retval = -1;
369 struct option options[] = {
370 OPT_END()
371 };
372
373 if (argc)
374 argc = parse_options(argc, argv, prefix, options,
375 git_notes_list_usage, 0);
376
377 if (1 < argc) {
378 error(_("too many parameters"));
379 usage_with_options(git_notes_list_usage, options);
380 }
381
382 t = init_notes_check("list", 0);
383 if (argc) {
384 if (get_oid(argv[0], &object))
385 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
386 note = get_note(t, &object);
387 if (note) {
388 puts(oid_to_hex(note));
389 retval = 0;
390 } else
391 retval = error(_("no note found for object %s."),
392 oid_to_hex(&object));
393 } else
394 retval = for_each_note(t, 0, list_each_note, NULL);
395
396 free_notes(t);
397 return retval;
398}
399
400static int append_edit(int argc, const char **argv, const char *prefix);
401
402static int add(int argc, const char **argv, const char *prefix)
403{
404 int force = 0, allow_empty = 0;
405 const char *object_ref;
406 struct notes_tree *t;
407 struct object_id object, new_note;
408 const struct object_id *note;
409 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
410 struct option options[] = {
411 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
412 N_("note contents as a string"), PARSE_OPT_NONEG,
413 parse_msg_arg},
414 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
415 N_("note contents in a file"), PARSE_OPT_NONEG,
416 parse_file_arg},
417 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
418 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
419 parse_reedit_arg},
420 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
421 N_("reuse specified note object"), PARSE_OPT_NONEG,
422 parse_reuse_arg},
423 OPT_BOOL(0, "allow-empty", &allow_empty,
424 N_("allow storing empty note")),
425 OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE),
426 OPT_END()
427 };
428
429 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
430 PARSE_OPT_KEEP_ARGV0);
431
432 if (2 < argc) {
433 error(_("too many parameters"));
434 usage_with_options(git_notes_add_usage, options);
435 }
436
437 object_ref = argc > 1 ? argv[1] : "HEAD";
438
439 if (get_oid(object_ref, &object))
440 die(_("failed to resolve '%s' as a valid ref."), object_ref);
441
442 t = init_notes_check("add", NOTES_INIT_WRITABLE);
443 note = get_note(t, &object);
444
445 if (note) {
446 if (!force) {
447 free_notes(t);
448 if (d.given) {
449 free_note_data(&d);
450 return error(_("Cannot add notes. "
451 "Found existing notes for object %s. "
452 "Use '-f' to overwrite existing notes"),
453 oid_to_hex(&object));
454 }
455 /*
456 * Redirect to "edit" subcommand.
457 *
458 * We only end up here if none of -m/-F/-c/-C or -f are
459 * given. The original args are therefore still in
460 * argv[0-1].
461 */
462 argv[0] = "edit";
463 return append_edit(argc, argv, prefix);
464 }
465 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
466 oid_to_hex(&object));
467 }
468
469 prepare_note_data(&object, &d, note);
470 if (d.buf.len || allow_empty) {
471 write_note_data(&d, &new_note);
472 if (add_note(t, &object, &new_note, combine_notes_overwrite))
473 BUG("combine_notes_overwrite failed");
474 commit_notes(t, "Notes added by 'git notes add'");
475 } else {
476 fprintf(stderr, _("Removing note for object %s\n"),
477 oid_to_hex(&object));
478 remove_note(t, object.hash);
479 commit_notes(t, "Notes removed by 'git notes add'");
480 }
481
482 free_note_data(&d);
483 free_notes(t);
484 return 0;
485}
486
487static int copy(int argc, const char **argv, const char *prefix)
488{
489 int retval = 0, force = 0, from_stdin = 0;
490 const struct object_id *from_note, *note;
491 const char *object_ref;
492 struct object_id object, from_obj;
493 struct notes_tree *t;
494 const char *rewrite_cmd = NULL;
495 struct option options[] = {
496 OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE),
497 OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")),
498 OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"),
499 N_("load rewriting config for <command> (implies "
500 "--stdin)")),
501 OPT_END()
502 };
503
504 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
505 0);
506
507 if (from_stdin || rewrite_cmd) {
508 if (argc) {
509 error(_("too many parameters"));
510 usage_with_options(git_notes_copy_usage, options);
511 } else {
512 return notes_copy_from_stdin(force, rewrite_cmd);
513 }
514 }
515
516 if (argc < 2) {
517 error(_("too few parameters"));
518 usage_with_options(git_notes_copy_usage, options);
519 }
520 if (2 < argc) {
521 error(_("too many parameters"));
522 usage_with_options(git_notes_copy_usage, options);
523 }
524
525 if (get_oid(argv[0], &from_obj))
526 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
527
528 object_ref = 1 < argc ? argv[1] : "HEAD";
529
530 if (get_oid(object_ref, &object))
531 die(_("failed to resolve '%s' as a valid ref."), object_ref);
532
533 t = init_notes_check("copy", NOTES_INIT_WRITABLE);
534 note = get_note(t, &object);
535
536 if (note) {
537 if (!force) {
538 retval = error(_("Cannot copy notes. Found existing "
539 "notes for object %s. Use '-f' to "
540 "overwrite existing notes"),
541 oid_to_hex(&object));
542 goto out;
543 }
544 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
545 oid_to_hex(&object));
546 }
547
548 from_note = get_note(t, &from_obj);
549 if (!from_note) {
550 retval = error(_("missing notes on source object %s. Cannot "
551 "copy."), oid_to_hex(&from_obj));
552 goto out;
553 }
554
555 if (add_note(t, &object, from_note, combine_notes_overwrite))
556 BUG("combine_notes_overwrite failed");
557 commit_notes(t, "Notes added by 'git notes copy'");
558out:
559 free_notes(t);
560 return retval;
561}
562
563static int append_edit(int argc, const char **argv, const char *prefix)
564{
565 int allow_empty = 0;
566 const char *object_ref;
567 struct notes_tree *t;
568 struct object_id object, new_note;
569 const struct object_id *note;
570 char *logmsg;
571 const char * const *usage;
572 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
573 struct option options[] = {
574 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
575 N_("note contents as a string"), PARSE_OPT_NONEG,
576 parse_msg_arg},
577 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
578 N_("note contents in a file"), PARSE_OPT_NONEG,
579 parse_file_arg},
580 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
581 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
582 parse_reedit_arg},
583 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
584 N_("reuse specified note object"), PARSE_OPT_NONEG,
585 parse_reuse_arg},
586 OPT_BOOL(0, "allow-empty", &allow_empty,
587 N_("allow storing empty note")),
588 OPT_END()
589 };
590 int edit = !strcmp(argv[0], "edit");
591
592 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
593 argc = parse_options(argc, argv, prefix, options, usage,
594 PARSE_OPT_KEEP_ARGV0);
595
596 if (2 < argc) {
597 error(_("too many parameters"));
598 usage_with_options(usage, options);
599 }
600
601 if (d.given && edit)
602 fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
603 "for the 'edit' subcommand.\n"
604 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
605
606 object_ref = 1 < argc ? argv[1] : "HEAD";
607
608 if (get_oid(object_ref, &object))
609 die(_("failed to resolve '%s' as a valid ref."), object_ref);
610
611 t = init_notes_check(argv[0], NOTES_INIT_WRITABLE);
612 note = get_note(t, &object);
613
614 prepare_note_data(&object, &d, edit && note ? note : NULL);
615
616 if (note && !edit) {
617 /* Append buf to previous note contents */
618 unsigned long size;
619 enum object_type type;
620 char *prev_buf = read_object_file(note, &type, &size);
621
622 strbuf_grow(&d.buf, size + 1);
623 if (d.buf.len && prev_buf && size)
624 strbuf_insert(&d.buf, 0, "\n", 1);
625 if (prev_buf && size)
626 strbuf_insert(&d.buf, 0, prev_buf, size);
627 free(prev_buf);
628 }
629
630 if (d.buf.len || allow_empty) {
631 write_note_data(&d, &new_note);
632 if (add_note(t, &object, &new_note, combine_notes_overwrite))
633 BUG("combine_notes_overwrite failed");
634 logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]);
635 } else {
636 fprintf(stderr, _("Removing note for object %s\n"),
637 oid_to_hex(&object));
638 remove_note(t, object.hash);
639 logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]);
640 }
641 commit_notes(t, logmsg);
642
643 free(logmsg);
644 free_note_data(&d);
645 free_notes(t);
646 return 0;
647}
648
649static int show(int argc, const char **argv, const char *prefix)
650{
651 const char *object_ref;
652 struct notes_tree *t;
653 struct object_id object;
654 const struct object_id *note;
655 int retval;
656 struct option options[] = {
657 OPT_END()
658 };
659
660 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
661 0);
662
663 if (1 < argc) {
664 error(_("too many parameters"));
665 usage_with_options(git_notes_show_usage, options);
666 }
667
668 object_ref = argc ? argv[0] : "HEAD";
669
670 if (get_oid(object_ref, &object))
671 die(_("failed to resolve '%s' as a valid ref."), object_ref);
672
673 t = init_notes_check("show", 0);
674 note = get_note(t, &object);
675
676 if (!note)
677 retval = error(_("no note found for object %s."),
678 oid_to_hex(&object));
679 else {
680 const char *show_args[3] = {"show", oid_to_hex(note), NULL};
681 retval = execv_git_cmd(show_args);
682 }
683 free_notes(t);
684 return retval;
685}
686
687static int merge_abort(struct notes_merge_options *o)
688{
689 int ret = 0;
690
691 /*
692 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
693 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
694 */
695
696 if (delete_ref(NULL, "NOTES_MERGE_PARTIAL", NULL, 0))
697 ret += error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
698 if (delete_ref(NULL, "NOTES_MERGE_REF", NULL, REF_NO_DEREF))
699 ret += error(_("failed to delete ref NOTES_MERGE_REF"));
700 if (notes_merge_abort(o))
701 ret += error(_("failed to remove 'git notes merge' worktree"));
702 return ret;
703}
704
705static int merge_commit(struct notes_merge_options *o)
706{
707 struct strbuf msg = STRBUF_INIT;
708 struct object_id oid, parent_oid;
709 struct notes_tree *t;
710 struct commit *partial;
711 struct pretty_print_context pretty_ctx;
712 void *local_ref_to_free;
713 int ret;
714
715 /*
716 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
717 * and target notes ref from .git/NOTES_MERGE_REF.
718 */
719
720 if (get_oid("NOTES_MERGE_PARTIAL", &oid))
721 die(_("failed to read ref NOTES_MERGE_PARTIAL"));
722 else if (!(partial = lookup_commit_reference(the_repository, &oid)))
723 die(_("could not find commit from NOTES_MERGE_PARTIAL."));
724 else if (parse_commit(partial))
725 die(_("could not parse commit from NOTES_MERGE_PARTIAL."));
726
727 if (partial->parents)
728 oidcpy(&parent_oid, &partial->parents->item->object.oid);
729 else
730 oidclr(&parent_oid);
731
732 t = xcalloc(1, sizeof(struct notes_tree));
733 init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
734
735 o->local_ref = local_ref_to_free =
736 resolve_refdup("NOTES_MERGE_REF", 0, &oid, NULL);
737 if (!o->local_ref)
738 die(_("failed to resolve NOTES_MERGE_REF"));
739
740 if (notes_merge_commit(o, t, partial, &oid))
741 die(_("failed to finalize notes merge"));
742
743 /* Reuse existing commit message in reflog message */
744 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
745 format_commit_message(partial, "%s", &msg, &pretty_ctx);
746 strbuf_trim(&msg);
747 strbuf_insert(&msg, 0, "notes: ", 7);
748 update_ref(msg.buf, o->local_ref, &oid,
749 is_null_oid(&parent_oid) ? NULL : &parent_oid,
750 0, UPDATE_REFS_DIE_ON_ERR);
751
752 free_notes(t);
753 strbuf_release(&msg);
754 ret = merge_abort(o);
755 free(local_ref_to_free);
756 return ret;
757}
758
759static int git_config_get_notes_strategy(const char *key,
760 enum notes_merge_strategy *strategy)
761{
762 char *value;
763
764 if (git_config_get_string(key, &value))
765 return 1;
766 if (parse_notes_merge_strategy(value, strategy))
767 git_die_config(key, _("unknown notes merge strategy %s"), value);
768
769 free(value);
770 return 0;
771}
772
773static int merge(int argc, const char **argv, const char *prefix)
774{
775 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
776 struct object_id result_oid;
777 struct notes_tree *t;
778 struct notes_merge_options o;
779 int do_merge = 0, do_commit = 0, do_abort = 0;
780 int verbosity = 0, result;
781 const char *strategy = NULL;
782 struct option options[] = {
783 OPT_GROUP(N_("General options")),
784 OPT__VERBOSITY(&verbosity),
785 OPT_GROUP(N_("Merge options")),
786 OPT_STRING('s', "strategy", &strategy, N_("strategy"),
787 N_("resolve notes conflicts using the given strategy "
788 "(manual/ours/theirs/union/cat_sort_uniq)")),
789 OPT_GROUP(N_("Committing unmerged notes")),
790 OPT_SET_INT_F(0, "commit", &do_commit,
791 N_("finalize notes merge by committing unmerged notes"),
792 1, PARSE_OPT_NONEG),
793 OPT_GROUP(N_("Aborting notes merge resolution")),
794 OPT_SET_INT_F(0, "abort", &do_abort,
795 N_("abort notes merge"),
796 1, PARSE_OPT_NONEG),
797 OPT_END()
798 };
799
800 argc = parse_options(argc, argv, prefix, options,
801 git_notes_merge_usage, 0);
802
803 if (strategy || do_commit + do_abort == 0)
804 do_merge = 1;
805 if (do_merge + do_commit + do_abort != 1) {
806 error(_("cannot mix --commit, --abort or -s/--strategy"));
807 usage_with_options(git_notes_merge_usage, options);
808 }
809
810 if (do_merge && argc != 1) {
811 error(_("must specify a notes ref to merge"));
812 usage_with_options(git_notes_merge_usage, options);
813 } else if (!do_merge && argc) {
814 error(_("too many parameters"));
815 usage_with_options(git_notes_merge_usage, options);
816 }
817
818 init_notes_merge_options(&o);
819 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
820
821 if (do_abort)
822 return merge_abort(&o);
823 if (do_commit)
824 return merge_commit(&o);
825
826 o.local_ref = default_notes_ref();
827 strbuf_addstr(&remote_ref, argv[0]);
828 expand_loose_notes_ref(&remote_ref);
829 o.remote_ref = remote_ref.buf;
830
831 t = init_notes_check("merge", NOTES_INIT_WRITABLE);
832
833 if (strategy) {
834 if (parse_notes_merge_strategy(strategy, &o.strategy)) {
835 error(_("unknown -s/--strategy: %s"), strategy);
836 usage_with_options(git_notes_merge_usage, options);
837 }
838 } else {
839 struct strbuf merge_key = STRBUF_INIT;
840 const char *short_ref = NULL;
841
842 if (!skip_prefix(o.local_ref, "refs/notes/", &short_ref))
843 BUG("local ref %s is outside of refs/notes/",
844 o.local_ref);
845
846 strbuf_addf(&merge_key, "notes.%s.mergeStrategy", short_ref);
847
848 if (git_config_get_notes_strategy(merge_key.buf, &o.strategy))
849 git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy);
850
851 strbuf_release(&merge_key);
852 }
853
854 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
855 remote_ref.buf, default_notes_ref());
856 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
857
858 result = notes_merge(&o, t, &result_oid);
859
860 if (result >= 0) /* Merge resulted (trivially) in result_oid */
861 /* Update default notes ref with new commit */
862 update_ref(msg.buf, default_notes_ref(), &result_oid, NULL, 0,
863 UPDATE_REFS_DIE_ON_ERR);
864 else { /* Merge has unresolved conflicts */
865 const struct worktree *wt;
866 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
867 update_ref(msg.buf, "NOTES_MERGE_PARTIAL", &result_oid, NULL,
868 0, UPDATE_REFS_DIE_ON_ERR);
869 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
870 wt = find_shared_symref("NOTES_MERGE_REF", default_notes_ref());
871 if (wt)
872 die(_("a notes merge into %s is already in-progress at %s"),
873 default_notes_ref(), wt->path);
874 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
875 die(_("failed to store link to current notes ref (%s)"),
876 default_notes_ref());
877 fprintf(stderr, _("Automatic notes merge failed. Fix conflicts in %s "
878 "and commit the result with 'git notes merge --commit', "
879 "or abort the merge with 'git notes merge --abort'.\n"),
880 git_path(NOTES_MERGE_WORKTREE));
881 }
882
883 free_notes(t);
884 strbuf_release(&remote_ref);
885 strbuf_release(&msg);
886 return result < 0; /* return non-zero on conflicts */
887}
888
889#define IGNORE_MISSING 1
890
891static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
892{
893 int status;
894 struct object_id oid;
895 if (get_oid(name, &oid))
896 return error(_("Failed to resolve '%s' as a valid ref."), name);
897 status = remove_note(t, oid.hash);
898 if (status)
899 fprintf(stderr, _("Object %s has no note\n"), name);
900 else
901 fprintf(stderr, _("Removing note for object %s\n"), name);
902 return (flag & IGNORE_MISSING) ? 0 : status;
903}
904
905static int remove_cmd(int argc, const char **argv, const char *prefix)
906{
907 unsigned flag = 0;
908 int from_stdin = 0;
909 struct option options[] = {
910 OPT_BIT(0, "ignore-missing", &flag,
911 N_("attempt to remove non-existent note is not an error"),
912 IGNORE_MISSING),
913 OPT_BOOL(0, "stdin", &from_stdin,
914 N_("read object names from the standard input")),
915 OPT_END()
916 };
917 struct notes_tree *t;
918 int retval = 0;
919
920 argc = parse_options(argc, argv, prefix, options,
921 git_notes_remove_usage, 0);
922
923 t = init_notes_check("remove", NOTES_INIT_WRITABLE);
924
925 if (!argc && !from_stdin) {
926 retval = remove_one_note(t, "HEAD", flag);
927 } else {
928 while (*argv) {
929 retval |= remove_one_note(t, *argv, flag);
930 argv++;
931 }
932 }
933 if (from_stdin) {
934 struct strbuf sb = STRBUF_INIT;
935 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
936 strbuf_rtrim(&sb);
937 retval |= remove_one_note(t, sb.buf, flag);
938 }
939 strbuf_release(&sb);
940 }
941 if (!retval)
942 commit_notes(t, "Notes removed by 'git notes remove'");
943 free_notes(t);
944 return retval;
945}
946
947static int prune(int argc, const char **argv, const char *prefix)
948{
949 struct notes_tree *t;
950 int show_only = 0, verbose = 0;
951 struct option options[] = {
952 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
953 OPT__VERBOSE(&verbose, N_("report pruned notes")),
954 OPT_END()
955 };
956
957 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
958 0);
959
960 if (argc) {
961 error(_("too many parameters"));
962 usage_with_options(git_notes_prune_usage, options);
963 }
964
965 t = init_notes_check("prune", NOTES_INIT_WRITABLE);
966
967 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
968 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
969 if (!show_only)
970 commit_notes(t, "Notes removed by 'git notes prune'");
971 free_notes(t);
972 return 0;
973}
974
975static int get_ref(int argc, const char **argv, const char *prefix)
976{
977 struct option options[] = { OPT_END() };
978 argc = parse_options(argc, argv, prefix, options,
979 git_notes_get_ref_usage, 0);
980
981 if (argc) {
982 error(_("too many parameters"));
983 usage_with_options(git_notes_get_ref_usage, options);
984 }
985
986 puts(default_notes_ref());
987 return 0;
988}
989
990int cmd_notes(int argc, const char **argv, const char *prefix)
991{
992 int result;
993 const char *override_notes_ref = NULL;
994 struct option options[] = {
995 OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
996 N_("use notes from <notes-ref>")),
997 OPT_END()
998 };
999
1000 git_config(git_default_config, NULL);
1001 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
1002 PARSE_OPT_STOP_AT_NON_OPTION);
1003
1004 if (override_notes_ref) {
1005 struct strbuf sb = STRBUF_INIT;
1006 strbuf_addstr(&sb, override_notes_ref);
1007 expand_notes_ref(&sb);
1008 setenv("GIT_NOTES_REF", sb.buf, 1);
1009 strbuf_release(&sb);
1010 }
1011
1012 if (argc < 1 || !strcmp(argv[0], "list"))
1013 result = list(argc, argv, prefix);
1014 else if (!strcmp(argv[0], "add"))
1015 result = add(argc, argv, prefix);
1016 else if (!strcmp(argv[0], "copy"))
1017 result = copy(argc, argv, prefix);
1018 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
1019 result = append_edit(argc, argv, prefix);
1020 else if (!strcmp(argv[0], "show"))
1021 result = show(argc, argv, prefix);
1022 else if (!strcmp(argv[0], "merge"))
1023 result = merge(argc, argv, prefix);
1024 else if (!strcmp(argv[0], "remove"))
1025 result = remove_cmd(argc, argv, prefix);
1026 else if (!strcmp(argv[0], "prune"))
1027 result = prune(argc, argv, prefix);
1028 else if (!strcmp(argv[0], "get-ref"))
1029 result = get_ref(argc, argv, prefix);
1030 else {
1031 result = error(_("unknown subcommand: %s"), argv[0]);
1032 usage_with_options(git_notes_usage, options);
1033 }
1034
1035 return result ? 1 : 0;
1036}