wt-status.h: drop stdio.h include
[gitweb.git] / sequencer.c
index 7d46f76a8bff43a1b86752036b563609fea7f99d..f88a97fb10a322c21062ec8c102482677cfd0feb 100644 (file)
@@ -35,7 +35,7 @@
 
 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
 
-const char sign_off_header[] = "Signed-off-by: ";
+static const char sign_off_header[] = "Signed-off-by: ";
 static const char cherry_picked_prefix[] = "(cherry picked from commit ";
 
 GIT_PATH_FUNC(git_path_commit_editmsg, "COMMIT_EDITMSG")
@@ -149,6 +149,7 @@ static GIT_PATH_FUNC(rebase_path_refs_to_delete, "rebase-merge/refs-to-delete")
 static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt")
 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_quiet, "rebase-merge/quiet")
 static GIT_PATH_FUNC(rebase_path_signoff, "rebase-merge/signoff")
 static GIT_PATH_FUNC(rebase_path_head_name, "rebase-merge/head-name")
 static GIT_PATH_FUNC(rebase_path_onto, "rebase-merge/onto")
@@ -156,7 +157,7 @@ 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 GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate, "rebase-merge/allow_rerere_autoupdate")
-static GIT_PATH_FUNC(rebase_path_quiet, "rebase-merge/quiet")
+static GIT_PATH_FUNC(rebase_path_reschedule_failed_exec, "rebase-merge/reschedule-failed-exec")
 
 static int git_sequencer_config(const char *k, const char *v, void *cb)
 {
@@ -170,17 +171,22 @@ static int git_sequencer_config(const char *k, const char *v, void *cb)
                if (status)
                        return status;
 
-               if (!strcmp(s, "verbatim"))
+               if (!strcmp(s, "verbatim")) {
                        opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_NONE;
-               else if (!strcmp(s, "whitespace"))
+                       opts->explicit_cleanup = 1;
+               } else if (!strcmp(s, "whitespace")) {
                        opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SPACE;
-               else if (!strcmp(s, "strip"))
+                       opts->explicit_cleanup = 1;
+               } else if (!strcmp(s, "strip")) {
                        opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_ALL;
-               else if (!strcmp(s, "scissors"))
-                       opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SPACE;
-               else
+                       opts->explicit_cleanup = 1;
+               } else if (!strcmp(s, "scissors")) {
+                       opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SCISSORS;
+                       opts->explicit_cleanup = 1;
+               } else {
                        warning(_("invalid commit message cleanup mode '%s'"),
                                  s);
+               }
 
                free((char *)s);
                return status;
@@ -445,9 +451,9 @@ static struct tree *empty_tree(struct repository *r)
        return lookup_tree(r, the_hash_algo->empty_tree);
 }
 
