Merge branch 'pw/rebase-i-internal'
authorJunio C Hamano <gitster@pobox.com>
Mon, 13 May 2019 14:50:34 +0000 (23:50 +0900)
committerJunio C Hamano <gitster@pobox.com>
Mon, 13 May 2019 14:50:34 +0000 (23:50 +0900)
The internal implementation of "git rebase -i" has been updated to
avoid forking a separate "rebase--interactive" process.

* pw/rebase-i-internal:
rebase -i: run without forking rebase--interactive
rebase: use a common action enum
rebase -i: use struct rebase_options in do_interactive_rebase()
rebase -i: use struct rebase_options to parse args
rebase -i: use struct object_id for squash_onto
rebase -i: use struct commit when parsing options
rebase -i: remove duplication
rebase -i: combine rebase--interactive.c with rebase.c
rebase: use OPT_RERERE_AUTOUPDATE()
rebase: rename write_basic_state()
rebase: don't translate trace strings
sequencer: always discard index after checkout

1  2 
Makefile
builtin/rebase.c
parse-options-cb.c
parse-options.h
sequencer.c
sequencer.h
diff --combined Makefile
index 6d433f87039275c8e04831dfae55dbdc18a44b20,c64408924452c015f22fcf98ee9d4710f0cf032c..5b0855c7e88e83a8ba2f34ff2e896c70e98e0950
+++ b/Makefile
@@@ -479,11 -479,7 +479,11 @@@ all:
  #
  # Define DEVELOPER to enable more compiler warnings. Compiler version
  # and family are auto detected, but could be overridden by defining
 -# COMPILER_FEATURES (see config.mak.dev)
 +# COMPILER_FEATURES (see config.mak.dev). You can still set
 +# CFLAGS="..." in combination with DEVELOPER enables, whether that's
 +# for tweaking something unrelated (e.g. optimization level), or for
 +# selectively overriding something DEVELOPER or one of the DEVOPTS
 +# (see just below) brings in.
  #
  # When DEVELOPER is set, DEVOPTS can be used to control compiler
  # options.  This variable contains keywords separated by
@@@ -510,8 -506,17 +510,8 @@@ GIT-VERSION-FILE: FORC
        @$(SHELL_PATH) ./GIT-VERSION-GEN
  -include GIT-VERSION-FILE
  
 -# CFLAGS and LDFLAGS are for the users to override from the command line.
 -
 -CFLAGS = -g -O2 -Wall
 -LDFLAGS =
 -ALL_CFLAGS = $(CPPFLAGS) $(CFLAGS)
 -ALL_LDFLAGS = $(LDFLAGS)
 -STRIP ?= strip
 -
 -# Create as necessary, replace existing, make ranlib unneeded.
 -ARFLAGS = rcs
 -
 +# Set our default configuration.
 +#
  # Among the variables below, these:
  #   gitexecdir
  #   template_dir
