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

[PATCH 5/6] gitignore: do not do basename match with patterns that have '**'

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Oct 4, 2012, 07:39 UTC
Message-ID
<1349336392-1772-6-git-send-email-pclouds@gmail.com>
In-Reply-To
<1349336392-1772-1-git-send-email-pclouds@gmail.com>

"**" can match slashes, not like "*". "ab**ef" should be able to match "ab/cd/ef", or "ab/c/d/ef" and so on. Turn off the EXC_FLAG_NODIR in this case otherwise the pattern is only checked against the base name. This behavior is in sync with rsync.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/gitignore.txt        | 10 +++++-----
 attr.c                             |  2 +-
 dir.c                              |  2 +-
 t/t0003-attributes.sh              | 16 ++++++++++++++++
 t/t3001-ls-files-others-exclude.sh | 10 ++++++++++
 5 files changed, 33 insertions(+), 7 deletions(-)
diff --git a/Documentation/gitignore.txt b/Documentation/gitignore.txt
index eb81d31..4dfe8bd 100644
--- a/Documentation/gitignore.txt
+++ b/Documentation/gitignore.txt
@@ -81,11 +81,11 @@ PATTERN FORMAT
    regular file or a symbolic link `foo` (this is consistent
    with the way how pathspec works in general in git).
 
- - If the pattern does not contain a slash '/', git treats it as
-   a shell glob pattern and checks for a match against the
-   pathname relative to the location of the `.gitignore` file
-   (relative to the toplevel of the work tree if not from a
-   `.gitignore` file).
+ - If the pattern does not contain a slash '/' nor '**', git
+   treats it as a shell glob pattern and checks for a match
+   against the pathname relative to the location of the
+   `.gitignore` file (relative to the toplevel of the work tree
+   if not from a `.gitignore` file).
 
  - Otherwise, git treats the pattern as a shell glob suitable
    for consumption by fnmatch(3) with the FNM_PATHNAME flag:
diff --git a/attr.c b/attr.c
index 3fde9fa..634b39c 100644
--- a/attr.c
+++ b/attr.c
@@ -253,7 +253,7 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
 		res->pat.pattern = p;
 		res->pat.patternlen = strlen(p);
 		res->pat.nowildcardlen = simple_length(p);
-		if (!strchr(p, '/'))
+		if (!strchr(p, '/') && !strstr(p, "**"))
 			res->pat.flags |= EXC_FLAG_NODIR;
 		if (*p == '*' && no_wildcard(p+1))
 			res->pat.flags |= EXC_FLAG_ENDSWITH;
diff --git a/dir.c b/dir.c
index fd49336..6a5de98 100644
--- a/dir.c
+++ b/dir.c
@@ -340,7 +340,7 @@ void add_exclude(const char *string, const char *base,
 	x->base = base;
 	x->baselen = baselen;
 	x->flags = flags;
-	if (!strchr(string, '/'))
+	if (!strchr(string, '/') && !strstr(string, "**"))
 		x->flags |= EXC_FLAG_NODIR;
 	x->nowildcardlen = simple_length(string);
 	if (*string == '*' && no_wildcard(string+1))
diff --git a/t/t0003-attributes.sh b/t/t0003-attributes.sh
index 6c3c554..9b534a0 100755
--- a/t/t0003-attributes.sh
+++ b/t/t0003-attributes.sh
@@ -249,4 +249,20 @@ EOF
 	test_line_count = 0 err
 '
 
+test_expect_success '"**" with no slashes test' '
+	echo "a**f foo=bar" >.gitattributes &&
+	cat <<\EOF >expect &&
+f: foo: unspecified
+a/f: foo: bar
+a/b/f: foo: bar
+a/b/c/f: foo: bar
+EOF
+	git check-attr foo -- "f" >actual 2>err &&
+	git check-attr foo -- "a/f" >>actual 2>>err &&
+	git check-attr foo -- "a/b/f" >>actual 2>>err &&
+	git check-attr foo -- "a/b/c/f" >>actual 2>>err &&
+	test_cmp expect actual &&
+	test_line_count = 0 err
+'
+
 test_done
diff --git a/t/t3001-ls-files-others-exclude.sh b/t/t3001-ls-files-others-exclude.sh
index 67c8bcf..6a5a4ab 100755
--- a/t/t3001-ls-files-others-exclude.sh
+++ b/t/t3001-ls-files-others-exclude.sh
@@ -225,4 +225,14 @@ EOF
 	test_cmp expect actual
 '
 
+
+test_expect_success 'ls-files with "**" patterns and no slashes' '
+	cat <<\EOF >expect &&
+one/a.1
+one/two/a.1
+EOF
+	git ls-files -o -i --exclude "one**a.1" >actual
+	test_cmp expect actual
+'
+
 test_done
-- 
1.7.12.1.405.gb727dc9
Previous: Nguyễn Thái Ngọc DuyNext: Junio C Hamano
Message 11 of 28 in “What's cooking in git.git (Oct 2012, #01; Tue, 2)”
  1. Junio C HamanoOct 2, 2012
  2. Nguyen Thai Ngoc DuyOct 3, 2012
  3. Junio C HamanoOct 3, 2012
  4. Nguyen Thai Ngoc DuyOct 4, 2012
  5. Junio C HamanoOct 4, 2012
  6. 0/6 wildmatch part 2Nguyễn Thái Ngọc Duy, Oct 4, 2012
  7. 1/6 attr: remove the union in struct match_attrNguyễn Thái Ngọc Duy, Oct 4, 2012
  8. 2/6 attr: avoid strlen() on every matchNguyễn Thái Ngọc Duy, Oct 4, 2012
  9. 3/6 attr: avoid searching for basename on every matchNguyễn Thái Ngọc Duy, Oct 4, 2012
  10. 4/6 attr: more matching optimizations from .gitignoreNguyễn Thái Ngọc Duy, Oct 4, 2012
  11. 5/6 gitignore: do not do basename match with patterns that have '**'Nguyễn Thái Ngọc Duy, Oct 4, 2012
  12. Junio C HamanoOct 4, 2012
  13. Johannes SixtOct 5, 2012
  14. Nguyen Thai Ngoc DuyOct 5, 2012
  15. 6/6 t3001: note about expected "**" behaviorNguyễn Thái Ngọc Duy, Oct 4, 2012
  16. Junio C HamanoOct 4, 2012
  17. Junio C HamanoOct 4, 2012
  18. Michael HaggertyOct 4, 2012
  19. Nguyen Thai Ngoc DuyOct 4, 2012
  20. Michael HaggertyOct 4, 2012
  21. Junio C HamanoOct 4, 2012
  22. Andreas SchwabOct 5, 2012
  23. Matthieu MoyOct 5, 2012
  24. Andreas SchwabOct 5, 2012
  25. Nguyen Thai Ngoc DuyOct 5, 2012
  26. David Michael BarrOct 4, 2012
  27. Junio C HamanoOct 4, 2012
  28. Florian AchleitnerOct 30, 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.