rebase: factor out checkout for up to date branch

This code is heavily indented and it will be convenient later in the series to have it in its own function. Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Jan 26, 2022 at 13:05 UTC ae42fa4c03ef0ffe36468f8918b3d404a277da50
1 file changed +19 -14
builtin/rebase.c
+19 -14
@@ -812,6 +812,23 @@ static int rebase_config(const char *var, const char *value, void *data)
812 return git_default_config(var, value, data);
813 }
814
815 +static int checkout_up_to_date(struct rebase_options *options)
816 +{
817 + struct strbuf buf = STRBUF_INIT;
818 + int ret = 0;
819 +
820 + strbuf_addf(&buf, "%s: checkout %s",
821 + getenv(GIT_REFLOG_ACTION_ENVIRONMENT),
822 + options->switch_to);
823 + if (reset_head(the_repository, &options->orig_head, "checkout",
824 + options->head_name, RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
825 + NULL, buf.buf, DEFAULT_REFLOG_ACTION) < 0)
826 + ret = error(_("could not switch to %s"), options->switch_to);
827 + strbuf_release(&buf);
828 +
829 + return ret;
830 +}
831 +
832 /*
833 * Determines whether the commits in from..to are linear, i.e. contain
834 * no merge commits. This function *expects* `from` to be an ancestor of
@@ -1673,21 +1690,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1690 if (!(options.flags & REBASE_FORCE)) {
1691 /* Lazily switch to the target branch if needed... */
1692 if (options.switch_to) {
1676 - strbuf_reset(&buf);
1677 - strbuf_addf(&buf, "%s: checkout %s",
1678 - getenv(GIT_REFLOG_ACTION_ENVIRONMENT),
1679 - options.switch_to);
1680 - if (reset_head(the_repository,
1681 - &options.orig_head, "checkout",
1682 - options.head_name,
1683 - RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
1684 - NULL, buf.buf,
1685 - DEFAULT_REFLOG_ACTION) < 0) {
1686 - ret = error(_("could not switch to "
1687 - "%s"),
1688 - options.switch_to);
1693 + ret = checkout_up_to_date(&options);
1694 + if (ret)
1695 goto cleanup;
1690 - }
1696 }
1697
1698 if (!(options.flags & REBASE_NO_QUIET))