switch: stop accepting pathspec

This command is about switching branch (or creating a new one) and should not accept pathspec. This helps simplify ambiguation handling. The other two ("git checkout" and "git restore") of course do accept pathspec as before. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Mar 29, 2019 at 17:39 UTC 5c06e26903fd69cf7a71bf2cf8281a84868525b0
1 file changed +15 -5
builtin/checkout.c
+15 -5
@@ -54,6 +54,7 @@ struct checkout_opts {
54 int overlay_mode;
55 int no_dwim_new_local_branch;
56 int discard_changes;
57 + int accept_pathspec;
58
59 /*
60 * If new checkout options are added, skip_merge_working_tree
@@ -1176,10 +1177,16 @@ static int parse_branchname_arg(int argc, const char **argv,
1177 if (!argc)
1178 return 0;
1179
1180 + if (!opts->accept_pathspec) {
1181 + if (argc > 1)
1182 + die(_("only one reference expected"));
1183 + has_dash_dash = 1; /* helps disambiguate */
1184 + }
1185 +
1186 arg = argv[0];
1187 dash_dash_pos = -1;
1188 for (i = 0; i < argc; i++) {
1182 - if (!strcmp(argv[i], "--")) {
1189 + if (opts->accept_pathspec && !strcmp(argv[i], "--")) {
1190 dash_dash_pos = i;
1191 break;
1192 }
@@ -1213,11 +1220,12 @@ static int parse_branchname_arg(int argc, const char **argv,
1220 recover_with_dwim = 0;
1221
1222 /*
1216 - * Accept "git checkout foo" and "git checkout foo --"
1217 - * as candidates for dwim.
1223 + * Accept "git checkout foo", "git checkout foo --"
1224 + * and "git switch foo" as candidates for dwim.
1225 */
1226 if (!(argc == 1 && !has_dash_dash) &&
1220 - !(argc == 2 && has_dash_dash))
1227 + !(argc == 2 && has_dash_dash) &&
1228 + opts->accept_pathspec)
1229 recover_with_dwim = 0;
1230
1231 if (recover_with_dwim) {
@@ -1262,7 +1270,7 @@ static int parse_branchname_arg(int argc, const char **argv,
1270 */
1271 if (argc)
1272 verify_non_filename(opts->prefix, arg);
1265 - } else {
1273 + } else if (opts->accept_pathspec) {
1274 argcount++;
1275 argv++;
1276 argc--;
@@ -1585,6 +1593,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1593
1594 memset(&opts, 0, sizeof(opts));
1595 opts.no_dwim_new_local_branch = 0;
1596 + opts.accept_pathspec = 1;
1597
1598 options = parse_options_dup(checkout_options);
1599 options = add_common_options(&opts, options);
@@ -1614,6 +1623,7 @@ int cmd_switch(int argc, const char **argv, const char *prefix)
1623
1624 memset(&opts, 0, sizeof(opts));
1625 opts.no_dwim_new_local_branch = 0;
1626 + opts.accept_pathspec = 0;
1627
1628 options = parse_options_dup(switch_options);
1629 options = add_common_options(&opts, options);