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-files") of
course do accept pathspec as before.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclo...@gmail.com>
---
 builtin/checkout.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/builtin/checkout.c b/builtin/checkout.c
index ceb635de36..880030e929 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -55,6 +55,7 @@ struct checkout_opts {
        int ignore_other_worktrees;
        int show_progress;
        int dwim_new_local_branch;
+       int accept_pathspec;
 
        /*
         * If new checkout options are added, skip_merge_working_tree
@@ -1089,10 +1090,16 @@ static int parse_branchname_arg(int argc, const char 
**argv,
        if (!argc)
                return 0;
 
+       if (!opts->accept_pathspec) {
+               if (argc > 1)
+                       die(_("only one reference expected"));
+               has_dash_dash = 1; /* helps disambiguate */
+       }
+
        arg = argv[0];
        dash_dash_pos = -1;
        for (i = 0; i < argc; i++) {
-               if (!strcmp(argv[i], "--")) {
+               if (opts->accept_pathspec && !strcmp(argv[i], "--")) {
                        dash_dash_pos = i;
                        break;
                }
@@ -1167,7 +1174,7 @@ static int parse_branchname_arg(int argc, const char 
**argv,
                 */
                if (argc)
                        verify_non_filename(opts->prefix, arg);
-       } else {
+       } else if (opts->accept_pathspec) {
                argcount++;
                argv++;
                argc--;
@@ -1468,6 +1475,7 @@ int cmd_checkout(int argc, const char **argv, const char 
*prefix)
 
        memset(&opts, 0, sizeof(opts));
        opts.dwim_new_local_branch = 1;
+       opts.accept_pathspec = 1;
 
        options = parse_options_dup(checkout_options);
        options = add_common_options(&opts, options);
@@ -1494,6 +1502,7 @@ int cmd_switch_branch(int argc, const char **argv, const 
char *prefix)
 
        memset(&opts, 0, sizeof(opts));
        opts.dwim_new_local_branch = 1;
+       opts.accept_pathspec = 0;
 
        options = parse_options_dup(switch_options);
        options = add_common_options(&opts, options);
@@ -1513,6 +1522,7 @@ int cmd_restore_files(int argc, const char **argv, const 
char *prefix)
 
        memset(&opts, 0, sizeof(opts));
        opts.dwim_new_local_branch = 1;
+       opts.accept_pathspec = 1;
 
        options = parse_options_dup(options);
        options = add_common_options(&opts, options);
-- 
2.20.0.rc1.380.g3eb999425c.dirty

Reply via email to