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

[PATCH 04/10] attr: more matching optimizations from .gitignore

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

.gitattributes and .gitignore share the same pattern syntax but has separate matching implementation. Over the years, ignore's implementation accumulates more optimizations while attr's stays the same.

This patch adds those optimizations to .gitattributes. Basically it tries to avoid fnmatch/wildmatch in favor of strncmp as much as possible.

There are two syntaxes that .gitignore supports but .gitattributes does not: negative patterns and directory matching. They have never worked and whether they will is up for future discussion. Meanwhile make a note in the document and reject such patterns.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/gitattributes.txt |  2 ++
 attr.c                          | 68 ++++++++++++++++++++++++++++++++++-------
 dir.c                           |  8 ++---
 dir.h                           |  1 +
 4 files changed, 64 insertions(+), 15 deletions(-)
diff --git a/Documentation/gitattributes.txt b/Documentation/gitattributes.txt
index e16f3e1..702b8c1 100644
--- a/Documentation/gitattributes.txt
+++ b/Documentation/gitattributes.txt
@@ -56,6 +56,8 @@ When more than one pattern matches the path, a later line
 overrides an earlier line.  This overriding is done per
 attribute.  The rules how the pattern matches paths are the
 same as in `.gitignore` files; see linkgit:gitignore[5].
+Unlike `.gitignore`, negative patterns are not supported.
+Patterns that match directories are also not supported.
 
 When deciding what attributes are assigned to a path, git
 consults `$GIT_DIR/info/attributes` file (which has the highest
diff --git a/attr.c b/attr.c
index aeac564..1aa058e 100644
--- a/attr.c
+++ b/attr.c
@@ -115,6 +115,13 @@ struct attr_state {
 	const char *setto;
 };
 
+struct pattern {
+	const char *pattern;
+	int patternlen;
+	int nowildcardlen;
+	int flags;		/* EXC_FLAG_* */
+};
+
 /*
  * One rule, as from a .gitattributes file.
  *
@@ -131,7 +138,7 @@ struct attr_state {
  */
 struct match_attr {
 	union {
-		char *pattern;
+		struct pattern pat;
 		struct git_attr *attr;
 	} u;
 	char is_macro;
@@ -241,9 +248,18 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
 	if (is_macro)
 		res->u.attr = git_attr_internal(name, namelen);
 	else {
-		res->u.pattern = (char *)&(res->state[num_attr]);
-		memcpy(res->u.pattern, name, namelen);
-		res->u.pattern[namelen] = 0;
+		char *p = (char *)&(res->state[num_attr]);
+		memcpy(p, name, namelen);
+		p[namelen] = 0;
+		res->u.pat.pattern = p;
+		parse_exclude_pattern(&res->u.pat.pattern,
+				      &res->u.pat.patternlen,
+				      &res->u.pat.flags,
+				      &res->u.pat.nowildcardlen);
+		if (res->u.pat.flags & EXC_FLAG_NEGATIVE)
+			die(_("Negative patterns are not supported in git attributes"));
+		if (res->u.pat.flags & EXC_FLAG_MUSTBEDIR)
+			die(_("Directory patterns are not supported in git attributes"));
 	}
 	res->is_macro = is_macro;
 	res->num_attr = num_attr;
@@ -645,25 +661,55 @@ static void prepare_attr_stack(const char *path)
 
 static int path_matches(const char *pathname, int pathlen,
 			const char *basename,
-			const char *pattern,
+			const struct pattern *pat,
 			const char *base, int baselen)
 {
-	if (!strchr(pattern, '/')) {
+	const char *pattern = pat->pattern;
+	int prefix = pat->nowildcardlen;
+	const char *name;
+	int namelen;
+
+	if (pat->flags & EXC_FLAG_NODIR) {
+		if (prefix == pat->patternlen &&
+		    !strcmp_icase(pattern, basename))
+			return 1;
+
+		if (pat->flags & EXC_FLAG_ENDSWITH &&
+		    pat->patternlen - 1 <= pathlen &&
+		    !strcmp_icase(pattern + 1, pathname +
+				  pathlen - pat->patternlen + 1))
+			return 1;
+
 		return (fnmatch_icase(pattern, basename, 0) == 0);
 	}
 	/*
 	 * match with FNM_PATHNAME; the pattern has base implicitly
 	 * in front of it.
 	 */
-	if (*pattern == '/')
+	if (*pattern == '/') {
 		pattern++;
+		prefix--;
+	}
+
+	/*
+	 * note: unlike excluded_from_list, baselen here does not
+	 * contain the trailing slash
+	 */
+
 	if (pathlen < baselen ||
 	    (baselen && pathname[baselen] != '/') ||
 	    strncmp(pathname, base, baselen))
 		return 0;
-	if (baselen != 0)
-		baselen++;
-	return fnmatch_icase(pattern, pathname + baselen, FNM_PATHNAME) == 0;
+
+	namelen = baselen ? pathlen - baselen - 1 : pathlen;
+	name = pathname + pathlen - namelen;
+
+	/* if the non-wildcard part is longer than the remaining
+	   pathname, surely it cannot match */
+	if (!namelen || prefix > namelen)
+		return 0;
+
+	return fnmatch_icase(pattern, name, FNM_PATHNAME) == 0;
 }
 
 static int macroexpand_one(int attr_nr, int rem);
@@ -701,7 +747,7 @@ static int fill(const char *path, int pathlen, const char *basename,
 		if (a->is_macro)
 			continue;
 		if (path_matches(path, pathlen, basename,
-				 a->u.pattern, base, stk->originlen))
+				 &a->u.pat, base, stk->originlen))
 			rem = fill_one("fill", a, rem);
 	}
 	return rem;
diff --git a/dir.c b/dir.c
index cd13920..c6a0275 100644
--- a/dir.c
+++ b/dir.c
@@ -308,10 +308,10 @@ static int no_wildcard(const char *string)
 	return string[simple_length(string)] == '\0';
 }
 
-static void parse_exclude_pattern(const char **pattern,
-				  int *patternlen,
-				  int *flags,
-				  int *nowildcardlen)
+void parse_exclude_pattern(const char **pattern,
+			   int *patternlen,
+			   int *flags,
+			   int *nowildcardlen)
 {
 	const char *p = *pattern;
 	size_t i, len;
diff --git a/dir.h b/dir.h
index 41ea32d..fd5c2aa 100644
--- a/dir.h
+++ b/dir.h
@@ -97,6 +97,7 @@ extern int path_excluded(struct path_exclude_check *, const char *, int namelen,
 extern int add_excludes_from_file_to_list(const char *fname, const char *base, int baselen,
 					  char **buf_p, struct exclude_list *which, int check_index);
 extern void add_excludes_from_file(struct dir_struct *, const char *fname);
+extern void parse_exclude_pattern(const char **string, int *patternlen, int *flags, int *nowildcardlen);
 extern void add_exclude(const char *string, const char *base,
 			int baselen, struct exclude_list *which);
 extern void free_excludes(struct exclude_list *el);
-- 
1.7.12.1.405.gb727dc9
Previous: Nguyễn Thái Ngọc DuyNext: Junio C Hamano
Message 5 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.