Merge branch 'dl/merge-cleanup-scissors-fix'
authorJunio C Hamano <gitster@pobox.com>
Wed, 8 May 2019 15:37:24 +0000 (00:37 +0900)
committerJunio C Hamano <gitster@pobox.com>
Wed, 8 May 2019 15:37:24 +0000 (00:37 +0900)
The list of conflicted paths shown in the editor while concluding a
conflicted merge was shown above the scissors line when the
clean-up mode is set to "scissors", even though it was commented
out just like the list of updated paths and other information to
help the user explain the merge better.

* dl/merge-cleanup-scissors-fix:
cherry-pick/revert: add scissors line on merge conflict
sequencer.c: save and restore cleanup mode
merge: add scissors line on merge conflict
merge: cleanup messages like commit
parse-options.h: extract common --cleanup option
commit: extract cleanup_mode functions to sequencer
t7502: clean up style
t7604: clean up style
t3507: clean up style
t7600: clean up style

1  2 
builtin/commit.c
builtin/pull.c
builtin/tag.c
parse-options.h
sequencer.c
sequencer.h
t/t3507-cherry-pick-conflict.sh
diff --combined builtin/commit.c
index 833ecb316a8fa5a8bef7bc5894c2f6d571c57427,5401a60cbea6b4bfbb3bf3cf141c3597462c2d0e..bd4b180c43f51b73da04894f88754b843cd2d0d1
@@@ -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();
@@@ -668,6 -668,7 +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"
@@@ -1172,30 -1184,12 +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;
-       /*
-        * Please update _git_commit() in git-completion.bash when you
-        * add new options.
-        */
-       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;
@@@ -1491,7 -1485,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();
diff --combined builtin/pull.c
index 9bd6a78081c2b2ff7b53cb98d8f1183bd9275ad8,5bf82b4b2760999f8a3eca27ae17548756c280ee..9dd32a115bbbfeedb1c1648348ddd32ccaa9b641
@@@ -24,6 -24,7 +24,7 @@@
  #include "lockfile.h"
  #include "wt-status.h"
  #include "commit-reach.h"
