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