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