@@ -1117,12 +1117,43 @@ static void setup_new_branch_info_and_source_tree(
11171117 }
11181118}
11191119
1120+ static const char * parse_remote_branch (const char * arg ,
1121+ struct object_id * rev ,
1122+ int could_be_checkout_paths )
1123+ {
1124+ int num_matches = 0 ;
1125+ const char * remote = unique_tracking_name (arg , rev , & num_matches );
1126+
1127+ if (remote && could_be_checkout_paths ) {
1128+ die (_ ("'%s' could be both a local file and a tracking branch.\n"
1129+ "Please use -- (and optionally --no-guess) to disambiguate" ),
1130+ arg );
1131+ }
1132+
1133+ if (!remote && num_matches > 1 ) {
1134+ if (advice_checkout_ambiguous_remote_branch_name ) {
1135+ advise (_ ("If you meant to check out a remote tracking branch on, e.g. 'origin',\n"
1136+ "you can do so by fully qualifying the name with the --track option:\n"
1137+ "\n"
1138+ " git checkout --track origin/<name>\n"
1139+ "\n"
1140+ "If you'd like to always have checkouts of an ambiguous <name> prefer\n"
1141+ "one remote, e.g. the 'origin' remote, consider setting\n"
1142+ "checkout.defaultRemote=origin in your config." ));
1143+ }
1144+
1145+ die (_ ("'%s' matched multiple (%d) remote tracking branches" ),
1146+ arg , num_matches );
1147+ }
1148+
1149+ return remote ;
1150+ }
1151+
11201152static int parse_branchname_arg (int argc , const char * * argv ,
11211153 int dwim_new_local_branch_ok ,
11221154 struct branch_info * new_branch_info ,
11231155 struct checkout_opts * opts ,
1124- struct object_id * rev ,
1125- int * dwim_remotes_matched )
1156+ struct object_id * rev )
11261157{
11271158 const char * * new_branch = & opts -> new_branch ;
11281159 int argcount = 0 ;
@@ -1227,13 +1258,9 @@ static int parse_branchname_arg(int argc, const char **argv,
12271258 recover_with_dwim = 0 ;
12281259
12291260 if (recover_with_dwim ) {
1230- const char * remote = unique_tracking_name (arg , rev ,
1231- dwim_remotes_matched );
1261+ const char * remote = parse_remote_branch (arg , rev ,
1262+ could_be_checkout_paths );
12321263 if (remote ) {
1233- if (could_be_checkout_paths )
1234- die (_ ("'%s' could be both a local file and a tracking branch.\n"
1235- "Please use -- (and optionally --no-guess) to disambiguate" ),
1236- arg );
12371264 * new_branch = arg ;
12381265 arg = remote ;
12391266 /* DWIMmed to create local branch, case (3).(b) */
@@ -1498,7 +1525,6 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
14981525 const char * const usagestr [])
14991526{
15001527 struct branch_info new_branch_info ;
1501- int dwim_remotes_matched = 0 ;
15021528 int parseopt_flags = 0 ;
15031529
15041530 memset (& new_branch_info , 0 , sizeof (new_branch_info ));
@@ -1606,8 +1632,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
16061632 opts -> track == BRANCH_TRACK_UNSPECIFIED &&
16071633 !opts -> new_branch ;
16081634 int n = parse_branchname_arg (argc , argv , dwim_ok ,
1609- & new_branch_info , opts , & rev ,
1610- & dwim_remotes_matched );
1635+ & new_branch_info , opts , & rev );
16111636 argv += n ;
16121637 argc -= n ;
16131638 } else if (!opts -> accept_ref && opts -> from_treeish ) {
@@ -1684,28 +1709,10 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
16841709 }
16851710
16861711 UNLEAK (opts );
1687- if (opts -> patch_mode || opts -> pathspec .nr ) {
1688- int ret = checkout_paths (opts , new_branch_info .name );
1689- if (ret && dwim_remotes_matched > 1 &&
1690- advice_checkout_ambiguous_remote_branch_name )
1691- advise (_ ("'%s' matched more than one remote tracking branch.\n"
1692- "We found %d remotes with a reference that matched. So we fell back\n"
1693- "on trying to resolve the argument as a path, but failed there too!\n"
1694- "\n"
1695- "If you meant to check out a remote tracking branch on, e.g. 'origin',\n"
1696- "you can do so by fully qualifying the name with the --track option:\n"
1697- "\n"
1698- " git checkout --track origin/<name>\n"
1699- "\n"
1700- "If you'd like to always have checkouts of an ambiguous <name> prefer\n"
1701- "one remote, e.g. the 'origin' remote, consider setting\n"
1702- "checkout.defaultRemote=origin in your config." ),
1703- argv [0 ],
1704- dwim_remotes_matched );
1705- return ret ;
1706- } else {
1712+ if (opts -> patch_mode || opts -> pathspec .nr )
1713+ return checkout_paths (opts , new_branch_info .name );
1714+ else
17071715 return checkout_branch (opts , & new_branch_info );
1708- }
17091716}
17101717
17111718int cmd_checkout (int argc , const char * * argv , const char * prefix )
0 commit comments