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

[PATCH 42/45] pathspec: support :(literal) syntax for noglob pathspec

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Jun 9, 2013, 06:26 UTC
Message-ID
<1370759178-1709-43-git-send-email-pclouds@gmail.com>
In-Reply-To
<1370759178-1709-1-git-send-email-pclouds@gmail.com>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/glossary-content.txt | 20 ++++++++++++++++----
 builtin/add.c                      |  2 +-
 builtin/diff.c                     |  2 +-
 dir.c                              | 15 ++++++++++++---
 pathspec.c                         | 11 ++++++++---
 pathspec.h                         |  4 +++-
 t/t6130-pathspec-noglob.sh         | 18 ++++++++++++++++++
 tree-diff.c                        |  2 +-
 tree-walk.c                        |  5 ++++-
 9 files changed, 64 insertions(+), 15 deletions(-)
diff --git a/Documentation/glossary-content.txt b/Documentation/glossary-content.txt
index db2a74d..186b566 100644
--- a/Documentation/glossary-content.txt
+++ b/Documentation/glossary-content.txt
@@ -322,10 +322,22 @@ and a close parentheses `)`, and the remainder is the pattern to match
 against the path.
 +
 The "magic signature" consists of an ASCII symbol that is not
-alphanumeric. Currently only the slash `/` is recognized as a
-"magic signature": it makes the pattern match from the root of
-the working tree, even when you are running the command from
-inside a subdirectory.
+alphanumeric.
++
+--
+top `/`;;
+	The magic word `top` (mnemonic: `/`) makes the pattern match
+	from the root of the working tree, even when you are running
+	the command from inside a subdirectory.
+
+literal;;
+	Wildcards in the pattern such as `*` or `?` are treated
+	as literal characters.
+--
++
+Currently only the slash `/` is recognized as the "magic signature",
+but it is envisioned that we will support more types of magic in later
+versions of Git.
 +
 A pathspec with only a colon means "there is no pathspec". This form
 should not be combined with other pathspec.
diff --git a/builtin/add.c b/builtin/add.c
index 0b80fa8..663ddd1 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -541,7 +541,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 		/*
 		 * file_exists() assumes exact match
 		 */
