[PATCH v2 2/2] checkout: proper error message on 'git checkout foo bar --'

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxx>
---
> > +               die("only one reference expected, %d given.", has_dash_dash);
> 
> The translator in me says this string should be marked for translation
> like others in git-checkout...

Indeed. Sorry, it's not like I didn't knoW about _("...") /o\.

 builtin/checkout.c            | 11 ++++++++++-
 t/t2010-checkout-ambiguous.sh |  6 ++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/builtin/checkout.c b/builtin/checkout.c
index a5a12f6..3dd45cc 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -882,6 +882,7 @@ static int parse_branchname_arg(int argc, const char **argv,
 	unsigned char branch_rev[20];
 	const char *arg;
 	int has_dash_dash;
+	int i;
 
 	/*
 	 * case 1: git checkout <ref> -- [<paths>]
@@ -925,7 +926,15 @@ static int parse_branchname_arg(int argc, const char **argv,
 		return 1;
 
 	arg = argv[0];
-	has_dash_dash = (argc > 1) && !strcmp(argv[1], "--");
+	has_dash_dash = 0;
+	for (i = 0; i < argc; i++) {
+		if (!strcmp(argv[i], "--")) {
+			has_dash_dash = i;
+			break;
+		}
+	}
+	if (has_dash_dash >= 2)
+		die(_("only one reference expected, %d given."), has_dash_dash);
 
 	if (!strcmp(arg, "-"))
 		arg = "@{-1}";
diff --git a/t/t2010-checkout-ambiguous.sh b/t/t2010-checkout-ambiguous.sh
index 7cc0a35..ce6b6e2 100755
--- a/t/t2010-checkout-ambiguous.sh
+++ b/t/t2010-checkout-ambiguous.sh
@@ -47,4 +47,10 @@ test_expect_success 'disambiguate checking out from a tree-ish' '
 	git diff --exit-code --quiet
 '
 
+test_expect_success C_LOCALE_OUTPUT 'accurate error message with more than one ref' '
+	test_must_fail git checkout HEAD master -- 2>actual &&
+	echo "fatal: only one reference expected, 2 given." >expect &&
+	test_cmp expect actual
+'
+
 test_done
-- 
1.8.4.474.g128a96c

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]