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