+ #include "sequencer.h"
  
  enum rebase_type {
        REBASE_INVALID = -1,
@@@ -101,6 -102,7 +102,7 @@@ static char *opt_signoff
  static char *opt_squash;
  static char *opt_commit;
  static char *opt_edit;
+ static char *cleanup_arg;
  static char *opt_ff;
  static char *opt_verify_signatures;
  static int opt_autostash = -1;
@@@ -168,6 -170,7 +170,7 @@@ static struct option pull_options[] = 
        OPT_PASSTHRU(0, "edit", &opt_edit, NULL,
                N_("edit message before committing"),
                PARSE_OPT_NOARG),
+       OPT_CLEANUP(&cleanup_arg),
        OPT_PASSTHRU(0, "ff", &opt_ff, NULL,
                N_("allow fast-forward"),
                PARSE_OPT_NOARG),
@@@ -369,10 -372,9 +372,10 @@@ static void get_merge_heads(struct oid_
  
        fp = xfopen(filename, "r");
        while (strbuf_getline_lf(&sb, fp) != EOF) {
 -              if (get_oid_hex(sb.buf, &oid))
 -                      continue;  /* invalid line: does not start with SHA1 */
 -              if (starts_with(sb.buf + GIT_SHA1_HEXSZ, "\tnot-for-merge\t"))
 +              const char *p;
 +              if (parse_oid_hex(sb.buf, &oid, &p))
 +                      continue;  /* invalid line: does not start with object ID */
 +              if (starts_with(p, "\tnot-for-merge\t"))
                        continue;  /* ref is not-for-merge */
                oid_array_append(merge_heads, &oid);
        }
@@@ -645,6 -647,8 +648,8 @@@ static int run_merge(void
                argv_array_push(&args, opt_commit);
        if (opt_edit)
                argv_array_push(&args, opt_edit);
+       if (cleanup_arg)
+               argv_array_pushf(&args, "--cleanup=%s", cleanup_arg);
        if (opt_ff)
                argv_array_push(&args, opt_ff);
        if (opt_verify_signatures)
@@@ -761,7 -765,7 +766,7 @@@ static int get_rebase_fork_point(struc
        cp.no_stderr = 1;
        cp.git_cmd = 1;
  
 -      ret = capture_command(&cp, &sb, GIT_SHA1_HEXSZ);
 +      ret = capture_command(&cp, &sb, GIT_MAX_HEXSZ);
        if (ret)
                goto cleanup;
  
@@@ -806,7 -810,7 +811,7 @@@ static int get_octopus_merge_base(struc
  }
  
  /**
 - * Given the current HEAD SHA1, the merge head returned from git-fetch and the
 + * Given the current HEAD oid, the merge head returned from git-fetch and the
   * fork point calculated by get_rebase_fork_point(), runs git-rebase with the
   * appropriate arguments and returns its exit status.
   */
@@@ -876,6 -880,13 +881,13 @@@ int cmd_pull(int argc, const char **arg
  
        argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0);
  
+       if (cleanup_arg)
+               /*
+                * this only checks the validity of cleanup_arg; we don't need
+                * a valid value for use_editor
+                */
+               get_cleanup_mode(cleanup_arg, 0);
        parse_repo_refspecs(argc, argv, &repo, &refspecs);
  
        if (!opt_ff)
diff --combined builtin/tag.c
index 8399a0ed5414321f9893f94d93dab6788f2175af,a3870fbdba0c6ef283031cbd7238728004fe8674..1debd3a11c2ec0aa44fa75ebd908f78a46f371a1
  #include "ref-filter.h"
  
  static const char * const git_tag_usage[] = {
 -      N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>]"),
 +      N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>]\n"
 +              "\t\t<tagname> [<head>]"),
        N_("git tag -d <tagname>..."),
 -      N_("git tag -l [-n[<num>]] [--contains <commit>] [--no-contains <commit>] [--points-at <object>]"
 -              "\n\t\t[--format=<format>] [--[no-]merged [<commit>]] [<pattern>...]"),
 +      N_("git tag -l [-n[<num>]] [--contains <commit>] [--no-contains <commit>] [--points-at <object>]\n"
 +              "\t\t[--format=<format>] [--[no-]merged [<commit>]] [<pattern>...]"),
        N_("git tag -v [--format=<format>] <tagname>..."),
        NULL
  };
@@@ -206,14 -205,7 +206,14 @@@ struct create_tag_options 
        } cleanup_mode;
  };
  
 -static void create_tag(const struct object_id *object, const char *tag,
 +static const char message_advice_nested_tag[] =
 +      N_("You have created a nested tag. The object referred to by your new is\n"
 +         "already a tag. If you meant to tag the object that it points to, use:\n"
 +         "\n"
 +         "\tgit tag -f %s %s^{}");
 +
 +static void create_tag(const struct object_id *object, const char *object_ref,
 +                     const char *tag,
                       struct strbuf *buf, struct create_tag_options *opt,
                       struct object_id *prev, struct object_id *result)
  {
  
        type = oid_object_info(the_repository, object, NULL);
        if (type <= OBJ_NONE)
 -          die(_("bad object type."));
 +              die(_("bad object type."));
 +
 +      if (type == OBJ_TAG && advice_nested_tag)
 +              advise(_(message_advice_nested_tag), tag, object_ref);
  
        strbuf_addf(&header,
                    "object %s\n"
@@@ -408,8 -397,7 +408,7 @@@ int cmd_tag(int argc, const char **argv
                OPT_FILENAME('F', "file", &msgfile, N_("read message from file")),
                OPT_BOOL('e', "edit", &edit_flag, N_("force edit of tag message")),
                OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")),
-               OPT_STRING(0, "cleanup", &cleanup_arg, N_("mode"),
-                       N_("how to strip spaces and #comments from message")),
+               OPT_CLEANUP(&cleanup_arg),
                OPT_STRING('u', "local-user", &keyid, N_("key-id"),
                                        N_("use another key to sign the tag")),
                OPT__FORCE(&force, N_("replace the tag if exists"), 0),
                OPT_WITHOUT(&filter.no_commit, N_("print only tags that don't contain the commit")),
                OPT_MERGED(&filter, N_("print only tags that are merged")),
                OPT_NO_MERGED(&filter, N_("print only tags that are not merged")),
 -              OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
 -                           N_("field name to sort on"), &parse_opt_ref_sorting),
 +              OPT_REF_SORT(sorting_tail),
                {
                        OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
                        N_("print only tags of the object"), PARSE_OPT_LASTARG_DEFAULT,
        if (create_tag_object) {
                if (force_sign_annotate && !annotate)
                        opt.sign = 1;
 -              create_tag(&object, tag, &buf, &opt, &prev, &object);
 +              create_tag(&object, object_ref, tag, &buf, &opt, &prev, &object);
        }
  
        transaction = ref_transaction_begin(&err);
diff --combined parse-options.h
index cc9230adacb61f2a5066ac094c66bdfd18534354,b93169337f852e34f873a3cb6a82a8ee46c608db..1dd3523c15a0020d4886dcf1c516dad43b529d0c
@@@ -136,12 -136,10 +136,12 @@@ struct option 
  #define OPT_BOOL_F(s, l, v, h, f)   OPT_SET_INT_F(s, l, v, h, 1, f)
  #define OPT_CALLBACK_F(s, l, v, a, h, f, cb)                  \
        { OPTION_CALLBACK, (s), (l), (v), (a), (h), (f), (cb) }
 +#define OPT_STRING_F(s, l, v, a, h, f)   { OPTION_STRING,  (s), (l), (v), (a), (h), (f) }
 +#define OPT_INTEGER_F(s, l, v, h, f)     { OPTION_INTEGER, (s), (l), (v), N_("n"), (h), (f) }
  
  #define OPT_END()                   { OPTION_END }
 -#define OPT_ARGUMENT(l, h)          { OPTION_ARGUMENT, 0, (l), NULL, NULL, \
 -                                    (h), PARSE_OPT_NOARG}
 +#define OPT_ARGUMENT(l, v, h)       { OPTION_ARGUMENT, 0, (l), (v), NULL, \
 +                                    (h), PARSE_OPT_NOARG, NULL, 1 }
  #define OPT_GROUP(h)                { OPTION_GROUP, 0, NULL, NULL, NULL, (h) }
  #define OPT_BIT(s, l, v, h, b)      OPT_BIT_F(s, l, v, h, b, 0)
  #define OPT_BITOP(s, l, v, h, set, clear) { OPTION_BITOP, (s), (l), (v), NULL, (h), \
                                      (h), PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1}
  #define OPT_CMDMODE(s, l, v, h, i)  { OPTION_CMDMODE, (s), (l), (v), NULL, \
                                      (h), PARSE_OPT_NOARG|PARSE_OPT_NONEG, NULL, (i) }
 -#define OPT_INTEGER(s, l, v, h)     { OPTION_INTEGER, (s), (l), (v), N_("n"), (h) }
 +#define OPT_INTEGER(s, l, v, h)     OPT_INTEGER_F(s, l, v, h, 0)
  #define OPT_MAGNITUDE(s, l, v, h)   { OPTION_MAGNITUDE, (s), (l), (v), \
                                      N_("n"), (h), PARSE_OPT_NONEG }
 -#define OPT_STRING(s, l, v, a, h)   { OPTION_STRING,  (s), (l), (v), (a), (h) }
 +#define OPT_STRING(s, l, v, a, h)   OPT_STRING_F(s, l, v, a, h, 0)
  #define OPT_STRING_LIST(s, l, v, a, h) \
                                    { OPTION_CALLBACK, (s), (l), (v), (a), \
                                      (h), 0, &parse_opt_string_list }