@@@ -556,7 -561,6 +556,7 @@@ perllibdir_relative = $(patsubst $(pref
  
  export prefix bindir sharedir sysconfdir gitwebdir perllibdir localedir
  
 +# Set our default programs
  CC = cc
  AR = ar
  RM = rm -f
@@@ -569,14 -573,29 +569,14 @@@ TCLTK_PATH = wis
  XGETTEXT = xgettext
  MSGFMT = msgfmt
  CURL_CONFIG = curl-config
 -PTHREAD_LIBS = -lpthread
 -PTHREAD_CFLAGS =
  GCOV = gcov
 +STRIP = strip
  SPATCH = spatch
  
  export TCL_PATH TCLTK_PATH
  
 -# user customisation variable for 'sparse' target
 -SPARSE_FLAGS ?=
 -# internal/platform customisation variable for 'sparse'
 -SP_EXTRA_FLAGS =
 -
 -SPATCH_FLAGS = --all-includes --patch .
 -
 -
 -
 -### --- END CONFIGURATION SECTION ---
 -
 -# Those must not be GNU-specific; they are shared with perl/ which may
 -# be built by a different compiler. (Note that this is an artifact now
 -# but it still might be nice to keep that distinction.)
 -BASIC_CFLAGS = -I.
 -BASIC_LDFLAGS =
 +# Set our default LIBS variables
 +PTHREAD_LIBS = -lpthread
  
  # Guard against environment variables
  BUILTIN_OBJS =
@@@ -592,7 -611,6 +592,7 @@@ FUZZ_PROGRAMS 
  LIB_OBJS =
  PROGRAM_OBJS =
  PROGRAMS =
 +EXCLUDED_PROGRAMS =
  SCRIPT_PERL =
  SCRIPT_PYTHON =
  SCRIPT_SH =
@@@ -614,8 -632,10 +614,8 @@@ SCRIPT_SH += git-merge-one-file.s
  SCRIPT_SH += git-merge-resolve.sh
  SCRIPT_SH += git-mergetool.sh
  SCRIPT_SH += git-quiltimport.sh
 -SCRIPT_SH += git-legacy-rebase.sh
 -SCRIPT_SH += git-remote-testgit.sh
 +SCRIPT_SH += git-legacy-stash.sh
  SCRIPT_SH += git-request-pull.sh
 -SCRIPT_SH += git-stash.sh
  SCRIPT_SH += git-submodule.sh
  SCRIPT_SH += git-web--browse.sh
  
@@@ -637,11 -657,17 +637,11 @@@ SCRIPT_PERL += git-svn.per
  
  SCRIPT_PYTHON += git-p4.py
  
 -NO_INSTALL += git-remote-testgit
 -
  # Generated files for scripts
  SCRIPT_SH_GEN = $(patsubst %.sh,%,$(SCRIPT_SH))
  SCRIPT_PERL_GEN = $(patsubst %.perl,%,$(SCRIPT_PERL))
  SCRIPT_PYTHON_GEN = $(patsubst %.py,%,$(SCRIPT_PYTHON))
  
 -SCRIPT_SH_INS = $(filter-out $(NO_INSTALL),$(SCRIPT_SH_GEN))
 -SCRIPT_PERL_INS = $(filter-out $(NO_INSTALL),$(SCRIPT_PERL_GEN))
 -SCRIPT_PYTHON_INS = $(filter-out $(NO_INSTALL),$(SCRIPT_PYTHON_GEN))
 -
  # Individual rules to allow e.g.
  # "make -C ../.. SCRIPT_PERL=contrib/foo/bar.perl build-perl-script"
  # from subdirectories like contrib/*/
@@@ -651,11 -677,11 +651,11 @@@ build-sh-script: $(SCRIPT_SH_GEN
  build-python-script: $(SCRIPT_PYTHON_GEN)
  
  .PHONY: install-perl-script install-sh-script install-python-script
 -install-sh-script: $(SCRIPT_SH_INS)
 +install-sh-script: $(SCRIPT_SH_GEN)
        $(INSTALL) $^ '$(DESTDIR_SQ)$(gitexec_instdir_SQ)'
 -install-perl-script: $(SCRIPT_PERL_INS)
 +install-perl-script: $(SCRIPT_PERL_GEN)
        $(INSTALL) $^ '$(DESTDIR_SQ)$(gitexec_instdir_SQ)'
 -install-python-script: $(SCRIPT_PYTHON_INS)
 +install-python-script: $(SCRIPT_PYTHON_GEN)
        $(INSTALL) $^ '$(DESTDIR_SQ)$(gitexec_instdir_SQ)'
  
  .PHONY: clean-perl-script clean-sh-script clean-python-script
@@@ -666,9 -692,9 +666,9 @@@ clean-perl-script
  clean-python-script:
        $(RM) $(SCRIPT_PYTHON_GEN)
  
 -SCRIPTS = $(SCRIPT_SH_INS) \
 -        $(SCRIPT_PERL_INS) \
 -        $(SCRIPT_PYTHON_INS) \
 +SCRIPTS = $(SCRIPT_SH_GEN) \
 +        $(SCRIPT_PERL_GEN) \
 +        $(SCRIPT_PYTHON_GEN) \
          git-instaweb
  
  ETAGS_TARGET = TAGS
@@@ -738,7 -764,6 +738,7 @@@ TEST_BUILTINS_OBJS += test-repository.
  TEST_BUILTINS_OBJS += test-revision-walking.o
  TEST_BUILTINS_OBJS += test-run-command.o
  TEST_BUILTINS_OBJS += test-scrap-cache-tree.o
 +TEST_BUILTINS_OBJS += test-serve-v2.o
  TEST_BUILTINS_OBJS += test-sha1.o
  TEST_BUILTINS_OBJS += test-sha1-array.o
  TEST_BUILTINS_OBJS += test-sha256.o
@@@ -817,12 -842,12 +817,12 @@@ VCSSVN_LIB = vcs-svn/lib.
  
  GENERATED_H += command-list.h
  
 -LIB_H := $(shell git ls-files '*.h' ':!t/' ':!Documentation/' 2>/dev/null || \
 +LIB_H := $(sort $(shell git ls-files '*.h' ':!t/' ':!Documentation/' 2>/dev/null || \
        $(FIND) . \
        -name .git -prune -o \
        -name t -prune -o \
        -name Documentation -prune -o \
 -      -name '*.h' -print)
 +      -name '*.h' -print))
  
  LIB_OBJS += abspath.o
  LIB_OBJS += advice.o
@@@ -999,7 -1024,6 +999,7 @@@ LIB_OBJS += trace2/tr2_cfg.
  LIB_OBJS += trace2/tr2_cmd_name.o
  LIB_OBJS += trace2/tr2_dst.o
  LIB_OBJS += trace2/tr2_sid.o
 +LIB_OBJS += trace2/tr2_sysenv.o
  LIB_OBJS += trace2/tr2_tbuf.o
  LIB_OBJS += trace2/tr2_tgt_event.o
  LIB_OBJS += trace2/tr2_tgt_normal.o
@@@ -1106,7 -1130,6 +1106,6 @@@ BUILTIN_OBJS += builtin/push.
  BUILTIN_OBJS += builtin/range-diff.o
  BUILTIN_OBJS += builtin/read-tree.o
  BUILTIN_OBJS += builtin/rebase.o
- BUILTIN_OBJS += builtin/rebase--interactive.o
  BUILTIN_OBJS += builtin/receive-pack.o
  BUILTIN_OBJS += builtin/reflog.o
  BUILTIN_OBJS += builtin/remote.o
@@@ -1121,11 -1144,11 +1120,11 @@@ BUILTIN_OBJS += builtin/rev-parse.
  BUILTIN_OBJS += builtin/revert.o
  BUILTIN_OBJS += builtin/rm.o
  BUILTIN_OBJS += builtin/send-pack.o
 -BUILTIN_OBJS += builtin/serve.o
  BUILTIN_OBJS += builtin/shortlog.o
  BUILTIN_OBJS += builtin/show-branch.o
  BUILTIN_OBJS += builtin/show-index.o
  BUILTIN_OBJS += builtin/show-ref.o
 +BUILTIN_OBJS += builtin/stash.o
  BUILTIN_OBJS += builtin/stripspace.o
  BUILTIN_OBJS += builtin/submodule--helper.o
  BUILTIN_OBJS += builtin/symbolic-ref.o
@@@ -1153,25 -1176,6 +1152,25 @@@ ifeq ($(wildcard sha1collisiondetection
  DC_SHA1_SUBMODULE = auto
  endif
  
 +# Set CFLAGS, LDFLAGS and other *FLAGS variables. These might be
 +# tweaked by config.* below as well as the command-line, both of
 +# which'll override these defaults.
 +CFLAGS = -g -O2 -Wall
 +LDFLAGS =
 +BASIC_CFLAGS = -I.
 +BASIC_LDFLAGS =
 +
 +# library flags
 +ARFLAGS = rcs
 +PTHREAD_CFLAGS =
 +
 +# For the 'sparse' target
 +SPARSE_FLAGS ?=
 +SP_EXTRA_FLAGS =
 +
 +# For the 'coccicheck' target
 +SPATCH_FLAGS = --all-includes --patch .
 +
  include config.mak.uname
  -include config.mak.autogen
  -include config.mak
@@@ -1180,9 -1184,6 +1179,9 @@@ ifdef DEVELOPE
  include config.mak.dev
  endif
  
 +ALL_CFLAGS = $(DEVELOPER_CFLAGS) $(CPPFLAGS) $(CFLAGS)
 +ALL_LDFLAGS = $(LDFLAGS)
 +
  comma := ,
  empty :=
  space := $(empty) $(empty)
@@@ -1193,7 -1194,6 +1192,7 @@@ BASIC_CFLAGS += -fsanitize=$(SANITIZE) 
  BASIC_CFLAGS += -fno-omit-frame-pointer
  ifneq ($(filter undefined,$(SANITIZERS)),)
  BASIC_CFLAGS += -DNO_UNALIGNED_LOADS
 +BASIC_CFLAGS += -DSHA1DC_FORCE_ALIGNED_ACCESS
  endif
  ifneq ($(filter leak,$(SANITIZERS)),)
  BASIC_CFLAGS += -DSUPPRESS_ANNOTATED_LEAKS
@@@ -1337,7 -1337,6 +1336,7 @@@ ifdef NO_CUR
        REMOTE_CURL_PRIMARY =
        REMOTE_CURL_ALIASES =
        REMOTE_CURL_NAMES =
 +      EXCLUDED_PROGRAMS += git-http-fetch git-http-push
  else
        ifdef CURLDIR
                # Try "-Wl,-rpath=$(CURLDIR)/$(lib)" in such a case.
@@@ -1362,11 -1361,7 +1361,11 @@@ endi
        ifeq "$(curl_check)" "070908"
                ifndef NO_EXPAT
                        PROGRAM_OBJS += http-push.o
 +              else
 +                      EXCLUDED_PROGRAMS += git-http-push
                endif
 +      else
 +              EXCLUDED_PROGRAMS += git-http-push
        endif
        curl_check := $(shell (echo 072200; $(CURL_CONFIG) --vernum | sed -e '/^70[BC]/s/^/0/') 2>/dev/null | sort -r | sed -ne 2p)
        ifeq "$(curl_check)" "072200"
@@@ -1614,7 -1609,6 +1613,7 @@@ ifdef NO_INET_PTO
  endif
  ifdef NO_UNIX_SOCKETS
        BASIC_CFLAGS += -DNO_UNIX_SOCKETS
 +      EXCLUDED_PROGRAMS += git-credential-cache git-credential-cache--daemon
  else
        LIB_OBJS += unix-socket.o
        PROGRAM_OBJS += credential-cache.o
@@@ -2134,9 -2128,7 +2133,9 @@@ $(BUILT_INS): git$
  command-list.h: generate-cmdlist.sh command-list.txt
  
  command-list.h: $(wildcard Documentation/git*.txt) Documentation/*config.txt Documentation/config/*.txt
 -      $(QUIET_GEN)$(SHELL_PATH) ./generate-cmdlist.sh command-list.txt >$@+ && mv $@+ $@
 +      $(QUIET_GEN)$(SHELL_PATH) ./generate-cmdlist.sh \
 +              $(patsubst %,--exclude-program %,$(EXCLUDED_PROGRAMS)) \
 +              command-list.txt >$@+ && mv $@+ $@
  
  SCRIPT_DEFINES = $(SHELL_PATH_SQ):$(DIFF_SQ):$(GIT_VERSION):\
        $(localedir_SQ):$(NO_CURL):$(USE_GETTEXT_SCHEME):$(SANE_TOOL_PATH_SQ):\
@@@ -2469,14 -2461,6 +2468,14 @@@ $(VCSSVN_LIB): $(VCSSVN_OBJS
  
  export DEFAULT_EDITOR DEFAULT_PAGER
  
 +Documentation/GIT-EXCLUDED-PROGRAMS: FORCE
 +      @EXCLUDED='EXCLUDED_PROGRAMS := $(EXCLUDED_PROGRAMS)'; \
 +          if test x"$$EXCLUDED" != \
 +              x"`cat Documentation/GIT-EXCLUDED-PROGRAMS 2>/dev/null`" ; then \
 +              echo >&2 "    * new documentation flags"; \
 +              echo "$$EXCLUDED" >Documentation/GIT-EXCLUDED-PROGRAMS; \
 +            fi
 +
  .PHONY: doc man man-perl html info pdf
  doc: man-perl
        $(MAKE) -C Documentation all
@@@ -2715,6 -2699,7 +2714,6 @@@ endi
  test_bindir_programs := $(patsubst %,bin-wrappers/%,$(BINDIR_PROGRAMS_NEED_X) $(BINDIR_PROGRAMS_NO_X) $(TEST_PROGRAMS_NEED_X))
  
  all:: $(TEST_PROGRAMS) $(test_bindir_programs)
 -all:: $(NO_INSTALL)
  
  bin-wrappers/%: wrap-for-bin.sh
        @mkdir -p bin-wrappers
@@@ -3001,7 -2986,7 +3000,7 @@@ rpm:
  
  artifacts-tar:: $(ALL_PROGRAMS) $(SCRIPT_LIB) $(BUILT_INS) $(OTHER_PROGRAMS) \
                GIT-BUILD-OPTIONS $(TEST_PROGRAMS) $(test_bindir_programs) \
 -              $(NO_INSTALL) $(MOFILES)
 +              $(MOFILES)
        $(QUIET_SUBDIR0)templates $(QUIET_SUBDIR1) \
                SHELL_PATH='$(SHELL_PATH_SQ)' PERL_PATH='$(PERL_PATH_SQ)'
        test -n "$(ARTIFACTS_DIRECTORY)"
@@@ -3050,7 -3035,7 +3049,7 @@@ clean: profile-clean coverage-clean coc
        $(RM) $(OBJECTS)
        $(RM) $(LIB_FILE) $(XDIFF_LIB) $(VCSSVN_LIB)
        $(RM) $(ALL_PROGRAMS) $(SCRIPT_LIB) $(BUILT_INS) git$X
 -      $(RM) $(TEST_PROGRAMS) $(NO_INSTALL)
 +      $(RM) $(TEST_PROGRAMS)
        $(RM) $(FUZZ_PROGRAMS)
        $(RM) -r bin-wrappers $(dep_dirs)
        $(RM) -r po/build/
        $(RM) $(GIT_TARNAME).tar.gz git-core_$(GIT_VERSION)-*.tar.gz
        $(RM) $(htmldocs).tar.gz $(manpages).tar.gz
        $(MAKE) -C Documentation/ clean
 +      $(RM) Documentation/GIT-EXCLUDED-PROGRAMS
  ifndef NO_PERL
        $(MAKE) -C gitweb clean
        $(RM) -r perl/build/
@@@ -3089,13 -3073,13 +3088,13 @@@ ALL_COMMANDS += git-gui git-citoo
  .PHONY: check-docs
  check-docs::
        $(MAKE) -C Documentation lint-docs
 -      @(for v in $(ALL_COMMANDS); \
 +      @(for v in $(patsubst %$X,%,$(ALL_COMMANDS)); \
        do \
                case "$$v" in \
                git-merge-octopus | git-merge-ours | git-merge-recursive | \
                git-merge-resolve | git-merge-subtree | \
                git-fsck-objects | git-init-db | \
 -              git-remote-* | git-stage | \
 +              git-remote-* | git-stage | git-legacy-* | \
                git-?*--?* ) continue ;; \
                esac ; \
                test -f "Documentation/$$v.txt" || \
        ( \
                sed -e '1,/^### command list/d' \
                    -e '/^#/d' \
 +                  -e '/guide$$/d' \
                    -e 's/[     ].*//' \
                    -e 's/^/listed /' command-list.txt; \
                $(MAKE) -C Documentation print-man1 | \
                grep '\.txt$$' | \
 -              sed -e 's|Documentation/|documented |' \
 +              sed -e 's|^|documented |' \
                    -e 's/\.txt//'; \
        ) | while read how cmd; \
        do \
 -              case " $(ALL_COMMANDS) " in \
 +              case " $(patsubst %$X,%,$(ALL_COMMANDS) $(EXCLUDED_PROGRAMS)) " in \
                *" $$cmd "*)    ;; \
                *) echo "removed but $$how: $$cmd" ;; \
                esac; \
diff --combined builtin/rebase.c
index 966b953e86cc90c3683e4de01ef4b18a21229c74,cd233c61188ebdb8cfcde503c21a5fdbf5e474aa..ba3a574e4022c3dbd0e037782f520b1a199da42e
@@@ -25,6 -25,8 +25,8 @@@
  #include "commit-reach.h"
  #include "rerere.h"
  #include "branch.h"
+ #include "sequencer.h"
+ #include "rebase-interactive.h"
  
  static char const * const builtin_rebase_usage[] = {
        N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
@@@ -35,6 -37,8 +37,8 @@@
        NULL
  };
  
+ static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto")
+ static GIT_PATH_FUNC(path_interactive, "rebase-merge/interactive")
  static GIT_PATH_FUNC(apply_dir, "rebase-apply")
  static GIT_PATH_FUNC(merge_dir, "rebase-merge")
  
@@@ -83,9 -87,462 +87,440 @@@ struct rebase_options 
        char *strategy, *strategy_opts;
        struct strbuf git_format_patch_opt;
        int reschedule_failed_exec;
 +      int use_legacy_rebase;
  };
  
 -static int use_builtin_rebase(void)
 -{
 -      struct child_process cp = CHILD_PROCESS_INIT;
 -      struct strbuf out = STRBUF_INIT;
 -      int ret, env = git_env_bool("GIT_TEST_REBASE_USE_BUILTIN", -1);
 -
 -      if (env != -1)
 -              return env;
 -
 -      argv_array_pushl(&cp.args,
 -                       "config", "--bool", "rebase.usebuiltin", NULL);
 -      cp.git_cmd = 1;
 -      if (capture_command(&cp, &out, 6)) {
 -              strbuf_release(&out);
 -              return 1;
 -      }
 -
 -      strbuf_trim(&out);
 -      ret = !strcmp("true", out.buf);
 -      strbuf_release(&out);
 -      return ret;
 -}
 -
+ #define REBASE_OPTIONS_INIT {                         \
+               .type = REBASE_UNSPECIFIED,             \
+               .flags = REBASE_NO_QUIET,               \
+               .git_am_opts = ARGV_ARRAY_INIT,         \
+               .git_format_patch_opt = STRBUF_INIT     \
+       }
+ static struct replay_opts get_replay_opts(const struct rebase_options *opts)
+ {
+       struct replay_opts replay = REPLAY_OPTS_INIT;
+       replay.action = REPLAY_INTERACTIVE_REBASE;
+       sequencer_init_config(&replay);
+       replay.signoff = opts->signoff;
+       replay.allow_ff = !(opts->flags & REBASE_FORCE);
+       if (opts->allow_rerere_autoupdate)
+               replay.allow_rerere_auto = opts->allow_rerere_autoupdate;
+       replay.allow_empty = 1;
+       replay.allow_empty_message = opts->allow_empty_message;
+       replay.verbose = opts->flags & REBASE_VERBOSE;
+       replay.reschedule_failed_exec = opts->reschedule_failed_exec;
+       replay.gpg_sign = xstrdup_or_null(opts->gpg_sign_opt);
+       replay.strategy = opts->strategy;
+       if (opts->strategy_opts)
+               parse_strategy_opts(&replay, opts->strategy_opts);
+       return replay;
+ }
+ enum action {
+       ACTION_NONE = 0,
+       ACTION_CONTINUE,
+       ACTION_SKIP,
+       ACTION_ABORT,
+       ACTION_QUIT,
+       ACTION_EDIT_TODO,
+       ACTION_SHOW_CURRENT_PATCH,
+       ACTION_SHORTEN_OIDS,
+       ACTION_EXPAND_OIDS,
+       ACTION_CHECK_TODO_LIST,
+       ACTION_REARRANGE_SQUASH,
+       ACTION_ADD_EXEC
+ };
+ static const char *action_names[] = { "undefined",
+                                     "continue",
+                                     "skip",
+                                     "abort",
+                                     "quit",
+                                     "edit_todo",
+                                     "show_current_patch" };
+ static int add_exec_commands(struct string_list *commands)
+ {
+       const char *todo_file = rebase_path_todo();
+       struct todo_list todo_list = TODO_LIST_INIT;
+       int res;
+       if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
+               return error_errno(_("could not read '%s'."), todo_file);
+       if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
+                                       &todo_list)) {
+               todo_list_release(&todo_list);
+               return error(_("unusable todo list: '%s'"), todo_file);
+       }
+       todo_list_add_exec_commands(&todo_list, commands);
+       res = todo_list_write_to_file(the_repository, &todo_list,
+                                     todo_file, NULL, NULL, -1, 0);
+       todo_list_release(&todo_list);
+       if (res)
+               return error_errno(_("could not write '%s'."), todo_file);
+       return 0;
+ }
+ static int rearrange_squash_in_todo_file(void)
+ {
+       const char *todo_file = rebase_path_todo();
+       struct todo_list todo_list = TODO_LIST_INIT;
+       int res = 0;
+       if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
+               return error_errno(_("could not read '%s'."), todo_file);
+       if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
+                                       &todo_list)) {
+               todo_list_release(&todo_list);
+               return error(_("unusable todo list: '%s'"), todo_file);
+       }
+       res = todo_list_rearrange_squash(&todo_list);
+       if (!res)
+               res = todo_list_write_to_file(the_repository, &todo_list,
+                                             todo_file, NULL, NULL, -1, 0);
+       todo_list_release(&todo_list);
+       if (res)
+               return error_errno(_("could not write '%s'."), todo_file);
+       return 0;
+ }
+ static int transform_todo_file(unsigned flags)
+ {
+       const char *todo_file = rebase_path_todo();
+       struct todo_list todo_list = TODO_LIST_INIT;
+       int res;
+       if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
+               return error_errno(_("could not read '%s'."), todo_file);
+       if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
+                                       &todo_list)) {
+               todo_list_release(&todo_list);
+               return error(_("unusable todo list: '%s'"), todo_file);
+       }
+       res = todo_list_write_to_file(the_repository, &todo_list, todo_file,
+                                     NULL, NULL, -1, flags);
+       todo_list_release(&todo_list);
+       if (res)
+               return error_errno(_("could not write '%s'."), todo_file);
+       return 0;
+ }
+ static int edit_todo_file(unsigned flags)
+ {
+       const char *todo_file = rebase_path_todo();
+       struct todo_list todo_list = TODO_LIST_INIT,
+               new_todo = TODO_LIST_INIT;
+       int res = 0;
+       if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
+               return error_errno(_("could not read '%s'."), todo_file);
+       strbuf_stripspace(&todo_list.buf, 1);
+       res = edit_todo_list(the_repository, &todo_list, &new_todo, NULL, NULL, flags);
+       if (!res && todo_list_write_to_file(the_repository, &new_todo, todo_file,
+                                           NULL, NULL, -1, flags & ~(TODO_LIST_SHORTEN_IDS)))
+               res = error_errno(_("could not write '%s'"), todo_file);
+       todo_list_release(&todo_list);
+       todo_list_release(&new_todo);
+       return res;
+ }
+ static int get_revision_ranges(struct commit *upstream, struct commit *onto,
+                              const char **head_hash,
+                              char **revisions, char **shortrevisions)
+ {
+       struct commit *base_rev = upstream ? upstream : onto;
+       const char *shorthead;
+       struct object_id orig_head;
+       if (get_oid("HEAD", &orig_head))
+               return error(_("no HEAD?"));
+       *head_hash = find_unique_abbrev(&orig_head, GIT_MAX_HEXSZ);
+       *revisions = xstrfmt("%s...%s", oid_to_hex(&base_rev->object.oid),
+                                                  *head_hash);
+       shorthead = find_unique_abbrev(&orig_head, DEFAULT_ABBREV);
+       if (upstream) {
+               const char *shortrev;
+               shortrev = find_unique_abbrev(&base_rev->object.oid,
+                                             DEFAULT_ABBREV);
+               *shortrevisions = xstrfmt("%s..%s", shortrev, shorthead);
+       } else
+               *shortrevisions = xstrdup(shorthead);
+       return 0;
+ }
+ static int init_basic_state(struct replay_opts *opts, const char *head_name,
+                           struct commit *onto, const char *orig_head)
+ {
+       FILE *interactive;
+       if (!is_directory(merge_dir()) && mkdir_in_gitdir(merge_dir()))
+               return error_errno(_("could not create temporary %s"), merge_dir());
+       delete_reflog("REBASE_HEAD");
+       interactive = fopen(path_interactive(), "w");
+       if (!interactive)
+               return error_errno(_("could not mark as interactive"));
+       fclose(interactive);
+       return write_basic_state(opts, head_name, onto, orig_head);
+ }
+ static void split_exec_commands(const char *cmd, struct string_list *commands)
+ {
+       if (cmd && *cmd) {
+               string_list_split(commands, cmd, '\n', -1);
+               /* rebase.c adds a new line to cmd after every command,
+                * so here the last command is always empty */
+               string_list_remove_empty_items(commands, 0);
+       }
+ }
+ static int do_interactive_rebase(struct rebase_options *opts, unsigned flags)
+ {
+       int ret;
+       const char *head_hash = NULL;
+       char *revisions = NULL, *shortrevisions = NULL;
+       struct argv_array make_script_args = ARGV_ARRAY_INIT;
+       struct todo_list todo_list = TODO_LIST_INIT;
+       struct replay_opts replay = get_replay_opts(opts);
+       struct string_list commands = STRING_LIST_INIT_DUP;
+       if (prepare_branch_to_be_rebased(the_repository, &replay,
+                                        opts->switch_to))
+               return -1;
+       if (get_revision_ranges(opts->upstream, opts->onto, &head_hash,
+                               &revisions, &shortrevisions))
+               return -1;
+       if (init_basic_state(&replay,
+                            opts->head_name ? opts->head_name : "detached HEAD",
+                            opts->onto, head_hash)) {
+               free(revisions);
+               free(shortrevisions);
+               return -1;
+       }
+       if (!opts->upstream && opts->squash_onto)
+               write_file(path_squash_onto(), "%s\n",
+                          oid_to_hex(opts->squash_onto));
+       argv_array_pushl(&make_script_args, "", revisions, NULL);
+       if (opts->restrict_revision)
+               argv_array_push(&make_script_args,
+                               oid_to_hex(&opts->restrict_revision->object.oid));
+       ret = sequencer_make_script(the_repository, &todo_list.buf,
+                                   make_script_args.argc, make_script_args.argv,
+                                   flags);
+       if (ret)
+               error(_("could not generate todo list"));
+       else {
+               discard_cache();
+               if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
+                                               &todo_list))
+                       BUG("unusable todo list");
+               split_exec_commands(opts->cmd, &commands);
+               ret = complete_action(the_repository, &replay, flags,
+                       shortrevisions, opts->onto_name, opts->onto, head_hash,
+                       &commands, opts->autosquash, &todo_list);
+       }
+       string_list_clear(&commands, 0);
+       free(revisions);
+       free(shortrevisions);
+       todo_list_release(&todo_list);
+       argv_array_clear(&make_script_args);
+       return ret;
+ }
+ static int run_rebase_interactive(struct rebase_options *opts,
+                                 enum action command)
+ {
+       unsigned flags = 0;
+       int abbreviate_commands = 0, ret = 0;
+       git_config_get_bool("rebase.abbreviatecommands", &abbreviate_commands);
+       flags |= opts->keep_empty ? TODO_LIST_KEEP_EMPTY : 0;
+       flags |= abbreviate_commands ? TODO_LIST_ABBREVIATE_CMDS : 0;
+       flags |= opts->rebase_merges ? TODO_LIST_REBASE_MERGES : 0;
+       flags |= opts->rebase_cousins > 0 ? TODO_LIST_REBASE_COUSINS : 0;
+       flags |= command == ACTION_SHORTEN_OIDS ? TODO_LIST_SHORTEN_IDS : 0;
+       switch (command) {
+       case ACTION_NONE: {
+               if (!opts->onto && !opts->upstream)
+                       die(_("a base commit must be provided with --upstream or --onto"));
+               ret = do_interactive_rebase(opts, flags);
+               break;
+       }
+       case ACTION_SKIP: {
+               struct string_list merge_rr = STRING_LIST_INIT_DUP;
+               rerere_clear(the_repository, &merge_rr);
+       }
+               /* fallthrough */
+       case ACTION_CONTINUE: {
+               struct replay_opts replay_opts = get_replay_opts(opts);
+               ret = sequencer_continue(the_repository, &replay_opts);
+               break;
+       }
+       case ACTION_EDIT_TODO:
+               ret = edit_todo_file(flags);
+               break;
+       case ACTION_SHOW_CURRENT_PATCH: {
+               struct child_process cmd = CHILD_PROCESS_INIT;
+               cmd.git_cmd = 1;
+               argv_array_pushl(&cmd.args, "show", "REBASE_HEAD", "--", NULL);
+               ret = run_command(&cmd);
+               break;
+       }
+       case ACTION_SHORTEN_OIDS:
+       case ACTION_EXPAND_OIDS:
+               ret = transform_todo_file(flags);
+               break;
+       case ACTION_CHECK_TODO_LIST:
+               ret = check_todo_list_from_file(the_repository);
+               break;
+       case ACTION_REARRANGE_SQUASH:
+               ret = rearrange_squash_in_todo_file();
+               break;
+       case ACTION_ADD_EXEC: {
+               struct string_list commands = STRING_LIST_INIT_DUP;
+               split_exec_commands(opts->cmd, &commands);
+               ret = add_exec_commands(&commands);
+               string_list_clear(&commands, 0);
+               break;
+       }
+       default:
+               BUG("invalid command '%d'", command);
+       }
+       return ret;
+ }
+ static const char * const builtin_rebase_interactive_usage[] = {
+       N_("git rebase--interactive [<options>]"),
+       NULL
+ };
+ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix)
+ {
+       struct rebase_options opts = REBASE_OPTIONS_INIT;
+       struct object_id squash_onto = null_oid;
+       enum action command = ACTION_NONE;
+       struct option options[] = {
+               OPT_NEGBIT(0, "ff", &opts.flags, N_("allow fast-forward"),
+                          REBASE_FORCE),
+               OPT_BOOL(0, "keep-empty", &opts.keep_empty, N_("keep empty commits")),
+               OPT_BOOL(0, "allow-empty-message", &opts.allow_empty_message,
+                        N_("allow commits with empty messages")),
+               OPT_BOOL(0, "rebase-merges", &opts.rebase_merges, N_("rebase merge commits")),
+               OPT_BOOL(0, "rebase-cousins", &opts.rebase_cousins,
+                        N_("keep original branch points of cousins")),
+               OPT_BOOL(0, "autosquash", &opts.autosquash,
+                        N_("move commits that begin with squash!/fixup!")),
+               OPT_BOOL(0, "signoff", &opts.signoff, N_("sign commits")),
+               OPT_BIT('v', "verbose", &opts.flags,
+                       N_("display a diffstat of what changed upstream"),
+                       REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
+               OPT_CMDMODE(0, "continue", &command, N_("continue rebase"),
+                           ACTION_CONTINUE),
+               OPT_CMDMODE(0, "skip", &command, N_("skip commit"), ACTION_SKIP),
+               OPT_CMDMODE(0, "edit-todo", &command, N_("edit the todo list"),
+                           ACTION_EDIT_TODO),
+               OPT_CMDMODE(0, "show-current-patch", &command, N_("show the current patch"),
+                           ACTION_SHOW_CURRENT_PATCH),
+               OPT_CMDMODE(0, "shorten-ids", &command,
+                       N_("shorten commit ids in the todo list"), ACTION_SHORTEN_OIDS),
+               OPT_CMDMODE(0, "expand-ids", &command,
+                       N_("expand commit ids in the todo list"), ACTION_EXPAND_OIDS),
+               OPT_CMDMODE(0, "check-todo-list", &command,
+                       N_("check the todo list"), ACTION_CHECK_TODO_LIST),
+               OPT_CMDMODE(0, "rearrange-squash", &command,
+                       N_("rearrange fixup/squash lines"), ACTION_REARRANGE_SQUASH),
+               OPT_CMDMODE(0, "add-exec-commands", &command,
+                       N_("insert exec commands in todo list"), ACTION_ADD_EXEC),
+               { OPTION_CALLBACK, 0, "onto", &opts.onto, N_("onto"), N_("onto"),
+                 PARSE_OPT_NONEG, parse_opt_commit, 0 },
+               { OPTION_CALLBACK, 0, "restrict-revision", &opts.restrict_revision,
+                 N_("restrict-revision"), N_("restrict revision"),
+                 PARSE_OPT_NONEG, parse_opt_commit, 0 },
+               { OPTION_CALLBACK, 0, "squash-onto", &squash_onto, N_("squash-onto"),
+                 N_("squash onto"), PARSE_OPT_NONEG, parse_opt_object_id, 0 },
+               { OPTION_CALLBACK, 0, "upstream", &opts.upstream, N_("upstream"),
+                 N_("the upstream commit"), PARSE_OPT_NONEG, parse_opt_commit,
+                 0 },
+               OPT_STRING(0, "head-name", &opts.head_name, N_("head-name"), N_("head name")),
+               { OPTION_STRING, 'S', "gpg-sign", &opts.gpg_sign_opt, N_("key-id"),
+                       N_("GPG-sign commits"),
+                       PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
+               OPT_STRING(0, "strategy", &opts.strategy, N_("strategy"),
+                          N_("rebase strategy")),
+               OPT_STRING(0, "strategy-opts", &opts.strategy_opts, N_("strategy-opts"),
+                          N_("strategy options")),
+               OPT_STRING(0, "switch-to", &opts.switch_to, N_("switch-to"),
+                          N_("the branch or commit to checkout")),
+               OPT_STRING(0, "onto-name", &opts.onto_name, N_("onto-name"), N_("onto name")),
+               OPT_STRING(0, "cmd", &opts.cmd, N_("cmd"), N_("the command to run")),
+               OPT_RERERE_AUTOUPDATE(&opts.allow_rerere_autoupdate),
+               OPT_BOOL(0, "reschedule-failed-exec", &opts.reschedule_failed_exec,
+                        N_("automatically re-schedule any `exec` that fails")),
+               OPT_END()
+       };
+       opts.rebase_cousins = -1;
+       if (argc == 1)
+               usage_with_options(builtin_rebase_interactive_usage, options);
+       argc = parse_options(argc, argv, NULL, options,
+                       builtin_rebase_interactive_usage, PARSE_OPT_KEEP_ARGV0);
+       if (!is_null_oid(&squash_onto))
+               opts.squash_onto = &squash_onto;
+       if (opts.rebase_cousins >= 0 && !opts.rebase_merges)
+               warning(_("--[no-]rebase-cousins has no effect without "
+                         "--rebase-merges"));
+       return !!run_rebase_interactive(&opts, command);
+ }
  static int is_interactive(struct rebase_options *opts)
  {
        return opts->type == REBASE_INTERACTIVE ||
@@@ -184,14 -641,13 +619,13 @@@ static int read_basic_state(struct reba
                            &buf))
                        return -1;
                if (!strcmp(buf.buf, "--rerere-autoupdate"))
-                       opts->allow_rerere_autoupdate = 1;
+                       opts->allow_rerere_autoupdate = RERERE_AUTOUPDATE;
                else if (!strcmp(buf.buf, "--no-rerere-autoupdate"))
-                       opts->allow_rerere_autoupdate = 0;
+                       opts->allow_rerere_autoupdate = RERERE_NOAUTOUPDATE;
                else
                        warning(_("ignoring invalid allow_rerere_autoupdate: "
                                  "'%s'"), buf.buf);
-       } else
-               opts->allow_rerere_autoupdate = -1;
+       }
  
        if (file_exists(state_dir_path("gpg_sign_opt", opts))) {
                strbuf_reset(&buf);
        return 0;
  }
  
