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

[PATCH 1/2] refactor exclude handling

From
Jeff King <peff@peff.net>
Date
Feb 7, 2009, 06:44 UTC
Message-ID
<20090207064405.GA14906@coredump.intra.peff.net>
In-Reply-To
<20090207064221.GA14856@coredump.intra.peff.net>

The excluded function uses the static helper excluded_1 to perform the inner loop over all of the exclude patterns. The helper just tells us whether the path is included, excluded, or undecided.

However, it may be useful to know _which_ pattern was triggered. So let's pass out the entire exclude match, which contains the status information we were already passing out.

Further patches can make use of this.
Signed-off-by: Jeff King <peff@peff.net>
---
Just a cleanup for the next patch.
 dir.c |   25 ++++++++++++++-----------
 1 files changed, 14 insertions(+), 11 deletions(-)
diff --git a/dir.c b/dir.c
index cfd1ea5..0ea81b7 100644
--- a/dir.c
+++ b/dir.c
@@ -291,9 +291,10 @@ static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
 }
 
 /* Scan the list and let the last match determines the fate.
- * Return 1 for exclude, 0 for include and -1 for undecided.
+ * Returns the exclude_list element which matched, or NULL for
+ * undecided.
  */
-static int excluded_1(const char *pathname,
+static struct exclude *excluded_1(const char *pathname,
 		      int pathlen, const char *basename, int *dtype,
 		      struct exclude_list *el)
 {
@@ -303,7 +304,6 @@ static int excluded_1(const char *pathname,
 		for (i = el->nr - 1; 0 <= i; i--) {
 			struct exclude *x = el->excludes[i];
 			const char *exclude = x->pattern;
-			int to_exclude = x->to_exclude;
 
 			if (x->flags & EXC_FLAG_MUSTBEDIR) {
 				if (*dtype == DT_UNKNOWN)
@@ -316,14 +316,14 @@ static int excluded_1(const char *pathname,
 				/* match basename */
 				if (x->flags & EXC_FLAG_NOWILDCARD) {
 					if (!strcmp(exclude, basename))
-						return to_exclude;
+						return x;
 				} else if (x->flags & EXC_FLAG_ENDSWITH) {
 					if (x->patternlen - 1 <= pathlen &&
 					    !strcmp(exclude + 1, pathname + pathlen - x->patternlen + 1))
-						return to_exclude;
+						return x;
 				} else {
 					if (fnmatch(exclude, basename, 0) == 0)
-						return to_exclude;
+						return x;
 				}
 			}
 			else {
@@ -342,16 +342,16 @@ static int excluded_1(const char *pathname,
 
 				if (x->flags & EXC_FLAG_NOWILDCARD) {
 					if (!strcmp(exclude, pathname + baselen))
-						return to_exclude;
+						return x;
 				} else {
 					if (fnmatch(exclude, pathname+baselen,
 						    FNM_PATHNAME) == 0)
-					    return to_exclude;
+					    return x;
 				}
 			}
 		}
 	}
-	return -1; /* undecided */
+	return NULL;
 }
 
 int excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
@@ -363,8 +363,11 @@ int excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
 
 	prep_exclude(dir, pathname, basename-pathname);
 	for (st = EXC_CMDL; st <= EXC_FILE; st++) {
-		switch (excluded_1(pathname, pathlen, basename,
-				   dtype_p, &dir->exclude_list[st])) {
+		struct exclude *x = excluded_1(pathname, pathlen, basename,
+					dtype_p, &dir->exclude_list[st]);
+		if (!x)
+			continue;
+		switch (x->to_exclude) {
 		case 0:
 			return 0;
 		case 1:
-- 
1.6.1.2.552.g1682c.dirty
Previous: Jeff KingNext: Jeff King
Message 5 of 17 in “How to find out which gitignore blocks my git-add”
  1. GonzoFeb 6, 2009
  2. Jeff KingFeb 6, 2009
  3. Sitaram ChamartyFeb 7, 2009
  4. Jeff KingFeb 7, 2009
  5. 1/2 refactor exclude handlingJeff King, Feb 7, 2009
  6. 2/2 give exclude mechanism a debug optionJeff King, Feb 7, 2009
  7. Junio C HamanoFeb 7, 2009
  8. Junio C HamanoFeb 7, 2009
  9. Jeff KingFeb 7, 2009
  10. Jeff KingFeb 7, 2009
  11. Junio C HamanoFeb 7, 2009
  12. Jeff KingFeb 8, 2009
  13. Junio C HamanoFeb 8, 2009
  14. Jeff KingFeb 8, 2009
  15. Sitaram ChamartyFeb 7, 2009
  16. Jeff KingFeb 7, 2009
  17. bisaniFeb 6, 2009

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.