@@@ -316,5 -314,6 +316,6 @@@ int parse_opt_passthru_argv(const struc
  #define OPT_NO_CONTAINS(v, h) _OPT_CONTAINS_OR_WITH("no-contains", v, h, PARSE_OPT_NONEG)
  #define OPT_WITH(v, h) _OPT_CONTAINS_OR_WITH("with", v, h, PARSE_OPT_HIDDEN | PARSE_OPT_NONEG)
  #define OPT_WITHOUT(v, h) _OPT_CONTAINS_OR_WITH("without", v, h, PARSE_OPT_HIDDEN | PARSE_OPT_NONEG)
+ #define OPT_CLEANUP(v) OPT_STRING(0, "cleanup", v, N_("mode"), N_("how to strip spaces and #comments from message"))
  
  #endif
diff --combined sequencer.c
index 5da5949962dcca01d535eb2d2fb256342d7797a2,03756485791e83f9eb4288326ec2afbf8cc1ac00..4407a3f97858acdf47add8e8848cba12daf1de6d
@@@ -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
@@@ -181,7 -182,7 +181,7 @@@ static int git_sequencer_config(const c
                        opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_ALL;
                        opts->explicit_cleanup = 1;
                } else if (!strcmp(s, "scissors")) {
-                       opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SPACE;
+                       opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SCISSORS;
                        opts->explicit_cleanup = 1;
                } else {
                        warning(_("invalid commit message cleanup mode '%s'"),
@@@ -388,8 -389,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,11 -516,54 +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;) {
@@@ -587,7 -631,8 +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;
  }
@@@ -906,7 -951,6 +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))
@@@ -1017,6 -1061,16 +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.
@@@ -1522,6 -1576,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;
@@@ -2004,7 -2084,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);
@@@ -2017,14 -2116,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 error(_("could not parse '%.*s'"),
        return !item->commit;
  }
  
 -static int parse_insn_buffer(struct repository *r, char *buf,
 -                           struct todo_list *todo_list)
 +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)