- static int write_basic_state(struct rebase_options *opts)
+ static int rebase_write_basic_state(struct rebase_options *opts)
  {
        write_file(state_dir_path("head-name", opts), "%s",
                   opts->head_name ? opts->head_name : "detached HEAD");
        if (opts->strategy_opts)
                write_file(state_dir_path("strategy_opts", opts), "%s",
                           opts->strategy_opts);
-       if (opts->allow_rerere_autoupdate >= 0)
+       if (opts->allow_rerere_autoupdate > 0)
                write_file(state_dir_path("allow_rerere_autoupdate", opts),
                           "-%s-rerere-autoupdate",
-                          opts->allow_rerere_autoupdate ? "" : "-no");
+                          opts->allow_rerere_autoupdate == RERERE_AUTOUPDATE ?
+                               "" : "-no");
        if (opts->gpg_sign_opt)
                write_file(state_dir_path("gpg_sign_opt", opts), "%s",
                           opts->gpg_sign_opt);
@@@ -347,7 -804,6 +782,7 @@@ static void add_var(struct strbuf *buf
  #define RESET_HEAD_HARD (1<<1)
  #define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2)
  #define RESET_HEAD_REFS_ONLY (1<<3)
 +#define RESET_ORIG_HEAD (1<<4)
  
  static int reset_head(struct object_id *oid, const char *action,
                      const char *switch_to_branch, unsigned flags,
        unsigned reset_hard = flags & RESET_HEAD_HARD;
        unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
        unsigned refs_only = flags & RESET_HEAD_REFS_ONLY;
 +      unsigned update_orig_head = flags & RESET_ORIG_HEAD;
        struct object_id head_oid;
        struct tree_desc desc[2] = { { NULL }, { NULL } };
        struct lock_file lock = LOCK_INIT;
@@@ -434,21 -889,18 +869,21 @@@ reset_head_refs
        strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
        prefix_len = msg.len;
  
 -      if (!get_oid("ORIG_HEAD", &oid_old_orig))
 -              old_orig = &oid_old_orig;
 -      if (!get_oid("HEAD", &oid_orig)) {
 -              orig = &oid_orig;
 -              if (!reflog_orig_head) {
 -                      strbuf_addstr(&msg, "updating ORIG_HEAD");
 -                      reflog_orig_head = msg.buf;
 -              }
 -              update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0,
 -                         UPDATE_REFS_MSG_ON_ERR);
 -      } else if (old_orig)
 -              delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
 +      if (update_orig_head) {
 +              if (!get_oid("ORIG_HEAD", &oid_old_orig))
 +                      old_orig = &oid_old_orig;
 +              if (!get_oid("HEAD", &oid_orig)) {
 +                      orig = &oid_orig;
 +                      if (!reflog_orig_head) {
 +                              strbuf_addstr(&msg, "updating ORIG_HEAD");
 +                              reflog_orig_head = msg.buf;
 +                      }
 +                      update_ref(reflog_orig_head, "ORIG_HEAD", orig,
 +                                 old_orig, 0, UPDATE_REFS_MSG_ON_ERR);
 +              } else if (old_orig)
 +                      delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
 +      }
 +
        if (!reflog_head) {
                strbuf_setlen(&msg, prefix_len);
                strbuf_addstr(&msg, "updating HEAD");
                                 detach_head ? REF_NO_DEREF : 0,
                                 UPDATE_REFS_MSG_ON_ERR);
        else {
 -              ret = update_ref(reflog_orig_head, switch_to_branch, oid,
 +              ret = update_ref(reflog_head, switch_to_branch, oid,
                                 NULL, 0, UPDATE_REFS_MSG_ON_ERR);
                if (!ret)
                        ret = create_symref("HEAD", switch_to_branch,
@@@ -608,9 -1060,9 +1043,9 @@@ static int run_am(struct rebase_option
        argv_array_push(&am.args, "--rebasing");
        argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
        argv_array_push(&am.args, "--patch-format=mboxrd");
-       if (opts->allow_rerere_autoupdate > 0)
+       if (opts->allow_rerere_autoupdate == RERERE_AUTOUPDATE)
                argv_array_push(&am.args, "--rerere-autoupdate");
-       else if (opts->allow_rerere_autoupdate == 0)
+       else if (opts->allow_rerere_autoupdate == RERERE_NOAUTOUPDATE)
                argv_array_push(&am.args, "--no-rerere-autoupdate");
        if (opts->gpg_sign_opt)
                argv_array_push(&am.args, opts->gpg_sign_opt);
        }
  
        if (is_directory(opts->state_dir))
-               write_basic_state(opts);
+               rebase_write_basic_state(opts);
  
        return status;
  }
  
- static int run_specific_rebase(struct rebase_options *opts)
+ static int run_specific_rebase(struct rebase_options *opts, enum action action)
  {
        const char *argv[] = { NULL, NULL };
        struct strbuf script_snippet = STRBUF_INIT, buf = STRBUF_INIT;
  
        if (opts->type == REBASE_INTERACTIVE) {
                /* Run builtin interactive rebase */
-               struct child_process child = CHILD_PROCESS_INIT;
-               argv_array_pushf(&child.env_array, "GIT_CHERRY_PICK_HELP=%s",
-                                resolvemsg);
+               setenv("GIT_CHERRY_PICK_HELP", resolvemsg, 1);
                if (!(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) {
-                       argv_array_push(&child.env_array,
-                                       "GIT_SEQUENCE_EDITOR=:");
+                       setenv("GIT_SEQUENCE_EDITOR", ":", 1);
                        opts->autosquash = 0;
                }
+               if (opts->gpg_sign_opt) {
+                       /* remove the leading "-S" */
+                       char *tmp = xstrdup(opts->gpg_sign_opt + 2);
+                       free(opts->gpg_sign_opt);
+                       opts->gpg_sign_opt = tmp;
+               }
  
-               child.git_cmd = 1;
-               argv_array_push(&child.args, "rebase--interactive");
-               if (opts->action)
-                       argv_array_pushf(&child.args, "--%s", opts->action);
-               if (opts->keep_empty)
-                       argv_array_push(&child.args, "--keep-empty");
-               if (opts->rebase_merges)
-                       argv_array_push(&child.args, "--rebase-merges");
-               if (opts->rebase_cousins)
-                       argv_array_push(&child.args, "--rebase-cousins");
-               if (opts->autosquash)
-                       argv_array_push(&child.args, "--autosquash");
-               if (opts->flags & REBASE_VERBOSE)
-                       argv_array_push(&child.args, "--verbose");
-               if (opts->flags & REBASE_FORCE)
-                       argv_array_push(&child.args, "--no-ff");
-               if (opts->restrict_revision)
-                       argv_array_pushf(&child.args,
-                                        "--restrict-revision=^%s",
-                                        oid_to_hex(&opts->restrict_revision->object.oid));
-               if (opts->upstream)
-                       argv_array_pushf(&child.args, "--upstream=%s",
-                                        oid_to_hex(&opts->upstream->object.oid));
-               if (opts->onto)
-                       argv_array_pushf(&child.args, "--onto=%s",
-                                        oid_to_hex(&opts->onto->object.oid));
-               if (opts->squash_onto)
-                       argv_array_pushf(&child.args, "--squash-onto=%s",
-                                        oid_to_hex(opts->squash_onto));
-               if (opts->onto_name)
-                       argv_array_pushf(&child.args, "--onto-name=%s",
-                                        opts->onto_name);
-               argv_array_pushf(&child.args, "--head-name=%s",
-                                opts->head_name ?
-                                opts->head_name : "detached HEAD");
-               if (opts->strategy)
-                       argv_array_pushf(&child.args, "--strategy=%s",
-                                        opts->strategy);
-               if (opts->strategy_opts)
-                       argv_array_pushf(&child.args, "--strategy-opts=%s",
-                                        opts->strategy_opts);
-               if (opts->switch_to)
-                       argv_array_pushf(&child.args, "--switch-to=%s",
-                                        opts->switch_to);
-               if (opts->cmd)
-                       argv_array_pushf(&child.args, "--cmd=%s", opts->cmd);
-               if (opts->allow_empty_message)
-                       argv_array_push(&child.args, "--allow-empty-message");
-               if (opts->allow_rerere_autoupdate > 0)
-                       argv_array_push(&child.args, "--rerere-autoupdate");
-               else if (opts->allow_rerere_autoupdate == 0)
-                       argv_array_push(&child.args, "--no-rerere-autoupdate");
-               if (opts->gpg_sign_opt)
-                       argv_array_push(&child.args, opts->gpg_sign_opt);
-               if (opts->signoff)
-                       argv_array_push(&child.args, "--signoff");
-               if (opts->reschedule_failed_exec)
-                       argv_array_push(&child.args, "--reschedule-failed-exec");
-               status = run_command(&child);
+               status = run_rebase_interactive(opts, action);
                goto finished_rebase;
        }
  
        add_var(&script_snippet, "action", opts->action ? opts->action : "");
        add_var(&script_snippet, "signoff", opts->signoff ? "--signoff" : "");
        add_var(&script_snippet, "allow_rerere_autoupdate",
-               opts->allow_rerere_autoupdate < 0 ? "" :
                opts->allow_rerere_autoupdate ?
-               "--rerere-autoupdate" : "--no-rerere-autoupdate");
+                       opts->allow_rerere_autoupdate == RERERE_AUTOUPDATE ?
+                       "--rerere-autoupdate" : "--no-rerere-autoupdate" : "");
        add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : "");
        add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : "");
        add_var(&script_snippet, "gpg_sign_opt", opts->gpg_sign_opt);
@@@ -852,11 -1246,6 +1229,11 @@@ static int rebase_config(const char *va
                return 0;
        }
  
 +      if (!strcmp(var, "rebase.usebuiltin")) {
 +              opts->use_legacy_rebase = !git_config_bool(var, value);
 +              return 0;
 +      }
 +
        return git_default_config(var, value, data);
  }
  
@@@ -991,14 -1380,7 +1368,7 @@@ static int check_exec_cmd(const char *c
  
  int cmd_rebase(int argc, const char **argv, const char *prefix)
  {
-       struct rebase_options options = {
-               .type = REBASE_UNSPECIFIED,
-               .flags = REBASE_NO_QUIET,
-               .git_am_opts = ARGV_ARRAY_INIT,
-               .allow_rerere_autoupdate  = -1,
-               .allow_empty_message = 1,
-               .git_format_patch_opt = STRBUF_INIT,
-       };
+       struct rebase_options options = REBASE_OPTIONS_INIT;
        const char *branch_name;
        int ret, flags, total_argc, in_progress = 0;
        int ok_to_skip_pre_rebase = 0;
        struct strbuf revisions = STRBUF_INIT;
        struct strbuf buf = STRBUF_INIT;
        struct object_id merge_base;
-       enum {
-               NO_ACTION,
-               ACTION_CONTINUE,
-               ACTION_SKIP,
-               ACTION_ABORT,
-               ACTION_QUIT,
-               ACTION_EDIT_TODO,
-               ACTION_SHOW_CURRENT_PATCH,
-       } action = NO_ACTION;
-       static const char *action_names[] = { N_("undefined"),
-                                             N_("continue"),
-                                             N_("skip"),
-                                             N_("abort"),
-                                             N_("quit"),
-                                             N_("edit_todo"),
-                                             N_("show_current_patch"),
-                                             NULL };
+       enum action action = ACTION_NONE;
        const char *gpg_sign = NULL;
        struct string_list exec = STRING_LIST_INIT_NODUP;
        const char *rebase_merges = NULL;
                        PARSE_OPT_NOARG | PARSE_OPT_NONEG,
                        parse_opt_interactive },
                OPT_SET_INT('p', "preserve-merges", &options.type,
 -                          N_("try to recreate merges instead of ignoring "
 -                             "them"), REBASE_PRESERVE_MERGES),
 +                          N_("(DEPRECATED) try to recreate merges instead of "
 +                             "ignoring them"), REBASE_PRESERVE_MERGES),
-               OPT_BOOL(0, "rerere-autoupdate",
-                        &options.allow_rerere_autoupdate,
-                        N_("allow rerere to update index with resolved "
-                           "conflict")),
+               OPT_RERERE_AUTOUPDATE(&options.allow_rerere_autoupdate),
                OPT_BOOL('k', "keep-empty", &options.keep_empty,
                         N_("preserve empty commits during rebase")),
                OPT_BOOL(0, "autosquash", &options.autosquash,
        };
        int i;
  
 -      /*
 -       * NEEDSWORK: Once the builtin rebase has been tested enough
 -       * and git-legacy-rebase.sh is retired to contrib/, this preamble
 -       * can be removed.
 -       */
 -
 -      if (!use_builtin_rebase()) {
 -              const char *path = mkpath("%s/git-legacy-rebase",
 -                                        git_exec_path());
 -
 -              if (sane_execvp(path, (char **)argv) < 0)
 -                      die_errno(_("could not exec %s"), path);
 -              else
 -                      BUG("sane_execvp() returned???");
 -      }
 -
        if (argc == 2 && !strcmp(argv[1], "-h"))
                usage_with_options(builtin_rebase_usage,
                                   builtin_rebase_options);
        trace_repo_setup(prefix);
        setup_work_tree();
  
+       options.allow_empty_message = 1;
        git_config(rebase_config, &options);
  
 +      if (options.use_legacy_rebase ||
 +          !git_env_bool("GIT_TEST_REBASE_USE_BUILTIN", -1))
 +              warning(_("the rebase.useBuiltin support has been removed!\n"
 +                        "See its entry in 'git help config' for details."));
 +
        strbuf_reset(&buf);
        strbuf_addf(&buf, "%s/applying", apply_dir());
        if(file_exists(buf.buf))
                             builtin_rebase_options,
                             builtin_rebase_usage, 0);
  
