Merge branch 'pw/clean-sequencer-state-upon-final-commit'
authorJunio C Hamano <gitster@pobox.com>
Mon, 13 May 2019 14:50:35 +0000 (23:50 +0900)
committerJunio C Hamano <gitster@pobox.com>
Mon, 13 May 2019 14:50:35 +0000 (23:50 +0900)
"git chery-pick" (and "revert" that shares the same runtime engine)
that deals with multiple commits got confused when the final step
gets stopped with a conflict and the user concluded the sequence
with "git commit". Attempt to fix it by cleaning up the state
files used by these commands in such a situation.

* pw/clean-sequencer-state-upon-final-commit:
fix cherry-pick/revert status after commit
commit/reset: try to clean up sequencer state

1  2 
builtin/commit.c
sequencer.c
sequencer.h
t/t3507-cherry-pick-conflict.sh
wt-status.c
diff --combined builtin/commit.c
index bd4b180c43f51b73da04894f88754b843cd2d0d1,9df3414d80509a3cb961a157a30413a15ba0e4b0..1c9e8e2228c7ce58375bc247c4ad5850a1bd7d2d
@@@ -235,7 -235,7 +235,7 @@@ static int commit_index_files(void
   * and return the paths that match the given pattern in list.
   */
  static int list_paths(struct string_list *list, const char *with_tree,
 -                    const char *prefix, const struct pathspec *pattern)
 +                    const struct pathspec *pattern)
  {
        int i, ret;
        char *m;
                        item->util = item; /* better a valid pointer than a fake one */
        }
  
 -      ret = report_path_error(m, pattern, prefix);
 +      ret = report_path_error(m, pattern);
        free(m);
        return ret;
  }
@@@ -454,7 -454,7 +454,7 @@@ static const char *prepare_index(int ar
                        die(_("cannot do a partial commit during a cherry-pick."));
        }
  
 -      if (list_paths(&partial, !current_head ? NULL : "HEAD", prefix, &pathspec))
 +      if (list_paths(&partial, !current_head ? NULL : "HEAD", &pathspec))
                exit(1);
  
        discard_cache();
@@@ -609,8 -609,7 +609,8 @@@ static void determine_author_info(struc
                set_ident_var(&date, strbuf_detach(&date_buf, NULL));
        }
  
 -      strbuf_addstr(author_ident, fmt_ident(name, email, date, IDENT_STRICT));
 +      strbuf_addstr(author_ident, fmt_ident(name, email, WANT_AUTHOR_IDENT, date,
 +                              IDENT_STRICT));
        assert_split_ident(&author, author_ident);
        export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0);
        export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0);
@@@ -668,7 -667,6 +668,7 @@@ static int prepare_to_commit(const cha
        const char *hook_arg2 = NULL;
        int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE);
        int old_display_comment_prefix;
 +      int merge_contains_scissors = 0;
  
        /* This checks and barfs if author is badly specified */
        determine_author_info(author_ident);
                        strbuf_addbuf(&sb, &message);
                hook_arg1 = "message";
        } else if (!stat(git_path_merge_msg(the_repository), &statbuf)) {
 +              size_t merge_msg_start;
 +
                /*
                 * prepend SQUASH_MSG here if it exists and a
                 * "merge --squash" was originally performed
                        hook_arg1 = "squash";
                } else
                        hook_arg1 = "merge";
 +
 +              merge_msg_start = sb.len;
                if (strbuf_read_file(&sb, git_path_merge_msg(the_repository), 0) < 0)
                        die_errno(_("could not read MERGE_MSG"));
 +
 +              if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
 +                  wt_status_locate_end(sb.buf + merge_msg_start,
 +                                       sb.len - merge_msg_start) <
 +                              sb.len - merge_msg_start)
 +                      merge_contains_scissors = 1;
        } else if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
                if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
                        die_errno(_("could not read SQUASH_MSG"));
                struct ident_split ci, ai;
  
                if (whence != FROM_COMMIT) {
 -                      if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
 +                      if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
 +                              !merge_contains_scissors)
                                wt_status_add_cut_line(s->fp);
                        status_printf_ln(s, GIT_COLOR_NORMAL,
                            whence == FROM_MERGE
                                _("Please enter the commit message for your changes."
                                  " Lines starting\nwith '%c' will be ignored, and an empty"
                                  " message aborts the commit.\n"), comment_line_char);
 -              else if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
 -                       whence == FROM_COMMIT)
 -                      wt_status_add_cut_line(s->fp);
 -              else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
 +              else if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
 +                      if (whence == FROM_COMMIT && !merge_contains_scissors)
 +                              wt_status_add_cut_line(s->fp);
 +              else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
                        status_printf(s, GIT_COLOR_NORMAL,
                                _("Please enter the commit message for your changes."
                                  " Lines starting\n"
@@@ -1051,10 -1038,6 +1051,10 @@@ static void handle_untracked_files_arg(
                s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
        else if (!strcmp(untracked_files_arg, "all"))
                s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
 +      /*
 +       * Please update $__git_untracked_file_modes in
 +       * git-completion.bash when you add new options
 +       */
        else
                die(_("Invalid untracked files mode '%s'"), untracked_files_arg);
  }
