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