-static int error_dirty_index(struct index_state *istate, struct replay_opts *opts)
+static int error_dirty_index(struct repository *repo, struct replay_opts *opts)
 {
-       if (read_index_unmerged(istate))
+       if (repo_read_index_unmerged(repo))
                return error_resolve_conflict(_(action_name(opts)));
 
        error(_("your local changes would be overwritten by %s."),
@@ -482,7 +488,7 @@ static int fast_forward_to(struct repository *r,
        struct strbuf sb = STRBUF_INIT;
        struct strbuf err = STRBUF_INIT;
 
-       read_index(r->index);
+       repo_read_index(r);
        if (checkout_fast_forward(r, from, to, 1))
                return -1; /* the callee should have complained already */
 
@@ -509,11 +515,54 @@ static int fast_forward_to(struct repository *r,
        return 0;
 }
 
+enum commit_msg_cleanup_mode get_cleanup_mode(const char *cleanup_arg,
+       int use_editor)
+{
+       if (!cleanup_arg || !strcmp(cleanup_arg, "default"))
+               return use_editor ? COMMIT_MSG_CLEANUP_ALL :
+                                   COMMIT_MSG_CLEANUP_SPACE;
+       else if (!strcmp(cleanup_arg, "verbatim"))
+               return COMMIT_MSG_CLEANUP_NONE;
+       else if (!strcmp(cleanup_arg, "whitespace"))
+               return COMMIT_MSG_CLEANUP_SPACE;
+       else if (!strcmp(cleanup_arg, "strip"))
+               return COMMIT_MSG_CLEANUP_ALL;
+       else if (!strcmp(cleanup_arg, "scissors"))
+               return use_editor ? COMMIT_MSG_CLEANUP_SCISSORS :
+                                   COMMIT_MSG_CLEANUP_SPACE;
+       else
+               die(_("Invalid cleanup mode %s"), cleanup_arg);
+}
+
+/*
+ * NB using int rather than enum cleanup_mode to stop clang's
+ * -Wtautological-constant-out-of-range-compare complaining that the comparison
+ * is always true.
+ */
+static const char *describe_cleanup_mode(int cleanup_mode)
+{
+       static const char *modes[] = { "whitespace",
+                                      "verbatim",
+                                      "scissors",
+                                      "strip" };
+
+       if (cleanup_mode < ARRAY_SIZE(modes))
+               return modes[cleanup_mode];
+
+       BUG("invalid cleanup_mode provided (%d)", cleanup_mode);
+}
+
 void append_conflicts_hint(struct index_state *istate,
-                          struct strbuf *msgbuf)
+       struct strbuf *msgbuf, enum commit_msg_cleanup_mode cleanup_mode)
 {
        int i;
 
+       if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
+               strbuf_addch(msgbuf, '\n');
+               wt_status_append_cut_line(msgbuf);
+               strbuf_addch(msgbuf, comment_line_char);
+       }
+
        strbuf_addch(msgbuf, '\n');
        strbuf_commented_addf(msgbuf, "Conflicts:\n");
        for (i = 0; i < istate->cache_nr;) {
@@ -539,12 +588,12 @@ static int do_recursive_merge(struct repository *r,
        char **xopt;
        struct lock_file index_lock = LOCK_INIT;
 
-       if (hold_locked_index(&index_lock, LOCK_REPORT_ON_ERROR) < 0)
+       if (repo_hold_locked_index(r, &index_lock, LOCK_REPORT_ON_ERROR) < 0)
                return -1;
 
-       read_index(r->index);
+       repo_read_index(r);
 
-       init_merge_options(&o);
+       init_merge_options(&o, r);
        o.ancestor = base ? base_label : "(empty tree)";
        o.branch1 = "HEAD";
        o.branch2 = next ? next_label : "(empty tree)";
@@ -581,7 +630,8 @@ static int do_recursive_merge(struct repository *r,
                        _(action_name(opts)));
 
        if (!clean)
-               append_conflicts_hint(r->index, msgbuf);
+               append_conflicts_hint(r->index, msgbuf,
+                                     opts->default_msg_cleanup);
 
        return !clean;
 }
@@ -835,7 +885,7 @@ static const char *read_author_ident(struct strbuf *buf)
        }
 
        strbuf_reset(&out);
-       strbuf_addstr(&out, fmt_ident(name, email, date, 0));
+       strbuf_addstr(&out, fmt_ident(name, email, WANT_AUTHOR_IDENT, date, 0));
        strbuf_swap(buf, &out);
        strbuf_release(&out);
        free(name);
@@ -900,7 +950,6 @@ static int run_git_commit(struct repository *r,
                          unsigned int flags)
 {
        struct child_process cmd = CHILD_PROCESS_INIT;
-       const char *value;
 
        if ((flags & CREATE_ROOT_COMMIT) && !(flags & AMEND_MSG)) {
                struct strbuf msg = STRBUF_INIT, script = STRBUF_INIT;
@@ -970,7 +1019,7 @@ static int run_git_commit(struct repository *r,
                argv_array_push(&cmd.args, "-e");
        else if (!(flags & CLEANUP_MSG) &&
                 !opts->signoff && !opts->record_origin &&
-                git_config_get_value("commit.cleanup", &value))
+                !opts->explicit_cleanup)
                argv_array_push(&cmd.args, "--cleanup=verbatim");
 
        if ((flags & ALLOW_EMPTY))
@@ -1011,6 +1060,16 @@ static int rest_is_empty(const struct strbuf *sb, int start)
        return 1;
 }
 
+void cleanup_message(struct strbuf *msgbuf,
+       enum commit_msg_cleanup_mode cleanup_mode, int verbose)
+{
+       if (verbose || /* Truncate the message just before the diff, if any. */
+           cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
+               strbuf_setlen(msgbuf, wt_status_locate_end(msgbuf->buf, msgbuf->len));
+       if (cleanup_mode != COMMIT_MSG_CLEANUP_NONE)
+               strbuf_stripspace(msgbuf, cleanup_mode == COMMIT_MSG_CLEANUP_ALL);
+}
+
 /*
  * Find out if the message in the strbuf contains only whitespace and
  * Signed-off-by lines.
@@ -1101,6 +1160,7 @@ static int run_rewrite_hook(const struct object_id *oldoid,
        proc.argv = argv;
        proc.in = -1;
        proc.stdout_to_stderr = 1;
+       proc.trace2_hook_name = "post-rewrite";
 
        code = start_command(&proc);
        if (code)
@@ -1114,7 +1174,8 @@ static int run_rewrite_hook(const struct object_id *oldoid,
        return finish_command(&proc);
 }
 
-void commit_post_rewrite(const struct commit *old_head,
+void commit_post_rewrite(struct repository *r,
+                        const struct commit *old_head,
                         const struct object_id *new_head)
 {
        struct notes_rewrite_cfg *cfg;
@@ -1123,7 +1184,7 @@ void commit_post_rewrite(const struct commit *old_head,
        if (cfg) {
                /* we are amending, so old_head is not NULL */
                copy_note_for_rewrite(cfg, &old_head->object.oid, new_head);
-               finish_copy_notes_for_rewrite(cfg, "Notes added by 'git commit --amend'");
+               finish_copy_notes_for_rewrite(r, cfg, "Notes added by 'git commit --amend'");
        }
        run_rewrite_hook(&old_head->object.oid, new_head);
 }
@@ -1379,8 +1440,13 @@ static int try_to_commit(struct repository *r,
                msg = &commit_msg;
        }
 
-       cleanup = (flags & CLEANUP_MSG) ? COMMIT_MSG_CLEANUP_ALL :
-                                         opts->default_msg_cleanup;
+       if (flags & CLEANUP_MSG)
+               cleanup = COMMIT_MSG_CLEANUP_ALL;
+       else if ((opts->signoff || opts->record_origin) &&
+                !opts->explicit_cleanup)
+               cleanup = COMMIT_MSG_CLEANUP_SPACE;
+       else
+               cleanup = opts->default_msg_cleanup;
 
        if (cleanup != COMMIT_MSG_CLEANUP_NONE)
                strbuf_stripspace(msg, cleanup == COMMIT_MSG_CLEANUP_ALL);
@@ -1404,7 +1470,7 @@ static int try_to_commit(struct repository *r,
        }
 
        if (flags & AMEND_MSG)
-               commit_post_rewrite(current_head, oid);
+               commit_post_rewrite(r, current_head, oid);
 
 out:
        free_commit_extra_headers(extra);
@@ -1662,7 +1728,8 @@ static int update_squash_messages(struct repository *r,
        return res;
 }
 
-static void flush_rewritten_pending(void) {
+static void flush_rewritten_pending(void)
+{
        struct strbuf buf = STRBUF_INIT;
        struct object_id newoid;
        FILE *out;
@@ -1687,7 +1754,8 @@ static void flush_rewritten_pending(void) {
 }
 
 static void record_in_rewritten(struct object_id *oid,
-               enum todo_command next_command) {
+               enum todo_command next_command)
+{
        FILE *out = fopen_or_warn(rebase_path_rewritten_pending(), "a");
 
        if (!out)
@@ -1738,7 +1806,7 @@ static int do_pick_commit(struct repository *r,
                        oidcpy(&head, the_hash_algo->empty_tree);
                if (index_differs_from(r, unborn ? empty_tree_oid_hex() : "HEAD",
                                       NULL, 0))
-                       return error_dirty_index(r->index, opts);
+                       return error_dirty_index(r, opts);
        }
        discard_index(r->index);
 
@@ -1761,9 +1829,13 @@ static int do_pick_commit(struct repository *r,
                        return error(_("commit %s does not have parent %d"),
                                oid_to_hex(&commit->object.oid), opts->mainline);
                parent = p->item;
-       } else if (0 < opts->mainline)
-               return error(_("mainline was specified but commit %s is not a merge."),
-                       oid_to_hex(&commit->object.oid));
+       } else if (1 < opts->mainline)
+               /*
+                *  Non-first parent explicitly specified as mainline for
+                *  non-merge commit
+                */
+               return error(_("commit %s does not have parent %d"),
+                            oid_to_hex(&commit->object.oid), opts->mainline);
        else
                parent = commit->parents->item;
 
@@ -1964,8 +2036,8 @@ static int read_and_refresh_cache(struct repository *r,
                                  struct replay_opts *opts)
 {
        struct lock_file index_lock = LOCK_INIT;
-       int index_fd = hold_locked_index(&index_lock, 0);
-       if (read_index(r->index) < 0) {
+       int index_fd = repo_hold_locked_index(r, &index_lock, 0);
+       if (repo_read_index(r) < 0) {
                rollback_lock_file(&index_lock);
                return error(_("git %s: failed to read the index"),
                        _(action_name(opts)));
@@ -2089,12 +2161,48 @@ static int parse_insn_line(struct repository *r, struct todo_item *item,
        item->arg_len = (int)(eol - bol);
 
        if (status < 0)
-               return -1;
+               return error(_("could not parse '%.*s'"),
+                            (int)(end_of_object_name - bol), bol);
 
        item->commit = lookup_commit_reference(r, &commit_oid);
        return !item->commit;
 }
 
+int sequencer_get_last_command(struct repository *r, enum replay_action *action)
+{
+       struct todo_item item;
+       char *eol;
+       const char *todo_file;
+       struct strbuf buf = STRBUF_INIT;
+       int ret = -1;
+
+       todo_file = git_path_todo_file();
+       if (strbuf_read_file(&buf, todo_file, 0) < 0) {
+               if (errno == ENOENT)
+                       return -1;
+               else
+                       return error_errno("unable to open '%s'", todo_file);
+       }
+       eol = strchrnul(buf.buf, '\n');
+       if (buf.buf != eol && eol[-1] == '\r')
+               eol--; /* strip Carriage Return */
+       if (parse_insn_line(r, &item, buf.buf, buf.buf, eol))
+               goto fail;
+       if (item.command == TODO_PICK)
+               *action = REPLAY_PICK;
+       else if (item.command == TODO_REVERT)
+               *action = REPLAY_REVERT;
+       else
+               goto fail;
+
+       ret = 0;
+
+ fail:
+       strbuf_release(&buf);
+
+       return ret;
+}
+
 int todo_list_parse_insn_buffer(struct repository *r, char *buf,
                                struct todo_list *todo_list)
 {
@@ -2178,6 +2286,57 @@ static ssize_t strbuf_read_file_or_whine(struct strbuf *sb, const char *path)
        return len;
 }
 
+static int have_finished_the_last_pick(void)
+{
+       struct strbuf buf = STRBUF_INIT;
+       const char *eol;
+       const char *todo_path = git_path_todo_file();
+       int ret = 0;
+
+       if (strbuf_read_file(&buf, todo_path, 0) < 0) {
+               if (errno == ENOENT) {
+                       return 0;
+               } else {
+                       error_errno("unable to open '%s'", todo_path);
+                       return 0;
+               }
+       }
+       /* If there is only one line then we are done */
+       eol = strchr(buf.buf, '\n');
+       if (!eol || !eol[1])
+               ret = 1;
+
+       strbuf_release(&buf);
+
+       return ret;
+}
+
+void sequencer_post_commit_cleanup(struct repository *r)
+{
+       struct replay_opts opts = REPLAY_OPTS_INIT;
+       int need_cleanup = 0;
+
+       if (file_exists(git_path_cherry_pick_head(r))) {
+               unlink(git_path_cherry_pick_head(r));
+               opts.action = REPLAY_PICK;
+               need_cleanup = 1;
+       }
+
+       if (file_exists(git_path_revert_head(r))) {
+               unlink(git_path_revert_head(r));
+               opts.action = REPLAY_REVERT;
+               need_cleanup = 1;
+       }
+
+       if (!need_cleanup)
+               return;
+
+       if (!have_finished_the_last_pick())
+               return;
+
+       sequencer_remove_state(&opts);
+}
+
 static int read_populate_todo(struct repository *r,
                              struct todo_list *todo_list,
                              struct replay_opts *opts)
@@ -2265,6 +2424,15 @@ static int populate_opts_cb(const char *key, const char *value, void *data)
                opts->no_commit = git_config_bool_or_int(key, value, &error_flag);
        else if (!strcmp(key, "options.edit"))
                opts->edit = git_config_bool_or_int(key, value, &error_flag);
+       else if (!strcmp(key, "options.allow-empty"))
+               opts->allow_empty =
+                       git_config_bool_or_int(key, value, &error_flag);
+       else if (!strcmp(key, "options.allow-empty-message"))
+               opts->allow_empty_message =
+                       git_config_bool_or_int(key, value, &error_flag);
+       else if (!strcmp(key, "options.keep-redundant-commits"))
+               opts->keep_redundant_commits =
+                       git_config_bool_or_int(key, value, &error_flag);
        else if (!strcmp(key, "options.signoff"))
                opts->signoff = git_config_bool_or_int(key, value, &error_flag);
        else if (!strcmp(key, "options.record-origin"))
@@ -2284,7 +2452,10 @@ static int populate_opts_cb(const char *key, const char *value, void *data)
                opts->allow_rerere_auto =
                        git_config_bool_or_int(key, value, &error_flag) ?
                                RERERE_AUTOUPDATE : RERERE_NOAUTOUPDATE;
-       else
+       else if (!strcmp(key, "options.default-msg-cleanup")) {
+               opts->explicit_cleanup = 1;
+               opts->default_msg_cleanup = get_cleanup_mode(value, 1);
+       } else
                return error(_("invalid key: %s"), key);
 
        if (!error_flag)
@@ -2349,11 +2520,17 @@ static int read_populate_opts(struct replay_opts *opts)
                if (file_exists(rebase_path_verbose()))
                        opts->verbose = 1;
 
+               if (file_exists(rebase_path_quiet()))
+                       opts->quiet = 1;
+
                if (file_exists(rebase_path_signoff())) {
                        opts->allow_ff = 0;
                        opts->signoff = 1;
                }
 
+               if (file_exists(rebase_path_reschedule_failed_exec()))
+                       opts->reschedule_failed_exec = 1;
+
                read_strategy_opts(opts, &buf);
                strbuf_release(&buf);
 
@@ -2403,22 +2580,20 @@ static void write_strategy_opts(struct replay_opts *opts)
 }
 
 int write_basic_state(struct replay_opts *opts, const char *head_name,
-                     const char *onto, const char *orig_head)
+                     struct commit *onto, const char *orig_head)
 {
        const char *quiet = getenv("GIT_QUIET");
 
        if (head_name)
                write_file(rebase_path_head_name(), "%s\n", head_name);
        if (onto)
-               write_file(rebase_path_onto(), "%s\n", onto);
+               write_file(rebase_path_onto(), "%s\n",
+                          oid_to_hex(&onto->object.oid));
        if (orig_head)
                write_file(rebase_path_orig_head(), "%s\n", orig_head);
 
        if (quiet)
                write_file(rebase_path_quiet(), "%s\n", quiet);
-       else
-               write_file(rebase_path_quiet(), "\n");
-
        if (opts->verbose)
                write_file(rebase_path_verbose(), "%s", "");
        if (opts->strategy)
@@ -2435,6 +2610,8 @@ int write_basic_state(struct replay_opts *opts, const char *head_name,
                write_file(rebase_path_gpg_sign_opt(), "-S%s\n", opts->gpg_sign);
        if (opts->signoff)
                write_file(rebase_path_signoff(), "--signoff\n");
+       if (opts->reschedule_failed_exec)
+               write_file(rebase_path_reschedule_failed_exec(), "%s", "");
 
        return 0;
 }
@@ -2653,36 +2830,59 @@ static int save_opts(struct replay_opts *opts)
        int res = 0;
 
        if (opts->no_commit)
-               res |= git_config_set_in_file_gently(opts_file, "options.no-commit", "true");
+               res |= git_config_set_in_file_gently(opts_file,
+                                       "options.no-commit", "true");
        if (opts->edit)
-               res |= git_config_set_in_file_gently(opts_file, "options.edit", "true");
+               res |= git_config_set_in_file_gently(opts_file,
+                                       "options.edit", "true");
+       if (opts->allow_empty)
+               res |= git_config_set_in_file_gently(opts_file,
+                                       "options.allow-empty", "true");
+       if (opts->allow_empty_message)
+               res |= git_config_set_in_file_gently(opts_file,
+                               "options.allow-empty-message", "true");
+       if (opts->keep_redundant_commits)
+               res |= git_config_set_in_file_gently(opts_file,
+                               "options.keep-redundant-commits", "true");
        if (opts->signoff)
-               res |= git_config_set_in_file_gently(opts_file, "options.signoff", "true");
+               res |= git_config_set_in_file_gently(opts_file,
+                                       "options.signoff", "true");
        if (opts->record_origin)
-               res |= git_config_set_in_file_gently(opts_file, "options.record-origin", "true");
+               res |= git_config_set_in_file_gently(opts_file,
+                                       "options.record-origin", "true");
        if (opts->allow_ff)
-               res |= git_config_set_in_file_gently(opts_file, "options.allow-ff", "true");
+               res |= git_config_set_in_file_gently(opts_file,
+                                       "options.allow-ff", "true");
        if (opts->mainline) {
                struct strbuf buf = STRBUF_INIT;
                strbuf_addf(&buf, "%d", opts->mainline);
-               res |= git_config_set_in_file_gently(opts_file, "options.mainline", buf.buf);
+               res |= git_config_set_in_file_gently(opts_file,
+                                       "options.mainline", buf.buf);
                strbuf_release(&buf);
        }
        if (opts->strategy)
-               res |= git_config_set_in_file_gently(opts_file, "options.strategy", opts->strategy);
+               res |= git_config_set_in_file_gently(opts_file,
+                                       "options.strategy", opts->strategy);
        if (opts->gpg_sign)
-               res |= git_config_set_in_file_gently(opts_file, "options.gpg-sign", opts->gpg_sign);
+               res |= git_config_set_in_file_gently(opts_file,
+                                       "options.gpg-sign", opts->gpg_sign);
        if (opts->xopts) {
                int i;
                for (i = 0; i < opts->xopts_nr; i++)
                        res |= git_config_set_multivar_in_file_gently(opts_file,
-                                                       "options.strategy-option",
-                                                       opts->xopts[i], "^$", 0);
+                                       "options.strategy-option",
+                                       opts->xopts[i], "^$", 0);
        }
        if (opts->allow_rerere_auto)
-               res |= git_config_set_in_file_gently(opts_file, "options.allow-rerere-auto",
-                                                    opts->allow_rerere_auto == RERERE_AUTOUPDATE ?
-                                                    "true" : "false");
+               res |= git_config_set_in_file_gently(opts_file,
+                               "options.allow-rerere-auto",
+                               opts->allow_rerere_auto == RERERE_AUTOUPDATE ?
+                               "true" : "false");
+
+       if (opts->explicit_cleanup)
+               res |= git_config_set_in_file_gently(opts_file,
+                               "options.default-msg-cleanup",
+                               describe_cleanup_mode(opts->default_msg_cleanup));
        return res;
 }
 
@@ -2819,7 +3019,7 @@ static int do_exec(struct repository *r, const char *command_line)
                                          child_env.argv);
 
        /* force re-reading of the cache */
-       if (discard_index(r->index) < 0 || read_index(r->index) < 0)
+       if (discard_index(r->index) < 0 || repo_read_index(r) < 0)
                return error(_("could not read index"));
 
        dirty = require_clean_work_tree(r, "rebase", NULL, 1, 1);
@@ -2943,7 +3143,7 @@ static int do_reset(struct repository *r,
        struct unpack_trees_options unpack_tree_opts;
        int ret = 0;
 
-       if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0)
+       if (repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0)
                return -1;
 
        if (len == 10 && !strncmp("[new root]", name, len)) {
@@ -2988,7 +3188,7 @@ static int do_reset(struct repository *r,
        unpack_tree_opts.merge = 1;
        unpack_tree_opts.update = 1;
 
-       if (read_index_unmerged(r->index)) {
+       if (repo_read_index_unmerged(r)) {
                rollback_lock_file(&lock);
                strbuf_release(&ref_name);
                return error_resolve_conflict(_(action_name(opts)));
@@ -3061,7 +3261,7 @@ static int do_merge(struct repository *r,
        static struct lock_file lock;
        const char *p;
 
-       if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) {
+       if (repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
                ret = -1;
                goto leave_merge;
        }
@@ -3242,7 +3442,7 @@ static int do_merge(struct repository *r,
 
                /* force re-reading of the cache */
                if (!ret && (discard_index(r->index) < 0 ||
-                            read_index(r->index) < 0))
+                            repo_read_index(r) < 0))
                        ret = error(_("could not read index"));
                goto leave_merge;
        }
@@ -3264,8 +3464,8 @@ static int do_merge(struct repository *r,
                commit_list_insert(j->item, &reversed);
        free_commit_list(bases);
 
-       read_index(r->index);
-       init_merge_options(&o);
+       repo_read_index(r);
+       init_merge_options(&o, r);
        o.branch1 = "HEAD";
        o.branch2 = ref_name.buf;
        o.buffer_output = 2;
@@ -3404,10 +3604,11 @@ static const char *reflog_message(struct replay_opts *opts,
        return buf.buf;
 }
 
-static int run_git_checkout(struct replay_opts *opts, const char *commit,
-                           const char *action)
+static int run_git_checkout(struct repository *r, struct replay_opts *opts,
+                           const char *commit, const char *action)
 {
        struct child_process cmd = CHILD_PROCESS_INIT;
+       int ret;
 
        cmd.git_cmd = 1;
 
@@ -3416,26 +3617,32 @@ static int run_git_checkout(struct replay_opts *opts, const char *commit,
        argv_array_pushf(&cmd.env_array, GIT_REFLOG_ACTION "=%s", action);
 
        if (opts->verbose)
-               return run_command(&cmd);
+               ret = run_command(&cmd);
        else
-               return run_command_silent_on_success(&cmd);
+               ret = run_command_silent_on_success(&cmd);
+
+       if (!ret)
+               discard_index(r->index);
+
+       return ret;
 }
 
-int prepare_branch_to_be_rebased(struct replay_opts *opts, const char *commit)
+int prepare_branch_to_be_rebased(struct repository *r, struct replay_opts *opts,
+                                const char *commit)
 {
        const char *action;
 
        if (commit && *commit) {
                action = reflog_message(opts, "start", "checkout %s", commit);
-               if (run_git_checkout(opts, commit, action))
+               if (run_git_checkout(r, opts, commit, action))
                        return error(_("could not checkout %s"), commit);
        }
 
        return 0;
 }
 
-static int checkout_onto(struct replay_opts *opts,
-                        const char *onto_name, const char *onto,
+static int checkout_onto(struct repository *r, struct replay_opts *opts,
+                        const char *onto_name, const struct object_id *onto,
                         const char *orig_head)
 {
        struct object_id oid;
@@ -3444,7 +3651,7 @@ static int checkout_onto(struct replay_opts *opts,
        if (get_oid(orig_head, &oid))
                return error(_("%s: not a valid OID"), orig_head);
 
-       if (run_git_checkout(opts, onto, action)) {
+       if (run_git_checkout(r, opts, oid_to_hex(onto), action)) {
                apply_autostash(opts);
                sequencer_remove_state(opts);
                return error(_("could not detach HEAD"));
@@ -3511,10 +3718,11 @@ static int pick_commits(struct repository *r,
                                        fprintf(f, "%d\n", todo_list->done_nr);
                                        fclose(f);
                                }
-                               fprintf(stderr, "Rebasing (%d/%d)%s",
-                                       todo_list->done_nr,
-                                       todo_list->total_nr,
-                                       opts->verbose ? "\n" : "\r");
+                               if (!opts->quiet)
+                                       fprintf(stderr, "Rebasing (%d/%d)%s",
+                                               todo_list->done_nr,
+                                               todo_list->total_nr,
+                                               opts->verbose ? "\n" : "\r");
                        }
                        unlink(rebase_path_message());
                        unlink(rebase_path_author_script());
@@ -3592,13 +3800,14 @@ static int pick_commits(struct repository *r,
                        res = do_exec(r, arg);
                        *end_of_arg = saved;
 
-                       /* Reread the todo file if it has changed. */
-                       if (res)
-                               ; /* fall through */
-                       else if (stat(get_todo_path(opts), &st))
+                       if (res) {
+                               if (opts->reschedule_failed_exec)
+                                       reschedule = 1;
+                       else if (stat(get_todo_path(opts), &st))
                                res = error_errno(_("could not stat '%s'"),
                                                  get_todo_path(opts));
                        else if (match_stat_data(&todo_list->stat, &st)) {
+                               /* Reread the todo file if it has changed. */
                                todo_list_release(todo_list);
                                if (read_populate_todo(r, todo_list, opts))
                                        res = -1; /* message was printed */
@@ -3736,6 +3945,7 @@ static int pick_commits(struct repository *r,
                                hook.in = open(rebase_path_rewritten_list(),
                                        O_RDONLY);
                                hook.stdout_to_stderr = 1;
+                               hook.trace2_hook_name = "post-rewrite";
                                argv_array_push(&hook.args, post_rewrite_hook);
                                argv_array_push(&hook.args, "rebase");
                                /* we don't care if this hook failed */
@@ -3744,8 +3954,10 @@ static int pick_commits(struct repository *r,
                }
                apply_autostash(opts);
 
-               fprintf(stderr, "Successfully rebased and updated %s.\n",
-                       head_ref.buf);
+               if (!opts->quiet)
+                       fprintf(stderr,
+                               "Successfully rebased and updated %s.\n",
+                               head_ref.buf);
 
                strbuf_release(&buf);
                strbuf_release(&head_ref);
@@ -3936,7 +4148,7 @@ int sequencer_continue(struct repository *r, struct replay_opts *opts)
                                goto release_todo_list;
                }
                if (index_differs_from(r, "HEAD", NULL, 0)) {
-                       res = error_dirty_index(r->index, opts);
+                       res = error_dirty_index(r, opts);
                        goto release_todo_list;
                }
                todo_list.current++;
@@ -4046,8 +4258,7 @@ void append_signoff(struct strbuf *msgbuf, size_t ignore_footer, unsigned flag)
        int has_footer;
 
        strbuf_addstr(&sob, sign_off_header);
-       strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"),
-                               getenv("GIT_COMMITTER_EMAIL")));
+       strbuf_addstr(&sob, fmt_name(WANT_COMMITTER_IDENT));
        strbuf_addch(&sob, '\n');
 
        if (!ignore_footer)
@@ -4632,29 +4843,6 @@ int todo_list_write_to_file(struct repository *r, struct todo_list *todo_list,
        return res;
 }
 
-int transform_todo_file(struct repository *r, unsigned flags)
-{
-       const char *todo_file = rebase_path_todo();
-       struct todo_list todo_list = TODO_LIST_INIT;
-       int res;
-
-       if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
-               return error_errno(_("could not read '%s'."), todo_file);
-
-       if (todo_list_parse_insn_buffer(r, todo_list.buf.buf, &todo_list)) {
-               todo_list_release(&todo_list);
-               return error(_("unusable todo list: '%s'"), todo_file);
-       }
-
-       res = todo_list_write_to_file(r, &todo_list, todo_file,
-                                     NULL, NULL, -1, flags);
-       todo_list_release(&todo_list);
-
-       if (res)
-               return error_errno(_("could not write '%s'."), todo_file);
-       return 0;
-}
-
 static const char edit_todo_list_advice[] =
 N_("You can fix this with 'git rebase --edit-todo' "
 "and then run 'git rebase --continue'.\n"
@@ -4739,15 +4927,16 @@ static int skip_unnecessary_picks(struct repository *r,
 
 int complete_action(struct repository *r, struct replay_opts *opts, unsigned flags,
                    const char *shortrevisions, const char *onto_name,
-                   const char *onto, const char *orig_head, struct string_list *commands,
-                   unsigned autosquash, struct todo_list *todo_list)
+                   struct commit *onto, const char *orig_head,
+                   struct string_list *commands, unsigned autosquash,
+                   struct todo_list *todo_list)
 {
        const char *shortonto, *todo_file = rebase_path_todo();
        struct todo_list new_todo = TODO_LIST_INIT;
        struct strbuf *buf = &todo_list->buf;
-       struct object_id oid;
+       struct object_id oid = onto->object.oid;
+       int res;
 
-       get_oid(onto, &oid);
        shortonto = find_unique_abbrev(&oid, DEFAULT_ABBREV);
 
        if (buf->len == 0) {
@@ -4770,24 +4959,16 @@ int complete_action(struct repository *r, struct replay_opts *opts, unsigned fla
                return error(_("nothing to do"));
        }
 
-       if (todo_list_write_to_file(r, todo_list, todo_file,
-                                   shortrevisions, shortonto, -1,
-                                   flags | TODO_LIST_SHORTEN_IDS | TODO_LIST_APPEND_TODO_HELP))
-               return error_errno(_("could not write '%s'"), todo_file);
-
-       if (copy_file(rebase_path_todo_backup(), todo_file, 0666))
-               return error(_("could not copy '%s' to '%s'."), todo_file,
-                            rebase_path_todo_backup());
-
-       if (launch_sequence_editor(todo_file, &new_todo.buf, NULL)) {
+       res = edit_todo_list(r, todo_list, &new_todo, shortrevisions,
+                            shortonto, flags);
+       if (res == -1)
+               return -1;
+       else if (res == -2) {
                apply_autostash(opts);
                sequencer_remove_state(opts);
 
                return -1;
-       }
-
-       strbuf_stripspace(&new_todo.buf, 1);
-       if (new_todo.buf.len == 0) {
+       } else if (res == -3) {
                apply_autostash(opts);
                sequencer_remove_state(opts);
                todo_list_release(&new_todo);
@@ -4798,7 +4979,7 @@ int complete_action(struct repository *r, struct replay_opts *opts, unsigned fla
        if (todo_list_parse_insn_buffer(r, new_todo.buf.buf, &new_todo) ||
            todo_list_check(todo_list, &new_todo)) {
                fprintf(stderr, _(edit_todo_list_advice));
-               checkout_onto(opts, onto_name, onto, orig_head);
+               checkout_onto(r, opts, onto_name, &onto->object.oid, orig_head);
                todo_list_release(&new_todo);
 
                return -1;
@@ -4817,7 +4998,7 @@ int complete_action(struct repository *r, struct replay_opts *opts, unsigned fla
 
        todo_list_release(&new_todo);
 
-       if (checkout_onto(opts, onto_name, oid_to_hex(&oid), orig_head))
+       if (checkout_onto(r, opts, onto_name, &oid, orig_head))
                return -1;
 
        if (require_clean_work_tree(r, "rebase", "", 1, 1))