]> git.ipfire.org Git - thirdparty/git.git/commitdiff
checkout: proper error message on 'git checkout foo bar --'
authorMatthieu Moy <Matthieu.Moy@imag.fr>
Fri, 18 Oct 2013 09:25:58 +0000 (11:25 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 18 Oct 2013 19:57:16 +0000 (12:57 -0700)
The previous code was detecting the presence of "--" by looking only at
argument 1. As a result, "git checkout foo bar --" was interpreted as an
ambiguous file/revision list, and errored out with:

error: pathspec 'foo' did not match any file(s) known to git.
error: pathspec 'bar' did not match any file(s) known to git.
error: pathspec '--' did not match any file(s) known to git.

This patch fixes it by walking through the argument list to find the
"--", and now complains about the number of references given.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/checkout.c
t/t2010-checkout-ambiguous.sh

index c98008ab488b7321bd76f83041dc679f919fe7c4..e5103b9c9558d227a0a21c973fdbef456744e2a5 100644 (file)
@@ -872,7 +872,9 @@ static int parse_branchname_arg(int argc, const char **argv,
        int argcount = 0;
        unsigned char branch_rev[20];
        const char *arg;
-       int has_dash_dash;
+       int dash_dash_pos;
+       int has_dash_dash = 0;
+       int i;
 
        /*
         * case 1: git checkout <ref> -- [<paths>]
@@ -916,11 +918,20 @@ static int parse_branchname_arg(int argc, const char **argv,
        if (!argc)
                return 0;
 
-       if (!strcmp(argv[0], "--"))     /* case (2) */
-               return 1;
-
        arg = argv[0];
-       has_dash_dash = (argc > 1) && !strcmp(argv[1], "--");
+       dash_dash_pos = -1;
+       for (i = 0; i < argc; i++) {
+               if (!strcmp(argv[i], "--")) {
+                       dash_dash_pos = i;
+                       break;
+               }
+       }
+       if (dash_dash_pos == 0)
+               return 1; /* case (2) */
+       else if (dash_dash_pos == 1)
+               has_dash_dash = 1; /* case (3) or (1) */
+       else if (dash_dash_pos >= 2)
+               die(_("only one reference expected, %d given."), dash_dash_pos);
 
        if (!strcmp(arg, "-"))
                arg = "@{-1}";
index 7cc0a3582ef1bb1598bc5dfc5334bfbe006c3f5a..87bdf9c96bfacf4ac921ad9b195db46d9c3b079a 100755 (executable)
@@ -47,4 +47,10 @@ test_expect_success 'disambiguate checking out from a tree-ish' '
        git diff --exit-code --quiet
 '
 
+test_expect_success 'accurate error message with more than one ref' '
+       test_must_fail git checkout HEAD master -- 2>actual &&
+       grep 2 actual &&
+       test_i18ngrep "one reference expected, 2 given" actual
+'
+
 test_done