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