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

[PATCH 4/4] attr.c: fix matching "subdir" without the trailing slash

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Mar 25, 2013, 06:05 UTC
Message-ID
<1364191510-8900-5-git-send-email-pclouds@gmail.com>
In-Reply-To
<1364191510-8900-1-git-send-email-pclouds@gmail.com>

The story goes back to 94bc671 (Add directory pattern matching to attributes - 2012-12-08). Before this commit, directories are passed to path_matches without the trailing slash. This is fine for matching pattern "subdir" with "foo/subdir".

Patterns like "subdir/" (i.e. match _directory_ subdir) won't work though. So paths are now passed to path_matches with the trailing slash (i.e. "subdir/"). The trailing slash is used as the directory indicator (similar to dtype in exclude case). This makes pattern "subdir/" match directory "subdir/". Pattern "subdir" no longer match subdir, which is now "subdir/".

As the trailing slash in pathname is the directory indicator, we do not need to keep it in the pathname for matching. The trailing slash should be turned to dtype "DT_DIR" and stripped out of pathname. This keeps the code pattern similar to exclude.

The same applies for the pattern "subdir/". The trailing slash is converted to flag EXC_FLAG_MUSTBEDIR and should not remain in the pattern, as noted in parse_exclude_pattern(). prepare_attr_stack() breaks this and keeps the trailing slash anyway.

To sum up, both patterns and pathnames should never have the trailing slash when it comes to match_basename.

Reported-and-analyzed-by: Jeff King <peff@peff.net>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 attr.c                          | 11 +++++++++--
 t/t5002-archive-attr-pattern.sh |  6 ++++++
 2 files changed, 15 insertions(+), 2 deletions(-)
diff --git a/attr.c b/attr.c
index 1818ba5..a95c837 100644
--- a/attr.c
+++ b/attr.c
@@ -256,7 +256,7 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
 				      &res->u.pat.flags,
 				      &res->u.pat.nowildcardlen);
 		if (res->u.pat.flags & EXC_FLAG_MUSTBEDIR)
