t3301: verify that 'git notes' removes empty notes by default
[git/git.git] / builtin / notes.c
CommitLineData
cd067d3b
JH
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,
09b7e220 7 * and builtin/tag.c by Kristian Høgsberg and Carlos Rica.
cd067d3b
JH
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"
6956f858 19#include "string-list.h"
75ef3f4a 20#include "notes-merge.h"
49c24704 21#include "notes-utils.h"
f50fee4a 22
cd067d3b 23static const char * const git_notes_usage[] = {
e6b895ef
NTND
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"),
74884b52
SB
36 NULL
37};
38
39static const char * const git_notes_list_usage[] = {
e6b895ef 40 N_("git notes [list [<object>]]"),
74884b52
SB
41 NULL
42};
43
44static const char * const git_notes_add_usage[] = {
e6b895ef 45 N_("git notes add [<options>] [<object>]"),
74884b52
SB
46 NULL
47};
48
49static const char * const git_notes_copy_usage[] = {
e6b895ef
NTND
50 N_("git notes copy [<options>] <from-object> <to-object>"),
51 N_("git notes copy --stdin [<from-object> <to-object>]..."),
74884b52
SB
52 NULL
53};
54
55static const char * const git_notes_append_usage[] = {
e6b895ef 56 N_("git notes append [<options>] [<object>]"),
74884b52
SB
57 NULL
58};
59
60static const char * const git_notes_edit_usage[] = {
e6b895ef 61 N_("git notes edit [<object>]"),
74884b52
SB
62 NULL
63};
64
65static const char * const git_notes_show_usage[] = {
e6b895ef 66 N_("git notes show [<object>]"),
74884b52
SB
67 NULL
68};
69
75ef3f4a 70static const char * const git_notes_merge_usage[] = {
e6b895ef
NTND
71 N_("git notes merge [<options>] <notes_ref>"),
72 N_("git notes merge --commit [<options>]"),
73 N_("git notes merge --abort [<options>]"),
75ef3f4a
JH
74 NULL
75};
76
74884b52 77static const char * const git_notes_remove_usage[] = {
e6b895ef 78 N_("git notes remove [<object>]"),
74884b52
SB
79 NULL
80};
81
82static const char * const git_notes_prune_usage[] = {
e6b895ef 83 N_("git notes prune [<options>]"),
cd067d3b
JH
84 NULL
85};
86
618cd757 87static const char * const git_notes_get_ref_usage[] = {
e6b895ef 88 N_("git notes get-ref"),
618cd757
JH
89 NULL
90};
91
cd067d3b 92static const char note_template[] =
eff80a9f 93 "\nWrite/edit the notes for the following object:\n";
cd067d3b 94
348f199b
JH
95struct msg_arg {
96 int given;
0691cff7 97 int use_editor;
348f199b
JH
98 struct strbuf buf;
99};
100
e397421a
JH
101static 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
cd067d3b
JH
109static 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
121static 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};
d3180279 125 struct child_process show = CHILD_PROCESS_INIT;
cd067d3b 126 struct strbuf buf = STRBUF_INIT;
eff80a9f 127 struct strbuf cbuf = STRBUF_INIT;
cd067d3b
JH
128
129 /* Invoke "git show --stat --no-notes $object" */
cd067d3b
JH
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))
caeba0ef 136 die(_("unable to start 'show' for object '%s'"),
cd067d3b
JH
137 sha1_to_hex(object));
138
eff80a9f
JH
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);
cd067d3b 143
eff80a9f 144 strbuf_release(&cbuf);
cd067d3b 145 strbuf_release(&buf);
eff80a9f 146
cd067d3b 147 if (finish_command(&show))
caeba0ef 148 die(_("failed to finish 'show' for object '%s'"),
cd067d3b
JH
149 sha1_to_hex(object));
150}
151
348f199b
JH
152static void create_note(const unsigned char *object, struct msg_arg *msg,
153 int append_only, const unsigned char *prev,
cd067d3b
JH
154 unsigned char *result)
155{
156 char *path = NULL;
157
0691cff7 158 if (msg->use_editor || !msg->given) {
cd067d3b 159 int fd;
eff80a9f 160 struct strbuf buf = STRBUF_INIT;
cd067d3b
JH
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)
caeba0ef 166 die_errno(_("could not create file '%s'"), path);
cd067d3b 167
0691cff7
JH
168 if (msg->given)
169 write_or_die(fd, msg->buf.buf, msg->buf.len);
170 else if (prev && !append_only)
cd067d3b 171 write_note_data(fd, prev);
eff80a9f
JH
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);
cd067d3b
JH
177
178 write_commented_object(fd, object);
179
180 close(fd);
eff80a9f 181 strbuf_release(&buf);
0691cff7 182 strbuf_reset(&(msg->buf));
cd067d3b 183
348f199b 184 if (launch_editor(path, &(msg->buf), NULL)) {
caeba0ef
ÆAB
185 die(_("Please supply the note contents using either -m" \
186 " or -F option"));
cd067d3b 187 }
348f199b 188 stripspace(&(msg->buf), 1);
cd067d3b
JH
189 }
190
2347fae5
JH
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
348f199b
JH
197 strbuf_grow(&(msg->buf), size + 1);
198 if (msg->buf.len && prev_buf && size)
199 strbuf_insert(&(msg->buf), 0, "\n", 1);
2347fae5 200 if (prev_buf && size)
348f199b 201 strbuf_insert(&(msg->buf), 0, prev_buf, size);
2347fae5
JH
202 free(prev_buf);
203 }
204
348f199b 205 if (!msg->buf.len) {
caeba0ef 206 fprintf(stderr, _("Removing note for object %s\n"),
cd067d3b
JH
207 sha1_to_hex(object));
208 hashclr(result);
209 } else {
348f199b 210 if (write_sha1_file(msg->buf.buf, msg->buf.len, blob_type, result)) {
caeba0ef 211 error(_("unable to write note object"));
cd067d3b 212 if (path)
ad5fe377 213 error(_("The note contents have been left in %s"),
cd067d3b
JH
214 path);
215 exit(128);
216 }
217 }
218
219 if (path) {
220 unlink_or_warn(path);
221 free(path);
222 }
223}
224
cd067d3b
JH
225static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
226{
227 struct msg_arg *msg = opt->value;
228
348f199b 229 strbuf_grow(&(msg->buf), strlen(arg) + 2);
cd067d3b 230 if (msg->buf.len)
43a61b84 231 strbuf_addch(&(msg->buf), '\n');
cd067d3b 232 strbuf_addstr(&(msg->buf), arg);
348f199b
JH
233 stripspace(&(msg->buf), 0);
234
235 msg->given = 1;
236 return 0;
237}
238
239static int parse_file_arg(const struct option *opt, const char *arg, int unset)
240{
241 struct msg_arg *msg = opt->value;
242
348f199b 243 if (msg->buf.len)
43a61b84 244 strbuf_addch(&(msg->buf), '\n');
348f199b
JH
245 if (!strcmp(arg, "-")) {
246 if (strbuf_read(&(msg->buf), 0, 1024) < 0)
caeba0ef 247 die_errno(_("cannot read '%s'"), arg);
348f199b 248 } else if (strbuf_read_file(&(msg->buf), arg, 1024) < 0)
caeba0ef 249 die_errno(_("could not open or read '%s'"), arg);
348f199b
JH
250 stripspace(&(msg->buf), 0);
251
cd067d3b
JH
252 msg->given = 1;
253 return 0;
254}
255
0691cff7
JH
256static 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
0691cff7 264 if (msg->buf.len)
43a61b84 265 strbuf_addch(&(msg->buf), '\n');
0691cff7
JH
266
267 if (get_sha1(arg, object))
caeba0ef 268 die(_("Failed to resolve '%s' as a valid ref."), arg);
511726e4 269 if (!(buf = read_sha1_file(object, &type, &len))) {
0691cff7 270 free(buf);
ce8daa1e
JH
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);
0691cff7
JH
276 }
277 strbuf_add(&(msg->buf), buf, len);
278 free(buf);
279
280 msg->given = 1;
281 return 0;
282}
283
284static 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
c2e86add 291static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
160baa0d
TR
292{
293 struct strbuf buf = STRBUF_INIT;
6956f858 294 struct notes_rewrite_cfg *c = NULL;
ef7a8e3b 295 struct notes_tree *t = NULL;
160baa0d 296 int ret = 0;
80a14665 297 const char *msg = "Notes added by 'git notes copy'";
160baa0d 298
6956f858
TR
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 }
160baa0d
TR
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])
caeba0ef 315 die(_("Malformed input line: '%s'."), buf.buf);
160baa0d
TR
316 strbuf_rtrim(split[0]);
317 strbuf_rtrim(split[1]);
318 if (get_sha1(split[0]->buf, from_obj))
caeba0ef 319 die(_("Failed to resolve '%s' as a valid ref."), split[0]->buf);
160baa0d 320 if (get_sha1(split[1]->buf, to_obj))
caeba0ef 321 die(_("Failed to resolve '%s' as a valid ref."), split[1]->buf);
160baa0d 322
6956f858
TR
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);
160baa0d
TR
328
329 if (err) {
caeba0ef 330 error(_("Failed to copy notes from '%s' to '%s'"),
160baa0d
TR
331 split[0]->buf, split[1]->buf);
332 ret = 1;
333 }
334
335 strbuf_list_free(split);
336 }
337
6956f858 338 if (!rewrite_cmd) {
80a14665 339 commit_notes(t, msg);
6956f858
TR
340 free_notes(t);
341 } else {
80a14665 342 finish_copy_notes_for_rewrite(c, msg);
6956f858 343 }
160baa0d
TR
344 return ret;
345}
346
74884b52 347static struct notes_tree *init_notes_check(const char *subcommand)
cd067d3b 348{
cd067d3b 349 struct notes_tree *t;
74884b52
SB
350 init_notes(NULL, NULL, NULL, 0);
351 t = &default_notes_tree;
a0b4dfa9 352
59556548 353 if (!starts_with(t->ref, "refs/notes/"))
74884b52
SB
354 die("Refusing to %s notes in %s (outside of refs/notes/)",
355 subcommand, t->ref);
356 return t;
357}
358
359static int list(int argc, const char **argv, const char *prefix)
cd067d3b 360{
cd067d3b 361 struct notes_tree *t;
74884b52 362 unsigned char object[20];
cd067d3b 363 const unsigned char *note;
74884b52 364 int retval = -1;
cd067d3b 365 struct option options[] = {
cd067d3b
JH
366 OPT_END()
367 };
368
74884b52
SB
369 if (argc)
370 argc = parse_options(argc, argv, prefix, options,
371 git_notes_list_usage, 0);
cd067d3b 372
74884b52 373 if (1 < argc) {
caeba0ef 374 error(_("too many parameters"));
74884b52 375 usage_with_options(git_notes_list_usage, options);
dcf783a2
TR
376 }
377
74884b52
SB
378 t = init_notes_check("list");
379 if (argc) {
380 if (get_sha1(argv[0], object))
caeba0ef 381 die(_("Failed to resolve '%s' as a valid ref."), argv[0]);
74884b52
SB
382 note = get_note(t, object);
383 if (note) {
384 puts(sha1_to_hex(note));
385 retval = 0;
386 } else
caeba0ef 387 retval = error(_("No note found for object %s."),
74884b52
SB
388 sha1_to_hex(object));
389 } else
390 retval = for_each_note(t, 0, list_each_note, NULL);
cd067d3b 391
74884b52
SB
392 free_notes(t);
393 return retval;
394}
cd067d3b 395
84a7e35e
JH
396static int append_edit(int argc, const char **argv, const char *prefix);
397
74884b52
SB
398static int add(int argc, const char **argv, const char *prefix)
399{
400 int retval = 0, force = 0;
92b3385f 401 const char *object_ref;
74884b52
SB
402 struct notes_tree *t;
403 unsigned char object[20], new_note[20];
92b3385f 404 char logmsg[100];
74884b52 405 const unsigned char *note;
0691cff7 406 struct msg_arg msg = { 0, 0, STRBUF_INIT };
cd067d3b 407 struct option options[] = {
b0ff9654 408 { OPTION_CALLBACK, 'm', "message", &msg, N_("message"),
e6b895ef 409 N_("note contents as a string"), PARSE_OPT_NONEG,
43a61b84 410 parse_msg_arg},
e6b895ef
NTND
411 { OPTION_CALLBACK, 'F', "file", &msg, N_("file"),
412 N_("note contents in a file"), PARSE_OPT_NONEG,
43a61b84 413 parse_file_arg},
e6b895ef
NTND
414 { OPTION_CALLBACK, 'c', "reedit-message", &msg, N_("object"),
415 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
43a61b84 416 parse_reedit_arg},
e6b895ef
NTND
417 { OPTION_CALLBACK, 'C', "reuse-message", &msg, N_("object"),
418 N_("reuse specified note object"), PARSE_OPT_NONEG,
43a61b84 419 parse_reuse_arg},
e6b895ef 420 OPT__FORCE(&force, N_("replace existing notes")),
cd067d3b
JH
421 OPT_END()
422 };
423
74884b52 424 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
84a7e35e 425 PARSE_OPT_KEEP_ARGV0);
cd067d3b 426
84a7e35e 427 if (2 < argc) {
caeba0ef 428 error(_("too many parameters"));
74884b52 429 usage_with_options(git_notes_add_usage, options);
dcf783a2
TR
430 }
431
84a7e35e 432 object_ref = argc > 1 ? argv[1] : "HEAD";
cd067d3b 433
74884b52 434 if (get_sha1(object_ref, object))
caeba0ef 435 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
cd067d3b 436
74884b52
SB
437 t = init_notes_check("add");
438 note = get_note(t, object);
92b3385f 439
74884b52
SB
440 if (note) {
441 if (!force) {
84a7e35e
JH
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 }
caeba0ef 454 retval = error(_("Cannot add notes. Found existing notes "
74884b52 455 "for object %s. Use '-f' to overwrite "
caeba0ef 456 "existing notes"), sha1_to_hex(object));
74884b52
SB
457 goto out;
458 }
caeba0ef 459 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
74884b52 460 sha1_to_hex(object));
aaec9bcf
JH
461 }
462
74884b52 463 create_note(object, &msg, 0, note, new_note);
7aa4754e 464
74884b52
SB
465 if (is_null_sha1(new_note))
466 remove_note(t, object);
180619a5
JH
467 else if (add_note(t, object, new_note, combine_notes_overwrite))
468 die("BUG: combine_notes_overwrite failed");
160baa0d 469
74884b52
SB
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);
473out:
474 free_notes(t);
475 strbuf_release(&(msg.buf));
476 return retval;
477}
cd067d3b 478
74884b52
SB
479static 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[] = {
e6b895ef 488 OPT__FORCE(&force, N_("replace existing notes")),
d5d09d47 489 OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")),
e6b895ef
NTND
490 OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"),
491 N_("load rewriting config for <command> (implies "
492 "--stdin)")),
74884b52
SB
493 OPT_END()
494 };
cd067d3b 495
74884b52
SB
496 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
497 0);
e397421a 498
74884b52
SB
499 if (from_stdin || rewrite_cmd) {
500 if (argc) {
caeba0ef 501 error(_("too many parameters"));
74884b52 502 usage_with_options(git_notes_copy_usage, options);
5848769f 503 } else {
74884b52 504 return notes_copy_from_stdin(force, rewrite_cmd);
e73bbd96 505 }
e73bbd96
JH
506 }
507
bbb1b8a3 508 if (argc < 2) {
caeba0ef 509 error(_("too few parameters"));
bbb1b8a3
JK
510 usage_with_options(git_notes_copy_usage, options);
511 }
74884b52 512 if (2 < argc) {
caeba0ef 513 error(_("too many parameters"));
74884b52 514 usage_with_options(git_notes_copy_usage, options);
cd067d3b
JH
515 }
516
74884b52 517 if (get_sha1(argv[0], from_obj))
caeba0ef 518 die(_("Failed to resolve '%s' as a valid ref."), argv[0]);
cd067d3b 519
74884b52 520 object_ref = 1 < argc ? argv[1] : "HEAD";
cd067d3b 521
74884b52 522 if (get_sha1(object_ref, object))
caeba0ef 523 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
cd067d3b 524
74884b52 525 t = init_notes_check("copy");
cd067d3b
JH
526 note = get_note(t, object);
527
74884b52 528 if (note) {
5848769f 529 if (!force) {
caeba0ef 530 retval = error(_("Cannot copy notes. Found existing "
74884b52 531 "notes for object %s. Use '-f' to "
caeba0ef 532 "overwrite existing notes"),
74884b52
SB
533 sha1_to_hex(object));
534 goto out;
5848769f 535 }
caeba0ef 536 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
5848769f 537 sha1_to_hex(object));
e397421a
JH
538 }
539
74884b52
SB
540 from_note = get_note(t, from_obj);
541 if (!from_note) {
caeba0ef
ÆAB
542 retval = error(_("Missing notes on source object %s. Cannot "
543 "copy."), sha1_to_hex(from_obj));
74884b52 544 goto out;
cd067d3b
JH
545 }
546
180619a5
JH
547 if (add_note(t, object, from_note, combine_notes_overwrite))
548 die("BUG: combine_notes_overwrite failed");
74884b52
SB
549 commit_notes(t, "Notes added by 'git notes copy'");
550out:
551 free_notes(t);
552 return retval;
553}
7aa4754e 554
74884b52
SB
555static 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[] = {
b0ff9654 565 { OPTION_CALLBACK, 'm', "message", &msg, N_("message"),
e6b895ef 566 N_("note contents as a string"), PARSE_OPT_NONEG,
74884b52 567 parse_msg_arg},
e6b895ef
NTND
568 { OPTION_CALLBACK, 'F', "file", &msg, N_("file"),
569 N_("note contents in a file"), PARSE_OPT_NONEG,
74884b52 570 parse_file_arg},
e6b895ef
NTND
571 { OPTION_CALLBACK, 'c', "reedit-message", &msg, N_("object"),
572 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
74884b52 573 parse_reedit_arg},
e6b895ef
NTND
574 { OPTION_CALLBACK, 'C', "reuse-message", &msg, N_("object"),
575 N_("reuse specified note object"), PARSE_OPT_NONEG,
74884b52
SB
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);
92b3385f 584
74884b52 585 if (2 < argc) {
caeba0ef 586 error(_("too many parameters"));
74884b52 587 usage_with_options(usage, options);
cd067d3b
JH
588 }
589
74884b52 590 if (msg.given && edit)
caeba0ef 591 fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
74884b52 592 "for the 'edit' subcommand.\n"
caeba0ef 593 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
74884b52
SB
594
595 object_ref = 1 < argc ? argv[1] : "HEAD";
596
597 if (get_sha1(object_ref, object))
caeba0ef 598 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
74884b52
SB
599
600 t = init_notes_check(argv[0]);
601 note = get_note(t, object);
602
603 create_note(object, &msg, !edit, note, new_note);
5848769f
JH
604
605 if (is_null_sha1(new_note))
606 remove_note(t, object);
180619a5
JH
607 else if (add_note(t, object, new_note, combine_notes_overwrite))
608 die("BUG: combine_notes_overwrite failed");
5848769f 609
5848769f 610 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
92b3385f 611 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
a0b4dfa9 612 commit_notes(t, logmsg);
cd067d3b 613 free_notes(t);
348f199b 614 strbuf_release(&(msg.buf));
74884b52
SB
615 return 0;
616}
617
618static 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) {
caeba0ef 633 error(_("too many parameters"));
74884b52
SB
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))
caeba0ef 640 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
74884b52
SB
641
642 t = init_notes_check("show");
643 note = get_note(t, object);
644
645 if (!note)
caeba0ef 646 retval = error(_("No note found for object %s."),
74884b52
SB
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);
5848769f 653 return retval;
cd067d3b 654}
74884b52 655
6abb3655
JH
656static 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
674static int merge_commit(struct notes_merge_options *o)
675{
676 struct strbuf msg = STRBUF_INIT;
6cfd6a9d 677 unsigned char sha1[20], parent_sha1[20];
6abb3655
JH
678 struct notes_tree *t;
679 struct commit *partial;
680 struct pretty_print_context pretty_ctx;
96ec7b1e 681 void *local_ref_to_free;
d5a35c11 682 int ret;
6abb3655
JH
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
6cfd6a9d
JH
696 if (partial->parents)
697 hashcpy(parent_sha1, partial->parents->item->object.sha1);
698 else
699 hashclr(parent_sha1);
700
6abb3655
JH
701 t = xcalloc(1, sizeof(struct notes_tree));
702 init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
703
96ec7b1e 704 o->local_ref = local_ref_to_free =
7695d118 705 resolve_refdup("NOTES_MERGE_REF", 0, sha1, NULL);
6abb3655
JH
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);
6cfd6a9d
JH
717 update_ref(msg.buf, o->local_ref, sha1,
718 is_null_sha1(parent_sha1) ? NULL : parent_sha1,
f4124112 719 0, UPDATE_REFS_DIE_ON_ERR);
6abb3655
JH
720
721 free_notes(t);
722 strbuf_release(&msg);
d5a35c11 723 ret = merge_abort(o);
96ec7b1e 724 free(local_ref_to_free);
d5a35c11 725 return ret;
6abb3655
JH
726}
727
75ef3f4a
JH
728static 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];
2085b16a 732 struct notes_tree *t;
75ef3f4a 733 struct notes_merge_options o;
6abb3655 734 int do_merge = 0, do_commit = 0, do_abort = 0;
75ef3f4a 735 int verbosity = 0, result;
3228e671 736 const char *strategy = NULL;
75ef3f4a 737 struct option options[] = {
e6b895ef 738 OPT_GROUP(N_("General options")),
75ef3f4a 739 OPT__VERBOSITY(&verbosity),
e6b895ef
NTND
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")),
4741edd5 745 { OPTION_SET_INT, 0, "commit", &do_commit, NULL,
e6b895ef 746 N_("finalize notes merge by committing unmerged notes"),
4741edd5 747 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
e6b895ef 748 OPT_GROUP(N_("Aborting notes merge resolution")),
4741edd5 749 { OPTION_SET_INT, 0, "abort", &do_abort, NULL,
e6b895ef 750 N_("abort notes merge"),
4741edd5 751 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
75ef3f4a
JH
752 OPT_END()
753 };
754
755 argc = parse_options(argc, argv, prefix, options,
756 git_notes_merge_usage, 0);
757
6abb3655
JH
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) {
75ef3f4a
JH
766 error("Must specify a notes ref to merge");
767 usage_with_options(git_notes_merge_usage, options);
6abb3655
JH
768 } else if (!do_merge && argc) {
769 error("too many parameters");
770 usage_with_options(git_notes_merge_usage, options);
75ef3f4a
JH
771 }
772
773 init_notes_merge_options(&o);
774 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
775
6abb3655
JH
776 if (do_abort)
777 return merge_abort(&o);
778 if (do_commit)
779 return merge_commit(&o);
780
75ef3f4a
JH
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
3228e671
JH
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;
a6a09095
JH
795 else if (!strcmp(strategy, "cat_sort_uniq"))
796 o.strategy = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ;
3228e671
JH
797 else {
798 error("Unknown -s/--strategy: %s", strategy);
799 usage_with_options(git_notes_merge_usage, options);
800 }
801 }
802
2085b16a 803 t = init_notes_check("merge");
75ef3f4a
JH
804
805 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
806 remote_ref.buf, default_notes_ref());
443259cf 807 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
2085b16a
JH
808
809 result = notes_merge(&o, t, result_sha1);
810
811 if (result >= 0) /* Merge resulted (trivially) in result_sha1 */
75ef3f4a
JH
812 /* Update default notes ref with new commit */
813 update_ref(msg.buf, default_notes_ref(), result_sha1, NULL,
f4124112 814 0, UPDATE_REFS_DIE_ON_ERR);
6abb3655
JH
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,
f4124112 818 0, UPDATE_REFS_DIE_ON_ERR);
6abb3655
JH
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",
809f38c8 826 git_path(NOTES_MERGE_WORKTREE));
6abb3655 827 }
75ef3f4a 828
2085b16a 829 free_notes(t);
75ef3f4a
JH
830 strbuf_release(&remote_ref);
831 strbuf_release(&msg);
809f38c8 832 return result < 0; /* return non-zero on conflicts */
75ef3f4a
JH
833}
834
46538012 835#define IGNORE_MISSING 1
2d370d2f
JH
836
837static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
c3ab1a8e
JH
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);
46538012 848 return (flag & IGNORE_MISSING) ? 0 : status;
c3ab1a8e
JH
849}
850
74884b52
SB
851static int remove_cmd(int argc, const char **argv, const char *prefix)
852{
2d370d2f 853 unsigned flag = 0;
46538012 854 int from_stdin = 0;
74884b52 855 struct option options[] = {
2d370d2f 856 OPT_BIT(0, "ignore-missing", &flag,
e6b895ef 857 N_("attempt to remove non-existent note is not an error"),
46538012 858 IGNORE_MISSING),
d5d09d47 859 OPT_BOOL(0, "stdin", &from_stdin,
e6b895ef 860 N_("read object names from the standard input")),
74884b52
SB
861 OPT_END()
862 };
74884b52 863 struct notes_tree *t;
c3ab1a8e 864 int retval = 0;
74884b52
SB
865
866 argc = parse_options(argc, argv, prefix, options,
867 git_notes_remove_usage, 0);
868
74884b52
SB
869 t = init_notes_check("remove");
870
46538012 871 if (!argc && !from_stdin) {
2d370d2f 872 retval = remove_one_note(t, "HEAD", flag);
c3ab1a8e
JH
873 } else {
874 while (*argv) {
2d370d2f 875 retval |= remove_one_note(t, *argv, flag);
c3ab1a8e
JH
876 argv++;
877 }
1ee1e43d 878 }
46538012
JH
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 }
c3ab1a8e
JH
887 if (!retval)
888 commit_notes(t, "Notes removed by 'git notes remove'");
74884b52 889 free_notes(t);
1ee1e43d 890 return retval;
74884b52
SB
891}
892
893static int prune(int argc, const char **argv, const char *prefix)
894{
895 struct notes_tree *t;
a9f2adff 896 int show_only = 0, verbose = 0;
74884b52 897 struct option options[] = {
e21adb8c 898 OPT__DRY_RUN(&show_only, "do not remove, show only"),
fd03881a 899 OPT__VERBOSE(&verbose, "report pruned notes"),
74884b52
SB
900 OPT_END()
901 };
902
903 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
904 0);
905
906 if (argc) {
caeba0ef 907 error(_("too many parameters"));
74884b52
SB
908 usage_with_options(git_notes_prune_usage, options);
909 }
910
911 t = init_notes_check("prune");
912
a9f2adff
MG
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'");
74884b52
SB
917 free_notes(t);
918 return 0;
919}
920
618cd757
JH
921static 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
74884b52
SB
936int 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[] = {
e703d711 941 OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
e6b895ef 942 N_("use notes from <notes_ref>")),
74884b52
SB
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;
74884b52 952 strbuf_addstr(&sb, override_notes_ref);
8ef313e1 953 expand_notes_ref(&sb);
74884b52
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);
75ef3f4a
JH
968 else if (!strcmp(argv[0], "merge"))
969 result = merge(argc, argv, prefix);
74884b52
SB
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);
618cd757
JH
974 else if (!strcmp(argv[0], "get-ref"))
975 result = get_ref(argc, argv, prefix);
74884b52 976 else {
caeba0ef 977 result = error(_("Unknown subcommand: %s"), argv[0]);
74884b52
SB
978 usage_with_options(git_notes_usage, options);
979 }
980
981 return result ? 1 : 0;
982}