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