sequencer (rebase -i): show only failed cherry-picks' output
[gitweb.git] / sequencer.c
index a2002f1c12e44189397dd0527cf7cb9f2703a5dc..27dc91cc95601b7a6ddaf498ce4182cc7371778b 100644 (file)
@@ -95,6 +95,15 @@ static GIT_PATH_FUNC(rebase_path_amend, "rebase-merge/amend")
  * the abbreviated commit name of the corresponding patch.
  */
 static GIT_PATH_FUNC(rebase_path_stopped_sha, "rebase-merge/stopped-sha")
+/*
+ * For the post-rewrite hook, we make a list of rewritten commits and
+ * their new sha1s.  The rewritten-pending list keeps the sha1s of
+ * commits that have been processed, but not committed yet,
+ * e.g. because they are waiting for a 'squash' command.
+ */
+static GIT_PATH_FUNC(rebase_path_rewritten_list, "rebase-merge/rewritten-list")
+static GIT_PATH_FUNC(rebase_path_rewritten_pending,
+       "rebase-merge/rewritten-pending")
 /*
  * The following files are written by git-rebase just after parsing the
  * command-line (and are only consumed, not modified, by the sequencer).
@@ -104,6 +113,9 @@ static GIT_PATH_FUNC(rebase_path_orig_head, "rebase-merge/orig-head")
 static GIT_PATH_FUNC(rebase_path_verbose, "rebase-merge/verbose")
 static GIT_PATH_FUNC(rebase_path_head_name, "rebase-merge/head-name")
 static GIT_PATH_FUNC(rebase_path_onto, "rebase-merge/onto")
+static GIT_PATH_FUNC(rebase_path_autostash, "rebase-merge/autostash")
+static GIT_PATH_FUNC(rebase_path_strategy, "rebase-merge/strategy")
+static GIT_PATH_FUNC(rebase_path_strategy_opts, "rebase-merge/strategy_opts")
 
 static inline int is_rebase_i(const struct replay_opts *opts)
 {
@@ -421,6 +433,8 @@ static int do_recursive_merge(struct commit *base, struct commit *next,
        o.ancestor = base ? base_label : "(empty tree)";
        o.branch1 = "HEAD";
        o.branch2 = next ? next_label : "(empty tree)";
+       if (is_rebase_i(opts))
+               o.buffer_output = 2;
 
        head_tree = parse_tree_indirect(head);
        next_tree = next ? next->tree : empty_tree();
@@ -432,6 +446,8 @@ static int do_recursive_merge(struct commit *base, struct commit *next,
        clean = merge_trees(&o,
                            head_tree,
                            next_tree, base_tree, &result);
+       if (is_rebase_i(opts) && clean <= 0)
+               fputs(o.obuf.buf, stdout);
        strbuf_release(&o.obuf);
        if (clean < 0)
                return clean;
@@ -532,18 +548,17 @@ static int write_author_script(const char *message)
 }
 
 /*
- * Read the author-script file into an environment block, ready for use in
- * run_command(), that can be free()d afterwards.
+ * Read a list of environment variable assignments (such as the author-script
+ * file) into an environment block. Returns -1 on error, 0 otherwise.
  */
-static char **read_author_script(void)
+static int read_env_script(struct argv_array *env)
 {
        struct strbuf script = STRBUF_INIT;
        int i, count = 0;
-       char *p, *p2, **env;
-       size_t env_size;
+       char *p, *p2;
 
        if (strbuf_read_file(&script, rebase_path_author_script(), 256) <= 0)
-               return NULL;
+               return -1;
 
        for (p = script.buf; *p; p++)
                if (skip_prefix(p, "'\\\\''", (const char **)&p2))
@@ -555,19 +570,12 @@ static char **read_author_script(void)
                        count++;
                }
 