@@@ -2215,7 -2302,7 +2268,7 @@@ static int read_populate_todo(struct re
                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;
@@@ -2285,15 -2372,6 +2338,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)
@@@ -2492,7 -2573,7 +2548,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);
@@@ -2687,54 -2768,41 +2743,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;
  }
  
@@@ -3549,8 -3617,6 +3610,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;
  
                        if (res) {
                                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++;
@@@ -4270,7 -4339,7 +4331,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;
 +      int i, max = todo_list->nr;
  
 -      if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
 -              return error(_("could not read '%s'."), todo_file);
 +      if (num > 0 && num < max)
 +              max = num;
  
 -      if (parse_insn_buffer(r, todo_list.buf.buf, &todo_list)) {
 -              todo_list_release(&todo_list);
 -              return error(_("unusable todo list: '%s'"), todo_file);
 -      }
 -
 -      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)
 +                  const char *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 todo_list new_todo = TODO_LIST_INIT;
 +      struct strbuf *buf = &todo_list->buf;
        struct object_id oid;
 -      struct stat st;
 +      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 (commands->nr)
 +              todo_list_add_exec_commands(todo_list, commands);
  
 -      if (strbuf_read_file(buf, todo_file, 0) < 0)
 -              return error_errno(_("could not read '%s'."), todo_file);
 -
 -      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 (check_todo_list(r)) {
 +      if (todo_list_parse_insn_buffer(r, new_todo.buf.buf, &new_todo) ||
 +          todo_list_check(todo_list, &new_todo)) {
 +              fprintf(stderr, _(edit_todo_list_advice));
                checkout_onto(opts, onto_name, onto, orig_head);
 +              todo_list_release(&new_todo);
 +
                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))
                return -1;
@@@ -4876,13 -5069,21 +4937,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 b69e7686c97df0e543b2d3c86abefc0b2e62b3d0,7023da9b135000ed7376b12791a43713e85e1435..75e292c03bdd002da58ec5089b1fbf321971b4bf
@@@ -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)
  
@@@ -68,60 -67,14 +68,60 @@@ struct replay_opts 
  };
  #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
  };
  
 -int write_message(const void *buf, size_t len, const char *filename,
 -                int append_eol);
 +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;
 +};
 +
 +#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 -94,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);
 +                  const char *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,
index 1ef8e9d534eae78014d9cccebc5f827a3cef91f3,a584b11c9871c82b5b771cd1858b3d34d8ea8b33..1a8818fbe02c72164fbaa676c1bc734390f7dbc0
@@@ -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
  '
@@@ -180,23 -175,63 +180,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
  '
  
@@@ -289,7 -324,7 +329,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
@@@ -324,7 -359,7 +364,7 @@@ test_expect_success 'failed commit doe
  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 &&
@@@ -350,7 -431,7 +436,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 &&
@@@ -383,7 -464,7 +469,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
  '
  
@@@ -410,23 -491,4 +496,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