rebase: support automatic notes copying
[git/jnareb-git.git] / builtin-notes.c
blob026cfd32a823027b26811b007c98b2fcc9f2394a
1 /*
2 * Builtin "git notes"
4 * Copyright (c) 2010 Johan Herland <johan@herland.net>
6 * Based on git-notes.sh by Johannes Schindelin,
7 * and builtin-tag.c by Kristian Høgsberg and Carlos Rica.
8 */
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"
19 #include "string-list.h"
21 static const char * const git_notes_usage[] = {
22 "git notes [list [<object>]]",
23 "git notes add [-f] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
24 "git notes copy [-f] <from-object> <to-object>",
25 "git notes append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
26 "git notes edit [<object>]",
27 "git notes show [<object>]",
28 "git notes remove [<object>]",
29 "git notes prune",
30 NULL
33 static const char note_template[] =
34 "\n"
35 "#\n"
36 "# Write/edit the notes for the following object:\n"
37 "#\n";
39 struct msg_arg {
40 int given;
41 int use_editor;
42 struct strbuf buf;
45 static int list_each_note(const unsigned char *object_sha1,
46 const unsigned char *note_sha1, char *note_path,
47 void *cb_data)
49 printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
50 return 0;
53 static void write_note_data(int fd, const unsigned char *sha1)
55 unsigned long size;
56 enum object_type type;
57 char *buf = read_sha1_file(sha1, &type, &size);
58 if (buf) {
59 if (size)
60 write_or_die(fd, buf, size);
61 free(buf);
65 static void write_commented_object(int fd, const unsigned char *object)
67 const char *show_args[5] =
68 {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
69 struct child_process show;
70 struct strbuf buf = STRBUF_INIT;
71 FILE *show_out;
73 /* Invoke "git show --stat --no-notes $object" */
74 memset(&show, 0, sizeof(show));
75 show.argv = show_args;
76 show.no_stdin = 1;
77 show.out = -1;
78 show.err = 0;
79 show.git_cmd = 1;
80 if (start_command(&show))
81 die("unable to start 'show' for object '%s'",
82 sha1_to_hex(object));
84 /* Open the output as FILE* so strbuf_getline() can be used. */
85 show_out = xfdopen(show.out, "r");
86 if (show_out == NULL)
87 die_errno("can't fdopen 'show' output fd");
89 /* Prepend "# " to each output line and write result to 'fd' */
90 while (strbuf_getline(&buf, show_out, '\n') != EOF) {
91 write_or_die(fd, "# ", 2);
92 write_or_die(fd, buf.buf, buf.len);
93 write_or_die(fd, "\n", 1);
95 strbuf_release(&buf);
96 if (fclose(show_out))
97 die_errno("failed to close pipe to 'show' for object '%s'",
98 sha1_to_hex(object));
99 if (finish_command(&show))
100 die("failed to finish 'show' for object '%s'",
101 sha1_to_hex(object));
104 static void create_note(const unsigned char *object, struct msg_arg *msg,
105 int append_only, const unsigned char *prev,
106 unsigned char *result)
108 char *path = NULL;
110 if (msg->use_editor || !msg->given) {
111 int fd;
113 /* write the template message before editing: */
114 path = git_pathdup("NOTES_EDITMSG");
115 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
116 if (fd < 0)
117 die_errno("could not create file '%s'", path);
119 if (msg->given)
120 write_or_die(fd, msg->buf.buf, msg->buf.len);
121 else if (prev && !append_only)
122 write_note_data(fd, prev);
123 write_or_die(fd, note_template, strlen(note_template));
125 write_commented_object(fd, object);
127 close(fd);
128 strbuf_reset(&(msg->buf));
130 if (launch_editor(path, &(msg->buf), NULL)) {
131 die("Please supply the note contents using either -m" \
132 " or -F option");
134 stripspace(&(msg->buf), 1);
137 if (prev && append_only) {
138 /* Append buf to previous note contents */
139 unsigned long size;
140 enum object_type type;
141 char *prev_buf = read_sha1_file(prev, &type, &size);
143 strbuf_grow(&(msg->buf), size + 1);
144 if (msg->buf.len && prev_buf && size)
145 strbuf_insert(&(msg->buf), 0, "\n", 1);
146 if (prev_buf && size)
147 strbuf_insert(&(msg->buf), 0, prev_buf, size);
148 free(prev_buf);
151 if (!msg->buf.len) {
152 fprintf(stderr, "Removing note for object %s\n",
153 sha1_to_hex(object));
154 hashclr(result);
155 } else {
156 if (write_sha1_file(msg->buf.buf, msg->buf.len, blob_type, result)) {
157 error("unable to write note object");
158 if (path)
159 error("The note contents has been left in %s",
160 path);
161 exit(128);
165 if (path) {
166 unlink_or_warn(path);
167 free(path);
171 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
173 struct msg_arg *msg = opt->value;
175 if (!arg)
176 return -1;
178 strbuf_grow(&(msg->buf), strlen(arg) + 2);
179 if (msg->buf.len)
180 strbuf_addstr(&(msg->buf), "\n");
181 strbuf_addstr(&(msg->buf), arg);
182 stripspace(&(msg->buf), 0);
184 msg->given = 1;
185 return 0;
188 static int parse_file_arg(const struct option *opt, const char *arg, int unset)
190 struct msg_arg *msg = opt->value;
192 if (!arg)
193 return -1;
195 if (msg->buf.len)
196 strbuf_addstr(&(msg->buf), "\n");
197 if (!strcmp(arg, "-")) {
198 if (strbuf_read(&(msg->buf), 0, 1024) < 0)
199 die_errno("cannot read '%s'", arg);
200 } else if (strbuf_read_file(&(msg->buf), arg, 1024) < 0)
201 die_errno("could not open or read '%s'", arg);
202 stripspace(&(msg->buf), 0);
204 msg->given = 1;
205 return 0;
208 static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
210 struct msg_arg *msg = opt->value;
211 char *buf;
212 unsigned char object[20];
213 enum object_type type;
214 unsigned long len;
216 if (!arg)
217 return -1;
219 if (msg->buf.len)
220 strbuf_addstr(&(msg->buf), "\n");
222 if (get_sha1(arg, object))
223 die("Failed to resolve '%s' as a valid ref.", arg);
224 if (!(buf = read_sha1_file(object, &type, &len)) || !len) {
225 free(buf);
226 die("Failed to read object '%s'.", arg);;
228 strbuf_add(&(msg->buf), buf, len);
229 free(buf);
231 msg->given = 1;
232 return 0;
235 static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
237 struct msg_arg *msg = opt->value;
238 msg->use_editor = 1;
239 return parse_reuse_arg(opt, arg, unset);
242 int commit_notes(struct notes_tree *t, const char *msg)
244 struct commit_list *parent;
245 unsigned char tree_sha1[20], prev_commit[20], new_commit[20];
246 struct strbuf buf = STRBUF_INIT;
248 if (!t)
249 t = &default_notes_tree;
250 if (!t->initialized || !t->ref || !*t->ref)
251 die("Cannot commit uninitialized/unreferenced notes tree");
253 /* Prepare commit message and reflog message */
254 strbuf_addstr(&buf, "notes: "); /* commit message starts at index 7 */
255 strbuf_addstr(&buf, msg);
256 if (buf.buf[buf.len - 1] != '\n')
257 strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
259 /* Convert notes tree to tree object */
260 if (write_notes_tree(t, tree_sha1))
261 die("Failed to write current notes tree to database");
263 /* Create new commit for the tree object */
264 if (!read_ref(t->ref, prev_commit)) { /* retrieve parent commit */
265 parent = xmalloc(sizeof(*parent));
266 parent->item = lookup_commit(prev_commit);
267 parent->next = NULL;
268 } else {
269 hashclr(prev_commit);
270 parent = NULL;
272 if (commit_tree(buf.buf + 7, tree_sha1, parent, new_commit, NULL))
273 die("Failed to commit notes tree to database");
275 /* Update notes ref with new commit */
276 update_ref(buf.buf, t->ref, new_commit, prev_commit, 0, DIE_ON_ERR);
278 strbuf_release(&buf);
279 return 0;
283 combine_notes_fn *parse_combine_notes_fn(const char *v)
285 if (!strcasecmp(v, "overwrite"))
286 return combine_notes_overwrite;
287 else if (!strcasecmp(v, "ignore"))
288 return combine_notes_ignore;
289 else if (!strcasecmp(v, "concatenate"))
290 return combine_notes_concatenate;
291 else
292 return NULL;
295 static int notes_rewrite_config(const char *k, const char *v, void *cb)
297 struct notes_rewrite_cfg *c = cb;
298 if (!prefixcmp(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
299 c->enabled = git_config_bool(k, v);
300 return 0;
301 } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
302 if (!v)
303 config_error_nonbool(k);
304 c->combine = parse_combine_notes_fn(v);
305 if (!c->combine) {
306 error("Bad notes.rewriteMode value: '%s'", v);
307 return 1;
309 return 0;
310 } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
311 /* note that a refs/ prefix is implied in the
312 * underlying for_each_glob_ref */
313 if (!prefixcmp(v, "refs/notes/"))
314 string_list_add_refs_by_glob(c->refs, v);
315 else
316 warning("Refusing to rewrite notes in %s"
317 " (outside of refs/notes/)", v);
318 return 0;
321 return 0;
325 struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
327 struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
328 const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
329 const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
330 c->cmd = cmd;
331 c->enabled = 1;
332 c->combine = combine_notes_concatenate;
333 c->refs = xcalloc(1, sizeof(struct string_list));
334 c->refs->strdup_strings = 1;
335 c->refs_from_env = 0;
336 c->mode_from_env = 0;
337 if (rewrite_mode_env) {
338 c->mode_from_env = 1;
339 c->combine = parse_combine_notes_fn(rewrite_mode_env);
340 if (!c->combine)
341 error("Bad " GIT_NOTES_REWRITE_MODE_ENVIRONMENT
342 " value: '%s'", rewrite_mode_env);
344 if (rewrite_refs_env) {
345 c->refs_from_env = 1;
346 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
348 git_config(notes_rewrite_config, c);
349 if (!c->enabled || !c->refs->nr) {
350 string_list_clear(c->refs, 0);
351 free(c->refs);
352 free(c);
353 return NULL;
355 c->trees = load_notes_trees(c->refs);
356 string_list_clear(c->refs, 0);
357 free(c->refs);
358 return c;
361 int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
362 const unsigned char *from_obj, const unsigned char *to_obj)
364 int ret = 0;
365 int i;
366 for (i = 0; c->trees[i]; i++)
367 ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
368 return ret;
371 void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c)
373 int i;
374 for (i = 0; c->trees[i]; i++) {
375 commit_notes(c->trees[i], "Notes added by 'git notes copy'");
376 free_notes(c->trees[i]);
378 free(c->trees);
379 free(c);
382 int notes_copy_from_stdin(int force, const char *rewrite_cmd)
384 struct strbuf buf = STRBUF_INIT;
385 struct notes_rewrite_cfg *c = NULL;
386 struct notes_tree *t;
387 int ret = 0;
389 if (rewrite_cmd) {
390 c = init_copy_notes_for_rewrite(rewrite_cmd);
391 if (!c)
392 return 0;
393 } else {
394 init_notes(NULL, NULL, NULL, 0);
395 t = &default_notes_tree;
398 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
399 unsigned char from_obj[20], to_obj[20];
400 struct strbuf **split;
401 int err;
403 split = strbuf_split(&buf, ' ');
404 if (!split[0] || !split[1])
405 die("Malformed input line: '%s'.", buf.buf);
406 strbuf_rtrim(split[0]);
407 strbuf_rtrim(split[1]);
408 if (get_sha1(split[0]->buf, from_obj))
409 die("Failed to resolve '%s' as a valid ref.", split[0]->buf);
410 if (get_sha1(split[1]->buf, to_obj))
411 die("Failed to resolve '%s' as a valid ref.", split[1]->buf);
413 if (rewrite_cmd)
414 err = copy_note_for_rewrite(c, from_obj, to_obj);
415 else
416 err = copy_note(t, from_obj, to_obj, force,
417 combine_notes_overwrite);
419 if (err) {
420 error("Failed to copy notes from '%s' to '%s'",
421 split[0]->buf, split[1]->buf);
422 ret = 1;
425 strbuf_list_free(split);
428 if (!rewrite_cmd) {
429 commit_notes(t, "Notes added by 'git notes copy'");
430 free_notes(t);
431 } else {
432 finish_copy_notes_for_rewrite(c);
434 return ret;
437 int cmd_notes(int argc, const char **argv, const char *prefix)
439 struct notes_tree *t;
440 unsigned char object[20], from_obj[20], new_note[20];
441 const unsigned char *note;
442 const char *object_ref;
443 char logmsg[100];
445 int list = 0, add = 0, copy = 0, append = 0, edit = 0, show = 0,
446 remove = 0, prune = 0, force = 0, from_stdin = 0;
447 int given_object = 0, i = 1, retval = 0;
448 struct msg_arg msg = { 0, 0, STRBUF_INIT };
449 const char *rewrite_cmd = NULL;
450 struct option options[] = {
451 OPT_GROUP("Notes options"),
452 OPT_CALLBACK('m', "message", &msg, "MSG",
453 "note contents as a string", parse_msg_arg),
454 OPT_CALLBACK('F', "file", &msg, "FILE",
455 "note contents in a file", parse_file_arg),
456 OPT_CALLBACK('c', "reedit-message", &msg, "OBJECT",
457 "reuse and edit specified note object", parse_reedit_arg),
458 OPT_CALLBACK('C', "reuse-message", &msg, "OBJECT",
459 "reuse specified note object", parse_reuse_arg),
460 OPT_BOOLEAN('f', "force", &force, "replace existing notes"),
461 OPT_BOOLEAN(0, "stdin", &from_stdin, "read objects from stdin"),
462 OPT_STRING(0, "for-rewrite", &rewrite_cmd, "command",
463 "load rewriting config for <command> (implies --stdin)"),
464 OPT_END()
467 git_config(git_default_config, NULL);
469 argc = parse_options(argc, argv, prefix, options, git_notes_usage, 0);
471 if (argc && !strcmp(argv[0], "list"))
472 list = 1;
473 else if (argc && !strcmp(argv[0], "add"))
474 add = 1;
475 else if (argc && !strcmp(argv[0], "copy"))
476 copy = 1;
477 else if (argc && !strcmp(argv[0], "append"))
478 append = 1;
479 else if (argc && !strcmp(argv[0], "edit"))
480 edit = 1;
481 else if (argc && !strcmp(argv[0], "show"))
482 show = 1;
483 else if (argc && !strcmp(argv[0], "remove"))
484 remove = 1;
485 else if (argc && !strcmp(argv[0], "prune"))
486 prune = 1;
487 else if (!argc) {
488 list = 1; /* Default to 'list' if no other subcommand given */
489 i = 0;
492 if (list + add + copy + append + edit + show + remove + prune != 1)
493 usage_with_options(git_notes_usage, options);
495 if (msg.given && !(add || append || edit)) {
496 error("cannot use -m/-F/-c/-C options with %s subcommand.",
497 argv[0]);
498 usage_with_options(git_notes_usage, options);
501 if (msg.given && edit) {
502 fprintf(stderr, "The -m/-F/-c/-C options have been deprecated "
503 "for the 'edit' subcommand.\n"
504 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n");
507 if (force && !(add || copy)) {
508 error("cannot use -f option with %s subcommand.", argv[0]);
509 usage_with_options(git_notes_usage, options);
512 if (!copy && rewrite_cmd) {
513 error("cannot use --for-rewrite with %s subcommand.", argv[0]);
514 usage_with_options(git_notes_usage, options);
516 if (!copy && from_stdin) {
517 error("cannot use --stdin with %s subcommand.", argv[0]);
518 usage_with_options(git_notes_usage, options);
521 if (copy) {
522 const char *from_ref;
523 if (from_stdin || rewrite_cmd) {
524 if (argc > 1) {
525 error("too many parameters");
526 usage_with_options(git_notes_usage, options);
527 } else {
528 return notes_copy_from_stdin(force, rewrite_cmd);
531 if (argc < 3) {
532 error("too few parameters");
533 usage_with_options(git_notes_usage, options);
535 from_ref = argv[i++];
536 if (get_sha1(from_ref, from_obj))
537 die("Failed to resolve '%s' as a valid ref.", from_ref);
540 given_object = argc > i;
541 object_ref = given_object ? argv[i++] : "HEAD";
543 if (argc > i || (prune && given_object)) {
544 error("too many parameters");
545 usage_with_options(git_notes_usage, options);
548 if (get_sha1(object_ref, object))
549 die("Failed to resolve '%s' as a valid ref.", object_ref);
551 init_notes(NULL, NULL, NULL, 0);
552 t = &default_notes_tree;
554 if (prefixcmp(t->ref, "refs/notes/"))
555 die("Refusing to %s notes in %s (outside of refs/notes/)",
556 argv[0], t->ref);
558 note = get_note(t, object);
560 /* list command */
562 if (list) {
563 if (given_object) {
564 if (note) {
565 puts(sha1_to_hex(note));
566 goto end;
568 } else {
569 retval = for_each_note(t, 0, list_each_note, NULL);
570 goto end;
574 /* show command */
576 if ((list || show) && !note) {
577 error("No note found for object %s.", sha1_to_hex(object));
578 retval = 1;
579 goto end;
580 } else if (show) {
581 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
582 retval = execv_git_cmd(show_args);
583 goto end;
586 /* add/append/edit/remove/prune command */
588 if ((add || copy) && note) {
589 if (!force) {
590 error("Cannot %s notes. Found existing notes for object"
591 " %s. Use '-f' to overwrite existing notes",
592 argv[0], sha1_to_hex(object));
593 retval = 1;
594 goto end;
596 fprintf(stderr, "Overwriting existing notes for object %s\n",
597 sha1_to_hex(object));
600 if (remove) {
601 msg.given = 1;
602 msg.use_editor = 0;
603 strbuf_reset(&(msg.buf));
606 if (prune) {
607 hashclr(new_note);
608 prune_notes(t);
609 goto commit;
610 } else if (copy) {
611 const unsigned char *from_note = get_note(t, from_obj);
612 if (!from_note) {
613 error("Missing notes on source object %s. Cannot copy.",
614 sha1_to_hex(from_obj));
615 retval = 1;
616 goto end;
618 hashcpy(new_note, from_note);
619 } else
620 create_note(object, &msg, append, note, new_note);
622 if (is_null_sha1(new_note))
623 remove_note(t, object);
624 else
625 add_note(t, object, new_note, combine_notes_overwrite);
627 commit:
628 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
629 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
630 commit_notes(t, logmsg);
632 end:
633 free_notes(t);
634 strbuf_release(&(msg.buf));
635 return retval;