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