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