-		GUARD_PATHSPEC(&pathspec, PATHSPEC_FROMTOP);
+		GUARD_PATHSPEC(&pathspec, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
 
 		for (i = 0; i < pathspec.nr; i++) {
 			const char *path = pathspec.items[i].match;
diff --git a/builtin/diff.c b/builtin/diff.c
index 6b4e3f9..b78435f 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -372,7 +372,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 	}
 	if (rev.prune_data.nr) {
 		/* builtin_diff_b_f() */
-		GUARD_PATHSPEC(&rev.prune_data, PATHSPEC_FROMTOP);
+		GUARD_PATHSPEC(&rev.prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
 		if (!path)
 			path = rev.prune_data.items[0].match;
 		paths += rev.prune_data.nr;
diff --git a/dir.c b/dir.c
index 76a2e1a..65cac36 100644
--- a/dir.c
+++ b/dir.c
@@ -108,7 +108,10 @@ static size_t common_prefix_len(const struct pathspec *pathspec)
 	int n;
 	size_t max = 0;
 
-	GUARD_PATHSPEC(pathspec, PATHSPEC_FROMTOP | PATHSPEC_MAXDEPTH);
+	GUARD_PATHSPEC(pathspec,
+		       PATHSPEC_FROMTOP |
+		       PATHSPEC_MAXDEPTH |
+		       PATHSPEC_LITERAL);
 
 	for (n = 0; n < pathspec->nr; n++) {
 		size_t i = 0, len = 0;
@@ -232,7 +235,10 @@ int match_pathspec_depth(const struct pathspec *ps,
 {
 	int i, retval = 0;
 
-	GUARD_PATHSPEC(ps, PATHSPEC_FROMTOP | PATHSPEC_MAXDEPTH);
+	GUARD_PATHSPEC(ps,
+		       PATHSPEC_FROMTOP |
+		       PATHSPEC_MAXDEPTH |
+		       PATHSPEC_LITERAL);
 
 	if (!ps->nr) {
 		if (!ps->recursive ||
@@ -1290,7 +1296,10 @@ int read_directory(struct dir_struct *dir, const char *path, int len, const stru
 	 * Check out create_simplify()
 	 */
 	if (pathspec)
-		GUARD_PATHSPEC(pathspec, PATHSPEC_FROMTOP | PATHSPEC_MAXDEPTH);
+		GUARD_PATHSPEC(pathspec,
+			       PATHSPEC_FROMTOP |
+			       PATHSPEC_MAXDEPTH |
+			       PATHSPEC_LITERAL);
 
 	if (has_symlink_leading_path(path, len))
 		return dir->nr;
diff --git a/pathspec.c b/pathspec.c
index 69adaba..cc6545f 100644
--- a/pathspec.c
+++ b/pathspec.c
@@ -70,6 +70,7 @@ static struct pathspec_magic {
 	const char *name;
 } pathspec_magic[] = {
 	{ PATHSPEC_FROMTOP, '/', "top" },
+	{ PATHSPEC_LITERAL,   0, "literal" },
 };
 
 /*
@@ -92,13 +93,15 @@ static unsigned prefix_pathspec(struct pathspec_item *item,
 				const char *elt)
 {
 	static int literal_global = -1;
-	unsigned magic = 0, short_magic = 0;
+	unsigned magic = 0, short_magic = 0, global_magic = 0;
 	const char *copyfrom = elt, *long_magic_end = NULL;
 	char *match;
 	int i, pathspec_prefix = -1;
 
 	if (literal_global < 0)
 		literal_global = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
+	if (literal_global)
+		global_magic |= PATHSPEC_LITERAL;
 
 	if (elt[0] != ':') {
 		; /* nothing to do */
@@ -164,6 +167,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item,
 
 	magic |= short_magic;
 	*p_short_magic = short_magic;
+	magic |= global_magic;
 
 	if (pathspec_prefix >= 0 &&
 	    (prefixlen || (prefix && *prefix)))
@@ -236,7 +240,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item,
 				     elt, ce_len, ce->name);
 		}
 
-	if (literal_global)
+	if (magic & PATHSPEC_LITERAL)
 		item->nowildcard_len = item->len;
 	else {
 		item->nowildcard_len = simple_length(item->match);
@@ -402,7 +406,8 @@ const char **get_pathspec(const char *prefix, const char **pathspec)
 {
 	struct pathspec ps;
 	parse_pathspec(&ps,
-		       PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,
+		       PATHSPEC_ALL_MAGIC &
+		       ~(PATHSPEC_FROMTOP | PATHSPEC_LITERAL),
 		       PATHSPEC_PREFER_CWD,
 		       prefix, pathspec);
 	return ps.raw;
diff --git a/pathspec.h b/pathspec.h
index 4ebaadc..ec15fe6 100644
--- a/pathspec.h
+++ b/pathspec.h
@@ -4,9 +4,11 @@
 /* Pathspec magic */
 #define PATHSPEC_FROMTOP	(1<<0)
 #define PATHSPEC_MAXDEPTH	(1<<1)
+#define PATHSPEC_LITERAL	(1<<2)
 #define PATHSPEC_ALL_MAGIC	  \
 	(PATHSPEC_FROMTOP	| \
-	 PATHSPEC_MAXDEPTH)
+	 PATHSPEC_MAXDEPTH	| \
+	 PATHSPEC_LITERAL)
 
 #define PATHSPEC_ONESTAR 1	/* the pathspec pattern sastisfies GFNM_ONESTAR */
 
diff --git a/t/t6130-pathspec-noglob.sh b/t/t6130-pathspec-noglob.sh
index 39ef619..49c148e 100755
--- a/t/t6130-pathspec-noglob.sh
+++ b/t/t6130-pathspec-noglob.sh
@@ -47,18 +47,36 @@ test_expect_success 'no-glob option matches literally (vanilla)' '
 	test_cmp expect actual
 '
 
+test_expect_success 'no-glob option matches literally (vanilla)' '
+	echo vanilla >expect &&
+	git log --format=%s -- ":(literal)foo" >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'no-glob option matches literally (star)' '
 	echo star >expect &&
 	git --literal-pathspecs log --format=%s -- "f*" >actual &&
 	test_cmp expect actual
 '
 
+test_expect_success 'no-glob option matches literally (star)' '
+	echo star >expect &&
+	git log --format=%s -- ":(literal)f*" >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'no-glob option matches literally (bracket)' '
 	echo bracket >expect &&
 	git --literal-pathspecs log --format=%s -- "f[o][o]" >actual &&
 	test_cmp expect actual
 '
 
+test_expect_success 'no-glob option matches literally (bracket)' '
+	echo bracket >expect &&
+	git log --format=%s -- ":(literal)f[o][o]" >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'no-glob environment variable works' '
 	echo star >expect &&
 	GIT_LITERAL_PATHSPECS=1 git log --format=%s -- "f*" >actual &&
diff --git a/tree-diff.c b/tree-diff.c
index 21a50d8..ccf9d7c 100644
--- a/tree-diff.c
+++ b/tree-diff.c
@@ -202,7 +202,7 @@ static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, co
 	 * path. Magic that matches more than one path is not
 	 * supported.
 	 */
-	GUARD_PATHSPEC(&opt->pathspec, PATHSPEC_FROMTOP);
+	GUARD_PATHSPEC(&opt->pathspec, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
 #if 0
 	/*
 	 * We should reject wildcards as well. Unfortunately we
diff --git a/tree-walk.c b/tree-walk.c
index 37b157e..676bd7f 100644
--- a/tree-walk.c
+++ b/tree-walk.c
@@ -636,7 +636,10 @@ enum interesting tree_entry_interesting(const struct name_entry *entry,
 	enum interesting never_interesting = ps->has_wildcard ?
 		entry_not_interesting : all_entries_not_interesting;
 
-	GUARD_PATHSPEC(ps, PATHSPEC_FROMTOP | PATHSPEC_MAXDEPTH);
+	GUARD_PATHSPEC(ps,
+		       PATHSPEC_FROMTOP |
+		       PATHSPEC_MAXDEPTH |
+		       PATHSPEC_LITERAL);
 
 	if (!ps->nr) {
 		if (!ps->recursive ||
-- 
1.8.2.83.gc99314b
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 51 of 54 in “"struct pathspec" conversion and :(glob)”
  1. 00/45 "struct pathspec" conversion and :(glob)Nguyễn Thái Ngọc Duy, Jun 9, 2013
  2. 01/45 clean: remove unused variable "seen"Nguyễn Thái Ngọc Duy, Jun 9, 2013
  3. 02/45 Move struct pathspec and related functions to pathspec.[ch]Nguyễn Thái Ngọc Duy, Jun 9, 2013
  4. 03/45 pathspec: i18n-ize error strings in pathspec parsing codeNguyễn Thái Ngọc Duy, Jun 9, 2013
  5. 04/45 pathspec: add copy_pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  6. Eric SunshineJun 9, 2013
  7. Junio C HamanoJun 12, 2013
  8. 05/45 Add parse_pathspec() that converts cmdline args to struct pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  9. Eric SunshineJun 10, 2013
  10. 06/45 parse_pathspec: save original pathspec for reportingNguyễn Thái Ngọc Duy, Jun 9, 2013
  11. 07/45 parse_pathspec: add PATHSPEC_PREFER_{CWD,FULL}Nguyễn Thái Ngọc Duy, Jun 9, 2013
  12. Eric SunshineJun 10, 2013
  13. 08/45 Convert some get_pathspec() calls to parse_pathspec()Nguyễn Thái Ngọc Duy, Jun 9, 2013
  14. 09/45 parse_pathspec: a special flag for max_depth featureNguyễn Thái Ngọc Duy, Jun 9, 2013
  15. Eric SunshineJun 10, 2013
  16. 10/45 parse_pathspec: support stripping submodule trailing slashesNguyễn Thái Ngọc Duy, Jun 9, 2013
  17. Eric SunshineJun 10, 2013
  18. 11/45 parse_pathspec: support stripping/checking submodule pathsNguyễn Thái Ngọc Duy, Jun 9, 2013
  19. 12/45 parse_pathspec: support prefixing original patternsNguyễn Thái Ngọc Duy, Jun 9, 2013
  20. Eric SunshineJun 10, 2013
  21. 13/45 Guard against new pathspec magic in pathspec matching codeNguyễn Thái Ngọc Duy, Jun 9, 2013
  22. Eric SunshineJun 10, 2013
  23. 14/45 clean: convert to use parse_pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  24. 15/45 commit: convert to use parse_pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  25. 16/45 status: convert to use parse_pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  26. 17/45 rerere: convert to use parse_pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  27. 18/45 checkout: convert to use parse_pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  28. 19/45 rm: convert to use parse_pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  29. 20/45 ls-files: convert to use parse_pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  30. 21/45 archive: convert to use parse_pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  31. 22/45 check-ignore: convert to use parse_pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  32. 23/45 add: convert to use parse_pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  33. 24/45 reset: convert to use parse_pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  34. 25/45 line-log: convert to use parse_pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  35. 26/45 Convert read_cache_preload() to take struct pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  36. 27/45 Convert run_add_interactive to use struct pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  37. 28/45 Convert unmerge_cache to take struct pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  38. 29/45 checkout: convert read_tree_some to take struct pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  39. 30/45 Convert report_path_error to take struct pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  40. 31/45 Convert refresh_index to take struct pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  41. 32/45 Convert {read,fill}_directory to take struct pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  42. 33/45 Convert add_files_to_cache to take struct pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  43. 34/45 Convert common_prefix() to use struct pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  44. 35/45 Remove diff_tree_{setup,release}_pathsNguyễn Thái Ngọc Duy, Jun 9, 2013
  45. 36/45 Remove init_pathspec() in favor of parse_pathspec()Nguyễn Thái Ngọc Duy, Jun 9, 2013
  46. 37/45 Remove match_pathspec() in favor of match_pathspec_depth()Nguyễn Thái Ngọc Duy, Jun 9, 2013
  47. 38/45 tree-diff: remove the use of pathspec's raw[] in follow-rename codepathNguyễn Thái Ngọc Duy, Jun 9, 2013
  48. 39/45 parse_pathspec: make sure the prefix part is wildcard-freeNguyễn Thái Ngọc Duy, Jun 9, 2013
  49. 40/45 parse_pathspec: preserve prefix length via PATHSPEC_PREFIX_ORIGINNguyễn Thái Ngọc Duy, Jun 9, 2013
  50. 41/45 Kill limit_pathspec_to_literal() as it's only used by parse_pathspec()Nguyễn Thái Ngọc Duy, Jun 9, 2013
  51. 42/45 pathspec: support :(literal) syntax for noglob pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013
  52. 43/45 pathspec: make --literal-pathspecs disable pathspec magicNguyễn Thái Ngọc Duy, Jun 9, 2013
  53. 44/45 pathspec: support :(glob) syntaxNguyễn Thái Ngọc Duy, Jun 9, 2013
  54. 45/45 Rename field "raw" to "_raw" in struct pathspecNguyễn Thái Ngọc Duy, Jun 9, 2013

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.