Skip to content

Commit 5c06e26

Browse files
pcloudsgitster
authored andcommitted
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>
1 parent 492eded commit 5c06e26

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

builtin/checkout.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ struct checkout_opts {
5454
int overlay_mode;
5555
int no_dwim_new_local_branch;
5656
int discard_changes;
57+
int accept_pathspec;
5758

5859
/*
5960
* If new checkout options are added, skip_merge_working_tree
@@ -1176,10 +1177,16 @@ static int parse_branchname_arg(int argc, const char **argv,
11761177
if (!argc)
11771178
return 0;
11781179

1180+
if (!opts->accept_pathspec) {
1181+
if (argc > 1)
1182+
die(_("only one reference expected"));
1183+
has_dash_dash = 1; /* helps disambiguate */
1184+
}
1185+
11791186
arg = argv[0];
11801187
dash_dash_pos = -1;
11811188
for (i = 0; i < argc; i++) {
1182-
if (!strcmp(argv[i], "--")) {
1189+
if (opts->accept_pathspec && !strcmp(argv[i], "--")) {
11831190
dash_dash_pos = i;
11841191
break;
11851192
}
@@ -1213,11 +1220,12 @@ static int parse_branchname_arg(int argc, const char **argv,
12131220
recover_with_dwim = 0;
12141221

12151222
/*
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.
12181225
*/
12191226
if (!(argc == 1 && !has_dash_dash) &&
1220-
!(argc == 2 && has_dash_dash))
1227+
!(argc == 2 && has_dash_dash) &&
1228+
opts->accept_pathspec)
12211229
recover_with_dwim = 0;
12221230

12231231
if (recover_with_dwim) {
@@ -1262,7 +1270,7 @@ static int parse_branchname_arg(int argc, const char **argv,
12621270
*/
12631271
if (argc)
12641272
verify_non_filename(opts->prefix, arg);
1265-
} else {
1273+
} else if (opts->accept_pathspec) {
12661274
argcount++;
12671275
argv++;
12681276
argc--;
@@ -1585,6 +1593,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
15851593

15861594
memset(&opts, 0, sizeof(opts));
15871595
opts.no_dwim_new_local_branch = 0;
1596+
opts.accept_pathspec = 1;
15881597

15891598
options = parse_options_dup(checkout_options);
15901599
options = add_common_options(&opts, options);
@@ -1614,6 +1623,7 @@ int cmd_switch(int argc, const char **argv, const char *prefix)
16141623

16151624
memset(&opts, 0, sizeof(opts));
16161625
opts.no_dwim_new_local_branch = 0;
1626+
opts.accept_pathspec = 0;
16171627

16181628
options = parse_options_dup(switch_options);
16191629
options = add_common_options(&opts, options);

0 commit comments

Comments
 (0)