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