Skip to content

Commit ae42fa4

Browse files
phillipwoodgitster
authored andcommitted
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>
1 parent abe6bb3 commit ae42fa4

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

builtin/rebase.c

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,23 @@ static int rebase_config(const char *var, const char *value, void *data)
812812
return git_default_config(var, value, data);
813813
}
814814

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+
815832
/*
816833
* Determines whether the commits in from..to are linear, i.e. contain
817834
* 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)
16731690
if (!(options.flags & REBASE_FORCE)) {
16741691
/* Lazily switch to the target branch if needed... */
16751692
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)
16891695
goto cleanup;
1690-
}
16911696
}
16921697

16931698
if (!(options.flags & REBASE_NO_QUIET))

0 commit comments

Comments
 (0)