@@@ -1184,13 -1167,25 +1184,13 @@@ static int parse_and_validate_options(i
                die(_("Only one of --include/--only/--all/--interactive/--patch can be used."));
        if (argc == 0 && (also || (only && !amend && !allow_empty)))
                die(_("No paths with --include/--only does not make sense."));
 -      if (!cleanup_arg || !strcmp(cleanup_arg, "default"))
 -              cleanup_mode = use_editor ? COMMIT_MSG_CLEANUP_ALL :
 -                                          COMMIT_MSG_CLEANUP_SPACE;
 -      else if (!strcmp(cleanup_arg, "verbatim"))
 -              cleanup_mode = COMMIT_MSG_CLEANUP_NONE;
 -      else if (!strcmp(cleanup_arg, "whitespace"))
 -              cleanup_mode = COMMIT_MSG_CLEANUP_SPACE;
 -      else if (!strcmp(cleanup_arg, "strip"))
 -              cleanup_mode = COMMIT_MSG_CLEANUP_ALL;
 -      else if (!strcmp(cleanup_arg, "scissors"))
 -              cleanup_mode = use_editor ? COMMIT_MSG_CLEANUP_SCISSORS :
 -                                          COMMIT_MSG_CLEANUP_SPACE;
 -      else
 -              die(_("Invalid cleanup mode %s"), cleanup_arg);
 +      cleanup_mode = get_cleanup_mode(cleanup_arg, use_editor);
  
        handle_untracked_files_arg(s);
  
        if (all && argc > 0)
 -              die(_("Paths with -a does not make sense."));
 +              die(_("paths '%s ...' with -a does not make sense"),
 +                  argv[0]);
  
        if (status_format != STATUS_FORMAT_NONE)
                dry_run = 1;
@@@ -1486,7 -1481,7 +1486,7 @@@ int cmd_commit(int argc, const char **a
                OPT_BOOL('s', "signoff", &signoff, N_("add Signed-off-by:")),
                OPT_FILENAME('t', "template", &template_file, N_("use specified template file")),
                OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")),
 -              OPT_STRING(0, "cleanup", &cleanup_arg, N_("default"), N_("how to strip spaces and #comments from message")),
 +              OPT_CLEANUP(&cleanup_arg),
                OPT_BOOL(0, "status", &include_status, N_("include status in commit message template")),
                { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
                  N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
                die(_("could not read commit message: %s"), strerror(saved_errno));
        }
  
 -      if (verbose || /* Truncate the message just before the diff, if any. */
 -          cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
 -              strbuf_setlen(&sb, wt_status_locate_end(sb.buf, sb.len));
 -      if (cleanup_mode != COMMIT_MSG_CLEANUP_NONE)
 -              strbuf_stripspace(&sb, cleanup_mode == COMMIT_MSG_CLEANUP_ALL);
 +      cleanup_message(&sb, cleanup_mode, verbose);
  
        if (message_is_empty(&sb, cleanup_mode) && !allow_empty_message) {
                rollback_index_files();
                die("%s", err.buf);
        }
  
-       unlink(git_path_cherry_pick_head(the_repository));
-       unlink(git_path_revert_head(the_repository));
+       sequencer_post_commit_cleanup(the_repository);
        unlink(git_path_merge_head(the_repository));
        unlink(git_path_merge_msg(the_repository));
        unlink(git_path_merge_mode(the_repository));
diff --combined sequencer.c
index 72a1bc5fc56e108fb9e2e89a507b9c7c39c39a40,c6a9a354222d8f9c2b65c703d58b9e3a52419387..f88a97fb10a322c21062ec8c102482677cfd0feb
@@@ -55,7 -55,8 +55,7 @@@ static GIT_PATH_FUNC(rebase_path, "reba
   * file and written to the tail of 'done'.
   */
  GIT_PATH_FUNC(rebase_path_todo, "rebase-merge/git-rebase-todo")
 -static GIT_PATH_FUNC(rebase_path_todo_backup,
 -                   "rebase-merge/git-rebase-todo.backup")
 +GIT_PATH_FUNC(rebase_path_todo_backup, "rebase-merge/git-rebase-todo.backup")
  
  /*
   * The rebase command lines that have already been processed. A line
@@@ -171,22 -172,17 +171,22 @@@ static int git_sequencer_config(const c
                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;
@@@ -388,8 -384,8 +388,8 @@@ static void print_advice(struct reposit
        }
  }
  
 -int write_message(const void *buf, size_t len, const char *filename,
 -                int append_eol)
 +static int write_message(const void *buf, size_t len, const char *filename,
 +                       int append_eol)
  {
        struct lock_file msg_file = LOCK_INIT;
  
@@@ -515,54 -511,11 +515,54 @@@ static int fast_forward_to(struct repos
        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;) {
@@@ -630,8 -583,7 +630,8 @@@ static int do_recursive_merge(struct re
                        _(action_name(opts)));
  
        if (!clean)
 -              append_conflicts_hint(r->index, msgbuf);
 +              append_conflicts_hint(r->index, msgbuf,
 +                                    opts->default_msg_cleanup);
  
        return !clean;
  }
@@@ -885,7 -837,7 +885,7 @@@ static const char *read_author_ident(st
        }
  
        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);
@@@ -950,6 -902,7 +950,6 @@@ static int run_git_commit(struct reposi
                          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;
                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))
@@@ -1060,16 -1013,6 +1060,16 @@@ static int rest_is_empty(const struct s
        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.
@@@ -1160,7 -1103,6 +1160,7 @@@ static int run_rewrite_hook(const struc
        proc.argv = argv;
        proc.in = -1;
        proc.stdout_to_stderr = 1;
 +      proc.trace2_hook_name = "post-rewrite";
  
        code = start_command(&proc);
        if (code)
@@@ -1440,13 -1382,8 +1440,13 @@@ static int try_to_commit(struct reposit
                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);
@@@ -1575,6 -1512,32 +1575,6 @@@ static int allow_empty(struct repositor
                return 1;
  }
  
 -/*
 - * Note that ordering matters in this enum. Not only must it match the mapping
 - * below, it is also divided into several sections that matter.  When adding
 - * new commands, make sure you add it in the right section.
 - */
 -enum todo_command {
 -      /* commands that handle commits */
 -      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,
 -      TODO_BREAK,
 -      TODO_LABEL,
 -      TODO_RESET,
 -      TODO_MERGE,
 -      /* commands that do nothing but are counted for reporting progress */
 -      TODO_NOOP,
 -      TODO_DROP,
 -      /* comments (not counted for reporting progress) */
 -      TODO_COMMENT
 -};
 -
  static struct {
        char c;
        const char *str;
@@@ -2057,7 -2020,26 +2057,7 @@@ enum todo_item_flags 
        TODO_EDIT_MERGE_MSG = 1
  };
  
 -struct todo_item {
 -      enum todo_command command;
 -      struct commit *commit;
 -      unsigned int flags;
 -      const char *arg;
 -      int arg_len;
 -      size_t offset_in_buf;
 -};
 -
 -struct todo_list {
 -      struct strbuf buf;
 -      struct todo_item *items;
 -      int nr, alloc, current;
 -      int done_nr, total_nr;
 -      struct stat_data stat;
 -};
 -
 -#define TODO_LIST_INIT { STRBUF_INIT }
 -
 -static void todo_list_release(struct todo_list *todo_list)
 +void todo_list_release(struct todo_list *todo_list)
  {
        strbuf_release(&todo_list->buf);
        FREE_AND_NULL(todo_list->items);
@@@ -2070,14 -2052,8 +2070,14 @@@ static struct todo_item *append_new_tod
        return todo_list->items + todo_list->nr++;
  }
  
 +const char *todo_item_get_arg(struct todo_list *todo_list,
 +                            struct todo_item *item)
 +{
 +      return todo_list->buf.buf + item->arg_offset;
 +}
 +
  static int parse_insn_line(struct repository *r, struct todo_item *item,
 -                         const char *bol, char *eol)
 +                         const char *buf, const char *bol, char *eol)
  {
        struct object_id commit_oid;
        char *end_of_object_name;
        if (bol == eol || *bol == '\r' || *bol == comment_line_char) {
                item->command = TODO_COMMENT;
                item->commit = NULL;
 -              item->arg = bol;
 +              item->arg_offset = bol - buf;
                item->arg_len = eol - bol;
                return 0;
        }
                        return error(_("%s does not accept arguments: '%s'"),
                                     command_to_string(item->command), bol);
                item->commit = NULL;
 -              item->arg = bol;
 +              item->arg_offset = bol - buf;
                item->arg_len = eol - bol;
                return 0;
        }
        if (item->command == TODO_EXEC || item->command == TODO_LABEL ||
            item->command == TODO_RESET) {
                item->commit = NULL;
 -              item->arg = bol;
 +              item->arg_offset = bol - buf;
                item->arg_len = (int)(eol - bol);
                return 0;
        }
                } else {
                        item->flags |= TODO_EDIT_MERGE_MSG;
                        item->commit = NULL;
 -                      item->arg = bol;
 +                      item->arg_offset = bol - buf;
                        item->arg_len = (int)(eol - bol);
                        return 0;
                }
        status = get_oid(bol, &commit_oid);
        *end_of_object_name = saved;
  
 -      item->arg = end_of_object_name + strspn(end_of_object_name, " \t");
 -      item->arg_len = (int)(eol - item->arg);
 +      bol = end_of_object_name + strspn(end_of_object_name, " \t");
 +      item->arg_offset = bol - buf;
 +      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;
  }
  
 -      if (parse_insn_line(r, &item, buf.buf, eol))
+ 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 */
 -static int parse_insn_buffer(struct repository *r, char *buf,
 -                           struct todo_list *todo_list)
++      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)
  {
        struct todo_item *item;
        char *p = buf, *next_p;
        int i, res = 0, fixup_okay = file_exists(rebase_path_done());
  
 +      todo_list->current = todo_list->nr = 0;
 +
        for (i = 1; *p; i++, p = next_p) {
                char *eol = strchrnul(p, '\n');
  
  
                item = append_new_todo(todo_list);
                item->offset_in_buf = p - todo_list->buf.buf;
 -              if (parse_insn_line(r, item, p, eol)) {
 +              if (parse_insn_line(r, item, buf, p, eol)) {
                        res = error(_("invalid line %d: %.*s"),
                                i, (int)(eol - p), p);
 -                      item->command = TODO_NOOP;
 +                      item->command = TODO_COMMENT + 1;
 +                      item->arg_offset = p - buf;
 +                      item->arg_len = (int)(eol - p);
 +                      item->commit = NULL;
                }
  
                if (fixup_okay)
@@@ -2251,6 -2255,57 +2286,57 @@@ static ssize_t strbuf_read_file_or_whin
        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)
                return error(_("could not stat '%s'"), todo_file);
        fill_stat_data(&todo_list->stat, &st);
  
 -      res = parse_insn_buffer(r, todo_list->buf.buf, todo_list);
 +      res = todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list);
        if (res) {
                if (is_rebase_i(opts))
                        return error(_("please fix this using "
                FILE *f = fopen_or_warn(rebase_path_msgtotal(), "w");
  
                if (strbuf_read_file(&done.buf, rebase_path_done(), 0) > 0 &&
 -                  !parse_insn_buffer(r, done.buf.buf, &done))
 +                  !todo_list_parse_insn_buffer(r, done.buf.buf, &done))
                        todo_list->done_nr = count_commands(&done);
                else
                        todo_list->done_nr = 0;
@@@ -2338,15 -2393,6 +2424,15 @@@ static int populate_opts_cb(const char 
                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"))
                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)
@@@ -2494,15 -2537,14 +2580,15 @@@ static void write_strategy_opts(struct 
  }
  
  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);
  
@@@ -2549,7 -2591,7 +2635,7 @@@ static int walk_revs_populate_todo(stru
  
                item->command = command;
                item->commit = commit;
 -              item->arg = NULL;
 +              item->arg_offset = 0;
                item->arg_len = 0;
                item->offset_in_buf = todo_list->buf.len;
                subject_len = find_commit_subject(commit_buffer, &subject);
@@@ -2744,59 -2786,36 +2830,59 @@@ static int save_opts(struct replay_opt
        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;
  }
  
@@@ -3518,11 -3537,10 +3604,11 @@@ static const char *reflog_message(struc
        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;
  
        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;
        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"));
@@@ -3618,8 -3630,6 +3704,8 @@@ static int pick_commits(struct reposito
  
        while (todo_list->current < todo_list->nr) {
                struct todo_item *item = todo_list->items + todo_list->current;
 +              const char *arg = todo_item_get_arg(todo_list, item);
 +
                if (save_todo(todo_list, opts))
                        return -1;
                if (is_rebase_i(opts)) {
                                        fprintf(stderr,
                                                _("Stopped at %s...  %.*s\n"),
                                                short_commit_name(commit),
 -                                              item->arg_len, item->arg);
 +                                              item->arg_len, arg);
                                return error_with_patch(r, commit,
 -                                      item->arg, item->arg_len, opts, res,
 -                                      !res);
 +                                      arg, item->arg_len, opts, res, !res);
                        }
                        if (is_rebase_i(opts) && !res)
                                record_in_rewritten(&item->commit->object.oid,
                                if (res == 1)
                                        intend_to_amend();
                                return error_failed_squash(r, item->commit, opts,
 -                                      item->arg_len, item->arg);
 +                                      item->arg_len, arg);
                        } else if (res && is_rebase_i(opts) && item->commit) {
                                int to_amend = 0;
                                struct object_id oid;
                                        to_amend = 1;
  
                                return res | error_with_patch(r, item->commit,
 -                                              item->arg, item->arg_len, opts,
 +                                              arg, item->arg_len, opts,
                                                res, to_amend);
                        }
                } else if (item->command == TODO_EXEC) {
 -                      char *end_of_arg = (char *)(item->arg + item->arg_len);
 +                      char *end_of_arg = (char *)(arg + item->arg_len);
                        int saved = *end_of_arg;
                        struct stat st;
  
                        *end_of_arg = '\0';
 -                      res = do_exec(r, item->arg);
 +                      res = do_exec(r, arg);
                        *end_of_arg = saved;
  
 -                      /* Reread the todo file if it has changed. */
                        if (res) {
                                if (opts->reschedule_failed_exec)
                                        reschedule = 1;
                                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 */
                                todo_list->current = -1;
                        }
                } else if (item->command == TODO_LABEL) {
 -                      if ((res = do_label(r, item->arg, item->arg_len)))
 +                      if ((res = do_label(r, arg, item->arg_len)))
                                reschedule = 1;
                } else if (item->command == TODO_RESET) {
 -                      if ((res = do_reset(r, item->arg, item->arg_len, opts)))
 +                      if ((res = do_reset(r, arg, item->arg_len, opts)))
                                reschedule = 1;
                } else if (item->command == TODO_MERGE) {
                        if ((res = do_merge(r, item->commit,
 -                                          item->arg, item->arg_len,
 +                                          arg, item->arg_len,
                                            item->flags, opts)) < 0)
                                reschedule = 1;
                        else if (item->commit)
                        if (res > 0)
                                /* failed with merge conflicts */
                                return error_with_patch(r, item->commit,
 -                                                      item->arg,
 -                                                      item->arg_len, opts,
 -                                                      res, 0);
 +                                                      arg, item->arg_len,
 +                                                      opts, res, 0);
                } else if (!is_noop(item->command))
                        return error(_("unknown command %d"), item->command);
  
                        if (item->commit)
                                return error_with_patch(r,
                                                        item->commit,
 -                                                      item->arg,
 -                                                      item->arg_len, opts,
 -                                                      res, 0);
 +                                                      arg, item->arg_len,
 +                                                      opts, res, 0);
                }
  
                todo_list->current++;
