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

[PATCH 10/10] gitignore: forbid "abc**def"

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Oct 5, 2012, 04:41 UTC
Message-ID
<1349412069-627-11-git-send-email-pclouds@gmail.com>
In-Reply-To
<1349412069-627-1-git-send-email-pclouds@gmail.com>

Deep down wildmatch() sees "**" as "*" that can also match slashes. On the surface, it may be confusing to users as the above pattern can match "abcdef", "abcxyzdef", "abc/def", "abc/x/def", "abc/x/y/def"... For now we just forbid it. Users can only do "**/def", "abc/**" or "abc/**/def". The syntax may be re-enabled in future.

There's a minor problem with this particular approach. "**" inside square brackets are mistaken as the wildcard while they are not. Git shows a confusing message when users do that.

Note that this patch hides a potential problem that if "abc**def" is ever supported, EXC_FLAG_NODIR flag should be turned off or only the base name is matched against the pattern, which makes no sense.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/gitignore.txt        |  6 ++++--
 dir.c                              | 12 +++++++++---
 t/t0003-attributes.sh              |  5 +++++
 t/t3001-ls-files-others-exclude.sh |  5 +++++
 4 files changed, 23 insertions(+), 5 deletions(-)
diff --git a/Documentation/gitignore.txt b/Documentation/gitignore.txt
index eb81d31..ad9fc2f 100644
--- a/Documentation/gitignore.txt
+++ b/Documentation/gitignore.txt
@@ -94,8 +94,10 @@ PATTERN FORMAT
    "Documentation/git.html" but not "Documentation/ppc/ppc.html"
    or "tools/perf/Documentation/perf.html".
 +
-Contrary to fnmatch(3), git matches "**" to anything including
-slashes, similar to rsync(1).
+In addition to fnmatch(3) syntax, "**" can be used to match one or
+more directories. For example, "abc/**/def" matches "abc/x/def",
+"abc/x/y/def", "abc/x/y/z/def" and so on. "**" must be wrapped by
+slashes.
 
  - A leading slash matches the beginning of the pathname.
    For example, "/{asterisk}.c" matches "cat-file.c" but not
diff --git a/dir.c b/dir.c
index cb78273..f30117f 100644
--- a/dir.c
+++ b/dir.c
@@ -327,12 +327,18 @@ void parse_exclude_pattern(const char **pattern,
 		len--;
 		*flags |= EXC_FLAG_MUSTBEDIR;
 	}
+	*flags |= EXC_FLAG_NODIR;
 	for (i = 0; i < len; i++) {
 		if (p[i] == '/')
-			break;
+			*flags &= ~EXC_FLAG_NODIR;
+		if ((p[i] == '*' && p[i + 1] == '*' &&
+		     (i == 0 || p[i - 1] != '\\')) &&
+		    !((i == 0 || p[i - 1] == '/') &&
+		      (p[i + 2] == '\0' ||
+		       p[i + 2] == '/' ||
+		       (p[i + 2] == '\\' && p[i + 3] == '/'))))
+			die(_("** in .gitignore or .gitattributes must be wrapped by slashes"));
 	}
-	if (i == len)
-		*flags |= EXC_FLAG_NODIR;
 	*nowildcardlen = simple_length(p);
 	/*
 	 * we should have excluded the trailing slash from 'p' too,
diff --git a/t/t0003-attributes.sh b/t/t0003-attributes.sh
index 6c3c554..ddeb321 100755
--- a/t/t0003-attributes.sh
+++ b/t/t0003-attributes.sh
@@ -249,4 +249,9 @@ EOF
 	test_line_count = 0 err
 '
 
+test_expect_success '"**" with no slashes test' '
+	echo "a**f foo=bar" >.gitattributes &&
+	test_must_fail git check-attr foo -- "f"
+'
+
 test_done
diff --git a/t/t3001-ls-files-others-exclude.sh b/t/t3001-ls-files-others-exclude.sh
index 67c8bcf..f5c62d0 100755
--- a/t/t3001-ls-files-others-exclude.sh
+++ b/t/t3001-ls-files-others-exclude.sh
@@ -225,4 +225,9 @@ EOF
 	test_cmp expect actual
 '
 
+
+test_expect_success 'ls-files with "**" patterns and no slashes' '
+	test_must_fail git ls-files -o -i --exclude "one**a.1"
+'
+
 test_done
-- 
1.7.12.1.405.gb727dc9
Previous: Nguyễn Thái Ngọc Duy
Message 22 of 22 in “nd/wildmatch take 2”
  1. 00/10 nd/wildmatch take 2Nguyễn Thái Ngọc Duy, Oct 5, 2012
  2. 01/10 gitignore: make pattern parsing code a separate functionNguyễn Thái Ngọc Duy, Oct 5, 2012
  3. 02/10 attr: avoid strlen() on every matchNguyễn Thái Ngọc Duy, Oct 5, 2012
  4. 03/10 attr: avoid searching for basename on every matchNguyễn Thái Ngọc Duy, Oct 5, 2012
  5. 04/10 attr: more matching optimizations from .gitignoreNguyễn Thái Ngọc Duy, Oct 5, 2012
  6. Junio C HamanoOct 5, 2012
  7. Nguyen Thai Ngoc DuyOct 6, 2012
  8. Junio C HamanoOct 6, 2012
  9. Nguyen Thai Ngoc DuyOct 6, 2012
  10. Junio C HamanoOct 6, 2012
  11. Nguyen Thai Ngoc DuyOct 8, 2012
  12. Junio C HamanoOct 8, 2012
  13. 05/10 Import wildmatch from rsyncNguyễn Thái Ngọc Duy, Oct 5, 2012
  14. Peter KreftingOct 5, 2012
  15. Nguyen Thai Ngoc DuyOct 5, 2012
  16. 06/10 wildmatch: remove static variable force_lower_caseNguyễn Thái Ngọc Duy, Oct 5, 2012
  17. 07/10 wildmatch: fix case-insensitive matchingNguyễn Thái Ngọc Duy, Oct 5, 2012
  18. 08/10 Integrate wildmatch to gitNguyễn Thái Ngọc Duy, Oct 5, 2012
  19. Thiago FarinaOct 5, 2012
  20. Joachim SchmitzOct 6, 2012
  21. 09/10 Support "**" in .gitignore and .gitattributes patterns using wildmatch()Nguyễn Thái Ngọc Duy, Oct 5, 2012
  22. 10/10 gitignore: forbid "abc**def"Nguyễn Thái Ngọc Duy, Oct 5, 2012

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.