rebase: support automatic notes copying
[gitweb.git] / builtin-notes.c
index 48bc455a1a10022e407ca46a74e3016110627ee9..026cfd32a823027b26811b007c98b2fcc9f2394a 100644 (file)
 #include "exec_cmd.h"
 #include "run-command.h"
 #include "parse-options.h"
+#include "string-list.h"
 
 static const char * const git_notes_usage[] = {
-       "git notes edit [-m <msg> | -F <file>] [<object>]",
+       "git notes [list [<object>]]",
+       "git notes add [-f] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
+       "git notes copy [-f] <from-object> <to-object>",
+       "git notes append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
+       "git notes edit [<object>]",
        "git notes show [<object>]",
        "git notes remove [<object>]",
        "git notes prune",
@@ -31,6 +36,20 @@ static const char note_template[] =
        "# Write/edit the notes for the following object:\n"
        "#\n";
 
+struct msg_arg {
+       int given;
+       int use_editor;
+       struct strbuf buf;
+};
+
+static int list_each_note(const unsigned char *object_sha1,
+               const unsigned char *note_sha1, char *note_path,
+               void *cb_data)
+{
+       printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
+       return 0;
+}
+
 static void write_note_data(int fd, const unsigned char *sha1)
 {
        unsigned long size;
@@ -82,15 +101,13 @@ static void write_commented_object(int fd, const unsigned char *object)
                    sha1_to_hex(object));
 }
 
