]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/notes.c
git notes merge: Manual conflict resolution, part 1/2
[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>",
74884b52 30 "git notes [--ref <notes_ref>] remove [<object>]",
a9f2adff 31 "git notes [--ref <notes_ref>] prune [-n | -v]",
74884b52
SB
32 NULL
33};
34
35static const char * const git_notes_list_usage[] = {
e397421a 36 "git notes [list [<object>]]",
74884b52
SB
37 NULL
38};
39
40static const char * const git_notes_add_usage[] = {
41 "git notes add [<options>] [<object>]",
42 NULL
43};
44
45static const char * const git_notes_copy_usage[] = {
46 "git notes copy [<options>] <from-object> <to-object>",
47 "git notes copy --stdin [<from-object> <to-object>]...",
48 NULL
49};
50
51static const char * const git_notes_append_usage[] = {
52 "git notes append [<options>] [<object>]",
53 NULL
54};
55
56static const char * const git_notes_edit_usage[] = {
aaec9bcf 57 "git notes edit [<object>]",
74884b52
SB
58 NULL
59};
60
61static const char * const git_notes_show_usage[] = {
cd067d3b 62 "git notes show [<object>]",
74884b52
SB
63 NULL
64};
65
75ef3f4a
JH
66static const char * const git_notes_merge_usage[] = {
67 "git notes merge [<options>] <notes_ref>",
68 NULL
69};
70
74884b52 71static const char * const git_notes_remove_usage[] = {
92b3385f 72 "git notes remove [<object>]",
74884b52
SB
73 NULL
74};
75
76static const char * const git_notes_prune_usage[] = {
a9f2adff 77 "git notes prune [<options>]",
cd067d3b
JH
78 NULL
79};
80
81static const char note_template[] =
82 "\n"
83 "#\n"
84 "# Write/edit the notes for the following object:\n"
85 "#\n";
86
348f199b
JH
87struct msg_arg {
88 int given;
0691cff7 89 int use_editor;
348f199b
JH
90 struct strbuf buf;
91};
92
8ef313e1
JH
93static void expand_notes_ref(struct strbuf *sb)
94{
95 if (!prefixcmp(sb->buf, "refs/notes/"))
96 return; /* we're happy */
97 else if (!prefixcmp(sb->buf, "notes/"))
98 strbuf_insert(sb, 0, "refs/", 5);
99 else
100 strbuf_insert(sb, 0, "refs/notes/", 11);
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))
139 die("unable to start 'show' for object '%s'",
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)
145 die_errno("can't fdopen 'show' output fd");
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))
155 die_errno("failed to close pipe to 'show' for object '%s'",
156 sha1_to_hex(object));
157 if (finish_command(&show))
158 die("failed to finish 'show' for object '%s'",
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)
175 die_errno("could not create file '%s'", path);
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)) {
cd067d3b
JH
189 die("Please supply the note contents using either -m" \
190 " or -F option");
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) {
cd067d3b
JH
210 fprintf(stderr, "Removing note for object %s\n",
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)) {
cd067d3b
JH
215 error("unable to write note object");
216 if (path)
217 error("The note contents has been left in %s",
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)
251 die_errno("cannot read '%s'", arg);
252 } else if (strbuf_read_file(&(msg->buf), arg, 1024) < 0)
253 die_errno("could not open or read '%s'", arg);
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))
272 die("Failed to resolve '%s' as a valid ref.", arg);
273 if (!(buf = read_sha1_file(object, &type, &len)) || !len) {
274 free(buf);
275 die("Failed to read object '%s'.", arg);;
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)
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;
323 else
324 return NULL;
325}
326
327static int notes_rewrite_config(const char *k, const char *v, void *cb)
328{
329 struct notes_rewrite_cfg *c = cb;
330 if (!prefixcmp(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
331 c->enabled = git_config_bool(k, v);
332 return 0;
333 } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
334 if (!v)
335 config_error_nonbool(k);
336 c->combine = parse_combine_notes_fn(v);
337 if (!c->combine) {
338 error("Bad notes.rewriteMode value: '%s'", v);
339 return 1;
340 }
341 return 0;
342 } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
343 /* note that a refs/ prefix is implied in the
344 * underlying for_each_glob_ref */
345 if (!prefixcmp(v, "refs/notes/"))
346 string_list_add_refs_by_glob(c->refs, v);
347 else
348 warning("Refusing to rewrite notes in %s"
349 " (outside of refs/notes/)", v);
350 return 0;
351 }
352
353 return 0;
354}
355
356
357struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
358{
359 struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
360 const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
361 const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
362 c->cmd = cmd;
363 c->enabled = 1;
364 c->combine = combine_notes_concatenate;
365 c->refs = xcalloc(1, sizeof(struct string_list));
366 c->refs->strdup_strings = 1;
367 c->refs_from_env = 0;
368 c->mode_from_env = 0;
369 if (rewrite_mode_env) {
370 c->mode_from_env = 1;
371 c->combine = parse_combine_notes_fn(rewrite_mode_env);
372 if (!c->combine)
373 error("Bad " GIT_NOTES_REWRITE_MODE_ENVIRONMENT
374 " value: '%s'", rewrite_mode_env);
375 }
376 if (rewrite_refs_env) {
377 c->refs_from_env = 1;
378 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
379 }
380 git_config(notes_rewrite_config, c);
381 if (!c->enabled || !c->refs->nr) {
382 string_list_clear(c->refs, 0);
383 free(c->refs);
384 free(c);
385 return NULL;
386 }
387 c->trees = load_notes_trees(c->refs);
388 string_list_clear(c->refs, 0);
389 free(c->refs);
390 return c;
391}
392
393int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
394 const unsigned char *from_obj, const unsigned char *to_obj)
395{
396 int ret = 0;
397 int i;
398 for (i = 0; c->trees[i]; i++)
399 ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
400 return ret;
401}
402
403void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c)
404{
405 int i;
406 for (i = 0; c->trees[i]; i++) {
407 commit_notes(c->trees[i], "Notes added by 'git notes copy'");
408 free_notes(c->trees[i]);
409 }
410 free(c->trees);
411 free(c);
412}
413
414int notes_copy_from_stdin(int force, const char *rewrite_cmd)
160baa0d
TR
415{
416 struct strbuf buf = STRBUF_INIT;
6956f858 417 struct notes_rewrite_cfg *c = NULL;
ef7a8e3b 418 struct notes_tree *t = NULL;
160baa0d
TR
419 int ret = 0;
420
6956f858
TR
421 if (rewrite_cmd) {
422 c = init_copy_notes_for_rewrite(rewrite_cmd);
423 if (!c)
424 return 0;
425 } else {
426 init_notes(NULL, NULL, NULL, 0);
427 t = &default_notes_tree;
428 }
160baa0d
TR
429
430 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
431 unsigned char from_obj[20], to_obj[20];
432 struct strbuf **split;
433 int err;
434
435 split = strbuf_split(&buf, ' ');
436 if (!split[0] || !split[1])
437 die("Malformed input line: '%s'.", buf.buf);
438 strbuf_rtrim(split[0]);
439 strbuf_rtrim(split[1]);
440 if (get_sha1(split[0]->buf, from_obj))
441 die("Failed to resolve '%s' as a valid ref.", split[0]->buf);
442 if (get_sha1(split[1]->buf, to_obj))
443 die("Failed to resolve '%s' as a valid ref.", split[1]->buf);
444
6956f858
TR
445 if (rewrite_cmd)
446 err = copy_note_for_rewrite(c, from_obj, to_obj);
447 else
448 err = copy_note(t, from_obj, to_obj, force,
449 combine_notes_overwrite);
160baa0d
TR
450
451 if (err) {
452 error("Failed to copy notes from '%s' to '%s'",
453 split[0]->buf, split[1]->buf);
454 ret = 1;
455 }
456
457 strbuf_list_free(split);
458 }
459
6956f858
TR
460 if (!rewrite_cmd) {
461 commit_notes(t, "Notes added by 'git notes copy'");
462 free_notes(t);
463 } else {
464 finish_copy_notes_for_rewrite(c);
465 }
160baa0d
TR
466 return ret;
467}
468
74884b52 469static struct notes_tree *init_notes_check(const char *subcommand)
cd067d3b 470{
cd067d3b 471 struct notes_tree *t;
74884b52
SB
472 init_notes(NULL, NULL, NULL, 0);
473 t = &default_notes_tree;
a0b4dfa9 474
74884b52
SB
475 if (prefixcmp(t->ref, "refs/notes/"))
476 die("Refusing to %s notes in %s (outside of refs/notes/)",
477 subcommand, t->ref);
478 return t;
479}
480
481static int list(int argc, const char **argv, const char *prefix)
cd067d3b 482{
cd067d3b 483 struct notes_tree *t;
74884b52 484 unsigned char object[20];
cd067d3b 485 const unsigned char *note;
74884b52 486 int retval = -1;
cd067d3b 487 struct option options[] = {
cd067d3b
JH
488 OPT_END()
489 };
490
74884b52
SB
491 if (argc)
492 argc = parse_options(argc, argv, prefix, options,
493 git_notes_list_usage, 0);
cd067d3b 494
74884b52
SB
495 if (1 < argc) {
496 error("too many parameters");
497 usage_with_options(git_notes_list_usage, options);
dcf783a2
TR
498 }
499
74884b52
SB
500 t = init_notes_check("list");
501 if (argc) {
502 if (get_sha1(argv[0], object))
503 die("Failed to resolve '%s' as a valid ref.", argv[0]);
504 note = get_note(t, object);
505 if (note) {
506 puts(sha1_to_hex(note));
507 retval = 0;
508 } else
509 retval = error("No note found for object %s.",
510 sha1_to_hex(object));
511 } else
512 retval = for_each_note(t, 0, list_each_note, NULL);
cd067d3b 513
74884b52
SB
514 free_notes(t);
515 return retval;
516}
cd067d3b 517
74884b52
SB
518static int add(int argc, const char **argv, const char *prefix)
519{
520 int retval = 0, force = 0;
92b3385f 521 const char *object_ref;
74884b52
SB
522 struct notes_tree *t;
523 unsigned char object[20], new_note[20];
92b3385f 524 char logmsg[100];
74884b52 525 const unsigned char *note;
0691cff7 526 struct msg_arg msg = { 0, 0, STRBUF_INIT };
cd067d3b 527 struct option options[] = {
43a61b84
JH
528 { OPTION_CALLBACK, 'm', "message", &msg, "MSG",
529 "note contents as a string", PARSE_OPT_NONEG,
530 parse_msg_arg},
531 { OPTION_CALLBACK, 'F', "file", &msg, "FILE",
532 "note contents in a file", PARSE_OPT_NONEG,
533 parse_file_arg},
534 { OPTION_CALLBACK, 'c', "reedit-message", &msg, "OBJECT",
535 "reuse and edit specified note object", PARSE_OPT_NONEG,
536 parse_reedit_arg},
537 { OPTION_CALLBACK, 'C', "reuse-message", &msg, "OBJECT",
538 "reuse specified note object", PARSE_OPT_NONEG,
539 parse_reuse_arg},
7aa4754e 540 OPT_BOOLEAN('f', "force", &force, "replace existing notes"),
cd067d3b
JH
541 OPT_END()
542 };
543
74884b52
SB
544 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
545 0);
cd067d3b 546
74884b52
SB
547 if (1 < argc) {
548 error("too many parameters");
549 usage_with_options(git_notes_add_usage, options);
dcf783a2
TR
550 }
551
74884b52 552 object_ref = argc ? argv[0] : "HEAD";
cd067d3b 553
74884b52
SB
554 if (get_sha1(object_ref, object))
555 die("Failed to resolve '%s' as a valid ref.", object_ref);
cd067d3b 556
74884b52
SB
557 t = init_notes_check("add");
558 note = get_note(t, object);
92b3385f 559
74884b52
SB
560 if (note) {
561 if (!force) {
562 retval = error("Cannot add notes. Found existing notes "
563 "for object %s. Use '-f' to overwrite "
564 "existing notes", sha1_to_hex(object));
565 goto out;
566 }
567 fprintf(stderr, "Overwriting existing notes for object %s\n",
568 sha1_to_hex(object));
aaec9bcf
JH
569 }
570
74884b52 571 create_note(object, &msg, 0, note, new_note);
7aa4754e 572
74884b52
SB
573 if (is_null_sha1(new_note))
574 remove_note(t, object);
180619a5
JH
575 else if (add_note(t, object, new_note, combine_notes_overwrite))
576 die("BUG: combine_notes_overwrite failed");
160baa0d 577
74884b52
SB
578 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
579 is_null_sha1(new_note) ? "removed" : "added", "add");
580 commit_notes(t, logmsg);
581out:
582 free_notes(t);
583 strbuf_release(&(msg.buf));
584 return retval;
585}
cd067d3b 586
74884b52
SB
587static int copy(int argc, const char **argv, const char *prefix)
588{
589 int retval = 0, force = 0, from_stdin = 0;
590 const unsigned char *from_note, *note;
591 const char *object_ref;
592 unsigned char object[20], from_obj[20];
593 struct notes_tree *t;
594 const char *rewrite_cmd = NULL;
595 struct option options[] = {
596 OPT_BOOLEAN('f', "force", &force, "replace existing notes"),
597 OPT_BOOLEAN(0, "stdin", &from_stdin, "read objects from stdin"),
598 OPT_STRING(0, "for-rewrite", &rewrite_cmd, "command",
599 "load rewriting config for <command> (implies "
600 "--stdin)"),
601 OPT_END()
602 };
cd067d3b 603
74884b52
SB
604 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
605 0);
e397421a 606
74884b52
SB
607 if (from_stdin || rewrite_cmd) {
608 if (argc) {
609 error("too many parameters");
610 usage_with_options(git_notes_copy_usage, options);
5848769f 611 } else {
74884b52 612 return notes_copy_from_stdin(force, rewrite_cmd);
e73bbd96 613 }
e73bbd96
JH
614 }
615
bbb1b8a3
JK
616 if (argc < 2) {
617 error("too few parameters");
618 usage_with_options(git_notes_copy_usage, options);
619 }
74884b52 620 if (2 < argc) {
cd067d3b 621 error("too many parameters");
74884b52 622 usage_with_options(git_notes_copy_usage, options);
cd067d3b
JH
623 }
624
74884b52
SB
625 if (get_sha1(argv[0], from_obj))
626 die("Failed to resolve '%s' as a valid ref.", argv[0]);
cd067d3b 627
74884b52 628 object_ref = 1 < argc ? argv[1] : "HEAD";
cd067d3b 629
74884b52
SB
630 if (get_sha1(object_ref, object))
631 die("Failed to resolve '%s' as a valid ref.", object_ref);
cd067d3b 632
74884b52 633 t = init_notes_check("copy");
cd067d3b
JH
634 note = get_note(t, object);
635
74884b52 636 if (note) {
5848769f 637 if (!force) {
74884b52
SB
638 retval = error("Cannot copy notes. Found existing "
639 "notes for object %s. Use '-f' to "
640 "overwrite existing notes",
641 sha1_to_hex(object));
642 goto out;
5848769f 643 }
5848769f
JH
644 fprintf(stderr, "Overwriting existing notes for object %s\n",
645 sha1_to_hex(object));
e397421a
JH
646 }
647
74884b52
SB
648 from_note = get_note(t, from_obj);
649 if (!from_note) {
650 retval = error("Missing notes on source object %s. Cannot "
651 "copy.", sha1_to_hex(from_obj));
652 goto out;
cd067d3b
JH
653 }
654
180619a5
JH
655 if (add_note(t, object, from_note, combine_notes_overwrite))
656 die("BUG: combine_notes_overwrite failed");
74884b52
SB
657 commit_notes(t, "Notes added by 'git notes copy'");
658out:
659 free_notes(t);
660 return retval;
661}
7aa4754e 662
74884b52
SB
663static int append_edit(int argc, const char **argv, const char *prefix)
664{
665 const char *object_ref;
666 struct notes_tree *t;
667 unsigned char object[20], new_note[20];
668 const unsigned char *note;
669 char logmsg[100];
670 const char * const *usage;
671 struct msg_arg msg = { 0, 0, STRBUF_INIT };
672 struct option options[] = {
673 { OPTION_CALLBACK, 'm', "message", &msg, "MSG",
674 "note contents as a string", PARSE_OPT_NONEG,
675 parse_msg_arg},
676 { OPTION_CALLBACK, 'F', "file", &msg, "FILE",
677 "note contents in a file", PARSE_OPT_NONEG,
678 parse_file_arg},
679 { OPTION_CALLBACK, 'c', "reedit-message", &msg, "OBJECT",
680 "reuse and edit specified note object", PARSE_OPT_NONEG,
681 parse_reedit_arg},
682 { OPTION_CALLBACK, 'C', "reuse-message", &msg, "OBJECT",
683 "reuse specified note object", PARSE_OPT_NONEG,
684 parse_reuse_arg},
685 OPT_END()
686 };
687 int edit = !strcmp(argv[0], "edit");
688
689 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
690 argc = parse_options(argc, argv, prefix, options, usage,
691 PARSE_OPT_KEEP_ARGV0);
92b3385f 692
74884b52
SB
693 if (2 < argc) {
694 error("too many parameters");
695 usage_with_options(usage, options);
cd067d3b
JH
696 }
697
74884b52
SB
698 if (msg.given && edit)
699 fprintf(stderr, "The -m/-F/-c/-C options have been deprecated "
700 "for the 'edit' subcommand.\n"
701 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n");
702
703 object_ref = 1 < argc ? argv[1] : "HEAD";
704
705 if (get_sha1(object_ref, object))
706 die("Failed to resolve '%s' as a valid ref.", object_ref);
707
708 t = init_notes_check(argv[0]);
709 note = get_note(t, object);
710
711 create_note(object, &msg, !edit, note, new_note);
5848769f
JH
712
713 if (is_null_sha1(new_note))
714 remove_note(t, object);
180619a5
JH
715 else if (add_note(t, object, new_note, combine_notes_overwrite))
716 die("BUG: combine_notes_overwrite failed");
5848769f 717
5848769f 718 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
92b3385f 719 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
a0b4dfa9 720 commit_notes(t, logmsg);
cd067d3b 721 free_notes(t);
348f199b 722 strbuf_release(&(msg.buf));
74884b52
SB
723 return 0;
724}
725
726static int show(int argc, const char **argv, const char *prefix)
727{
728 const char *object_ref;
729 struct notes_tree *t;
730 unsigned char object[20];
731 const unsigned char *note;
732 int retval;
733 struct option options[] = {
734 OPT_END()
735 };
736
737 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
738 0);
739
740 if (1 < argc) {
741 error("too many parameters");
742 usage_with_options(git_notes_show_usage, options);
743 }
744
745 object_ref = argc ? argv[0] : "HEAD";
746
747 if (get_sha1(object_ref, object))
748 die("Failed to resolve '%s' as a valid ref.", object_ref);
749
750 t = init_notes_check("show");
751 note = get_note(t, object);
752
753 if (!note)
754 retval = error("No note found for object %s.",
755 sha1_to_hex(object));
756 else {
757 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
758 retval = execv_git_cmd(show_args);
759 }
760 free_notes(t);
5848769f 761 return retval;
cd067d3b 762}
74884b52 763
75ef3f4a
JH
764static int merge(int argc, const char **argv, const char *prefix)
765{
766 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
767 unsigned char result_sha1[20];
2085b16a 768 struct notes_tree *t;
75ef3f4a
JH
769 struct notes_merge_options o;
770 int verbosity = 0, result;
3228e671 771 const char *strategy = NULL;
75ef3f4a
JH
772 struct option options[] = {
773 OPT__VERBOSITY(&verbosity),
3228e671
JH
774 OPT_STRING('s', "strategy", &strategy, "strategy",
775 "resolve notes conflicts using the given "
776 "strategy (manual/ours/theirs/union)"),
75ef3f4a
JH
777 OPT_END()
778 };
779
780 argc = parse_options(argc, argv, prefix, options,
781 git_notes_merge_usage, 0);
782
783 if (argc != 1) {
784 error("Must specify a notes ref to merge");
785 usage_with_options(git_notes_merge_usage, options);
786 }
787
788 init_notes_merge_options(&o);
789 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
790
791 o.local_ref = default_notes_ref();
792 strbuf_addstr(&remote_ref, argv[0]);
793 expand_notes_ref(&remote_ref);
794 o.remote_ref = remote_ref.buf;
795
3228e671
JH
796 if (strategy) {
797 if (!strcmp(strategy, "manual"))
798 o.strategy = NOTES_MERGE_RESOLVE_MANUAL;
799 else if (!strcmp(strategy, "ours"))
800 o.strategy = NOTES_MERGE_RESOLVE_OURS;
801 else if (!strcmp(strategy, "theirs"))
802 o.strategy = NOTES_MERGE_RESOLVE_THEIRS;
803 else if (!strcmp(strategy, "union"))
804 o.strategy = NOTES_MERGE_RESOLVE_UNION;
805 else {
806 error("Unknown -s/--strategy: %s", strategy);
807 usage_with_options(git_notes_merge_usage, options);
808 }
809 }
810
2085b16a 811 t = init_notes_check("merge");
75ef3f4a
JH
812
813 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
814 remote_ref.buf, default_notes_ref());
2085b16a
JH
815 o.commit_msg = msg.buf + 7; // skip "notes: " prefix
816
817 result = notes_merge(&o, t, result_sha1);
818
819 if (result >= 0) /* Merge resulted (trivially) in result_sha1 */
75ef3f4a
JH
820 /* Update default notes ref with new commit */
821 update_ref(msg.buf, default_notes_ref(), result_sha1, NULL,
822 0, DIE_ON_ERR);
809f38c8
JH
823 else /* Merge has unresolved conflicts */
824 printf("Automatic notes merge failed. Fix conflicts in %s.\n",
825 git_path(NOTES_MERGE_WORKTREE));
75ef3f4a 826
2085b16a 827 free_notes(t);
75ef3f4a
JH
828 strbuf_release(&remote_ref);
829 strbuf_release(&msg);
809f38c8 830 return result < 0; /* return non-zero on conflicts */
75ef3f4a
JH
831}
832
74884b52
SB
833static int remove_cmd(int argc, const char **argv, const char *prefix)
834{
835 struct option options[] = {
836 OPT_END()
837 };
838 const char *object_ref;
839 struct notes_tree *t;
840 unsigned char object[20];
841
842 argc = parse_options(argc, argv, prefix, options,
843 git_notes_remove_usage, 0);
844
845 if (1 < argc) {
846 error("too many parameters");
847 usage_with_options(git_notes_remove_usage, options);
848 }
849
850 object_ref = argc ? argv[0] : "HEAD";
851
852 if (get_sha1(object_ref, object))
853 die("Failed to resolve '%s' as a valid ref.", object_ref);
854
855 t = init_notes_check("remove");
856
857 fprintf(stderr, "Removing note for object %s\n", sha1_to_hex(object));
858 remove_note(t, object);
859
860 commit_notes(t, "Notes removed by 'git notes remove'");
861 free_notes(t);
862 return 0;
863}
864
865static int prune(int argc, const char **argv, const char *prefix)
866{
867 struct notes_tree *t;
a9f2adff 868 int show_only = 0, verbose = 0;
74884b52 869 struct option options[] = {
e93487d2
RS
870 OPT_BOOLEAN('n', "dry-run", &show_only,
871 "do not remove, show only"),
872 OPT_BOOLEAN('v', "verbose", &verbose, "report pruned notes"),
74884b52
SB
873 OPT_END()
874 };
875
876 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
877 0);
878
879 if (argc) {
880 error("too many parameters");
881 usage_with_options(git_notes_prune_usage, options);
882 }
883
884 t = init_notes_check("prune");
885
a9f2adff
MG
886 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
887 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
888 if (!show_only)
889 commit_notes(t, "Notes removed by 'git notes prune'");
74884b52
SB
890 free_notes(t);
891 return 0;
892}
893
894int cmd_notes(int argc, const char **argv, const char *prefix)
895{
896 int result;
897 const char *override_notes_ref = NULL;
898 struct option options[] = {
899 OPT_STRING(0, "ref", &override_notes_ref, "notes_ref",
900 "use notes from <notes_ref>"),
901 OPT_END()
902 };
903
904 git_config(git_default_config, NULL);
905 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
906 PARSE_OPT_STOP_AT_NON_OPTION);
907
908 if (override_notes_ref) {
909 struct strbuf sb = STRBUF_INIT;
74884b52 910 strbuf_addstr(&sb, override_notes_ref);
8ef313e1 911 expand_notes_ref(&sb);
74884b52
SB
912 setenv("GIT_NOTES_REF", sb.buf, 1);
913 strbuf_release(&sb);
914 }
915
916 if (argc < 1 || !strcmp(argv[0], "list"))
917 result = list(argc, argv, prefix);
918 else if (!strcmp(argv[0], "add"))
919 result = add(argc, argv, prefix);
920 else if (!strcmp(argv[0], "copy"))
921 result = copy(argc, argv, prefix);
922 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
923 result = append_edit(argc, argv, prefix);
924 else if (!strcmp(argv[0], "show"))
925 result = show(argc, argv, prefix);
75ef3f4a
JH
926 else if (!strcmp(argv[0], "merge"))
927 result = merge(argc, argv, prefix);
74884b52
SB
928 else if (!strcmp(argv[0], "remove"))
929 result = remove_cmd(argc, argv, prefix);
930 else if (!strcmp(argv[0], "prune"))
931 result = prune(argc, argv, prefix);
932 else {
933 result = error("Unknown subcommand: %s", argv[0]);
934 usage_with_options(git_notes_usage, options);
935 }
936
937 return result ? 1 : 0;
938}