-       if (action != NO_ACTION && total_argc != 2) {
+       if (action != ACTION_NONE && total_argc != 2) {
                usage_with_options(builtin_rebase_usage,
                                   builtin_rebase_options);
        }
                usage_with_options(builtin_rebase_usage,
                                   builtin_rebase_options);
  
-       if (action != NO_ACTION && !in_progress)
 +      if (options.type == REBASE_PRESERVE_MERGES)
 +              warning(_("git rebase --preserve-merges is deprecated. "
 +                        "Use --rebase-merges instead."));
 +
+       if (action != ACTION_NONE && !in_progress)
                die(_("No rebase in progress?"));
        setenv(GIT_REFLOG_ACTION_ENVIRONMENT, "rebase", 0);
  
                options.action = "show-current-patch";
                options.dont_finish_rebase = 1;
                goto run_rebase;
-       case NO_ACTION:
+       case ACTION_NONE:
                break;
        default:
                BUG("action: %d", action);
                                branch_name = options.head_name;
  
                } else {
 -                      free(options.head_name);
 -                      options.head_name = NULL;
 +                      FREE_AND_NULL(options.head_name);
                        branch_name = "HEAD";
                }
                if (get_oid("HEAD", &options.orig_head))
        strbuf_addf(&msg, "%s: checkout %s",
                    getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
        if (reset_head(&options.onto->object.oid, "checkout", NULL,
 -                     RESET_HEAD_DETACH | RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
 +                     RESET_HEAD_DETACH | RESET_ORIG_HEAD | 
 +                     RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
                       NULL, msg.buf))
                die(_("Could not detach HEAD"));
        strbuf_release(&msg);
         * we just fast-forwarded.
         */
        strbuf_reset(&msg);
 -      if (!oidcmp(&merge_base, &options.orig_head)) {
 +      if (oideq(&merge_base, &options.orig_head)) {
                printf(_("Fast-forwarded %s to %s.\n"),
                        branch_name, options.onto_name);
                strbuf_addf(&msg, "rebase finished: %s onto %s",
                        options.head_name ? options.head_name : "detached HEAD",
                        oid_to_hex(&options.onto->object.oid));
 -              reset_head(NULL, "Fast-forwarded", options.head_name, 0,
 -                         "HEAD", msg.buf);
 +              reset_head(NULL, "Fast-forwarded", options.head_name,
 +                         RESET_HEAD_REFS_ONLY, "HEAD", msg.buf);
                strbuf_release(&msg);
                ret = !!finish_rebase(&options);
                goto cleanup;
        options.revisions = revisions.buf;
  
  run_rebase:
-       ret = !!run_specific_rebase(&options);
+       ret = !!run_specific_rebase(&options, action);
  
  cleanup:
        strbuf_release(&revisions);
diff --combined parse-options-cb.c
index 6e2e8d6273a2b3c35248a956f77a19925ded3e7d,28ad5cd94b9c55882e4b895e001d35d12302386b..4b95d04a37c028322c6fbf7d3f1b59e0d541fdb3
@@@ -22,8 -22,8 +22,8 @@@ int parse_opt_abbrev_cb(const struct op
                                     opt->long_name);
                if (v && v < MINIMUM_ABBREV)
                        v = MINIMUM_ABBREV;
 -              else if (v > 40)
 -                      v = 40;
 +              else if (v > the_hash_algo->hexsz)
 +                      v = the_hash_algo->hexsz;
        }
        *(int *)(opt->value) = v;
        return 0;
@@@ -96,6 -96,23 +96,23 @@@ int parse_opt_commits(const struct opti
        return 0;
  }
  
+ int parse_opt_commit(const struct option *opt, const char *arg, int unset)
+ {
+       struct object_id oid;
+       struct commit *commit;
+       struct commit **target = opt->value;
+       if (!arg)
+               return -1;
+       if (get_oid(arg, &oid))
+               return error("malformed object name %s", arg);
+       commit = lookup_commit_reference(the_repository, &oid);
+       if (!commit)
+               return error("no such commit %s", arg);
+       *target = commit;
+       return 0;
+ }
  int parse_opt_object_name(const struct option *opt, const char *arg, int unset)
  {
        struct object_id oid;
        return 0;
  }
  
+ int parse_opt_object_id(const struct option *opt, const char *arg, int unset)
+ {
+       struct object_id oid;
+       struct object_id *target = opt->value;
+       if (unset) {
+               *target = null_oid;
+               return 0;
+       }
+       if (!arg)
+               return -1;
+       if (get_oid(arg, &oid))
+               return error(_("malformed object name '%s'"), arg);
+       *target = oid;
+       return 0;
+ }
  int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
  {
        int *target = opt->value;
diff --combined parse-options.h
index 1dd3523c15a0020d4886dcf1c516dad43b529d0c,0ab1103bc779edb2c2f9c8d368d0818779e7da99..bd00cf004900c478a0dfae2f0d311d356a67b3c6
@@@ -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 }
@@@ -224,17 -222,6 +224,17 @@@ const char *optname(const struct optio
                BUG("option callback does not expect an argument"); \
  } while (0)
  
 +/*
 + * Similar to the assertions above, but checks that "arg" is always non-NULL.
 + * This assertion also implies BUG_ON_OPT_NEG(), letting you declare both
 + * assertions in a single line.
 + */
 +#define BUG_ON_OPT_NEG_NOARG(unset, arg) do { \
 +      BUG_ON_OPT_NEG(unset); \
 +      if(!(arg)) \
 +              BUG("option callback expects an argument"); \
 +} while(0)
 +
  /*----- incremental advanced APIs -----*/
  
  enum parse_opt_result {
@@@ -277,8 -264,12 +277,12 @@@ int parse_opt_abbrev_cb(const struct op
  int parse_opt_expiry_date_cb(const struct option *, const char *, int);
  int parse_opt_color_flag_cb(const struct option *, const char *, int);
  int parse_opt_verbosity_cb(const struct option *, const char *, int);
+ /* value is struct oid_array* */
  int parse_opt_object_name(const struct option *, const char *, int);
+ /* value is struct object_id* */
+ int parse_opt_object_id(const struct option *, const char *, int);
  int parse_opt_commits(const struct option *, const char *, int);
+ int parse_opt_commit(const struct option *, const char *, int);
  int parse_opt_tertiary(const struct option *, const char *, int);
  int parse_opt_string_list(const struct option *, const char *, int);
  int parse_opt_noop_cb(const struct option *, const char *, int);
@@@ -316,6 -307,5 +320,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 4407a3f97858acdf47add8e8848cba12daf1de6d,610b7ece140bf1fa72c2ab66b5cb139fba102db4..72a1bc5fc56e108fb9e2e89a507b9c7c39c39a40
@@@ -171,22 -171,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;
@@@ -515,54 -510,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 -582,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;
  }
@@@ -950,6 -901,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 -1012,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.
@@@ -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);
@@@ -2161,8 -2098,7 +2161,8 @@@ static int parse_insn_line(struct repos
        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;
@@@ -2338,15 -2274,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)
@@@ -2494,14 -2418,15 +2494,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);
  