@@@ -3859,7 -3872,6 +3945,7 @@@ cleanup_head_ref
                                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 */
@@@ -4172,7 -4184,8 +4258,7 @@@ void append_signoff(struct strbuf *msgb
        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)
@@@ -4339,7 -4352,7 +4425,7 @@@ static const char *label_oid(struct obj
  }
  
  static int make_script_with_merges(struct pretty_print_context *pp,
 -                                 struct rev_info *revs, FILE *out,
 +                                 struct rev_info *revs, struct strbuf *out,
                                   unsigned flags)
  {
        int keep_empty = flags & TODO_LIST_KEEP_EMPTY;
         * gathering commits not yet shown, reversing the list on the fly,
         * then outputting that list (labeling revisions as needed).
         */
 -      fprintf(out, "%s onto\n", cmd_label);
 +      strbuf_addf(out, "%s onto\n", cmd_label);
        for (iter = tips; iter; iter = iter->next) {
                struct commit_list *list = NULL, *iter2;
  
                entry = oidmap_get(&state.commit2label, &commit->object.oid);
  
                if (entry)
 -                      fprintf(out, "\n%c Branch %s\n", comment_line_char, entry->string);
 +                      strbuf_addf(out, "\n%c Branch %s\n", comment_line_char, entry->string);
                else
 -                      fprintf(out, "\n");
 +                      strbuf_addch(out, '\n');
  
                while (oidset_contains(&interesting, &commit->object.oid) &&
                       !oidset_contains(&shown, &commit->object.oid)) {
                }
  
                if (!commit)
 -                      fprintf(out, "%s %s\n", cmd_reset,
 -                              rebase_cousins ? "onto" : "[new root]");
 +                      strbuf_addf(out, "%s %s\n", cmd_reset,
 +                                  rebase_cousins ? "onto" : "[new root]");
                else {
                        const char *to = NULL;
  
                                               &state);
  
                        if (!to || !strcmp(to, "onto"))
 -                              fprintf(out, "%s onto\n", cmd_reset);
 +                              strbuf_addf(out, "%s onto\n", cmd_reset);
                        else {
                                strbuf_reset(&oneline);
                                pretty_print_commit(pp, commit, &oneline);
 -                              fprintf(out, "%s %s # %s\n",
 -                                      cmd_reset, to, oneline.buf);
 +                              strbuf_addf(out, "%s %s # %s\n",
 +                                          cmd_reset, to, oneline.buf);
                        }
                }
  
                        entry = oidmap_get(&commit2todo, oid);
                        /* only show if not already upstream */
                        if (entry)
 -                              fprintf(out, "%s\n", entry->string);
 +                              strbuf_addf(out, "%s\n", entry->string);
                        entry = oidmap_get(&state.commit2label, oid);
                        if (entry)
 -                              fprintf(out, "%s %s\n",
 -                                      cmd_label, entry->string);
 +                              strbuf_addf(out, "%s %s\n",
 +                                          cmd_label, entry->string);
                        oidset_insert(&shown, oid);
                }
  
        return 0;
  }
  
 -int sequencer_make_script(struct repository *r, FILE *out,
 -                        int argc, const char **argv,
 -                        unsigned flags)
 +int sequencer_make_script(struct repository *r, struct strbuf *out, int argc,
 +                        const char **argv, unsigned flags)
  {
        char *format = NULL;
        struct pretty_print_context pp = {0};
 -      struct strbuf buf = STRBUF_INIT;
        struct rev_info revs;
        struct commit *commit;
        int keep_empty = flags & TODO_LIST_KEEP_EMPTY;
  
                if (!is_empty && (commit->object.flags & PATCHSAME))
                        continue;
 -              strbuf_reset(&buf);
                if (!keep_empty && is_empty)
 -                      strbuf_addf(&buf, "%c ", comment_line_char);
 -              strbuf_addf(&buf, "%s %s ", insn,
 +                      strbuf_addf(out, "%c ", comment_line_char);
 +              strbuf_addf(out, "%s %s ", insn,
                            oid_to_hex(&commit->object.oid));
 -              pretty_print_commit(&pp, commit, &buf);
 -              strbuf_addch(&buf, '\n');
 -              fputs(buf.buf, out);
 +              pretty_print_commit(&pp, commit, out);
 +              strbuf_addch(out, '\n');
        }
 -      strbuf_release(&buf);
        return 0;
  }
  
   * Add commands after pick and (series of) squash/fixup commands
   * in the todo list.
   */
 -int sequencer_add_exec_commands(struct repository *r,
 -                              const char *commands)
 +void todo_list_add_exec_commands(struct todo_list *todo_list,
 +                               struct string_list *commands)
  {
 -      const char *todo_file = rebase_path_todo();
 -      struct todo_list todo_list = TODO_LIST_INIT;
 -      struct strbuf *buf = &todo_list.buf;
 -      size_t offset = 0, commands_len = strlen(commands);
 -      int i, insert;
 +      struct strbuf *buf = &todo_list->buf;
 +      size_t base_offset = buf->len;
 +      int i, insert, nr = 0, alloc = 0;
 +      struct todo_item *items = NULL, *base_items = NULL;
  
 -      if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
 -              return error(_("could not read '%s'."), todo_file);
 +      base_items = xcalloc(commands->nr, sizeof(struct todo_item));
 +      for (i = 0; i < commands->nr; i++) {
 +              size_t command_len = strlen(commands->items[i].string);
  
 -      if (parse_insn_buffer(r, todo_list.buf.buf, &todo_list)) {
 -              todo_list_release(&todo_list);
 -              return error(_("unusable todo list: '%s'"), todo_file);
 +              strbuf_addstr(buf, commands->items[i].string);
 +              strbuf_addch(buf, '\n');
 +
 +              base_items[i].command = TODO_EXEC;
 +              base_items[i].offset_in_buf = base_offset;
 +              base_items[i].arg_offset = base_offset + strlen("exec ");
 +              base_items[i].arg_len = command_len - strlen("exec ");
 +
 +              base_offset += command_len + 1;
        }
  
        /*
         * Insert <commands> after every pick. Here, fixup/squash chains
         * are considered part of the pick, so we insert the commands *after*
         * those chains if there are any.
 +       *
 +       * As we insert the exec commands immediatly after rearranging
 +       * any fixups and before the user edits the list, a fixup chain
 +       * can never contain comments (any comments are empty picks that
 +       * have been commented out because the user did not specify
 +       * --keep-empty).  So, it is safe to insert an exec command
 +       * without looking at the command following a comment.
         */
 -      insert = -1;
 -      for (i = 0; i < todo_list.nr; i++) {
 -              enum todo_command command = todo_list.items[i].command;
 -
 -              if (insert >= 0) {
 -                      /* skip fixup/squash chains */
 -                      if (command == TODO_COMMENT)
 -                              continue;
 -                      else if (is_fixup(command)) {
 -                              insert = i + 1;
 -                              continue;
 -                      }
 -                      strbuf_insert(buf,
 -                                    todo_list.items[insert].offset_in_buf +
 -                                    offset, commands, commands_len);
 -                      offset += commands_len;
 -                      insert = -1;
 +      insert = 0;
 +      for (i = 0; i < todo_list->nr; i++) {
 +              enum todo_command command = todo_list->items[i].command;
 +              if (insert && !is_fixup(command)) {
 +                      ALLOC_GROW(items, nr + commands->nr, alloc);
 +                      COPY_ARRAY(items + nr, base_items, commands->nr);
 +                      nr += commands->nr;
 +
 +                      insert = 0;
                }
  
 +              ALLOC_GROW(items, nr + 1, alloc);
 +              items[nr++] = todo_list->items[i];
 +
                if (command == TODO_PICK || command == TODO_MERGE)
 -                      insert = i + 1;
 +                      insert = 1;
        }
  
        /* insert or append final <commands> */
 -      if (insert >= 0 && insert < todo_list.nr)
 -              strbuf_insert(buf, todo_list.items[insert].offset_in_buf +
 -                            offset, commands, commands_len);
 -      else if (insert >= 0 || !offset)
 -              strbuf_add(buf, commands, commands_len);
 +      if (insert || nr == todo_list->nr) {
 +              ALLOC_GROW(items, nr + commands->nr, alloc);
 +              COPY_ARRAY(items + nr, base_items, commands->nr);
 +              nr += commands->nr;
 +      }
  
 -      i = write_message(buf->buf, buf->len, todo_file, 0);
 -      todo_list_release(&todo_list);
 -      return i;
 +      free(base_items);
 +      FREE_AND_NULL(todo_list->items);
 +      todo_list->items = items;
 +      todo_list->nr = nr;
 +      todo_list->alloc = alloc;
  }
  
 -int transform_todos(struct repository *r, unsigned flags)
 +static void todo_list_to_strbuf(struct repository *r, struct todo_list *todo_list,
 +                              struct strbuf *buf, int num, unsigned flags)
  {
 -      const char *todo_file = rebase_path_todo();
 -      struct todo_list todo_list = TODO_LIST_INIT;
 -      struct strbuf buf = STRBUF_INIT;
        struct todo_item *item;
 -      int i;
 -
 -      if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
 -              return error(_("could not read '%s'."), todo_file);
 +      int i, max = todo_list->nr;
  
 -      if (parse_insn_buffer(r, todo_list.buf.buf, &todo_list)) {
 -              todo_list_release(&todo_list);
 -              return error(_("unusable todo list: '%s'"), todo_file);
 -      }
 +      if (num > 0 && num < max)
 +              max = num;
  
 -      for (item = todo_list.items, i = 0; i < todo_list.nr; i++, item++) {
 +      for (item = todo_list->items, i = 0; i < max; i++, item++) {
                /* if the item is not a command write it and continue */
                if (item->command >= TODO_COMMENT) {
 -                      strbuf_addf(&buf, "%.*s\n", item->arg_len, item->arg);
 +                      strbuf_addf(buf, "%.*s\n", item->arg_len,
 +                                  todo_item_get_arg(todo_list, item));
                        continue;
                }
  
                /* add command to the buffer */
                if (flags & TODO_LIST_ABBREVIATE_CMDS)
 -                      strbuf_addch(&buf, command_to_char(item->command));
 +                      strbuf_addch(buf, command_to_char(item->command));
                else
 -                      strbuf_addstr(&buf, command_to_string(item->command));
 +                      strbuf_addstr(buf, command_to_string(item->command));
  
                /* add commit id */
                if (item->commit) {
  
                        if (item->command == TODO_MERGE) {
                                if (item->flags & TODO_EDIT_MERGE_MSG)
 -                                      strbuf_addstr(&buf, " -c");
 +                                      strbuf_addstr(buf, " -c");
                                else
 -                                      strbuf_addstr(&buf, " -C");
 +                                      strbuf_addstr(buf, " -C");
                        }
  
 -                      strbuf_addf(&buf, " %s", oid);
 +                      strbuf_addf(buf, " %s", oid);
                }
  
                /* add all the rest */
                if (!item->arg_len)
 -                      strbuf_addch(&buf, '\n');
 +                      strbuf_addch(buf, '\n');
                else
 -                      strbuf_addf(&buf, " %.*s\n", item->arg_len, item->arg);
 +                      strbuf_addf(buf, " %.*s\n", item->arg_len,
 +                                  todo_item_get_arg(todo_list, item));
        }
 -
 -      i = write_message(buf.buf, buf.len, todo_file, 0);
 -      todo_list_release(&todo_list);
 -      return i;
  }
  
 -enum missing_commit_check_level get_missing_commit_check_level(void)
 +int todo_list_write_to_file(struct repository *r, struct todo_list *todo_list,
 +                          const char *file, const char *shortrevisions,
 +                          const char *shortonto, int num, unsigned flags)
  {
 -      const char *value;
 -
 -      if (git_config_get_value("rebase.missingcommitscheck", &value) ||
 -                      !strcasecmp("ignore", value))
 -              return MISSING_COMMIT_CHECK_IGNORE;
 -      if (!strcasecmp("warn", value))
 -              return MISSING_COMMIT_CHECK_WARN;
 -      if (!strcasecmp("error", value))
 -              return MISSING_COMMIT_CHECK_ERROR;
 -      warning(_("unrecognized setting %s for option "
 -                "rebase.missingCommitsCheck. Ignoring."), value);
 -      return MISSING_COMMIT_CHECK_IGNORE;
 -}
 +      int res;
 +      struct strbuf buf = STRBUF_INIT;
  
 -define_commit_slab(commit_seen, unsigned char);
 -/*
 - * Check if the user dropped some commits by mistake
 - * Behaviour determined by rebase.missingCommitsCheck.
 - * Check if there is an unrecognized command or a
 - * bad SHA-1 in a command.
 - */
 -int check_todo_list(struct repository *r)
 -{
 -      enum missing_commit_check_level check_level = get_missing_commit_check_level();
 -      struct strbuf todo_file = STRBUF_INIT;
 -      struct todo_list todo_list = TODO_LIST_INIT;
 -      struct strbuf missing = STRBUF_INIT;
 -      int advise_to_edit_todo = 0, res = 0, i;
 -      struct commit_seen commit_seen;
 +      todo_list_to_strbuf(r, todo_list, &buf, num, flags);
 +      if (flags & TODO_LIST_APPEND_TODO_HELP)
 +              append_todo_help(flags & TODO_LIST_KEEP_EMPTY, count_commands(todo_list),
 +                               shortrevisions, shortonto, &buf);
  
 -      init_commit_seen(&commit_seen);
 +      res = write_message(buf.buf, buf.len, file, 0);
 +      strbuf_release(&buf);
  
 -      strbuf_addstr(&todo_file, rebase_path_todo());
 -      if (strbuf_read_file_or_whine(&todo_list.buf, todo_file.buf) < 0) {
 -              res = -1;
 -              goto leave_check;
 -      }
 -      advise_to_edit_todo = res =
 -              parse_insn_buffer(r, todo_list.buf.buf, &todo_list);
 +      return res;
 +}
  
 -      if (res || check_level == MISSING_COMMIT_CHECK_IGNORE)
 -              goto leave_check;
 +static const char edit_todo_list_advice[] =
 +N_("You can fix this with 'git rebase --edit-todo' "
 +"and then run 'git rebase --continue'.\n"
 +"Or you can abort the rebase with 'git rebase"
 +" --abort'.\n");
  
 -      /* Mark the commits in git-rebase-todo as seen */
 -      for (i = 0; i < todo_list.nr; i++) {
 -              struct commit *commit = todo_list.items[i].commit;
 -              if (commit)
 -                      *commit_seen_at(&commit_seen, commit) = 1;
 -      }
 +int check_todo_list_from_file(struct repository *r)
 +{
 +      struct todo_list old_todo = TODO_LIST_INIT, new_todo = TODO_LIST_INIT;
 +      int res = 0;
  
 -      todo_list_release(&todo_list);
 -      strbuf_addstr(&todo_file, ".backup");
 -      if (strbuf_read_file_or_whine(&todo_list.buf, todo_file.buf) < 0) {
 +      if (strbuf_read_file_or_whine(&new_todo.buf, rebase_path_todo()) < 0) {
                res = -1;
 -              goto leave_check;
 -      }
 -      strbuf_release(&todo_file);
 -      res = !!parse_insn_buffer(r, todo_list.buf.buf, &todo_list);
 -
 -      /* Find commits in git-rebase-todo.backup yet unseen */
 -      for (i = todo_list.nr - 1; i >= 0; i--) {
 -              struct todo_item *item = todo_list.items + i;
 -              struct commit *commit = item->commit;
 -              if (commit && !*commit_seen_at(&commit_seen, commit)) {
 -                      strbuf_addf(&missing, " - %s %.*s\n",
 -                                  short_commit_name(commit),
 -                                  item->arg_len, item->arg);
 -                      *commit_seen_at(&commit_seen, commit) = 1;
 -              }
 +              goto out;
        }
  
 -      /* Warn about missing commits */
 -      if (!missing.len)
 -              goto leave_check;
 -
 -      if (check_level == MISSING_COMMIT_CHECK_ERROR)
 -              advise_to_edit_todo = res = 1;
 -
 -      fprintf(stderr,
 -              _("Warning: some commits may have been dropped accidentally.\n"
 -              "Dropped commits (newer to older):\n"));
 -
 -      /* Make the list user-friendly and display */
 -      fputs(missing.buf, stderr);
 -      strbuf_release(&missing);
 -
 -      fprintf(stderr, _("To avoid this message, use \"drop\" to "
 -              "explicitly remove a commit.\n\n"
 -              "Use 'git config rebase.missingCommitsCheck' to change "
 -              "the level of warnings.\n"
 -              "The possible behaviours are: ignore, warn, error.\n\n"));
 -
 -leave_check:
 -      clear_commit_seen(&commit_seen);
 -      strbuf_release(&todo_file);
 -      todo_list_release(&todo_list);
 +      if (strbuf_read_file_or_whine(&old_todo.buf, rebase_path_todo_backup()) < 0) {
 +              res = -1;
 +              goto out;
 +      }
  
 -      if (advise_to_edit_todo)
 -              fprintf(stderr,
 -                      _("You can fix this with 'git rebase --edit-todo' "
 -                        "and then run 'git rebase --continue'.\n"
 -                        "Or you can abort the rebase with 'git rebase"
 -                        " --abort'.\n"));
 +      res = todo_list_parse_insn_buffer(r, old_todo.buf.buf, &old_todo);
 +      if (!res)
 +              res = todo_list_parse_insn_buffer(r, new_todo.buf.buf, &new_todo);
 +      if (!res)
 +              res = todo_list_check(&old_todo, &new_todo);
 +      if (res)
 +              fprintf(stderr, _(edit_todo_list_advice));
 +out:
 +      todo_list_release(&old_todo);
 +      todo_list_release(&new_todo);
  
        return res;
  }
  
 -static int rewrite_file(const char *path, const char *buf, size_t len)
 -{
 -      int rc = 0;
 -      int fd = open(path, O_WRONLY | O_TRUNC);
 -      if (fd < 0)
 -              return error_errno(_("could not open '%s' for writing"), path);
 -      if (write_in_full(fd, buf, len) < 0)
 -              rc = error_errno(_("could not write to '%s'"), path);
 -      if (close(fd) && !rc)
 -              rc = error_errno(_("could not close '%s'"), path);
 -      return rc;
 -}
 -
  /* skip picking commits whose parents are unchanged */
 -static int skip_unnecessary_picks(struct repository *r, struct object_id *output_oid)
 +static int skip_unnecessary_picks(struct repository *r,
 +                                struct todo_list *todo_list,
 +                                struct object_id *base_oid)
  {
 -      const char *todo_file = rebase_path_todo();
 -      struct strbuf buf = STRBUF_INIT;
 -      struct todo_list todo_list = TODO_LIST_INIT;
        struct object_id *parent_oid;
 -      int fd, i;
 -
 -      if (!read_oneliner(&buf, rebase_path_onto(), 0))
 -              return error(_("could not read 'onto'"));
 -      if (get_oid(buf.buf, output_oid)) {
 -              strbuf_release(&buf);
 -              return error(_("need a HEAD to fixup"));
 -      }
 -      strbuf_release(&buf);
 -
 -      if (strbuf_read_file_or_whine(&todo_list.buf, todo_file) < 0)
 -              return -1;
 -      if (parse_insn_buffer(r, todo_list.buf.buf, &todo_list) < 0) {
 -              todo_list_release(&todo_list);
 -              return -1;
 -      }
 +      int i;
  
 -      for (i = 0; i < todo_list.nr; i++) {
 -              struct todo_item *item = todo_list.items + i;
 +      for (i = 0; i < todo_list->nr; i++) {
 +              struct todo_item *item = todo_list->items + i;
  
                if (item->command >= TODO_NOOP)
                        continue;
                if (item->command != TODO_PICK)
                        break;
                if (parse_commit(item->commit)) {
 -                      todo_list_release(&todo_list);
                        return error(_("could not parse commit '%s'"),
                                oid_to_hex(&item->commit->object.oid));
                }
                if (item->commit->parents->next)
                        break; /* merge commit */
                parent_oid = &item->commit->parents->item->object.oid;
 -              if (!oideq(parent_oid, output_oid))
 +              if (!oideq(parent_oid, base_oid))
                        break;
 -              oidcpy(output_oid, &item->commit->object.oid);
 +              oidcpy(base_oid, &item->commit->object.oid);
        }
        if (i > 0) {
 -              int offset = get_item_line_offset(&todo_list, i);
                const char *done_path = rebase_path_done();
  
 -              fd = open(done_path, O_CREAT | O_WRONLY | O_APPEND, 0666);
 -              if (fd < 0) {
 -                      error_errno(_("could not open '%s' for writing"),
 -                                  done_path);
 -                      todo_list_release(&todo_list);
 -                      return -1;
 -              }
 -              if (write_in_full(fd, todo_list.buf.buf, offset) < 0) {
 +              if (todo_list_write_to_file(r, todo_list, done_path, NULL, NULL, i, 0)) {
                        error_errno(_("could not write to '%s'"), done_path);
 -                      todo_list_release(&todo_list);
 -                      close(fd);
                        return -1;
                }
 -              close(fd);
  
 -              if (rewrite_file(rebase_path_todo(), todo_list.buf.buf + offset,
 -                               todo_list.buf.len - offset) < 0) {
 -                      todo_list_release(&todo_list);
 -                      return -1;
 -              }
 +              MOVE_ARRAY(todo_list->items, todo_list->items + i, todo_list->nr - i);
 +              todo_list->nr -= i;
 +              todo_list->current = 0;
  
 -              todo_list.current = i;
 -              if (is_fixup(peek_command(&todo_list, 0)))
 -                      record_in_rewritten(output_oid, peek_command(&todo_list, 0));
 +              if (is_fixup(peek_command(todo_list, 0)))
 +                      record_in_rewritten(base_oid, peek_command(todo_list, 0));
        }
  
 -      todo_list_release(&todo_list);
 -
        return 0;
  }
  
  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, const char *cmd,
 -                  unsigned autosquash)
 +                  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 todo_list = TODO_LIST_INIT;
 -      struct strbuf *buf = &(todo_list.buf);
 -      struct object_id oid;
 -      struct stat st;
 +      struct todo_list new_todo = TODO_LIST_INIT;
 +      struct strbuf *buf = &todo_list->buf;
 +      struct object_id oid = onto->object.oid;
 +      int res;
  
 -      get_oid(onto, &oid);
        shortonto = find_unique_abbrev(&oid, DEFAULT_ABBREV);
  
 -      if (!lstat(todo_file, &st) && st.st_size == 0 &&
 -          write_message("noop\n", 5, todo_file, 0))
 -              return -1;
 +      if (buf->len == 0) {
 +              struct todo_item *item = append_new_todo(todo_list);
 +              item->command = TODO_NOOP;
 +              item->commit = NULL;
 +              item->arg_len = item->arg_offset = item->flags = item->offset_in_buf = 0;
 +      }
  
 -      if (autosquash && rearrange_squash(r))
 +      if (autosquash && todo_list_rearrange_squash(todo_list))
                return -1;
  
 -      if (cmd && *cmd)
 -              sequencer_add_exec_commands(r, cmd);
 -
 -      if (strbuf_read_file(buf, todo_file, 0) < 0)
 -              return error_errno(_("could not read '%s'."), todo_file);
 +      if (commands->nr)
 +              todo_list_add_exec_commands(todo_list, commands);
  
 -      if (parse_insn_buffer(r, buf->buf, &todo_list)) {
 -              todo_list_release(&todo_list);
 -              return error(_("unusable todo list: '%s'"), todo_file);
 -      }
 -
 -      if (count_commands(&todo_list) == 0) {
 +      if (count_commands(todo_list) == 0) {
                apply_autostash(opts);
                sequencer_remove_state(opts);
 -              todo_list_release(&todo_list);
  
                return error(_("nothing to do"));
        }
  
 -      strbuf_addch(buf, '\n');
 -      strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)",
 -                                    "Rebase %s onto %s (%d commands)",
 -                                    count_commands(&todo_list)),
 -                            shortrevisions, shortonto, count_commands(&todo_list));
 -      append_todo_help(0, flags & TODO_LIST_KEEP_EMPTY, buf);
 -
 -      if (write_message(buf->buf, buf->len, todo_file, 0)) {
 -              todo_list_release(&todo_list);
 +      res = edit_todo_list(r, todo_list, &new_todo, shortrevisions,
 +                           shortonto, flags);
 +      if (res == -1)
                return -1;
 -      }
 -
 -      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 (transform_todos(r, flags | TODO_LIST_SHORTEN_IDS))
 -              return error(_("could not transform the todo list"));
 -
 -      strbuf_reset(buf);
 -
 -      if (launch_sequence_editor(todo_file, buf, NULL)) {
 +      else if (res == -2) {
                apply_autostash(opts);
                sequencer_remove_state(opts);
 -              todo_list_release(&todo_list);
  
                return -1;
 -      }
 -
 -      strbuf_stripspace(buf, 1);
 -      if (buf->len == 0) {
 +      } else if (res == -3) {
                apply_autostash(opts);
                sequencer_remove_state(opts);
 -              todo_list_release(&todo_list);
 +              todo_list_release(&new_todo);
  
                return error(_("nothing to do"));
        }
  
 -      todo_list_release(&todo_list);
 +      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(r, opts, onto_name, &onto->object.oid, orig_head);
 +              todo_list_release(&new_todo);
  
 -      if (check_todo_list(r)) {
 -              checkout_onto(opts, onto_name, onto, orig_head);
                return -1;
        }
  
 -      if (transform_todos(r, flags & ~(TODO_LIST_SHORTEN_IDS)))
 -              return error(_("could not transform the todo list"));
 -
 -      if (opts->allow_ff && skip_unnecessary_picks(r, &oid))
 +      if (opts->allow_ff && skip_unnecessary_picks(r, &new_todo, &oid)) {
 +              todo_list_release(&new_todo);
                return error(_("could not skip unnecessary pick commands"));
 +      }
 +
 +      if (todo_list_write_to_file(r, &new_todo, todo_file, NULL, NULL, -1,
 +                                  flags & ~(TODO_LIST_SHORTEN_IDS))) {
 +              todo_list_release(&new_todo);
 +              return error_errno(_("could not write '%s'"), todo_file);
 +      }
 +
 +      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))
@@@ -4945,13 -5082,21 +5031,13 @@@ define_commit_slab(commit_todo_item, st
   * message will have to be retrieved from the commit (as the oneline in the
   * script cannot be trusted) in order to normalize the autosquash arrangement.
   */
 -int rearrange_squash(struct repository *r)
 +int todo_list_rearrange_squash(struct todo_list *todo_list)
  {
 -      const char *todo_file = rebase_path_todo();
 -      struct todo_list todo_list = TODO_LIST_INIT;
        struct hashmap subject2item;
 -      int res = 0, rearranged = 0, *next, *tail, i;
 +      int rearranged = 0, *next, *tail, i, nr = 0, alloc = 0;
        char **subjects;
        struct commit_todo_item commit_todo;
 -
 -      if (strbuf_read_file_or_whine(&todo_list.buf, todo_file) < 0)
 -              return -1;
 -      if (parse_insn_buffer(r, todo_list.buf.buf, &todo_list) < 0) {
 -              todo_list_release(&todo_list);
 -              return -1;
 -      }
 +      struct todo_item *items = NULL;
  
        init_commit_todo_item(&commit_todo);
        /*
         * be moved to appear after the i'th.
         */
        hashmap_init(&subject2item, (hashmap_cmp_fn) subject2item_cmp,
 -                   NULL, todo_list.nr);
 -      ALLOC_ARRAY(next, todo_list.nr);
 -      ALLOC_ARRAY(tail, todo_list.nr);
 -      ALLOC_ARRAY(subjects, todo_list.nr);
 -      for (i = 0; i < todo_list.nr; i++) {
 +                   NULL, todo_list->nr);
 +      ALLOC_ARRAY(next, todo_list->nr);
 +      ALLOC_ARRAY(tail, todo_list->nr);
 +      ALLOC_ARRAY(subjects, todo_list->nr);
 +      for (i = 0; i < todo_list->nr; i++) {
                struct strbuf buf = STRBUF_INIT;
 -              struct todo_item *item = todo_list.items + i;
 +              struct todo_item *item = todo_list->items + i;
                const char *commit_buffer, *subject, *p;
                size_t subject_len;
                int i2 = -1;
                }
  
                if (is_fixup(item->command)) {
 -                      todo_list_release(&todo_list);
                        clear_commit_todo_item(&commit_todo);
                        return error(_("the script was already rearranged."));
                }
                                 *commit_todo_item_at(&commit_todo, commit2))
                                /* found by commit name */
                                i2 = *commit_todo_item_at(&commit_todo, commit2)
 -                                      - todo_list.items;
 +                                      - todo_list->items;
                        else {
                                /* copy can be a prefix of the commit subject */
                                for (i2 = 0; i2 < i; i2++)
                }
                if (i2 >= 0) {
                        rearranged = 1;
 -                      todo_list.items[i].command =
 +                      todo_list->items[i].command =
                                starts_with(subject, "fixup!") ?
                                TODO_FIXUP : TODO_SQUASH;
                        if (next[i2] < 0)
        }
  
        if (rearranged) {
 -              struct strbuf buf = STRBUF_INIT;
 -
 -              for (i = 0; i < todo_list.nr; i++) {
 -                      enum todo_command command = todo_list.items[i].command;
 +              for (i = 0; i < todo_list->nr; i++) {
 +                      enum todo_command command = todo_list->items[i].command;
                        int cur = i;
  
                        /*
                                continue;
  
                        while (cur >= 0) {
 -                              const char *bol =
 -                                      get_item_line(&todo_list, cur);
 -                              const char *eol =
 -                                      get_item_line(&todo_list, cur + 1);
 -
 -                              /* replace 'pick', by 'fixup' or 'squash' */
 -                              command = todo_list.items[cur].command;
 -                              if (is_fixup(command)) {
 -                                      strbuf_addstr(&buf,
 -                                              todo_command_info[command].str);
 -                                      bol += strcspn(bol, " \t");
 -                              }
 -
 -                              strbuf_add(&buf, bol, eol - bol);
 -
 +                              ALLOC_GROW(items, nr + 1, alloc);
 +                              items[nr++] = todo_list->items[cur];
                                cur = next[cur];
                        }
                }
  
 -              res = rewrite_file(todo_file, buf.buf, buf.len);
 -              strbuf_release(&buf);
 +              FREE_AND_NULL(todo_list->items);
 +              todo_list->items = items;
 +              todo_list->nr = nr;
 +              todo_list->alloc = alloc;
        }
  
        free(next);
        free(tail);
 -      for (i = 0; i < todo_list.nr; i++)
 +      for (i = 0; i < todo_list->nr; i++)
                free(subjects[i]);
        free(subjects);
        hashmap_free(&subject2item, 1);
 -      todo_list_release(&todo_list);
  
        clear_commit_todo_item(&commit_todo);
 -      return res;
 +
 +      return 0;
  }
diff --combined sequencer.h
index f4a8f4e44399325b7f62c0467061fad53c555156,c4b79165d3cedf434057674b8ccc5c53ec9d5929..0c494b83d43e2c0d71822a23dd274176fe743490
@@@ -10,7 -10,6 +10,7 @@@ struct repository
  const char *git_path_commit_editmsg(void);
  const char *git_path_seq_dir(void);
  const char *rebase_path_todo(void);
 +const char *rebase_path_todo_backup(void);
  
  #define APPEND_SIGNOFF_DEDUP (1u << 0)
  
@@@ -48,7 -47,6 +48,7 @@@ struct replay_opts 
  
        char *gpg_sign;
        enum commit_msg_cleanup_mode default_msg_cleanup;
 +      int explicit_cleanup;
  
        /* Merge strategy */
        char *strategy;
  };
  #define REPLAY_OPTS_INIT { .action = -1, .current_fixups = STRBUF_INIT }
  
 -enum missing_commit_check_level {
 -      MISSING_COMMIT_CHECK_IGNORE = 0,
 -      MISSING_COMMIT_CHECK_WARN,
 -      MISSING_COMMIT_CHECK_ERROR
 +/*
 + * Note that ordering matters in this enum. Not only must it match the mapping
 + * of todo_command_info (in sequencer.c), it is also divided into several
 + * sections that matter.  When adding new commands, make sure you add it in the
 + * right section.
 + */
 +enum todo_command {
 +      /* commands that handle commits */
 +      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,
 +      TODO_BREAK,
 +      TODO_LABEL,
 +      TODO_RESET,
 +      TODO_MERGE,
 +      /* commands that do nothing but are counted for reporting progress */
 +      TODO_NOOP,
 +      TODO_DROP,
 +      /* comments (not counted for reporting progress) */
 +      TODO_COMMENT
 +};
 +
 +struct todo_item {
 +      enum todo_command command;
 +      struct commit *commit;
 +      unsigned int flags;
 +      int arg_len;
 +      /* The offset of the command and its argument in the strbuf */
 +      size_t offset_in_buf, arg_offset;
 +};
 +
 +struct todo_list {
 +      struct strbuf buf;
 +      struct todo_item *items;
 +      int nr, alloc, current;
 +      int done_nr, total_nr;
 +      struct stat_data stat;
  };
  
 -int write_message(const void *buf, size_t len, const char *filename,
 -                int append_eol);
 +#define TODO_LIST_INIT { STRBUF_INIT }
 +
 +int todo_list_parse_insn_buffer(struct repository *r, char *buf,
 +                              struct todo_list *todo_list);
 +int todo_list_write_to_file(struct repository *r, struct todo_list *todo_list,
 +                          const char *file, const char *shortrevisions,
 +                          const char *shortonto, int num, unsigned flags);
 +void todo_list_release(struct todo_list *todo_list);
 +const char *todo_item_get_arg(struct todo_list *todo_list,
 +                            struct todo_item *item);
  
  /* Call this to setup defaults before parsing command line options */
  void sequencer_init_config(struct replay_opts *opts);
@@@ -141,19 -93,19 +141,19 @@@ int sequencer_remove_state(struct repla
   * commits should be rebased onto the new base, this flag needs to be passed.
   */
  #define TODO_LIST_REBASE_COUSINS (1U << 4)
 -int sequencer_make_script(struct repository *repo, FILE *out,
 -                        int argc, const char **argv,
 -                        unsigned flags);
 -
 -int sequencer_add_exec_commands(struct repository *r, const char *command);
 -int transform_todos(struct repository *r, unsigned flags);
 -enum missing_commit_check_level get_missing_commit_check_level(void);
 -int check_todo_list(struct repository *r);
 +#define TODO_LIST_APPEND_TODO_HELP (1U << 5)
 +
 +int sequencer_make_script(struct repository *r, struct strbuf *out, int argc,
 +                        const char **argv, unsigned flags);
 +
 +void todo_list_add_exec_commands(struct todo_list *todo_list,
 +                               struct string_list *commands);
 +int check_todo_list_from_file(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, const char *cmd,
 -                  unsigned autosquash);
 -int rearrange_squash(struct repository *r);
 +                  struct commit *onto, const char *orig_head, struct string_list *commands,
 +                  unsigned autosquash, struct todo_list *todo_list);
 +int todo_list_rearrange_squash(struct todo_list *todo_list);
  
  /*
   * Append a signoff to the commit message in "msgbuf". The ignore_footer
   */
  void append_signoff(struct strbuf *msgbuf, size_t ignore_footer, unsigned flag);
  
 -void append_conflicts_hint(struct index_state *istate, struct strbuf *msgbuf);
 +void append_conflicts_hint(struct index_state *istate,
 +              struct strbuf *msgbuf, enum commit_msg_cleanup_mode cleanup_mode);
 +enum commit_msg_cleanup_mode get_cleanup_mode(const char *cleanup_arg,
 +      int use_editor);
 +
 +void cleanup_message(struct strbuf *msgbuf,
 +      enum commit_msg_cleanup_mode cleanup_mode, int verbose);
 +
  int message_is_empty(const struct strbuf *sb,
                     enum commit_msg_cleanup_mode cleanup_mode);
  int template_untouched(const struct strbuf *sb, const char *template_file,
@@@ -183,8 -128,7 +183,8 @@@ void commit_post_rewrite(struct reposit
                         const struct commit *current_head,
                         const struct object_id *new_head);
  
 -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);
  
  #define SUMMARY_INITIAL_COMMIT   (1 << 0)
  #define SUMMARY_SHOW_AUTHOR_DATE (1 << 1)
@@@ -199,4 -143,7 +199,7 @@@ int read_author_script(const char *path
  
  void parse_strategy_opts(struct replay_opts *opts, char *raw_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);
+ void sequencer_post_commit_cleanup(struct repository *r);
+ int sequencer_get_last_command(struct repository* r,
+                              enum replay_action *action);
index 1a8818fbe02c72164fbaa676c1bc734390f7dbc0,cebf91dce2a89a54d8c1d3ddc1e1aaf316cb33ea..9b9b4ca8d4f2a2188c206a0e1e2de763d00a1ab6
@@@ -25,11 -25,6 +25,11 @@@ test_expect_success setup 
        test_commit base foo b &&
        test_commit picked foo c &&
        test_commit --signoff picked-signed foo d &&
 +      git checkout -b topic initial &&
 +      test_commit redundant-pick foo c redundant &&
 +      git commit --allow-empty --allow-empty-message &&
 +      git tag empty &&
 +      git checkout master &&
        git config advice.detachedhead false
  
  '
@@@ -93,7 -88,7 +93,7 @@@ test_expect_success 'cherry-pick --no-c
  
  test_expect_success 'cherry-pick w/dirty tree does not set CHERRY_PICK_HEAD' '
        pristine_detach initial &&
 -      echo foo > foo &&
 +      echo foo >foo &&
        test_must_fail git cherry-pick base &&
        test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
  '
  test_expect_success \
        'cherry-pick --strategy=resolve w/dirty tree does not set CHERRY_PICK_HEAD' '
        pristine_detach initial &&
 -      echo foo > foo &&
 +      echo foo >foo &&
        test_must_fail git cherry-pick --strategy=resolve base &&
        test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
  '
@@@ -161,6 -156,25 +161,25 @@@ test_expect_success 'successful commit 
  
        test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
  '
+ test_expect_success 'successful final commit clears cherry-pick state' '
+       pristine_detach initial &&
+       test_must_fail git cherry-pick base picked-signed &&
+       echo resolved >foo &&
+       test_path_is_file .git/sequencer/todo &&
+       git commit -a &&
+       test_must_fail test_path_exists .git/sequencer
+ '
+ test_expect_success 'reset after final pick clears cherry-pick state' '
+       pristine_detach initial &&
+       test_must_fail git cherry-pick base picked-signed &&
+       echo resolved >foo &&
+       test_path_is_file .git/sequencer/todo &&
+       git reset &&
+       test_must_fail test_path_exists .git/sequencer
+ '
  
  test_expect_success 'failed cherry-pick produces dirty index' '
        pristine_detach initial &&
@@@ -180,63 -194,23 +199,63 @@@ test_expect_success 'failed cherry-pic
                git ls-files --stage foo &&
                git checkout picked -- foo &&
                git ls-files --stage foo
 -      } > stages &&
 +      } >stages &&
        sed "
                1 s/ 0  / 1     /
                2 s/ 0  / 2     /
                3 s/ 0  / 3     /
 -      " < stages > expected &&
 +      " stages >expected &&
        git read-tree -u --reset HEAD &&
  
        test_must_fail git cherry-pick picked &&
 -      git ls-files --stage --unmerged > actual &&
 +      git ls-files --stage --unmerged >actual &&
  
        test_cmp expected actual
  '
  
 +test_expect_success \
 +      'cherry-pick conflict, ensure commit.cleanup = scissors places scissors line properly' '
 +      pristine_detach initial &&
 +      git config commit.cleanup scissors &&
 +      cat <<-EOF >expected &&
 +              picked
 +
 +              # ------------------------ >8 ------------------------
 +              # Do not modify or remove the line above.
 +              # Everything below it will be ignored.
 +              #
 +              # Conflicts:
 +              #       foo
 +              EOF
 +
 +      test_must_fail git cherry-pick picked &&
 +
 +      test_i18ncmp expected .git/MERGE_MSG
 +'
 +
 +test_expect_success \
 +      'cherry-pick conflict, ensure cleanup=scissors places scissors line properly' '
 +      pristine_detach initial &&
 +      git config --unset commit.cleanup &&
 +      cat <<-EOF >expected &&
 +              picked
 +
 +              # ------------------------ >8 ------------------------
 +              # Do not modify or remove the line above.
 +              # Everything below it will be ignored.
 +              #
 +              # Conflicts:
 +              #       foo
 +              EOF
 +
 +      test_must_fail git cherry-pick --cleanup=scissors picked &&
 +
 +      test_i18ncmp expected .git/MERGE_MSG
 +'
 +
  test_expect_success 'failed cherry-pick describes conflict in work tree' '
        pristine_detach initial &&
 -      cat <<-EOF > expected &&
 +      cat <<-EOF >expected &&
        <<<<<<< HEAD
        a
        =======
  
        test_must_fail git cherry-pick picked &&
  
 -      sed "s/[a-f0-9]*\.\.\./objid/" foo > actual &&
 +      sed "s/[a-f0-9]*\.\.\./objid/" foo >actual &&
        test_cmp expected actual
  '
  
  test_expect_success 'diff3 -m style' '
        pristine_detach initial &&
        git config merge.conflictstyle diff3 &&
 -      cat <<-EOF > expected &&
 +      cat <<-EOF >expected &&
        <<<<<<< HEAD
        a
        ||||||| parent of objid picked
  
        test_must_fail git cherry-pick picked &&
  
 -      sed "s/[a-f0-9]*\.\.\./objid/" foo > actual &&
 +      sed "s/[a-f0-9]*\.\.\./objid/" foo >actual &&
        test_cmp expected actual
  '
  
  test_expect_success 'revert also handles conflicts sanely' '
        git config --unset merge.conflictstyle &&
        pristine_detach initial &&
 -      cat <<-EOF > expected &&
 +      cat <<-EOF >expected &&
        <<<<<<< HEAD
        a
        =======
                git ls-files --stage foo &&
                git checkout base -- foo &&
                git ls-files --stage foo
 -      } > stages &&
 +      } >stages &&
        sed "
                1 s/ 0  / 1     /
                2 s/ 0  / 2     /
                3 s/ 0  / 3     /
 -      " < stages > expected-stages &&
 +      " stages >expected-stages &&
        git read-tree -u --reset HEAD &&
  
        head=$(git rev-parse HEAD) &&
        test_must_fail git revert picked &&
        newhead=$(git rev-parse HEAD) &&
 -      git ls-files --stage --unmerged > actual-stages &&
 +      git ls-files --stage --unmerged >actual-stages &&
  
        test "$head" = "$newhead" &&
        test_must_fail git update-index --refresh -q &&
        test_must_fail git diff-index --exit-code HEAD &&
        test_cmp expected-stages actual-stages &&
 -      sed "s/[a-f0-9]*\.\.\./objid/" foo > actual &&
 +      sed "s/[a-f0-9]*\.\.\./objid/" foo >actual &&
        test_cmp expected actual
  '
  
