git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] Avoid the need of "--" when wildcard pathspec is used

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Jun 30, 2015, 12:40 UTC
Message-ID
<1435668007-31231-1-git-send-email-pclouds@gmail.com>

When "--" is lacking from the command line and a command can take both revs and paths, the idea is if an argument can be seen as both an extended SHA-1 and a path, then "--" is required or git refuses to continue. It's currently implemented as:

(1) if an argument is rev, then it must not exist in worktree
(2) else, it must exist in worktree
(3) else, "--" is required.

These rules work for literal paths, but when wildcard pathspec is involved, it always requires the user to add "--" because it fails (2) and (1) is never met.

This patch prefers wildcard over extended sha-1 syntax that includes wildcards, so that we can specify wildcards to select paths in worktree without "--" most of the time. If wildcards are found in extended sha-1 syntax, then "--" is _always_ required.

Because ref names don't allow wildcards, you can only hit that "needs '--'" new rule if you use ":/<pattern>", "<rev>^{/<pattern>}" or "<rev>:wildcards/in/literal/paths". So it's really rare.

The rules after this patch become:
(1) if an arg is a rev, then it must either exist in worktree or not
    a wild card
(2) else, it either exists in worktree or is a wild card
(3) else, "--" is required.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 setup.c                           |  2 ++
 t/t2019-checkout-ambiguous-ref.sh | 26 ++++++++++++++++++++++++++
 2 files changed, 28 insertions(+)
diff --git a/setup.c b/setup.c
index 82c0cc2..f7cb93b 100644
--- a/setup.c
+++ b/setup.c
@@ -143,6 +143,8 @@ int check_filename(const char *prefix, const char *arg)
 		name = arg + 2;
 	} else if (!no_wildcard(arg))
 		return 1;
+	else if (!no_wildcard(arg))
+		return 1;
 	else if (prefix)
 		name = prefix_filename(prefix, strlen(prefix), arg);
 	else
diff --git a/t/t2019-checkout-ambiguous-ref.sh b/t/t2019-checkout-ambiguous-ref.sh
index b99d519..e5ceba3 100755
--- a/t/t2019-checkout-ambiguous-ref.sh
+++ b/t/t2019-checkout-ambiguous-ref.sh
@@ -56,4 +56,30 @@ test_expect_success VAGUENESS_SUCCESS 'checkout reports switch to branch' '
 	test_i18ngrep ! "^HEAD is now at" stderr
 '
 
+test_expect_success 'wildcard ambiguation' '
+	git init ambi &&
+	(
+		cd ambi &&
+		echo a >a.c &&
+		git add a.c &&
+		echo b >a.c &&
+		git checkout "*.c" &&
+		echo a >expect &&
+		test_cmp expect a.c
+	)
+'
+
+test_expect_success 'wildcard ambiguation (2)' '
+	git init ambi2 &&
+	(
+		cd ambi2 &&
+		echo a >"*.c" &&
+		git add . &&
+		test_must_fail git show :"*.c" &&
+		git show :"*.c" -- >actual &&
+		echo a >expect &&
+		test_cmp expect actual
+	)
+'
+
 test_done
-- 
2.3.0.rc1.137.g477eb31
Next: Junio C Hamano
Message 1 of 5 in “Avoid the need of "--" when wildcard pathspec is used”
  1. Avoid the need of "--" when wildcard pathspec is usedNguyễn Thái Ngọc Duy, Jun 30, 2015
  2. Junio C HamanoJun 30, 2015
  3. Duy NguyenJun 30, 2015
  4. Add tests for wildcard "path vs ref" disambiguationNguyễn Thái Ngọc Duy, Jul 1, 2015
  5. Junio C HamanoJul 1, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.