@@@ -2743,59 -2668,36 +2744,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;
  }
  
@@@ -3517,10 -3419,11 +3518,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"));
@@@ -3706,6 -3615,7 +3714,6 @@@ static int pick_commits(struct reposito
                        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 */
@@@ -4833,16 -4742,16 +4841,16 @@@ static int skip_unnecessary_picks(struc
  
  int complete_action(struct repository *r, struct replay_opts *opts, unsigned flags,
                    const char *shortrevisions, const char *onto_name,
-                   const char *onto, const char *orig_head, struct string_list *commands,
-                   unsigned autosquash, struct todo_list *todo_list)
+                   struct commit *onto, const char *orig_head,
+                   struct string_list *commands, unsigned autosquash,
+                   struct todo_list *todo_list)
  {
        const char *shortonto, *todo_file = rebase_path_todo();
        struct todo_list new_todo = TODO_LIST_INIT;
        struct strbuf *buf = &todo_list->buf;
-       struct object_id oid;
+       struct object_id oid = onto->object.oid;
        int res;
  
-       get_oid(onto, &oid);
        shortonto = find_unique_abbrev(&oid, DEFAULT_ABBREV);
  
        if (buf->len == 0) {
        if (todo_list_parse_insn_buffer(r, new_todo.buf.buf, &new_todo) ||
            todo_list_check(todo_list, &new_todo)) {
                fprintf(stderr, _(edit_todo_list_advice));
-               checkout_onto(opts, onto_name, onto, orig_head);
+               checkout_onto(r, opts, onto_name, &onto->object.oid, orig_head);
                todo_list_release(&new_todo);
  
                return -1;
  
        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))
diff --combined sequencer.h
index 75e292c03bdd002da58ec5089b1fbf321971b4bf,e640ca21f23a068862c36e097911c53d4b74856d..f4a8f4e44399325b7f62c0467061fad53c555156
@@@ -48,7 -48,6 +48,7 @@@ struct replay_opts 
  
        char *gpg_sign;
        enum commit_msg_cleanup_mode default_msg_cleanup;
 +      int explicit_cleanup;
  
        /* Merge strategy */
        char *strategy;
@@@ -151,7 -150,7 +151,7 @@@ void todo_list_add_exec_commands(struc
  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, struct string_list *commands,
+                   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);
  
   */
  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,7 -175,8 +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)
@@@ -198,4 -191,4 +199,4 @@@ 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);