@@@ -329,7 -303,7 +348,7 @@@ test_expect_success 'revert --no-commi
  
  test_expect_success 'revert w/dirty tree does not set REVERT_HEAD' '
        pristine_detach base &&
 -      echo foo > foo &&
 +      echo foo >foo &&
        test_must_fail git revert base &&
        test_must_fail git rev-parse --verify CHERRY_PICK_HEAD &&
        test_must_fail git rev-parse --verify REVERT_HEAD
@@@ -361,10 -335,30 +380,30 @@@ test_expect_success 'failed commit doe
        test_cmp_rev picked REVERT_HEAD
  '
  
+ test_expect_success 'successful final commit clears revert state' '
+        pristine_detach picked-signed &&
+        test_must_fail git revert picked-signed base &&
+        echo resolved >foo &&
+        test_path_is_file .git/sequencer/todo &&
+        git commit -a &&
+        test_must_fail test_path_exists .git/sequencer
+ '
+ test_expect_success 'reset after final pick clears revert state' '
+        pristine_detach picked-signed &&
+        test_must_fail git revert picked-signed base &&
+        echo resolved >foo &&
+        test_path_is_file .git/sequencer/todo &&
+        git reset &&
+        test_must_fail test_path_exists .git/sequencer
+ '
  test_expect_success 'revert conflict, diff3 -m style' '
        pristine_detach initial &&
        git config merge.conflictstyle diff3 &&
 -      cat <<-EOF > expected &&
 +      cat <<-EOF >expected &&
        <<<<<<< HEAD
        a
        ||||||| objid picked
  
        test_must_fail git revert picked &&
  
 -      sed "s/[a-f0-9]*\.\.\./objid/" foo > actual &&
 +      sed "s/[a-f0-9]*\.\.\./objid/" foo >actual &&
        test_cmp expected actual
  '
  
 +test_expect_success \
 +      'revert conflict, ensure commit.cleanup = scissors places scissors line properly' '
 +      pristine_detach initial &&
 +      git config commit.cleanup scissors &&
 +      cat >expected <<-EOF &&
 +              Revert "picked"
 +
 +              This reverts commit OBJID.
 +
 +              # ------------------------ >8 ------------------------
 +              # Do not modify or remove the line above.
 +              # Everything below it will be ignored.
 +              #
 +              # Conflicts:
 +              #       foo
 +              EOF
 +
 +      test_must_fail git revert picked &&
 +
 +      sed "s/$OID_REGEX/OBJID/" .git/MERGE_MSG >actual &&
 +      test_i18ncmp expected actual
 +'
 +
 +test_expect_success \
 +      'revert conflict, ensure cleanup=scissors places scissors line properly' '
 +      pristine_detach initial &&
 +      git config --unset commit.cleanup &&
 +      cat >expected <<-EOF &&
 +              Revert "picked"
 +
 +              This reverts commit OBJID.
 +
 +              # ------------------------ >8 ------------------------
 +              # Do not modify or remove the line above.
 +              # Everything below it will be ignored.
 +              #
 +              # Conflicts:
 +              #       foo
 +              EOF
 +
 +      test_must_fail git revert --cleanup=scissors picked &&
 +
 +      sed "s/$OID_REGEX/OBJID/" .git/MERGE_MSG >actual &&
 +      test_i18ncmp expected actual
 +'
 +
  test_expect_success 'failed cherry-pick does not forget -s' '
        pristine_detach initial &&
        test_must_fail git cherry-pick -s picked &&