-static void create_note(const unsigned char *object,
-                       struct strbuf *buf,
-                       int skip_editor,
-                       const unsigned char *prev,
+static void create_note(const unsigned char *object, struct msg_arg *msg,
+                       int append_only, const unsigned char *prev,
                        unsigned char *result)
 {
        char *path = NULL;
 
-       if (!skip_editor) {
+       if (msg->use_editor || !msg->given) {
                int fd;
 
                /* write the template message before editing: */
@@ -99,28 +116,44 @@ static void create_note(const unsigned char *object,
                if (fd < 0)
                        die_errno("could not create file '%s'", path);
 
-               if (prev)
+               if (msg->given)
+                       write_or_die(fd, msg->buf.buf, msg->buf.len);
+               else if (prev && !append_only)
                        write_note_data(fd, prev);
                write_or_die(fd, note_template, strlen(note_template));
 
                write_commented_object(fd, object);
 
                close(fd);
+               strbuf_reset(&(msg->buf));
 
-               if (launch_editor(path, buf, NULL)) {
+               if (launch_editor(path, &(msg->buf), NULL)) {
                        die("Please supply the note contents using either -m" \
                            " or -F option");
                }
+               stripspace(&(msg->buf), 1);
        }
 
-       stripspace(buf, 1);
+       if (prev && append_only) {
+               /* Append buf to previous note contents */
+               unsigned long size;
+               enum object_type type;
+               char *prev_buf = read_sha1_file(prev, &type, &size);
+
+               strbuf_grow(&(msg->buf), size + 1);
+               if (msg->buf.len && prev_buf && size)
+                       strbuf_insert(&(msg->buf), 0, "\n", 1);
+               if (prev_buf && size)
+                       strbuf_insert(&(msg->buf), 0, prev_buf, size);
+               free(prev_buf);
+       }
 
-       if (!buf->len) {
+       if (!msg->buf.len) {
                fprintf(stderr, "Removing note for object %s\n",
                        sha1_to_hex(object));
                hashclr(result);
        } else {
-               if (write_sha1_file(buf->buf, buf->len, blob_type, result)) {
+               if (write_sha1_file(msg->buf.buf, msg->buf.len, blob_type, result)) {
                        error("unable to write note object");
                        if (path)
                                error("The note contents has been left in %s",
@@ -135,24 +168,77 @@ static void create_note(const unsigned char *object,
        }
 }
 
-struct msg_arg {
-       int given;
-       struct strbuf buf;
-};
-
 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
 {
        struct msg_arg *msg = opt->value;
 
        if (!arg)
                return -1;
+
+       strbuf_grow(&(msg->buf), strlen(arg) + 2);
        if (msg->buf.len)
-               strbuf_addstr(&(msg->buf), "\n\n");
+               strbuf_addstr(&(msg->buf), "\n");
        strbuf_addstr(&(msg->buf), arg);
+       stripspace(&(msg->buf), 0);
+
+       msg->given = 1;
+       return 0;
+}
+
+static int parse_file_arg(const struct option *opt, const char *arg, int unset)
+{
+       struct msg_arg *msg = opt->value;
+
+       if (!arg)
+               return -1;
+
+       if (msg->buf.len)
+               strbuf_addstr(&(msg->buf), "\n");
+       if (!strcmp(arg, "-")) {
+               if (strbuf_read(&(msg->buf), 0, 1024) < 0)
+                       die_errno("cannot read '%s'", arg);
+       } else if (strbuf_read_file(&(msg->buf), arg, 1024) < 0)
+               die_errno("could not open or read '%s'", arg);
+       stripspace(&(msg->buf), 0);
+
+       msg->given = 1;
+       return 0;
+}
+
+static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
+{
+       struct msg_arg *msg = opt->value;
+       char *buf;
+       unsigned char object[20];
+       enum object_type type;
+       unsigned long len;
+
+       if (!arg)
+               return -1;
+
+       if (msg->buf.len)
+               strbuf_addstr(&(msg->buf), "\n");
+
+       if (get_sha1(arg, object))
+               die("Failed to resolve '%s' as a valid ref.", arg);
+       if (!(buf = read_sha1_file(object, &type, &len)) || !len) {
+               free(buf);
+               die("Failed to read object '%s'.", arg);;
+       }
+       strbuf_add(&(msg->buf), buf, len);
+       free(buf);
+
        msg->given = 1;
        return 0;
 }
 
+static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
+{
+       struct msg_arg *msg = opt->value;
+       msg->use_editor = 1;
+       return parse_reuse_arg(opt, arg, unset);
+}
+
 int commit_notes(struct notes_tree *t, const char *msg)
 {
        struct commit_list *parent;
@@ -193,23 +279,188 @@ int commit_notes(struct notes_tree *t, const char *msg)
        return 0;
 }
 
-int cmd_notes(int argc, const char **argv, const char *prefix)
+
+combine_notes_fn *parse_combine_notes_fn(const char *v)
+{
+       if (!strcasecmp(v, "overwrite"))
+               return combine_notes_overwrite;
+       else if (!strcasecmp(v, "ignore"))
+               return combine_notes_ignore;
+       else if (!strcasecmp(v, "concatenate"))
+               return combine_notes_concatenate;
+       else
+               return NULL;
+}
+
+static int notes_rewrite_config(const char *k, const char *v, void *cb)
+{
+       struct notes_rewrite_cfg *c = cb;
+       if (!prefixcmp(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
+               c->enabled = git_config_bool(k, v);
+               return 0;
+       } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
+               if (!v)
+                       config_error_nonbool(k);
+               c->combine = parse_combine_notes_fn(v);
+               if (!c->combine) {
+                       error("Bad notes.rewriteMode value: '%s'", v);
+                       return 1;
+               }
+               return 0;
+       } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
+               /* note that a refs/ prefix is implied in the
+                * underlying for_each_glob_ref */
+               if (!prefixcmp(v, "refs/notes/"))
+                       string_list_add_refs_by_glob(c->refs, v);
+               else
+                       warning("Refusing to rewrite notes in %s"
+                               " (outside of refs/notes/)", v);
+               return 0;
+       }
+
+       return 0;
+}
+
+
+struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
+{
+       struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
+       const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
+       const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
+       c->cmd = cmd;
+       c->enabled = 1;
+       c->combine = combine_notes_concatenate;
+       c->refs = xcalloc(1, sizeof(struct string_list));
+       c->refs->strdup_strings = 1;
+       c->refs_from_env = 0;
+       c->mode_from_env = 0;
+       if (rewrite_mode_env) {
+               c->mode_from_env = 1;
+               c->combine = parse_combine_notes_fn(rewrite_mode_env);
+               if (!c->combine)
+                       error("Bad " GIT_NOTES_REWRITE_MODE_ENVIRONMENT
+                             " value: '%s'", rewrite_mode_env);
+       }
+       if (rewrite_refs_env) {
+               c->refs_from_env = 1;
+               string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
+       }
+       git_config(notes_rewrite_config, c);
+       if (!c->enabled || !c->refs->nr) {
+               string_list_clear(c->refs, 0);
+               free(c->refs);
+               free(c);
+               return NULL;
+       }
+       c->trees = load_notes_trees(c->refs);
+       string_list_clear(c->refs, 0);
+       free(c->refs);
+       return c;
+}
+
+int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
+                         const unsigned char *from_obj, const unsigned char *to_obj)
+{
+       int ret = 0;
+       int i;
+       for (i = 0; c->trees[i]; i++)
+               ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
+       return ret;
+}
+
+void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c)
+{
+       int i;
+       for (i = 0; c->trees[i]; i++) {
+               commit_notes(c->trees[i], "Notes added by 'git notes copy'");
+               free_notes(c->trees[i]);
+       }
+       free(c->trees);
+       free(c);
+}
+
+int notes_copy_from_stdin(int force, const char *rewrite_cmd)
 {
        struct strbuf buf = STRBUF_INIT;
+       struct notes_rewrite_cfg *c = NULL;
        struct notes_tree *t;
-       unsigned char object[20], new_note[20];
+       int ret = 0;
+
+       if (rewrite_cmd) {
+               c = init_copy_notes_for_rewrite(rewrite_cmd);
+               if (!c)
+                       return 0;
+       } else {
+               init_notes(NULL, NULL, NULL, 0);
+               t = &default_notes_tree;
+       }
+
+       while (strbuf_getline(&buf, stdin, '\n') != EOF) {
+               unsigned char from_obj[20], to_obj[20];
+               struct strbuf **split;
+               int err;
+
+               split = strbuf_split(&buf, ' ');
+               if (!split[0] || !split[1])
+                       die("Malformed input line: '%s'.", buf.buf);
+               strbuf_rtrim(split[0]);
+               strbuf_rtrim(split[1]);
+               if (get_sha1(split[0]->buf, from_obj))
+                       die("Failed to resolve '%s' as a valid ref.", split[0]->buf);
+               if (get_sha1(split[1]->buf, to_obj))
+                       die("Failed to resolve '%s' as a valid ref.", split[1]->buf);
+
+               if (rewrite_cmd)
+                       err = copy_note_for_rewrite(c, from_obj, to_obj);
+               else
+                       err = copy_note(t, from_obj, to_obj, force,
+                                       combine_notes_overwrite);
+
+               if (err) {
+                       error("Failed to copy notes from '%s' to '%s'",
+                             split[0]->buf, split[1]->buf);
+                       ret = 1;
+               }
+
+               strbuf_list_free(split);
+       }
+
+       if (!rewrite_cmd) {
+               commit_notes(t, "Notes added by 'git notes copy'");
+               free_notes(t);
+       } else {
+               finish_copy_notes_for_rewrite(c);
+       }
+       return ret;
+}
+
+int cmd_notes(int argc, const char **argv, const char *prefix)
+{
+       struct notes_tree *t;
+       unsigned char object[20], from_obj[20], new_note[20];
        const unsigned char *note;
        const char *object_ref;
        char logmsg[100];
 
-       int edit = 0, show = 0, remove = 0, prune = 0;
-       const char *msgfile = NULL;
-       struct msg_arg msg = { 0, STRBUF_INIT };
+       int list = 0, add = 0, copy = 0, append = 0, edit = 0, show = 0,
+           remove = 0, prune = 0, force = 0, from_stdin = 0;
+       int given_object = 0, i = 1, retval = 0;
+       struct msg_arg msg = { 0, 0, STRBUF_INIT };
+       const char *rewrite_cmd = NULL;
        struct option options[] = {
-               OPT_GROUP("Notes edit options"),
-               OPT_CALLBACK('m', NULL, &msg, "msg",
+               OPT_GROUP("Notes options"),
+               OPT_CALLBACK('m', "message", &msg, "MSG",
                             "note contents as a string", parse_msg_arg),
-               OPT_FILENAME('F', NULL, &msgfile, "note contents in a file"),
+               OPT_CALLBACK('F', "file", &msg, "FILE",
+                            "note contents in a file", parse_file_arg),
+               OPT_CALLBACK('c', "reedit-message", &msg, "OBJECT",
+                          "reuse and edit specified note object", parse_reedit_arg),
+               OPT_CALLBACK('C', "reuse-message", &msg, "OBJECT",
+                          "reuse specified note object", parse_reuse_arg),
+               OPT_BOOLEAN('f', "force", &force, "replace existing notes"),
+               OPT_BOOLEAN(0, "stdin", &from_stdin, "read objects from stdin"),
+               OPT_STRING(0, "for-rewrite", &rewrite_cmd, "command",
+                          "load rewriting config for <command> (implies --stdin)"),
                OPT_END()
        };
 
@@ -217,7 +468,15 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
 
        argc = parse_options(argc, argv, prefix, options, git_notes_usage, 0);
 
-       if (argc && !strcmp(argv[0], "edit"))
+       if (argc && !strcmp(argv[0], "list"))
+               list = 1;
+       else if (argc && !strcmp(argv[0], "add"))
+               add = 1;
+       else if (argc && !strcmp(argv[0], "copy"))
+               copy = 1;
+       else if (argc && !strcmp(argv[0], "append"))
+               append = 1;
+       else if (argc && !strcmp(argv[0], "edit"))
                edit = 1;
        else if (argc && !strcmp(argv[0], "show"))
                show = 1;
@@ -225,22 +484,63 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
                remove = 1;
        else if (argc && !strcmp(argv[0], "prune"))
                prune = 1;
+       else if (!argc) {
+               list = 1; /* Default to 'list' if no other subcommand given */
+               i = 0;
+       }
 
-       if (edit + show + remove + prune != 1)
+       if (list + add + copy + append + edit + show + remove + prune != 1)
                usage_with_options(git_notes_usage, options);
 
-       if ((msg.given || msgfile) && !edit) {
-               error("cannot use -m/-F options with %s subcommand.", argv[0]);
+       if (msg.given && !(add || append || edit)) {
+               error("cannot use -m/-F/-c/-C options with %s subcommand.",
+                     argv[0]);
                usage_with_options(git_notes_usage, options);
        }
 
-       if (msg.given && msgfile) {
-               error("mixing -m and -F options is not allowed.");
+       if (msg.given && edit) {
+               fprintf(stderr, "The -m/-F/-c/-C options have been deprecated "
+                       "for the 'edit' subcommand.\n"
+                       "Please use 'git notes add -f -m/-F/-c/-C' instead.\n");
+       }
+
+       if (force && !(add || copy)) {
+               error("cannot use -f option with %s subcommand.", argv[0]);
                usage_with_options(git_notes_usage, options);
        }
 
-       object_ref = argc == 2 ? argv[1] : "HEAD";
-       if (argc > 2 || (prune && argc > 1)) {
+       if (!copy && rewrite_cmd) {
+               error("cannot use --for-rewrite with %s subcommand.", argv[0]);
+               usage_with_options(git_notes_usage, options);
+       }
+       if (!copy && from_stdin) {
+               error("cannot use --stdin with %s subcommand.", argv[0]);
+               usage_with_options(git_notes_usage, options);
+       }
+
+       if (copy) {
+               const char *from_ref;
+               if (from_stdin || rewrite_cmd) {
+                       if (argc > 1) {
+                               error("too many parameters");
+                               usage_with_options(git_notes_usage, options);
+                       } else {
+                               return notes_copy_from_stdin(force, rewrite_cmd);
+                       }
+               }
+               if (argc < 3) {
+                       error("too few parameters");
+                       usage_with_options(git_notes_usage, options);
+               }
+               from_ref = argv[i++];
+               if (get_sha1(from_ref, from_obj))
+                       die("Failed to resolve '%s' as a valid ref.", from_ref);
+       }
+
+       given_object = argc > i;
+       object_ref = given_object ? argv[i++] : "HEAD";
+
+       if (argc > i || (prune && given_object)) {
                error("too many parameters");
                usage_with_options(git_notes_usage, options);
        }
@@ -257,46 +557,80 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
 
        note = get_note(t, object);
 
+       /* list command */
+
+       if (list) {
+               if (given_object) {
+                       if (note) {
+                               puts(sha1_to_hex(note));
+                               goto end;
+                       }
+               } else {
+                       retval = for_each_note(t, 0, list_each_note, NULL);
+                       goto end;
+               }
+       }
+
        /* show command */
 
-       if (show && !note) {
+       if ((list || show) && !note) {
                error("No note found for object %s.", sha1_to_hex(object));
-               return 1;
+               retval = 1;
+               goto end;
        } else if (show) {
                const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
-               return execv_git_cmd(show_args);
+               retval = execv_git_cmd(show_args);
+               goto end;
+       }
+
+       /* add/append/edit/remove/prune command */
+
+       if ((add || copy) && note) {
+               if (!force) {
+                       error("Cannot %s notes. Found existing notes for object"
+                             " %s. Use '-f' to overwrite existing notes",
+                             argv[0], sha1_to_hex(object));
+                       retval = 1;
+                       goto end;
+               }
+               fprintf(stderr, "Overwriting existing notes for object %s\n",
+                       sha1_to_hex(object));
        }
 
-       /* edit/remove/prune command */
-
-       if (remove)
-               strbuf_reset(&buf);
-       else if (msg.given)
-               strbuf_addbuf(&buf, &(msg.buf));
-       else if (msgfile) {
-               if (!strcmp(msgfile, "-")) {
-                       if (strbuf_read(&buf, 0, 1024) < 0)
-                               die_errno("cannot read '%s'", msgfile);
-               } else if (strbuf_read_file(&buf, msgfile, 1024) < 0)
-                       die_errno("could not open or read '%s'", msgfile);
+       if (remove) {
+               msg.given = 1;
+               msg.use_editor = 0;
+               strbuf_reset(&(msg.buf));
        }
 
        if (prune) {
                hashclr(new_note);
                prune_notes(t);
-       } else {
-               create_note(object, &buf, msg.given || msgfile || remove, note,
-                           new_note);
-               if (is_null_sha1(new_note))
-                       remove_note(t, object);
-               else
-                       add_note(t, object, new_note, combine_notes_overwrite);
-       }
-       snprintf(logmsg, sizeof(logmsg), "Note %s by 'git notes %s'",
+               goto commit;
+       } else if (copy) {
+               const unsigned char *from_note = get_note(t, from_obj);
+               if (!from_note) {
+                       error("Missing notes on source object %s. Cannot copy.",
+                             sha1_to_hex(from_obj));
+                       retval = 1;
+                       goto end;
+               }
+               hashcpy(new_note, from_note);
+       } else
+               create_note(object, &msg, append, note, new_note);
+
+       if (is_null_sha1(new_note))
+               remove_note(t, object);
+       else
+               add_note(t, object, new_note, combine_notes_overwrite);
+
+commit:
+       snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
                 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
        commit_notes(t, logmsg);
 
+end:
        free_notes(t);
-       strbuf_release(&buf);
-       return 0;
+       strbuf_release(&(msg.buf));
+       return retval;
 }