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