@@@ -436,7 -384,7 +475,7 @@@ test_expect_success 'commit after faile
        pristine_detach initial &&
        test_must_fail git cherry-pick -s picked-signed &&
        git commit -a -s &&
 -      test $(git show -s |grep -c "Signed-off-by") = 1
 +      test $(git show -s >tmp && grep -c "Signed-off-by" tmp && rm tmp) = 1
  '
  
  test_expect_success 'commit after failed cherry-pick adds -s at the right place' '
        Signed-off-by: C O Mitter <committer@example.com>
        # Conflicts:
        EOF
 -      grep -e "^# Conflicts:" -e '^Signed-off-by' <.git/COMMIT_EDITMSG >actual &&
 +      grep -e "^# Conflicts:" -e '^Signed-off-by' .git/COMMIT_EDITMSG >actual &&
        test_cmp expect actual &&
  
        cat <<-\EOF >expected &&
@@@ -469,7 -417,7 +508,7 @@@ test_expect_success 'commit --amend -s 
  
        # emulate old-style conflicts block
        mv .git/MERGE_MSG .git/MERGE_MSG+ &&
 -      sed -e "/^# Conflicts:/,\$s/^# *//" <.git/MERGE_MSG+ >.git/MERGE_MSG &&
 +      sed -e "/^# Conflicts:/,\$s/^# *//" .git/MERGE_MSG+ >.git/MERGE_MSG &&
  
        git commit -a &&
        git commit --amend -s &&
        Signed-off-by: C O Mitter <committer@example.com>
        Conflicts:
        EOF
 -      grep -e "^Conflicts:" -e '^Signed-off-by' <.git/COMMIT_EDITMSG >actual &&
 +      grep -e "^Conflicts:" -e '^Signed-off-by' .git/COMMIT_EDITMSG >actual &&
        test_cmp expect actual
  '
  
