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