rebase -m: don't fork git checkout

Now that reset_head() can handle the initial checkout of onto
correctly use it in the "merge" backend instead of forking "git
checkout".  This opens the way for us to stop calling the
post-checkout hook in the future. Not running "git checkout" means
that "rebase -i/m" no longer recurse submodules when checking out
"onto" (thanks to Philippe Blain for pointing this out). As the rest
of rebase does not know what to do with submodules this is probably a
good thing. When using merge-ort rebase ought be able to handle
submodules correctly if it parsed the submodule config, such a change
is left for a future patch series.

The "apply" based rebase has avoided forking git checkout
since ac7f467fef ("builtin/rebase: support running "git rebase
<upstream>"", 2018-08-07). The code that handles the checkout was
moved into libgit by b309a97108 ("reset: extract reset_head() from
rebase", 2020-04-07).

Signed-off-by: Phillip Wood <>
Signed-off-by: Junio C Hamano <>
Phillip Wood 2022-01-26 13:05:49 +00:00 committed by Junio C Hamano
parent cd1528ef8e
commit 38c541ce94
1 changed files with 11 additions and 27 deletions

View File

@ -4223,42 +4223,26 @@ int apply_autostash_oid(const char *stash_oid)
return apply_save_autostash_oid(stash_oid, 1);
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;
strvec_push(&cmd.args, "checkout");
strvec_push(&cmd.args, commit);
strvec_pushf(&cmd.env_array, GIT_REFLOG_ACTION "=%s", action);
if (opts->verbose)
ret = run_command(&cmd);
ret = run_command_silent_on_success(&cmd);
if (!ret)
return ret;
static int checkout_onto(struct repository *r, struct replay_opts *opts,
const char *onto_name, const struct object_id *onto,
const struct object_id *orig_head)
const char *action = reflog_message(opts, "start", "checkout %s", onto_name);
if (run_git_checkout(r, opts, oid_to_hex(onto), action)) {
struct reset_head_opts ropts = {
.oid = onto,
.orig_head = orig_head,
.head_msg = reflog_message(opts, "start", "checkout %s",
.default_reflog_action = "rebase"
if (reset_head(r, &ropts)) {
return error(_("could not detach HEAD"));
return update_ref(NULL, "ORIG_HEAD", orig_head, NULL, 0, UPDATE_REFS_MSG_ON_ERR);
return 0;
static int stopped_at_head(struct repository *r)