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