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