@@ -1117,12 +1117,43 @@ static void setup_new_branch_info_and_source_tree(
1117
1117
}
1118
1118
}
1119
1119
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
+
1120
1152
static int parse_branchname_arg (int argc , const char * * argv ,
1121
1153
int dwim_new_local_branch_ok ,
1122
1154
struct branch_info * new_branch_info ,
1123
1155
struct checkout_opts * opts ,
1124
- struct object_id * rev ,
1125
- int * dwim_remotes_matched )
1156
+ struct object_id * rev )
1126
1157
{
1127
1158
const char * * new_branch = & opts -> new_branch ;
1128
1159
int argcount = 0 ;
@@ -1227,13 +1258,9 @@ static int parse_branchname_arg(int argc, const char **argv,
1227
1258
recover_with_dwim = 0 ;
1228
1259
1229
1260
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 );
1232
1263
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 );
1237
1264
* new_branch = arg ;
1238
1265
arg = remote ;
1239
1266
/* DWIMmed to create local branch, case (3).(b) */
@@ -1498,7 +1525,6 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
1498
1525
const char * const usagestr [])
1499
1526
{
1500
1527
struct branch_info new_branch_info ;
1501
- int dwim_remotes_matched = 0 ;
1502
1528
int parseopt_flags = 0 ;
1503
1529
1504
1530
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,
1606
1632
opts -> track == BRANCH_TRACK_UNSPECIFIED &&
1607
1633
!opts -> new_branch ;
1608
1634
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 );
1611
1636
argv += n ;
1612
1637
argc -= n ;
1613
1638
} else if (!opts -> accept_ref && opts -> from_treeish ) {
@@ -1684,28 +1709,10 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
1684
1709
}
1685
1710
1686
1711
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
1707
1715
return checkout_branch (opts , & new_branch_info );
1708
- }
1709
1716
}
1710
1717
1711
1718
int cmd_checkout (int argc , const char * * argv , const char * prefix )
0 commit comments