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