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