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 <yoichi.nakayama@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
next
Yoichi NAKAYAMA 2026-08-27 14:41:54 +00:00 committed by Junio C Hamano
parent e9019fcafe
commit 1af4c26d69
1 changed files with 32 additions and 28 deletions

View File

@ -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/<name>\n"
"\n"
"If you'd like to always have checkouts of an ambiguous <name> 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/<name>\n"
"\n"
"If you'd like to always have checkouts of an ambiguous <name> 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;