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