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

[PATCH 01/10] gitignore: make pattern parsing code a separate function

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

This function can later be reused by attr.c. Also turn to_exclude field into a flag.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 If we go with glob->regex conversion way, this is where we could
 rewrite the pattern (and set EXC_FLAG_REGEX).
 dir.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++---------------------
 dir.h |  2 +-
 2 files changed, 50 insertions(+), 23 deletions(-)
diff --git a/dir.c b/dir.c
index 240bf0c..cd13920 100644
--- a/dir.c
+++ b/dir.c
@@ -308,42 +308,69 @@ 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)
+{
+	const char *p = *pattern;
+	size_t i, len;
+
+	*flags = 0;
+	if (*p == '!') {
+		*flags |= EXC_FLAG_NEGATIVE;
+		p++;
+	}
+	len = strlen(p);
+	if (len && p[len - 1] == '/') {
+		len--;
+		*flags |= EXC_FLAG_MUSTBEDIR;
+	}
+	for (i = 0; i < len; i++) {
+		if (p[i] == '/')
+			break;
+	}
+	if (i == len)
+		*flags |= EXC_FLAG_NODIR;
+	*nowildcardlen = simple_length(p);
+	/*
+	 * we should have excluded the trailing slash from 'p' too,
+	 * but that's one more allocation. Instead just make sure
+	 * nowildcardlen does not exceed real patternlen
+	 */
+	if (*nowildcardlen > len)
+		*nowildcardlen = len;
+	if (*p == '*' && no_wildcard(p + 1))
+		*flags |= EXC_FLAG_ENDSWITH;
+	*pattern = p;
+	*patternlen = len;
+}
+
 void add_exclude(const char *string, const char *base,
 		 int baselen, struct exclude_list *which)
 {
 	struct exclude *x;
-	size_t len;
-	int to_exclude = 1;
-	int flags = 0;
+	int patternlen;
+	int flags;
+	int nowildcardlen;
 
-	if (*string == '!') {
-		to_exclude = 0;
-		string++;
-	}
-	len = strlen(string);
-	if (len && string[len - 1] == '/') {
+	parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen);
+	if (flags & EXC_FLAG_MUSTBEDIR) {
 		char *s;
-		x = xmalloc(sizeof(*x) + len);
+		x = xmalloc(sizeof(*x) + patternlen + 1);
 		s = (char *)(x+1);
-		memcpy(s, string, len - 1);
-		s[len - 1] = '\0';
-		string = s;
+		memcpy(s, string, patternlen);
+		s[patternlen] = '\0';
 		x->pattern = s;
-		flags = EXC_FLAG_MUSTBEDIR;
 	} else {
 		x = xmalloc(sizeof(*x));
 		x->pattern = string;
 	}
-	x->to_exclude = to_exclude;
-	x->patternlen = strlen(string);
+	x->patternlen = patternlen;
+	x->nowildcardlen = nowildcardlen;
 	x->base = base;
 	x->baselen = baselen;
 	x->flags = flags;
-	if (!strchr(string, '/'))
-		x->flags |= EXC_FLAG_NODIR;
-	x->nowildcardlen = simple_length(string);
-	if (*string == '*' && no_wildcard(string+1))
-		x->flags |= EXC_FLAG_ENDSWITH;
 	ALLOC_GROW(which->excludes, which->nr + 1, which->alloc);
 	which->excludes[which->nr++] = x;
 }
@@ -518,7 +545,7 @@ int excluded_from_list(const char *pathname,
 	for (i = el->nr - 1; 0 <= i; i--) {
 		struct exclude *x = el->excludes[i];
 		const char *name, *exclude = x->pattern;
-		int to_exclude = x->to_exclude;
+		int to_exclude = x->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
 		int namelen, prefix = x->nowildcardlen;
 
 		if (x->flags & EXC_FLAG_MUSTBEDIR) {
diff --git a/dir.h b/dir.h
index 893465a..41ea32d 100644
--- a/dir.h
+++ b/dir.h
@@ -11,6 +11,7 @@ struct dir_entry {
 #define EXC_FLAG_NODIR 1
 #define EXC_FLAG_ENDSWITH 4
 #define EXC_FLAG_MUSTBEDIR 8
+#define EXC_FLAG_NEGATIVE 16
 
 struct exclude_list {
 	int nr;
@@ -21,7 +22,6 @@ struct exclude_list {
 		int nowildcardlen;
 		const char *base;
 		int baselen;
-		int to_exclude;
 		int flags;
 	} **excludes;
 };
-- 
1.7.12.1.405.gb727dc9
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 2 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.