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