@@@ -496,23 -444,4 +535,23 @@@ test_expect_success 'cherry-pick preser
        test_i18ngrep ! "Changes not staged for commit:" actual
  '
  
 +test_expect_success 'cherry-pick --continue remembers --keep-redundant-commits' '
 +      test_when_finished "git cherry-pick --abort || :" &&
 +      pristine_detach initial &&
 +      test_must_fail git cherry-pick --keep-redundant-commits picked redundant &&
 +      echo c >foo &&
 +      git add foo &&
 +      git cherry-pick --continue
 +'
 +
 +test_expect_success 'cherry-pick --continue remembers --allow-empty and --allow-empty-message' '
 +      test_when_finished "git cherry-pick --abort || :" &&
 +      pristine_detach initial &&
 +      test_must_fail git cherry-pick --allow-empty --allow-empty-message \
 +                                     picked empty &&
 +      echo c >foo &&
 +      git add foo &&
 +      git cherry-pick --continue
 +'
 +
  test_done
diff --combined wt-status.c
index f4fa98263857015b3d86e05df42e59591e74109a,1dbb4d949cbd44139409b944373d221dc540a656..e065558c312eefb4c3d5f8bf91dd8358069e0faa
@@@ -17,6 -17,7 +17,7 @@@
  #include "utf8.h"
  #include "worktree.h"
  #include "lockfile.h"