-			res->u.pat.patternlen++;
+			p[res->u.pat.patternlen] = '\0';
 		if (res->u.pat.flags & EXC_FLAG_NEGATIVE) {
 			warning(_("Negative patterns are ignored in git attributes\n"
 				  "Use '\\!' for literal leading exclamation."));
@@ -665,9 +665,16 @@ static int path_matches(const char *pathname, int pathlen,
 {
 	const char *pattern = pat->pattern;
 	int prefix = pat->nowildcardlen;
+	int dtype;
+
+	if (pathlen && pathname[pathlen-1] == '/') {
+		dtype = DT_DIR;
+		pathlen--;
+	} else
+		dtype = DT_REG;
 
 	if ((pat->flags & EXC_FLAG_MUSTBEDIR) &&
-	    ((!pathlen) || (pathname[pathlen-1] != '/')))
+	    dtype != DT_DIR)
 		return 0;
 
 	if (pat->flags & EXC_FLAG_NODIR) {
diff --git a/t/t5002-archive-attr-pattern.sh b/t/t5002-archive-attr-pattern.sh
index 0c847fb..98ccc3c 100755
--- a/t/t5002-archive-attr-pattern.sh
+++ b/t/t5002-archive-attr-pattern.sh
@@ -27,6 +27,10 @@ test_expect_success 'setup' '
 	echo ignored-only-if-dir/ export-ignore >>.git/info/attributes &&
 	git add ignored-only-if-dir &&
 
+	mkdir -p ignored-without-slash &&
+	echo ignored without slash >ignored-without-slash/foo &&
+	git add ignored-without-slash/foo &&
+	echo ignored-without-slash export-ignore >>.git/info/attributes &&
 
 	mkdir -p one-level-lower/two-levels-lower/ignored-only-if-dir &&
 	echo ignored by ignored dir >one-level-lower/two-levels-lower/ignored-only-if-dir/ignored-by-ignored-dir &&
@@ -49,6 +53,8 @@ test_expect_exists	archive/not-ignored-dir/ignored-only-if-dir
 test_expect_exists	archive/not-ignored-dir/
 test_expect_missing	archive/ignored-only-if-dir/
 test_expect_missing	archive/ignored-ony-if-dir/ignored-by-ignored-dir
+test_expect_missing	archive/ignored-without-slash/ &&
+test_expect_missing	archive/ignored-without-slash/foo &&
 test_expect_exists	archive/one-level-lower/
 test_expect_missing	archive/one-level-lower/two-levels-lower/ignored-only-if-dir/
 test_expect_missing	archive/one-level-lower/two-levels-lower/ignored-ony-if-dir/ignored-by-ignored-dir
-- 
1.8.2.82.gc24b958
Previous: Nguyễn Thái Ngọc DuyNext: Duy Nguyen
Message 63 of 66 in “[regression?] trailing slash required in .gitattributes”
  1. Jeff KingMar 19, 2013
  2. Junio C HamanoMar 19, 2013
  3. Jeff KingMar 19, 2013
  4. Jeff KingMar 22, 2013
  5. Junio C HamanoMar 22, 2013
  6. Jeff KingMar 23, 2013
  7. Junio C HamanoMar 24, 2013
  8. 0/4 attribute regression fix for maint-1.8.1 and upwardJunio C Hamano, Mar 26, 2013
  9. 1/4 attr.c::path_matches(): the basename is part of the pathnameJunio C Hamano, Mar 26, 2013
  10. Jeff KingMar 26, 2013
  11. Duy NguyenMar 27, 2013
  12. 2/4 dir.c::match_basename(): pay attention to the length of string parametersJunio C Hamano, Mar 26, 2013
  13. Jeff KingMar 26, 2013
  14. Jeff KingMar 26, 2013
  15. Junio C HamanoMar 26, 2013
  16. Jeff KingMar 26, 2013
  17. Junio C HamanoMar 26, 2013
  18. Jeff KingMar 27, 2013
  19. 3/4 attr.c::path_matches(): special case paths that end with a slashJunio C Hamano, Mar 26, 2013
  20. Jeff KingMar 26, 2013
  21. Jeff KingMar 26, 2013
  22. Duy NguyenMar 27, 2013
  23. Jeff KingMar 28, 2013
  24. 4/4 make sure a pattern without trailing slash matches a directoryJunio C Hamano, Mar 26, 2013
  25. Jeff KingMar 26, 2013
  26. Duy NguyenMar 27, 2013
  27. Junio C HamanoMar 27, 2013
  28. Duy NguyenMar 27, 2013
  29. 0/6 attribute regression fix for maint-1.8.1 and upwardJeff King, Mar 28, 2013
  30. 1/6 attr.c::path_matches(): the basename is part of the pathnameJeff King, Mar 28, 2013
  31. 2/6 dir.c::match_basename(): pay attention to the length of string parametersJeff King, Mar 28, 2013
  32. Jeff KingMar 28, 2013
  33. Jeff KingMar 28, 2013
  34. Junio C HamanoMar 28, 2013
  35. Duy NguyenMar 28, 2013
  36. Duy NguyenMar 29, 2013
  37. Jeff KingMar 29, 2013
  38. Junio C HamanoMar 29, 2013
  39. 3/6 dir.c::match_pathname(): adjust patternlen when shifting patternJeff King, Mar 28, 2013
  40. 4/6 dir.c::match_pathname(): pay attention to the length of string parametersJeff King, Mar 28, 2013
  41. Junio C HamanoMar 28, 2013
  42. Duy NguyenMar 29, 2013
  43. Duy NguyenMar 29, 2013
  44. Torsten BögershausenMar 29, 2013
  45. Duy NguyenMar 29, 2013
  46. Jeff KingMar 29, 2013
  47. Duy NguyenMar 29, 2013
  48. Junio C HamanoMar 29, 2013
  49. Jeff KingMar 29, 2013
  50. Junio C HamanoMar 29, 2013
  51. Jeff KingMar 29, 2013
  52. Duy NguyenMar 30, 2013
  53. 5/6 attr.c::path_matches(): special case paths that end with a slashJeff King, Mar 28, 2013
  54. 6/6 t: check that a pattern without trailing slash matches a directoryJeff King, Mar 28, 2013
  55. Eric SunshineMar 28, 2013
  56. Jeff KingMar 28, 2013
  57. Duy NguyenMar 23, 2013
  58. Duy NguyenMar 23, 2013
  59. 0/4 attr directory matching regressionNguyễn Thái Ngọc Duy, Mar 25, 2013
  60. 1/4 wildmatch: do not require "text" to be NUL-terminatedNguyễn Thái Ngọc Duy, Mar 25, 2013
  61. 2/4 attr.c: fix pattern{,len} inconsistency in struct match_attrNguyễn Thái Ngọc Duy, Mar 25, 2013
  62. 3/4 dir.c: make match_{base,path}name respect {basename,path}lenNguyễn Thái Ngọc Duy, Mar 25, 2013
  63. 4/4 attr.c: fix matching "subdir" without the trailing slashNguyễn Thái Ngọc Duy, Mar 25, 2013
  64. Duy NguyenMar 25, 2013
  65. Duy NguyenMar 25, 2013
  66. Junio C HamanoMar 26, 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.