Commit
1af4c26d6972ebf85633e56e67e28868b43f22be
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>
Yoichi NAKAYAMA <yoichi.nakayama@gmail.com> · 2026-08-27 14:41 UTC · committed by Junio C Hamano
1 file changed,
+33
−29
modified builtin/checkout.c
| @@ -1340,6 +1340,34 @@ enum checkout_command { | |||
| 1340 | 1340 | CHECKOUT_RESTORE = 3, | |
| 1341 | 1341 | }; | |
| 1342 | 1342 | ||
| 1343 | + | static void advise_disambiguating_remotes(enum checkout_command which_command) | |
| 1344 | + | { | |
| 1345 | + | const char *cmdname; | |
| 1346 | + | ||
| 1347 | + | switch (which_command) { | |
| 1348 | + | case CHECKOUT_CHECKOUT: | |
| 1349 | + | cmdname = "checkout"; | |
| 1350 | + | break; | |
| 1351 | + | case CHECKOUT_SWITCH: | |
| 1352 | + | cmdname = "switch"; | |
| 1353 | + | break; | |
| 1354 | + | default: | |
| 1355 | + | BUG("command <%d> should not reach advise_disambiguating_remotes", | |
| 1356 | + | which_command); | |
| 1357 | + | break; | |
| 1358 | + | } | |
| 1359 | + | ||
| 1360 | + | advise(_("If you meant to check out a remote tracking branch on, e.g. 'origin',\n" | |
| 1361 | + | "you can do so by fully qualifying the name with the --track option:\n" | |
| 1362 | + | "\n" | |
| 1363 | + | " git %s --track origin/<name>\n" | |
| 1364 | + | "\n" | |
| 1365 | + | "If you'd like to always have checkouts of an ambiguous <name> prefer\n" | |
| 1366 | + | "one remote, e.g. the 'origin' remote, consider setting\n" | |
| 1367 | + | "checkout.defaultRemote=origin in your config."), | |
| 1368 | + | cmdname); | |
| 1369 | + | } | |
| 1370 | + | ||
| 1343 | 1371 | static char *parse_remote_branch(const char *arg, | |
| 1344 | 1372 | struct object_id *rev, | |
| 1345 | 1373 | int could_be_checkout_paths, | |
| @@ -1355,35 +1383,11 @@ static char *parse_remote_branch(const char *arg, | |||
| 1355 | 1383 | } | |
| 1356 | 1384 | ||
| 1357 | 1385 | if (!remote && num_matches > 1) { | |
| 1358 | - | if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) { | |
| 1359 | - | const char *cmdname; | |
| 1360 | - | ||
| 1361 | - | switch (which_command) { | |
| 1362 | - | case CHECKOUT_CHECKOUT: | |
| 1363 | - | cmdname = "checkout"; | |
| 1364 | - | break; | |
| 1365 | - | case CHECKOUT_SWITCH: | |
| 1366 | - | cmdname = "switch"; | |
| 1367 | - | break; | |
| 1368 | - | default: | |
| 1369 | - | BUG("command <%d> should not reach parse_remote_branch", | |
| 1370 | - | which_command); | |
| 1371 | - | break; | |
| 1372 | - | } | |
| 1373 | - | ||
| 1374 | - | advise(_("If you meant to check out a remote tracking branch on, e.g. 'origin',\n" | |
| 1375 | - | "you can do so by fully qualifying the name with the --track option:\n" | |
| 1376 | - | "\n" | |
| 1377 | - | " git %s --track origin/<name>\n" | |
| 1378 | - | "\n" | |
| 1379 | - | "If you'd like to always have checkouts of an ambiguous <name> prefer\n" | |
| 1380 | - | "one remote, e.g. the 'origin' remote, consider setting\n" | |
| 1381 | - | "checkout.defaultRemote=origin in your config."), | |
| 1382 | - | cmdname); | |
| 1383 | - | } | |
| 1384 | - | ||
| 1385 | - | die(_("'%s' matched multiple (%d) remote tracking branches"), | |
| 1386 | - | arg, num_matches); | |
| 1386 | + | if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) | |
| 1387 | + | advise_disambiguating_remotes(which_command); | |
| 1388 | + | ||
| 1389 | + | die(_("'%s' matched multiple (%d) remote tracking branches"), | |
| 1390 | + | arg, num_matches); | |
| 1387 | 1391 | } | |
| 1388 | 1392 | ||
| 1389 | 1393 | return remote; | |