+ #include "sequencer.h"
  
  static const char cut_line[] =
  "------------------------ >8 ------------------------\n";
@@@ -748,23 -749,12 +749,23 @@@ static int has_unmerged(struct wt_statu
  
  void wt_status_collect(struct wt_status *s)
  {
 +      trace2_region_enter("status", "worktrees", s->repo);
        wt_status_collect_changes_worktree(s);
 -      if (s->is_initial)
 +      trace2_region_leave("status", "worktrees", s->repo);
 +
 +      if (s->is_initial) {
 +              trace2_region_enter("status", "initial", s->repo);
                wt_status_collect_changes_initial(s);
 -      else
 +              trace2_region_leave("status", "initial", s->repo);
 +      } else {
 +              trace2_region_enter("status", "index", s->repo);
                wt_status_collect_changes_index(s);
 +              trace2_region_leave("status", "index", s->repo);
 +      }
 +
 +      trace2_region_enter("status", "untracked", s->repo);
        wt_status_collect_untracked(s);
 +      trace2_region_leave("status", "untracked", s->repo);
  
        wt_status_get_state(s->repo, &s->state, s->branch && !strcmp(s->branch, "HEAD"));
        if (s->state.merge_in_progress && !has_unmerged(s))
@@@ -1006,19 -996,13 +1007,19 @@@ size_t wt_status_locate_end(const char 
        return len;
  }
  
 -void wt_status_add_cut_line(FILE *fp)
 +void wt_status_append_cut_line(struct strbuf *buf)
  {
        const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
 +
 +      strbuf_commented_addf(buf, "%s", cut_line);
 +      strbuf_add_commented_lines(buf, explanation, strlen(explanation));
 +}
 +
 +void wt_status_add_cut_line(FILE *fp)
 +{
        struct strbuf buf = STRBUF_INIT;
  
 -      fprintf(fp, "%c %s", comment_line_char, cut_line);
 -      strbuf_add_commented_lines(&buf, explanation, strlen(explanation));
 +      wt_status_append_cut_line(&buf);
        fputs(buf.buf, fp);
        strbuf_release(&buf);
  }
@@@ -1386,12 -1370,22 +1387,22 @@@ static void show_rebase_in_progress(str
  static void show_cherry_pick_in_progress(struct wt_status *s,
                                         const char *color)
  {
-       status_printf_ln(s, color, _("You are currently cherry-picking commit %s."),
-                       find_unique_abbrev(&s->state.cherry_pick_head_oid, DEFAULT_ABBREV));
+       if (is_null_oid(&s->state.cherry_pick_head_oid))
+               status_printf_ln(s, color,
+                       _("Cherry-pick currently in progress."));
+       else
+               status_printf_ln(s, color,
+                       _("You are currently cherry-picking commit %s."),
+                       find_unique_abbrev(&s->state.cherry_pick_head_oid,
+                                          DEFAULT_ABBREV));
        if (s->hints) {
                if (has_unmerged(s))
                        status_printf_ln(s, color,
                                _("  (fix conflicts and run \"git cherry-pick --continue\")"));
+               else if (is_null_oid(&s->state.cherry_pick_head_oid))
+                       status_printf_ln(s, color,
+                               _("  (run \"git cherry-pick --continue\" to continue)"));
                else
                        status_printf_ln(s, color,
                                _("  (all conflicts fixed: run \"git cherry-pick --continue\")"));
  static void show_revert_in_progress(struct wt_status *s,
                                    const char *color)
  {
-       status_printf_ln(s, color, _("You are currently reverting commit %s."),
-                        find_unique_abbrev(&s->state.revert_head_oid, DEFAULT_ABBREV));
+       if (is_null_oid(&s->state.revert_head_oid))
+               status_printf_ln(s, color,
+                       _("Revert currently in progress."));
+       else
+               status_printf_ln(s, color,
+                       _("You are currently reverting commit %s."),
+                       find_unique_abbrev(&s->state.revert_head_oid,
+                                          DEFAULT_ABBREV));
        if (s->hints) {
                if (has_unmerged(s))
                        status_printf_ln(s, color,
                                _("  (fix conflicts and run \"git revert --continue\")"));
+               else if (is_null_oid(&s->state.revert_head_oid))
+                       status_printf_ln(s, color,
+                               _("  (run \"git revert --continue\" to continue)"));
                else
                        status_printf_ln(s, color,
                                _("  (all conflicts fixed: run \"git revert --continue\")"));
@@@ -1580,6 -1583,7 +1600,7 @@@ void wt_status_get_state(struct reposit
  {
        struct stat st;
        struct object_id oid;
+       enum replay_action action;
  
        if (!stat(git_path_merge_head(r), &st)) {
                wt_status_check_rebase(NULL, state);
                state->revert_in_progress = 1;
                oidcpy(&state->revert_head_oid, &oid);
        }
+       if (!sequencer_get_last_command(r, &action)) {
+               if (action == REPLAY_PICK) {
+                       state->cherry_pick_in_progress = 1;
+                       oidcpy(&state->cherry_pick_head_oid, &null_oid);
+               } else {
+                       state->revert_in_progress = 1;
+                       oidcpy(&state->revert_head_oid, &null_oid);
+               }
+       }
        if (get_detached_from)
                wt_status_get_detached_from(r, state);
  }
@@@ -1857,7 -1869,7 +1886,7 @@@ static void wt_shortstatus_print_tracki
        color_fprintf(s->fp, branch_color_local, "%s", branch_name);
  
        sti = stat_tracking_info(branch, &num_ours, &num_theirs, &base,
 -                               s->ahead_behind_flags);
 +                               0, s->ahead_behind_flags);
        if (sti < 0) {
                if (!base)
                        goto conclude;
@@@ -1996,7 -2008,7 +2025,7 @@@ static void wt_porcelain_v2_print_track
                branch = branch_get(branch_name);
                base = NULL;
                ab_info = stat_tracking_info(branch, &nr_ahead, &nr_behind,
 -                                           &base, s->ahead_behind_flags);
 +                                           &base, 0, s->ahead_behind_flags);
                if (base) {
                        base = shorten_unambiguous_ref(base, 0);
                        fprintf(s->fp, "# branch.upstream %s%c", base, eol);
@@@ -2308,13 -2320,6 +2337,13 @@@ static void wt_porcelain_v2_print(struc
  
  void wt_status_print(struct wt_status *s)
  {
 +      trace2_data_intmax("status", s->repo, "count/changed", s->change.nr);
 +      trace2_data_intmax("status", s->repo, "count/untracked",
 +                         s->untracked.nr);
 +      trace2_data_intmax("status", s->repo, "count/ignored", s->ignored.nr);
 +
 +      trace2_region_enter("status", "print", s->repo);
 +
        switch (s->status_format) {
        case STATUS_FORMAT_SHORT:
                wt_shortstatus_print(s);
                wt_longstatus_print(s);
                break;
        }
 +
 +      trace2_region_leave("status", "print", s->repo);
  }
  
  /**