From 1af4c26d6972ebf85633e56e67e28868b43f22be Mon Sep 17 00:00:00 2001 From: Yoichi NAKAYAMA Date: Thu, 27 Aug 2026 14:41:54 +0000 Subject: [PATCH] checkout: extract function to display advice for ambiguous remotes Fix incorrect indentation and reduce nesting. We are going to extend this function in subsequent commits. Signed-off-by: Yoichi NAKAYAMA Signed-off-by: Junio C Hamano --- builtin/checkout.c | 60 ++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/builtin/checkout.c b/builtin/checkout.c index b78b3a1d16..0065458aa3 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -1340,6 +1340,34 @@ enum checkout_command { CHECKOUT_RESTORE = 3, }; +static void advise_disambiguating_remotes(enum checkout_command which_command) +{ + const char *cmdname; + + switch (which_command) { + case CHECKOUT_CHECKOUT: + cmdname = "checkout"; + break; + case CHECKOUT_SWITCH: + cmdname = "switch"; + break; + default: + BUG("command <%d> should not reach advise_disambiguating_remotes", + which_command); + break; + } + + advise(_("If you meant to check out a remote tracking branch on, e.g. 'origin',\n" + "you can do so by fully qualifying the name with the --track option:\n" + "\n" + " git %s --track origin/\n" + "\n" + "If you'd like to always have checkouts of an ambiguous prefer\n" + "one remote, e.g. the 'origin' remote, consider setting\n" + "checkout.defaultRemote=origin in your config."), + cmdname); +} + static char *parse_remote_branch(const char *arg, struct object_id *rev, int could_be_checkout_paths, @@ -1355,35 +1383,11 @@ static char *parse_remote_branch(const char *arg, } if (!remote && num_matches > 1) { - if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) { - const char *cmdname; + if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) + advise_disambiguating_remotes(which_command); - switch (which_command) { - case CHECKOUT_CHECKOUT: - cmdname = "checkout"; - break; - case CHECKOUT_SWITCH: - cmdname = "switch"; - break; - default: - BUG("command <%d> should not reach parse_remote_branch", - which_command); - break; - } - - advise(_("If you meant to check out a remote tracking branch on, e.g. 'origin',\n" - "you can do so by fully qualifying the name with the --track option:\n" - "\n" - " git %s --track origin/\n" - "\n" - "If you'd like to always have checkouts of an ambiguous prefer\n" - "one remote, e.g. the 'origin' remote, consider setting\n" - "checkout.defaultRemote=origin in your config."), - cmdname); - } - - die(_("'%s' matched multiple (%d) remote tracking branches"), - arg, num_matches); + die(_("'%s' matched multiple (%d) remote tracking branches"), + arg, num_matches); } return remote;