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