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