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