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