]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/notes.c
The fifth batch
[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 8 */
03eae9af 9#define USE_THE_REPOSITORY_VARIABLE
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"
d9f517d0 17#include "object-file.h"
dabab1d6 18#include "object-name.h"
68cd492a 19#include "object-store.h"
c339932b 20#include "path.h"
03eae9af 21
cf394719 22#include "pretty.h"
cd067d3b 23#include "refs.h"
d807c4a0 24#include "exec-cmd.h"
cd067d3b
JH
25#include "run-command.h"
26#include "parse-options.h"
6956f858 27#include "string-list.h"
75ef3f4a 28#include "notes-merge.h"
49c24704 29#include "notes-utils.h"
ac6c561b 30#include "worktree.h"
d48be35c 31#include "write-or-die.h"
f50fee4a 32
3d6a3164 33static const char *separator = "\n";
cd067d3b 34static const char * const git_notes_usage[] = {
9c9b4f2f 35 N_("git notes [--ref <notes-ref>] [list [<object>]]"),
dab0b9e1 36 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>] [-e]"),
9c9b4f2f 37 N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"),
dab0b9e1 38 N_("git notes [--ref <notes-ref>] append [--allow-empty] [--[no-]separator|--separator=<paragraph-break>] [--[no-]stripspace] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>] [-e]"),
9c9b4f2f
AH
39 N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
40 N_("git notes [--ref <notes-ref>] show [<object>]"),
41 N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"),
959d670d
JNA
42 "git notes merge --commit [-v | -q]",
43 "git notes merge --abort [-v | -q]",
9c9b4f2f 44 N_("git notes [--ref <notes-ref>] remove [<object>...]"),
e54b6335 45 N_("git notes [--ref <notes-ref>] prune [-n] [-v]"),
9c9b4f2f 46 N_("git notes [--ref <notes-ref>] get-ref"),
74884b52
SB
47 NULL
48};
49
50static const char * const git_notes_list_usage[] = {
e6b895ef 51 N_("git notes [list [<object>]]"),
74884b52
SB
52 NULL
53};
54
55static const char * const git_notes_add_usage[] = {
e6b895ef 56 N_("git notes add [<options>] [<object>]"),
74884b52
SB
57 NULL
58};
59
60static const char * const git_notes_copy_usage[] = {
e6b895ef
NTND
61 N_("git notes copy [<options>] <from-object> <to-object>"),
62 N_("git notes copy --stdin [<from-object> <to-object>]..."),
74884b52
SB
63 NULL
64};
65
66static const char * const git_notes_append_usage[] = {
e6b895ef 67 N_("git notes append [<options>] [<object>]"),
74884b52
SB
68 NULL
69};
70
71static const char * const git_notes_edit_usage[] = {
e6b895ef 72 N_("git notes edit [<object>]"),
74884b52
SB
73 NULL
74};
75
76static const char * const git_notes_show_usage[] = {
e6b895ef 77 N_("git notes show [<object>]"),
74884b52
SB
78 NULL
79};
80
75ef3f4a 81static const char * const git_notes_merge_usage[] = {
9c9b4f2f 82 N_("git notes merge [<options>] <notes-ref>"),
e6b895ef
NTND
83 N_("git notes merge --commit [<options>]"),
84 N_("git notes merge --abort [<options>]"),
75ef3f4a
JH
85 NULL
86};
87
74884b52 88static const char * const git_notes_remove_usage[] = {
e6b895ef 89 N_("git notes remove [<object>]"),
74884b52
SB
90 NULL
91};
92
93static const char * const git_notes_prune_usage[] = {
e6b895ef 94 N_("git notes prune [<options>]"),
cd067d3b
JH
95 NULL
96};
97
618cd757 98static const char * const git_notes_get_ref_usage[] = {
959d670d 99 "git notes get-ref",
618cd757
JH
100 NULL
101};
102
cd067d3b 103static const char note_template[] =
996ee6d2 104 N_("Write/edit the notes for the following object:");
cd067d3b 105
c4e2aa7d
TL
106enum notes_stripspace {
107 UNSPECIFIED = -1,
108 NO_STRIPSPACE = 0,
109 STRIPSPACE = 1,
110};
111
90bc19b3 112struct note_msg {
c4e2aa7d 113 enum notes_stripspace stripspace;
90bc19b3
TL
114 struct strbuf buf;
115};
116
bebf5c04 117struct note_data {
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"));
a1bb146a 182 strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_str);
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{
8b426c84 196 if (d->use_editor || !d->msg_nr) {
cd067d3b 197 int fd;
eff80a9f 198 struct strbuf buf = STRBUF_INIT;
cd067d3b
JH
199
200 /* write the template message before editing: */
bba59f58 201 d->edit_path = repo_git_path(the_repository, "NOTES_EDITMSG");
66e905b7 202 fd = xopen(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
cd067d3b 203
8b426c84 204 if (d->msg_nr)
bebf5c04 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');
a1bb146a 210 strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_str);
787cb8a4 211 strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)),
a1bb146a
JK
212 comment_line_str);
213 strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_str);
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)
2982b656 226 strbuf_stripspace(&d->buf, comment_line_str);
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)
2982b656 267 strbuf_stripspace(&d->buf, NULL);
90bc19b3
TL
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
6f33d8e2
KN
435static int list(int argc, const char **argv, const char *prefix,
436 struct repository *repo UNUSED)
cd067d3b 437{
cd067d3b 438 struct notes_tree *t;
bb7e4739 439 struct object_id object;
9ef72230 440 const struct object_id *note;
74884b52 441 int retval = -1;
cd067d3b 442 struct option options[] = {
cd067d3b
JH
443 OPT_END()
444 };
445
74884b52
SB
446 if (argc)
447 argc = parse_options(argc, argv, prefix, options,
448 git_notes_list_usage, 0);
cd067d3b 449
74884b52 450 if (1 < argc) {
b8657347 451 error(_("too many arguments"));
74884b52 452 usage_with_options(git_notes_list_usage, options);
dcf783a2
TR
453 }
454
ee76f92f 455 t = init_notes_check("list", 0);
74884b52 456 if (argc) {
d850b7a5 457 if (repo_get_oid(the_repository, argv[0], &object))
8d79589a 458 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
5ee8a954 459 note = get_note(t, &object);
74884b52 460 if (note) {
9ef72230 461 puts(oid_to_hex(note));
74884b52
SB
462 retval = 0;
463 } else
8d79589a 464 retval = error(_("no note found for object %s."),
bb7e4739 465 oid_to_hex(&object));
74884b52
SB
466 } else
467 retval = for_each_note(t, 0, list_each_note, NULL);
cd067d3b 468
74884b52
SB
469 free_notes(t);
470 return retval;
471}
cd067d3b 472
6f33d8e2
KN
473static int append_edit(int argc, const char **argv, const char *prefix,
474 struct repository *repo UNUSED);
84a7e35e 475
6f33d8e2
KN
476static int add(int argc, const char **argv, const char *prefix,
477 struct repository *repo)
74884b52 478{
d73a5b93 479 int force = 0, allow_empty = 0;
92b3385f 480 const char *object_ref;
74884b52 481 struct notes_tree *t;
bb7e4739 482 struct object_id object, new_note;
9ef72230 483 const struct object_id *note;
c4e2aa7d 484 struct note_data d = { .buf = STRBUF_INIT, .stripspace = UNSPECIFIED };
90bc19b3 485
cd067d3b 486 struct option options[] = {
203c8533 487 OPT_CALLBACK_F('m', "message", &d, N_("message"),
e6b895ef 488 N_("note contents as a string"), PARSE_OPT_NONEG,
203c8533
DL
489 parse_msg_arg),
490 OPT_CALLBACK_F('F', "file", &d, N_("file"),
e6b895ef 491 N_("note contents in a file"), PARSE_OPT_NONEG,
203c8533
DL
492 parse_file_arg),
493 OPT_CALLBACK_F('c', "reedit-message", &d, N_("object"),
e6b895ef 494 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
203c8533 495 parse_reedit_arg),
dab0b9e1
ASA
496 OPT_BOOL('e', "edit", &d.use_editor,
497 N_("edit note message in editor")),
203c8533 498 OPT_CALLBACK_F('C', "reuse-message", &d, N_("object"),
e6b895ef 499 N_("reuse specified note object"), PARSE_OPT_NONEG,
203c8533 500 parse_reuse_arg),
d73a5b93
JH
501 OPT_BOOL(0, "allow-empty", &allow_empty,
502 N_("allow storing empty note")),
7a60e3bb 503 OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE),
3d6a3164
TL
504 OPT_CALLBACK_F(0, "separator", &separator,
505 N_("<paragraph-break>"),
506 N_("insert <paragraph-break> between paragraphs"),
507 PARSE_OPT_OPTARG, parse_separator_arg),
c4e2aa7d
TL
508 OPT_BOOL(0, "stripspace", &d.stripspace,
509 N_("remove unnecessary whitespace")),
cd067d3b
JH
510 OPT_END()
511 };
512
74884b52 513 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
84a7e35e 514 PARSE_OPT_KEEP_ARGV0);
cd067d3b 515
84a7e35e 516 if (2 < argc) {
b8657347 517 error(_("too many arguments"));
74884b52 518 usage_with_options(git_notes_add_usage, options);
dcf783a2
TR
519 }
520
90bc19b3
TL
521 if (d.msg_nr)
522 concat_messages(&d);
90bc19b3 523
84a7e35e 524 object_ref = argc > 1 ? argv[1] : "HEAD";
cd067d3b 525
d850b7a5 526 if (repo_get_oid(the_repository, object_ref, &object))
8d79589a 527 die(_("failed to resolve '%s' as a valid ref."), object_ref);
cd067d3b 528
ee76f92f 529 t = init_notes_check("add", NOTES_INIT_WRITABLE);
5ee8a954 530 note = get_note(t, &object);
92b3385f 531
74884b52
SB
532 if (note) {
533 if (!force) {
b0de56c6 534 free_notes(t);
8b426c84 535 if (d.msg_nr) {
4282af0f 536 free_note_data(&d);
b0de56c6
JH
537 return error(_("Cannot add notes. "
538 "Found existing notes for object %s. "
539 "Use '-f' to overwrite existing notes"),
bb7e4739 540 oid_to_hex(&object));
84a7e35e 541 }
b0de56c6
JH
542 /*
543 * Redirect to "edit" subcommand.
544 *
545 * We only end up here if none of -m/-F/-c/-C or -f are
546 * given. The original args are therefore still in
547 * argv[0-1].
548 */
549 argv[0] = "edit";
6f33d8e2 550 return append_edit(argc, argv, prefix, repo);
74884b52 551 }
caeba0ef 552 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
bb7e4739 553 oid_to_hex(&object));
aaec9bcf
JH
554 }
555
e8adba25 556 prepare_note_data(&object, &d, note);
d73a5b93 557 if (d.buf.len || allow_empty) {
a09c985e 558 write_note_data(&d, &new_note);
5ee8a954 559 if (add_note(t, &object, &new_note, combine_notes_overwrite))
033abf97 560 BUG("combine_notes_overwrite failed");
1d18d758
NTND
561 commit_notes(the_repository, t,
562 "Notes added by 'git notes add'");
52694cda
JH
563 } else {
564 fprintf(stderr, _("Removing note for object %s\n"),
bb7e4739 565 oid_to_hex(&object));
566 remove_note(t, object.hash);
1d18d758
NTND
567 commit_notes(the_repository, t,
568 "Notes removed by 'git notes add'");
52694cda 569 }
160baa0d 570
52694cda 571 free_note_data(&d);
74884b52 572 free_notes(t);
b0de56c6 573 return 0;
74884b52 574}
cd067d3b 575
6f33d8e2
KN
576static int copy(int argc, const char **argv, const char *prefix,
577 struct repository *repo UNUSED)
74884b52
SB
578{
579 int retval = 0, force = 0, from_stdin = 0;
9ef72230 580 const struct object_id *from_note, *note;
74884b52 581 const char *object_ref;
bb7e4739 582 struct object_id object, from_obj;
74884b52
SB
583 struct notes_tree *t;
584 const char *rewrite_cmd = NULL;
585 struct option options[] = {
7a60e3bb 586 OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE),
d5d09d47 587 OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")),
e6b895ef
NTND
588 OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"),
589 N_("load rewriting config for <command> (implies "
590 "--stdin)")),
74884b52
SB
591 OPT_END()
592 };
cd067d3b 593
74884b52
SB
594 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
595 0);
e397421a 596
74884b52
SB
597 if (from_stdin || rewrite_cmd) {
598 if (argc) {
b8657347 599 error(_("too many arguments"));
74884b52 600 usage_with_options(git_notes_copy_usage, options);
5848769f 601 } else {
74884b52 602 return notes_copy_from_stdin(force, rewrite_cmd);
e73bbd96 603 }
e73bbd96
JH
604 }
605
d58deb9c 606 if (argc < 1) {
b8657347 607 error(_("too few arguments"));
bbb1b8a3
JK
608 usage_with_options(git_notes_copy_usage, options);
609 }
74884b52 610 if (2 < argc) {
b8657347 611 error(_("too many arguments"));
74884b52 612 usage_with_options(git_notes_copy_usage, options);
cd067d3b
JH
613 }
614
d850b7a5 615 if (repo_get_oid(the_repository, argv[0], &from_obj))
8d79589a 616 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
cd067d3b 617
74884b52 618 object_ref = 1 < argc ? argv[1] : "HEAD";
cd067d3b 619
d850b7a5 620 if (repo_get_oid(the_repository, object_ref, &object))
8d79589a 621 die(_("failed to resolve '%s' as a valid ref."), object_ref);
cd067d3b 622
ee76f92f 623 t = init_notes_check("copy", NOTES_INIT_WRITABLE);
5ee8a954 624 note = get_note(t, &object);
cd067d3b 625
74884b52 626 if (note) {
5848769f 627 if (!force) {
caeba0ef 628 retval = error(_("Cannot copy notes. Found existing "
74884b52 629 "notes for object %s. Use '-f' to "
caeba0ef 630 "overwrite existing notes"),
bb7e4739 631 oid_to_hex(&object));
74884b52 632 goto out;
5848769f 633 }
caeba0ef 634 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
bb7e4739 635 oid_to_hex(&object));
e397421a
JH
636 }
637
5ee8a954 638 from_note = get_note(t, &from_obj);
74884b52 639 if (!from_note) {
8d79589a 640 retval = error(_("missing notes on source object %s. Cannot "
bb7e4739 641 "copy."), oid_to_hex(&from_obj));
74884b52 642 goto out;
cd067d3b
JH
643 }
644
5ee8a954 645 if (add_note(t, &object, from_note, combine_notes_overwrite))
033abf97 646 BUG("combine_notes_overwrite failed");
1d18d758
NTND
647 commit_notes(the_repository, t,
648 "Notes added by 'git notes copy'");
74884b52
SB
649out:
650 free_notes(t);
651 return retval;
652}
7aa4754e 653
6f33d8e2
KN
654static int append_edit(int argc, const char **argv, const char *prefix,
655 struct repository *repo UNUSED)
74884b52 656{
d73a5b93 657 int allow_empty = 0;
74884b52
SB
658 const char *object_ref;
659 struct notes_tree *t;
bb7e4739 660 struct object_id object, new_note;
9ef72230 661 const struct object_id *note;
5b1ef2ce 662 char *logmsg;
74884b52 663 const char * const *usage;
c4e2aa7d 664 struct note_data d = { .buf = STRBUF_INIT, .stripspace = UNSPECIFIED };
74884b52 665 struct option options[] = {
203c8533 666 OPT_CALLBACK_F('m', "message", &d, N_("message"),
e6b895ef 667 N_("note contents as a string"), PARSE_OPT_NONEG,
203c8533
DL
668 parse_msg_arg),
669 OPT_CALLBACK_F('F', "file", &d, N_("file"),
e6b895ef 670 N_("note contents in a file"), PARSE_OPT_NONEG,
203c8533
DL
671 parse_file_arg),
672 OPT_CALLBACK_F('c', "reedit-message", &d, N_("object"),
e6b895ef 673 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
203c8533
DL
674 parse_reedit_arg),
675 OPT_CALLBACK_F('C', "reuse-message", &d, N_("object"),
e6b895ef 676 N_("reuse specified note object"), PARSE_OPT_NONEG,
203c8533 677 parse_reuse_arg),
dab0b9e1
ASA
678 OPT_BOOL('e', "edit", &d.use_editor,
679 N_("edit note message in editor")),
d73a5b93
JH
680 OPT_BOOL(0, "allow-empty", &allow_empty,
681 N_("allow storing empty note")),
3d6a3164
TL
682 OPT_CALLBACK_F(0, "separator", &separator,
683 N_("<paragraph-break>"),
684 N_("insert <paragraph-break> between paragraphs"),
685 PARSE_OPT_OPTARG, parse_separator_arg),
c4e2aa7d
TL
686 OPT_BOOL(0, "stripspace", &d.stripspace,
687 N_("remove unnecessary whitespace")),
74884b52
SB
688 OPT_END()
689 };
690 int edit = !strcmp(argv[0], "edit");
691
692 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
693 argc = parse_options(argc, argv, prefix, options, usage,
694 PARSE_OPT_KEEP_ARGV0);
92b3385f 695
74884b52 696 if (2 < argc) {
b8657347 697 error(_("too many arguments"));
74884b52 698 usage_with_options(usage, options);
cd067d3b
JH
699 }
700
8b426c84 701 if (d.msg_nr) {
90bc19b3 702 concat_messages(&d);
8b426c84
DD
703 if (edit)
704 fprintf(stderr, _("The -m/-F/-c/-C options have been "
705 "deprecated for the 'edit' subcommand.\n"
706 "Please use 'git notes add -f -m/-F/-c/-C' "
707 "instead.\n"));
708 }
74884b52
SB
709
710 object_ref = 1 < argc ? argv[1] : "HEAD";
711
d850b7a5 712 if (repo_get_oid(the_repository, object_ref, &object))
8d79589a 713 die(_("failed to resolve '%s' as a valid ref."), object_ref);
74884b52 714
ee76f92f 715 t = init_notes_check(argv[0], NOTES_INIT_WRITABLE);
5ee8a954 716 note = get_note(t, &object);
74884b52 717
e8adba25 718 prepare_note_data(&object, &d, edit && note ? note : NULL);
5848769f 719
52694cda
JH
720 if (note && !edit) {
721 /* Append buf to previous note contents */
722 unsigned long size;
723 enum object_type type;
b7d87ad5
TL
724 struct strbuf buf = STRBUF_INIT;
725 char *prev_buf = repo_read_object_file(the_repository, note, &type, &size);
52694cda 726
568459bf
JS
727 if (!prev_buf)
728 die(_("unable to read %s"), oid_to_hex(note));
729 if (size)
b7d87ad5 730 strbuf_add(&buf, prev_buf, size);
568459bf 731 if (d.buf.len && size)
b7d87ad5
TL
732 append_separator(&buf);
733 strbuf_insert(&d.buf, 0, buf.buf, buf.len);
734
52694cda 735 free(prev_buf);
b7d87ad5 736 strbuf_release(&buf);
52694cda 737 }
5848769f 738
d73a5b93 739 if (d.buf.len || allow_empty) {
a09c985e 740 write_note_data(&d, &new_note);
5ee8a954 741 if (add_note(t, &object, &new_note, combine_notes_overwrite))
033abf97 742 BUG("combine_notes_overwrite failed");
5b1ef2ce 743 logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]);
52694cda
JH
744 } else {
745 fprintf(stderr, _("Removing note for object %s\n"),
bb7e4739 746 oid_to_hex(&object));
747 remove_note(t, object.hash);
5b1ef2ce 748 logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]);
52694cda 749 }
1d18d758 750 commit_notes(the_repository, t, logmsg);
52694cda 751
5b1ef2ce 752 free(logmsg);
52694cda 753 free_note_data(&d);
cd067d3b 754 free_notes(t);
74884b52
SB
755 return 0;
756}
757
6f33d8e2
KN
758static int show(int argc, const char **argv, const char *prefix,
759 struct repository *repo UNUSED)
74884b52
SB
760{
761 const char *object_ref;
762 struct notes_tree *t;
bb7e4739 763 struct object_id object;
9ef72230 764 const struct object_id *note;
74884b52
SB
765 int retval;
766 struct option options[] = {
767 OPT_END()
768 };
769
770 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
771 0);
772
773 if (1 < argc) {
b8657347 774 error(_("too many arguments"));
74884b52
SB
775 usage_with_options(git_notes_show_usage, options);
776 }
777
778 object_ref = argc ? argv[0] : "HEAD";
779
d850b7a5 780 if (repo_get_oid(the_repository, object_ref, &object))
8d79589a 781 die(_("failed to resolve '%s' as a valid ref."), object_ref);
74884b52 782
ee76f92f 783 t = init_notes_check("show", 0);
5ee8a954 784 note = get_note(t, &object);
74884b52
SB
785
786 if (!note)
8d79589a 787 retval = error(_("no note found for object %s."),
bb7e4739 788 oid_to_hex(&object));
74884b52 789 else {
9ef72230 790 const char *show_args[3] = {"show", oid_to_hex(note), NULL};
74884b52
SB
791 retval = execv_git_cmd(show_args);
792 }
793 free_notes(t);
5848769f 794 return retval;
cd067d3b 795}
74884b52 796
6abb3655
JH
797static int merge_abort(struct notes_merge_options *o)
798{
799 int ret = 0;
800
801 /*
802 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
803 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
804 */
805
2e5c4758 806 if (refs_delete_ref(get_main_ref_store(the_repository), NULL, "NOTES_MERGE_PARTIAL", NULL, 0))
8d79589a 807 ret += error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
2e5c4758 808 if (refs_delete_ref(get_main_ref_store(the_repository), NULL, "NOTES_MERGE_REF", NULL, REF_NO_DEREF))
8d79589a 809 ret += error(_("failed to delete ref NOTES_MERGE_REF"));
6abb3655 810 if (notes_merge_abort(o))
8d79589a 811 ret += error(_("failed to remove 'git notes merge' worktree"));
6abb3655
JH
812 return ret;
813}
814
815static int merge_commit(struct notes_merge_options *o)
816{
817 struct strbuf msg = STRBUF_INIT;
2928325f 818 struct object_id oid, parent_oid;
187b623e 819 struct notes_tree t = {0};
6abb3655
JH
820 struct commit *partial;
821 struct pretty_print_context pretty_ctx;
96ec7b1e 822 void *local_ref_to_free;
d5a35c11 823 int ret;
6abb3655
JH
824
825 /*
826 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
827 * and target notes ref from .git/NOTES_MERGE_REF.
828 */
829
d850b7a5 830 if (repo_get_oid(the_repository, "NOTES_MERGE_PARTIAL", &oid))
8d79589a 831 die(_("failed to read ref NOTES_MERGE_PARTIAL"));
2122f675 832 else if (!(partial = lookup_commit_reference(the_repository, &oid)))
8d79589a 833 die(_("could not find commit from NOTES_MERGE_PARTIAL."));
ecb5091f 834 else if (repo_parse_commit(the_repository, partial))
8d79589a 835 die(_("could not parse commit from NOTES_MERGE_PARTIAL."));
6abb3655 836
6cfd6a9d 837 if (partial->parents)
2928325f 838 oidcpy(&parent_oid, &partial->parents->item->object.oid);
6cfd6a9d 839 else
9da95bda 840 oidclr(&parent_oid, the_repository->hash_algo);
6cfd6a9d 841
187b623e 842 init_notes(&t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
6abb3655 843
96ec7b1e 844 o->local_ref = local_ref_to_free =
2e5c4758
PS
845 refs_resolve_refdup(get_main_ref_store(the_repository),
846 "NOTES_MERGE_REF", 0, &oid, NULL);
6abb3655 847 if (!o->local_ref)
8d79589a 848 die(_("failed to resolve NOTES_MERGE_REF"));
6abb3655 849
187b623e 850 if (notes_merge_commit(o, &t, partial, &oid))
8d79589a 851 die(_("failed to finalize notes merge"));
6abb3655
JH
852
853 /* Reuse existing commit message in reflog message */
854 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
bab82164
ÆAB
855 repo_format_commit_message(the_repository, partial, "%s", &msg,
856 &pretty_ctx);
6abb3655 857 strbuf_trim(&msg);
a91cc7fa 858 strbuf_insertstr(&msg, 0, "notes: ");
2e5c4758
PS
859 refs_update_ref(get_main_ref_store(the_repository), msg.buf,
860 o->local_ref, &oid,
861 is_null_oid(&parent_oid) ? NULL : &parent_oid,
862 0, UPDATE_REFS_DIE_ON_ERR);
6abb3655 863
187b623e 864 free_notes(&t);
6abb3655 865 strbuf_release(&msg);
d5a35c11 866 ret = merge_abort(o);
96ec7b1e 867 free(local_ref_to_free);
d5a35c11 868 return ret;
6abb3655
JH
869}
870
d2d68d99
JK
871static int git_config_get_notes_strategy(const char *key,
872 enum notes_merge_strategy *strategy)
873{
344b5484 874 char *value;
d2d68d99 875
344b5484 876 if (git_config_get_string(key, &value))
d2d68d99
JK
877 return 1;
878 if (parse_notes_merge_strategy(value, strategy))
0c2c37d1 879 git_die_config(the_repository, key, _("unknown notes merge strategy %s"), value);
d2d68d99 880
344b5484 881 free(value);
d2d68d99
JK
882 return 0;
883}
884
6f33d8e2
KN
885static int merge(int argc, const char **argv, const char *prefix,
886 struct repository *repo UNUSED)
75ef3f4a
JH
887{
888 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
bb7e4739 889 struct object_id result_oid;
2085b16a 890 struct notes_tree *t;
75ef3f4a 891 struct notes_merge_options o;
6abb3655 892 int do_merge = 0, do_commit = 0, do_abort = 0;
75ef3f4a 893 int verbosity = 0, result;
3228e671 894 const char *strategy = NULL;
75ef3f4a 895 struct option options[] = {
e6b895ef 896 OPT_GROUP(N_("General options")),
75ef3f4a 897 OPT__VERBOSITY(&verbosity),
e6b895ef
NTND
898 OPT_GROUP(N_("Merge options")),
899 OPT_STRING('s', "strategy", &strategy, N_("strategy"),
900 N_("resolve notes conflicts using the given strategy "
901 "(manual/ours/theirs/union/cat_sort_uniq)")),
902 OPT_GROUP(N_("Committing unmerged notes")),
3e4a67b4
NTND
903 OPT_SET_INT_F(0, "commit", &do_commit,
904 N_("finalize notes merge by committing unmerged notes"),
905 1, PARSE_OPT_NONEG),
e6b895ef 906 OPT_GROUP(N_("Aborting notes merge resolution")),
3e4a67b4
NTND
907 OPT_SET_INT_F(0, "abort", &do_abort,
908 N_("abort notes merge"),
909 1, PARSE_OPT_NONEG),
75ef3f4a
JH
910 OPT_END()
911 };
1e7e4a11 912 char *notes_ref;
75ef3f4a
JH
913
914 argc = parse_options(argc, argv, prefix, options,
915 git_notes_merge_usage, 0);
916
6abb3655
JH
917 if (strategy || do_commit + do_abort == 0)
918 do_merge = 1;
919 if (do_merge + do_commit + do_abort != 1) {
5313827f 920 error(_("cannot mix --commit, --abort or -s/--strategy"));
6abb3655
JH
921 usage_with_options(git_notes_merge_usage, options);
922 }
923
924 if (do_merge && argc != 1) {
8d79589a 925 error(_("must specify a notes ref to merge"));
75ef3f4a 926 usage_with_options(git_notes_merge_usage, options);
6abb3655 927 } else if (!do_merge && argc) {
b8657347 928 error(_("too many arguments"));
6abb3655 929 usage_with_options(git_notes_merge_usage, options);
75ef3f4a
JH
930 }
931
5684200f 932 init_notes_merge_options(the_repository, &o);
75ef3f4a
JH
933 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
934
6abb3655
JH
935 if (do_abort)
936 return merge_abort(&o);
937 if (do_commit)
938 return merge_commit(&o);
939
1e7e4a11
PS
940 notes_ref = default_notes_ref(the_repository);
941 o.local_ref = notes_ref;
75ef3f4a 942 strbuf_addstr(&remote_ref, argv[0]);
b3715b75 943 expand_loose_notes_ref(&remote_ref);
75ef3f4a
JH
944 o.remote_ref = remote_ref.buf;
945
ee76f92f 946 t = init_notes_check("merge", NOTES_INIT_WRITABLE);
d2d68d99 947
3228e671 948 if (strategy) {
93efcad3 949 if (parse_notes_merge_strategy(strategy, &o.strategy)) {
8d79589a 950 error(_("unknown -s/--strategy: %s"), strategy);
3228e671
JH
951 usage_with_options(git_notes_merge_usage, options);
952 }
d2d68d99 953 } else {
4f655e22
JK
954 struct strbuf merge_key = STRBUF_INIT;
955 const char *short_ref = NULL;
3228e671 956
4f655e22 957 if (!skip_prefix(o.local_ref, "refs/notes/", &short_ref))
033abf97 958 BUG("local ref %s is outside of refs/notes/",
4f655e22
JK
959 o.local_ref);
960
961 strbuf_addf(&merge_key, "notes.%s.mergeStrategy", short_ref);
962
963 if (git_config_get_notes_strategy(merge_key.buf, &o.strategy))
964 git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy);
965
966 strbuf_release(&merge_key);
3228e671 967 }
75ef3f4a
JH
968
969 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
1e7e4a11 970 remote_ref.buf, notes_ref);
443259cf 971 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
2085b16a 972
5237e0eb 973 result = notes_merge(&o, t, &result_oid);
2085b16a 974
5237e0eb 975 if (result >= 0) /* Merge resulted (trivially) in result_oid */
75ef3f4a 976 /* Update default notes ref with new commit */
2e5c4758 977 refs_update_ref(get_main_ref_store(the_repository), msg.buf,
1e7e4a11 978 notes_ref, &result_oid, NULL, 0,
2e5c4758 979 UPDATE_REFS_DIE_ON_ERR);
6abb3655 980 else { /* Merge has unresolved conflicts */
c8dd491f 981 struct worktree **worktrees;
d3b9ac07 982 const struct worktree *wt;
88dd321c
PS
983 char *path;
984
6abb3655 985 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
2e5c4758
PS
986 refs_update_ref(get_main_ref_store(the_repository), msg.buf,
987 "NOTES_MERGE_PARTIAL", &result_oid, NULL,
988 0, UPDATE_REFS_DIE_ON_ERR);
6abb3655 989 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
c8dd491f
AK
990 worktrees = get_worktrees();
991 wt = find_shared_symref(worktrees, "NOTES_MERGE_REF",
1e7e4a11 992 notes_ref);
d3b9ac07 993 if (wt)
8d79589a 994 die(_("a notes merge into %s is already in-progress at %s"),
1e7e4a11 995 notes_ref, wt->path);
c8dd491f 996 free_worktrees(worktrees);
1e7e4a11 997 if (refs_update_symref(get_main_ref_store(the_repository), "NOTES_MERGE_REF", notes_ref, NULL))
8d79589a 998 die(_("failed to store link to current notes ref (%s)"),
1e7e4a11 999 notes_ref);
88dd321c
PS
1000
1001 path = repo_git_path(the_repository, NOTES_MERGE_WORKTREE);
89b9e31d
TZ
1002 fprintf(stderr, _("Automatic notes merge failed. Fix conflicts in %s "
1003 "and commit the result with 'git notes merge --commit', "
1004 "or abort the merge with 'git notes merge --abort'.\n"),
88dd321c
PS
1005 path);
1006 free(path);
6abb3655 1007 }
75ef3f4a 1008
2085b16a 1009 free_notes(t);
1e7e4a11 1010 free(notes_ref);
75ef3f4a
JH
1011 strbuf_release(&remote_ref);
1012 strbuf_release(&msg);
809f38c8 1013 return result < 0; /* return non-zero on conflicts */
75ef3f4a
JH
1014}
1015
46538012 1016#define IGNORE_MISSING 1
2d370d2f
JH
1017
1018static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
c3ab1a8e
JH
1019{
1020 int status;
bb7e4739 1021 struct object_id oid;
d850b7a5 1022 if (repo_get_oid(the_repository, name, &oid))
c3ab1a8e 1023 return error(_("Failed to resolve '%s' as a valid ref."), name);
bb7e4739 1024 status = remove_note(t, oid.hash);
c3ab1a8e
JH
1025 if (status)
1026 fprintf(stderr, _("Object %s has no note\n"), name);
1027 else
1028 fprintf(stderr, _("Removing note for object %s\n"), name);
46538012 1029 return (flag & IGNORE_MISSING) ? 0 : status;
c3ab1a8e
JH
1030}
1031
6f33d8e2
KN
1032static int remove_cmd(int argc, const char **argv, const char *prefix,
1033 struct repository *repo UNUSED)
74884b52 1034{
2d370d2f 1035 unsigned flag = 0;
46538012 1036 int from_stdin = 0;
74884b52 1037 struct option options[] = {
2d370d2f 1038 OPT_BIT(0, "ignore-missing", &flag,
e6b895ef 1039 N_("attempt to remove non-existent note is not an error"),
46538012 1040 IGNORE_MISSING),
d5d09d47 1041 OPT_BOOL(0, "stdin", &from_stdin,
e6b895ef 1042 N_("read object names from the standard input")),
74884b52
SB
1043 OPT_END()
1044 };
74884b52 1045 struct notes_tree *t;
c3ab1a8e 1046 int retval = 0;
74884b52
SB
1047
1048 argc = parse_options(argc, argv, prefix, options,
1049 git_notes_remove_usage, 0);
1050
ee76f92f 1051 t = init_notes_check("remove", NOTES_INIT_WRITABLE);
74884b52 1052
46538012 1053 if (!argc && !from_stdin) {
2d370d2f 1054 retval = remove_one_note(t, "HEAD", flag);
c3ab1a8e
JH
1055 } else {
1056 while (*argv) {
2d370d2f 1057 retval |= remove_one_note(t, *argv, flag);
c3ab1a8e
JH
1058 argv++;
1059 }
1ee1e43d 1060 }
46538012
JH
1061 if (from_stdin) {
1062 struct strbuf sb = STRBUF_INIT;
1063 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1064 strbuf_rtrim(&sb);
1065 retval |= remove_one_note(t, sb.buf, flag);
1066 }
1067 strbuf_release(&sb);
1068 }
c3ab1a8e 1069 if (!retval)
1d18d758
NTND
1070 commit_notes(the_repository, t,
1071 "Notes removed by 'git notes remove'");
74884b52 1072 free_notes(t);
1ee1e43d 1073 return retval;
74884b52
SB
1074}
1075
6f33d8e2
KN
1076static int prune(int argc, const char **argv, const char *prefix,
1077 struct repository *repo UNUSED)
74884b52
SB
1078{
1079 struct notes_tree *t;
a9f2adff 1080 int show_only = 0, verbose = 0;
74884b52 1081 struct option options[] = {
b34c77e3
VA
1082 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
1083 OPT__VERBOSE(&verbose, N_("report pruned notes")),
74884b52
SB
1084 OPT_END()
1085 };
1086
1087 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
1088 0);
1089
1090 if (argc) {
b8657347 1091 error(_("too many arguments"));
74884b52
SB
1092 usage_with_options(git_notes_prune_usage, options);
1093 }
1094
ee76f92f 1095 t = init_notes_check("prune", NOTES_INIT_WRITABLE);
74884b52 1096
a9f2adff
MG
1097 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
1098 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
1099 if (!show_only)
1d18d758
NTND
1100 commit_notes(the_repository, t,
1101 "Notes removed by 'git notes prune'");
74884b52
SB
1102 free_notes(t);
1103 return 0;
1104}
1105
6f33d8e2
KN
1106static int get_ref(int argc, const char **argv, const char *prefix,
1107 struct repository *repo UNUSED)
618cd757
JH
1108{
1109 struct option options[] = { OPT_END() };
1e7e4a11 1110 char *notes_ref;
618cd757
JH
1111 argc = parse_options(argc, argv, prefix, options,
1112 git_notes_get_ref_usage, 0);
1113
1114 if (argc) {
b8657347 1115 error(_("too many arguments"));
618cd757
JH
1116 usage_with_options(git_notes_get_ref_usage, options);
1117 }
1118
1e7e4a11
PS
1119 notes_ref = default_notes_ref(the_repository);
1120 puts(notes_ref);
1121 free(notes_ref);
618cd757
JH
1122 return 0;
1123}
1124
9b1cb507
JC
1125int cmd_notes(int argc,
1126 const char **argv,
1127 const char *prefix,
6f33d8e2 1128 struct repository *repo)
74884b52 1129{
74884b52 1130 const char *override_notes_ref = NULL;
1c7c25ae 1131 parse_opt_subcommand_fn *fn = NULL;
74884b52 1132 struct option options[] = {
e703d711 1133 OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
9c9b4f2f 1134 N_("use notes from <notes-ref>")),
54ef7676
SG
1135 OPT_SUBCOMMAND("list", &fn, list),
1136 OPT_SUBCOMMAND("add", &fn, add),
1137 OPT_SUBCOMMAND("copy", &fn, copy),
1138 OPT_SUBCOMMAND("append", &fn, append_edit),
1139 OPT_SUBCOMMAND("edit", &fn, append_edit),
1140 OPT_SUBCOMMAND("show", &fn, show),
1141 OPT_SUBCOMMAND("merge", &fn, merge),
1142 OPT_SUBCOMMAND("remove", &fn, remove_cmd),
1143 OPT_SUBCOMMAND("prune", &fn, prune),
1144 OPT_SUBCOMMAND("get-ref", &fn, get_ref),
74884b52
SB
1145 OPT_END()
1146 };
1147
1148 git_config(git_default_config, NULL);
1149 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
54ef7676 1150 PARSE_OPT_SUBCOMMAND_OPTIONAL);
1c7c25ae
SG
1151 if (!fn) {
1152 if (argc) {
dd834d75 1153 error(_("unknown subcommand: `%s'"), argv[0]);
1c7c25ae
SG
1154 usage_with_options(git_notes_usage, options);
1155 }
1156 fn = list;
54ef7676 1157 }
74884b52
SB
1158
1159 if (override_notes_ref) {
1160 struct strbuf sb = STRBUF_INIT;
74884b52 1161 strbuf_addstr(&sb, override_notes_ref);
8ef313e1 1162 expand_notes_ref(&sb);
74884b52
SB
1163 setenv("GIT_NOTES_REF", sb.buf, 1);
1164 strbuf_release(&sb);
1165 }
1166
6f33d8e2 1167 return !!fn(argc, argv, prefix, repo);
74884b52 1168}