-       env_size = (count + 1) * sizeof(*env);
-       strbuf_grow(&script, env_size);
-       memmove(script.buf + env_size, script.buf, script.len);
-       p = script.buf + env_size;
-       env = (char **)strbuf_detach(&script, NULL);
-
-       for (i = 0; i < count; i++) {
-               env[i] = p;
+       for (i = 0, p = script.buf; i < count; i++) {
+               argv_array_push(env, p);
                p += strlen(p) + 1;
        }
-       env[count] = NULL;
 
-       return env;
+       return 0;
 }
 
 static const char staged_changes_advice[] =
@@ -600,14 +608,18 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts,
                          int allow_empty, int edit, int amend,
                          int cleanup_commit_message)
 {
-       char **env = NULL;
-       struct argv_array array;
-       int rc;
+       struct child_process cmd = CHILD_PROCESS_INIT;
        const char *value;
 
+       cmd.git_cmd = 1;
+
        if (is_rebase_i(opts)) {
-               env = read_author_script();
-               if (!env) {
+               if (!edit) {
+                       cmd.stdout_to_stderr = 1;
+                       cmd.err = -1;
+               }
+
+               if (read_env_script(&cmd.env_array)) {
                        const char *gpg_opt = gpg_sign_opt_quoted(opts);
 
                        return error(_(staged_changes_advice),
@@ -615,39 +627,47 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts,
                }
        }
 
-       argv_array_init(&array);
-       argv_array_push(&array, "commit");
-       argv_array_push(&array, "-n");
+       argv_array_push(&cmd.args, "commit");
+       argv_array_push(&cmd.args, "-n");
 
        if (amend)
-               argv_array_push(&array, "--amend");
+               argv_array_push(&cmd.args, "--amend");
        if (opts->gpg_sign)
-               argv_array_pushf(&array, "-S%s", opts->gpg_sign);
+               argv_array_pushf(&cmd.args, "-S%s", opts->gpg_sign);
        if (opts->signoff)
-               argv_array_push(&array, "-s");
+               argv_array_push(&cmd.args, "-s");
        if (defmsg)
-               argv_array_pushl(&array, "-F", defmsg, NULL);
+               argv_array_pushl(&cmd.args, "-F", defmsg, NULL);
        if (cleanup_commit_message)
-               argv_array_push(&array, "--cleanup=strip");
+               argv_array_push(&cmd.args, "--cleanup=strip");
        if (edit)
-               argv_array_push(&array, "-e");
+               argv_array_push(&cmd.args, "-e");
        else if (!cleanup_commit_message &&
                 !opts->signoff && !opts->record_origin &&
                 git_config_get_value("commit.cleanup", &value))
-               argv_array_push(&array, "--cleanup=verbatim");
+               argv_array_push(&cmd.args, "--cleanup=verbatim");
 
        if (allow_empty)
-               argv_array_push(&array, "--allow-empty");
+               argv_array_push(&cmd.args, "--allow-empty");
 
        if (opts->allow_empty_message)
-               argv_array_push(&array, "--allow-empty-message");
+               argv_array_push(&cmd.args, "--allow-empty-message");
 
-       rc = run_command_v_opt_cd_env(array.argv, RUN_GIT_CMD, NULL,
-                       (const char *const *)env);
-       argv_array_clear(&array);
-       free(env);
+       if (cmd.err == -1) {
+               /* hide stderr on success */
+               struct strbuf buf = STRBUF_INIT;
+               int rc = pipe_command(&cmd,
+                                     NULL, 0,
+                                     /* stdout is already redirected */
+                                     NULL, 0,
+                                     &buf, 0);
+               if (rc)
+                       fputs(buf.buf, stderr);
+               strbuf_release(&buf);
+               return rc;
+       }
 
-       return rc;
+       return run_command(&cmd);
 }
 
 static int is_original_commit_empty(struct commit *commit)
@@ -718,12 +738,16 @@ enum todo_command {
        TODO_PICK = 0,
        TODO_REVERT,
        TODO_EDIT,
+       TODO_REWORD,
        TODO_FIXUP,
        TODO_SQUASH,
        /* commands that do something else than handling a single commit */
        TODO_EXEC,
        /* commands that do nothing but are counted for reporting progress */
-       TODO_NOOP
+       TODO_NOOP,
+       TODO_DROP,
+       /* comments (not counted for reporting progress) */
+       TODO_COMMENT
 };
 
 static struct {
@@ -733,22 +757,25 @@ static struct {
        { 'p', "pick" },
        { 0,   "revert" },
        { 'e', "edit" },
+       { 'r', "reword" },
        { 'f', "fixup" },
        { 's', "squash" },
        { 'x', "exec" },
-       { 0,   "noop" }
+       { 0,   "noop" },
+       { 'd', "drop" },
+       { 0,   NULL }
 };
 
 static const char *command_to_string(const enum todo_command command)
 {
-       if ((size_t)command < ARRAY_SIZE(todo_command_info))
+       if (command < TODO_COMMENT)
                return todo_command_info[command].str;
        die("Unknown command: %d", command);
 }
 
 static int is_noop(const enum todo_command command)
 {
-       return TODO_NOOP <= (size_t)command;
+       return TODO_NOOP <= command;
 }
 
 static int is_fixup(enum todo_command command)
@@ -848,6 +875,44 @@ static int update_squash_messages(enum todo_command command,
        return res;
 }
 
+static void flush_rewritten_pending(void) {
+       struct strbuf buf = STRBUF_INIT;
+       unsigned char newsha1[20];
+       FILE *out;
+
+       if (strbuf_read_file(&buf, rebase_path_rewritten_pending(), 82) > 0 &&
+                       !get_sha1("HEAD", newsha1) &&
+                       (out = fopen(rebase_path_rewritten_list(), "a"))) {
+               char *bol = buf.buf, *eol;
+
+               while (*bol) {
+                       eol = strchrnul(bol, '\n');
+                       fprintf(out, "%.*s %s\n", (int)(eol - bol),
+                                       bol, sha1_to_hex(newsha1));
+                       if (!*eol)
+                               break;
+                       bol = eol + 1;
+               }
+               fclose(out);
+               unlink(rebase_path_rewritten_pending());
+       }
+       strbuf_release(&buf);
+}
+
+static void record_in_rewritten(struct object_id *oid,
+               enum todo_command next_command) {
+       FILE *out = fopen(rebase_path_rewritten_pending(), "a");
+
+       if (!out)
+               return;
+
+       fprintf(out, "%s\n", oid_to_hex(oid));
+       fclose(out);
+
+       if (!is_fixup(next_command))
+               flush_rewritten_pending();
+}
+
 static int do_pick_commit(enum todo_command command, struct commit *commit,
                struct replay_opts *opts, int final_fixup)
 {
@@ -858,7 +923,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
        const char *base_label, *next_label;
        struct commit_message msg = { NULL, NULL, NULL, NULL };
        struct strbuf msgbuf = STRBUF_INIT;
-       int res, unborn = 0, amend = 0, allow;
+       int res, unborn = 0, amend = 0, allow = 0;
 
        if (opts->no_commit) {
                /*
@@ -903,11 +968,23 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
        else
                parent = commit->parents->item;
 
+       if (get_message(commit, &msg) != 0)
+               return error(_("cannot get commit message for %s"),
+                       oid_to_hex(&commit->object.oid));
+
        if (opts->allow_ff && !is_fixup(command) &&
            ((parent && !hashcmp(parent->object.oid.hash, head)) ||
-            (!parent && unborn)))
-               return fast_forward_to(commit->object.oid.hash, head, unborn, opts);
-
+            (!parent && unborn))) {
+               if (is_rebase_i(opts))
+                       write_author_script(msg.message);
+               res = fast_forward_to(commit->object.oid.hash, head, unborn,
+                       opts);
+               if (res || command != TODO_REWORD)
+                       goto leave;
+               edit = amend = 1;
+               msg_file = NULL;
+               goto fast_forward_edit;
+       }
        if (parent && parse_commit(parent) < 0)
                /* TRANSLATORS: The first %s will be a "todo" command like
                   "revert" or "pick", the second %s a SHA1. */
@@ -915,10 +992,6 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
                        command_to_string(command),
                        oid_to_hex(&parent->object.oid));
 
-       if (get_message(commit, &msg) != 0)
-               return error(_("cannot get commit message for %s"),
-                       oid_to_hex(&commit->object.oid));
-
        /*
         * "commit" is an existing commit.  We would want to apply
         * the difference it introduces since its first parent "prev"
@@ -962,7 +1035,9 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
                }
        }
 
-       if (is_fixup(command)) {
+       if (command == TODO_REWORD)
+               edit = 1;
+       else if (is_fixup(command)) {
                if (update_squash_messages(command, commit, opts))
                        return -1;
                amend = 1;
@@ -1040,6 +1115,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
                goto leave;
        }
        if (!opts->no_commit)
+fast_forward_edit:
                res = run_git_commit(msg_file, opts, allow, edit, amend,
                                     cleanup_commit_message);
 
@@ -1133,14 +1209,14 @@ static int parse_insn_line(struct todo_item *item, const char *bol, char *eol)
        bol += strspn(bol, " \t");
 
        if (bol == eol || *bol == '\r' || *bol == comment_line_char) {
-               item->command = TODO_NOOP;
+               item->command = TODO_COMMENT;
                item->commit = NULL;
                item->arg = bol;
                item->arg_len = eol - bol;
                return 0;
        }
 
-       for (i = 0; i < ARRAY_SIZE(todo_command_info); i++)
+       for (i = 0; i < TODO_COMMENT; i++)
                if (skip_prefix(bol, todo_command_info[i].str, &bol)) {
                        item->command = i;
                        break;
@@ -1149,7 +1225,7 @@ static int parse_insn_line(struct todo_item *item, const char *bol, char *eol)
                        item->command = i;
                        break;
                }
-       if (i >= ARRAY_SIZE(todo_command_info))
+       if (i >= TODO_COMMENT)
                return -1;
 
        if (item->command == TODO_NOOP) {
@@ -1307,6 +1383,26 @@ static int populate_opts_cb(const char *key, const char *value, void *data)
        return 0;
 }
 
+static void read_strategy_opts(struct replay_opts *opts, struct strbuf *buf)
+{
+       int i;
+
+       strbuf_reset(buf);
+       if (!read_oneliner(buf, rebase_path_strategy(), 0))
+               return;
+       opts->strategy = strbuf_detach(buf, NULL);
+       if (!read_oneliner(buf, rebase_path_strategy_opts(), 0))
+               return;
+
+       opts->xopts_nr = split_cmdline(buf->buf, (const char ***)&opts->xopts);
+       for (i = 0; i < opts->xopts_nr; i++) {
+               const char *arg = opts->xopts[i];
+
+               skip_prefix(arg, "--", &arg);
+               opts->xopts[i] = xstrdup(arg);
+       }
+}
+
 static int read_populate_opts(struct replay_opts *opts)
 {
        if (is_rebase_i(opts)) {
@@ -1320,11 +1416,13 @@ static int read_populate_opts(struct replay_opts *opts)
                                opts->gpg_sign = xstrdup(buf.buf + 2);
                        }
                }
-               strbuf_release(&buf);
 
                if (file_exists(rebase_path_verbose()))
                        opts->verbose = 1;
 
+               read_strategy_opts(opts, &buf);
+               strbuf_release(&buf);
+
                return 0;
        }
 
@@ -1730,6 +1828,78 @@ static int is_final_fixup(struct todo_list *todo_list)
        return 1;
 }
 
+static enum todo_command peek_command(struct todo_list *todo_list, int offset)
+{
+       int i;
+
+       for (i = todo_list->current + offset; i < todo_list->nr; i++)
+               if (!is_noop(todo_list->items[i].command))
+                       return todo_list->items[i].command;
+
+       return -1;
+}
+
+static int apply_autostash(struct replay_opts *opts)
+{
+       struct strbuf stash_sha1 = STRBUF_INIT;
+       struct child_process child = CHILD_PROCESS_INIT;
+       int ret = 0;
+
+       if (!read_oneliner(&stash_sha1, rebase_path_autostash(), 1)) {
+               strbuf_release(&stash_sha1);
+               return 0;
+       }
+       strbuf_trim(&stash_sha1);
+
+       child.git_cmd = 1;
+       argv_array_push(&child.args, "stash");
+       argv_array_push(&child.args, "apply");
+       argv_array_push(&child.args, stash_sha1.buf);
+       if (!run_command(&child))
+               printf(_("Applied autostash."));
+       else {
+               struct child_process store = CHILD_PROCESS_INIT;
+
+               store.git_cmd = 1;
+               argv_array_push(&store.args, "stash");
+               argv_array_push(&store.args, "store");
+               argv_array_push(&store.args, "-m");
+               argv_array_push(&store.args, "autostash");
+               argv_array_push(&store.args, "-q");
+               argv_array_push(&store.args, stash_sha1.buf);
+               if (run_command(&store))
+                       ret = error(_("cannot store %s"), stash_sha1.buf);
+               else
+                       printf(_("Applying autostash resulted in conflicts.\n"
+                               "Your changes are safe in the stash.\n"
+                               "You can run \"git stash pop\" or"
+                               " \"git stash drop\" at any time.\n"));
+       }
+
+       strbuf_release(&stash_sha1);
+       return ret;
+}
+
+static const char *reflog_message(struct replay_opts *opts,
+       const char *sub_action, const char *fmt, ...)
+{
+       va_list ap;
+       static struct strbuf buf = STRBUF_INIT;
+
+       va_start(ap, fmt);
+       strbuf_reset(&buf);
+       strbuf_addstr(&buf, action_name(opts));
+       if (sub_action)
+               strbuf_addf(&buf, " (%s)", sub_action);
+       if (fmt) {
+               strbuf_addstr(&buf, ": ");
+               strbuf_vaddf(&buf, fmt, ap);
+       }
+       va_end(ap);
+
+       return buf.buf;
+}
+
 static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
 {
        int res = 0;
@@ -1752,8 +1922,18 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
                        unlink(rebase_path_amend());
                }
                if (item->command <= TODO_SQUASH) {
+                       if (is_rebase_i(opts))
+                               setenv("GIT_REFLOG_ACTION", reflog_message(opts,
+                                       command_to_string(item->command), NULL),
+                                       1);
                        res = do_pick_commit(item->command, item->commit,
                                        opts, is_final_fixup(todo_list));
+                       if (is_rebase_i(opts) && res < 0) {
+                               /* Reschedule */
+                               todo_list->current--;
+                               if (save_todo(todo_list, opts))
+                                       return -1;
+                       }
                        if (item->command == TODO_EDIT) {
                                struct commit *commit = item->commit;
                                if (!res)
@@ -1764,6 +1944,9 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
                                        item->arg, item->arg_len, opts, res,
                                        !res);
                        }
+                       if (is_rebase_i(opts) && !res)
+                               record_in_rewritten(&item->commit->object.oid,
+                                       peek_command(todo_list, 1));
                        if (res && is_fixup(item->command)) {
                                if (res == 1)
                                        intend_to_amend();
@@ -1771,7 +1954,8 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
                                        item->arg_len, item->arg);
                        } else if (res && is_rebase_i(opts))
                                return res | error_with_patch(item->commit,
-                                       item->arg, item->arg_len, opts, res, 0);
+                                       item->arg, item->arg_len, opts, res,
+                                       item->command == TODO_REWORD);
                } else if (item->command == TODO_EXEC) {
                        char *end_of_arg = (char *)(item->arg + item->arg_len);
                        int saved = *end_of_arg;
@@ -1789,6 +1973,7 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
 
        if (is_rebase_i(opts)) {
                struct strbuf head_ref = STRBUF_INIT, buf = STRBUF_INIT;
+               struct stat st;
 
                /* Stopped in the middle, as planned? */
                if (todo_list->current < todo_list->nr)
@@ -1796,6 +1981,7 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
 
                if (read_oneliner(&head_ref, rebase_path_head_name(), 0) &&
                                starts_with(head_ref.buf, "refs/")) {
+                       const char *msg;
                        unsigned char head[20], orig[20];
                        int res;
 
@@ -1811,23 +1997,21 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
                                res = error(_("could not read orig-head"));
                                goto cleanup_head_ref;
                        }
-                       strbuf_addf(&buf, "rebase -i (finish): %s onto ",
-                               head_ref.buf);
                        if (!read_oneliner(&buf, rebase_path_onto(), 0)) {
                                res = error(_("could not read 'onto'"));
                                goto cleanup_head_ref;
                        }
-                       if (update_ref(buf.buf, head_ref.buf, head, orig,
+                       msg = reflog_message(opts, "finish", "%s onto %s",
+                               head_ref.buf, buf.buf);
+                       if (update_ref(msg, head_ref.buf, head, orig,
                                        REF_NODEREF, UPDATE_REFS_MSG_ON_ERR)) {
                                res = error(_("could not update %s"),
                                        head_ref.buf);
                                goto cleanup_head_ref;
                        }
-                       strbuf_reset(&buf);
-                       strbuf_addf(&buf,
-                               "rebase -i (finish): returning to %s",
+                       msg = reflog_message(opts, "finish", "returning to %s",
                                head_ref.buf);
-                       if (create_symref("HEAD", head_ref.buf, buf.buf)) {
+                       if (create_symref("HEAD", head_ref.buf, msg)) {
                                res = error(_("could not update HEAD to %s"),
                                        head_ref.buf);
                                goto cleanup_head_ref;
@@ -1854,6 +2038,35 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
                                log_tree_diff_flush(&log_tree_opt);
                        }
                }
+               flush_rewritten_pending();
+               if (!stat(rebase_path_rewritten_list(), &st) &&
+                               st.st_size > 0) {
+                       struct child_process child = CHILD_PROCESS_INIT;
+                       const char *post_rewrite_hook =
+                               find_hook("post-rewrite");
+
+                       child.in = open(rebase_path_rewritten_list(), O_RDONLY);
+                       child.git_cmd = 1;
+                       argv_array_push(&child.args, "notes");
+                       argv_array_push(&child.args, "copy");
+                       argv_array_push(&child.args, "--for-rewrite=rebase");
+                       /* we don't care if this copying failed */
+                       run_command(&child);
+
+                       if (post_rewrite_hook) {
+                               struct child_process hook = CHILD_PROCESS_INIT;
+
+                               hook.in = open(rebase_path_rewritten_list(),
+                                       O_RDONLY);
+                               hook.stdout_to_stderr = 1;
+                               argv_array_push(&hook.args, post_rewrite_hook);
+                               argv_array_push(&hook.args, "rebase");
+                               /* we don't care if this hook failed */
+                               run_command(&hook);
+                       }
+               }
+               apply_autostash(opts);
+
                strbuf_release(&buf);
                strbuf_release(&head_ref);
        }
@@ -1947,6 +2160,14 @@ int sequencer_continue(struct replay_opts *opts)
                        goto release_todo_list;
                }
                todo_list.current++;
+       } else if (file_exists(rebase_path_stopped_sha())) {
+               struct strbuf buf = STRBUF_INIT;
+               struct object_id oid;
+
+               if (read_oneliner(&buf, rebase_path_stopped_sha(), 1) &&
+                   !get_sha1_committish(buf.buf, oid.hash))
+                       record_in_rewritten(&oid, peek_command(&todo_list, 0));
+               strbuf_release(&buf);
        }
 
        res = pick_commits(&todo_list, opts);