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

[PATCH 1/5] tree-walk: support negative pathspec

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Mar 10, 2011, 03:13 UTC
Message-ID
<1299726819-5576-2-git-send-email-pclouds@gmail.com>
In-Reply-To
<1299726819-5576-1-git-send-email-pclouds@gmail.com>

While the UI will be simple, where negative pathspecs are always appended in the end, this could should be able to handle a mixed set of negative and positive pathspecs (ie. overlapping must be handled correctly).

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 cache.h     |    1 +
 tree-walk.c |   37 ++++++++++++++++++++++++++++---------
 2 files changed, 29 insertions(+), 9 deletions(-)
diff --git a/cache.h b/cache.h
index 08a9022..2189366 100644
--- a/cache.h
+++ b/cache.h
@@ -510,6 +510,7 @@ struct pathspec {
 		const char *match;
 		int len;
 		int has_wildcard:1;
+		int to_exclude:1;
 	} *items;
 };
 
diff --git a/tree-walk.c b/tree-walk.c
index 322becc..acbee1b 100644
--- a/tree-walk.c
+++ b/tree-walk.c
@@ -560,6 +560,7 @@ int tree_entry_interesting(const struct name_entry *entry,
 	int i;
 	int pathlen, baselen = base->len - base_offset;
 	int never_interesting = ps->has_wildcard ? 0 : -1;
+	int all_interesting_ok = !ps->recursive || ps->max_depth == -1;
 
 	if (!ps->nr) {
 		if (!ps->recursive || ps->max_depth == -1)
@@ -569,6 +570,19 @@ int tree_entry_interesting(const struct name_entry *entry,
 				      ps->max_depth);
 	}
 
+	/*
+	 * TODO: add prepare_pathspec() to scan for pathspecs
+	 * overlapped with negative ones. The ones that do not overlap
+	 * can still return 2.
+	 */
+	for (i = 0; i < ps->nr; i++) {
+		const struct pathspec_item *item = ps->items+i;
+		if (item->to_exclude) {
+			all_interesting_ok = 0;
+			break;
+		}
+	}
+
 	pathlen = tree_entry_len(entry->path, entry->sha1);
 
 	for (i = ps->nr - 1; i >= 0; i--) {
@@ -578,17 +592,22 @@ int tree_entry_interesting(const struct name_entry *entry,
 		int matchlen = item->len;
 
 		if (baselen >= matchlen) {
+			int ret;
+
 			/* If it doesn't match, move along... */
 			if (!match_dir_prefix(base_str, baselen, match, matchlen))
 				goto match_wildcards;
 
-			if (!ps->recursive || ps->max_depth == -1)
+			if (all_interesting_ok)
 				return 2;
 
-			return !!within_depth(base_str + matchlen + 1,
-					      baselen - matchlen - 1,
-					      !!S_ISDIR(entry->mode),
-					      ps->max_depth);
+			ret = !!within_depth(base_str + matchlen + 1,
+					     baselen - matchlen - 1,
+					     !!S_ISDIR(entry->mode),
+					     ps->max_depth);
+			if (item->to_exclude)
+				ret = !ret;
+			return ret;
 		}
 
 		/* Does the base match? */
@@ -596,18 +615,18 @@ int tree_entry_interesting(const struct name_entry *entry,
 			if (match_entry(entry, pathlen,
 					match + baselen, matchlen - baselen,
 					&never_interesting))
-				return 1;
+				return item->to_exclude ? 0 : 1;
 
 			if (ps->items[i].has_wildcard) {
 				if (!fnmatch(match + baselen, entry->path, 0))
-					return 1;
+					return item->to_exclude ? 0 : 1;
 
 				/*
 				 * Match all directories. We'll try to
 				 * match files later on.
 				 */
 				if (ps->recursive && S_ISDIR(entry->mode))
-					return 1;
+					return item->to_exclude ? 0 : 1;
 			}
 
 			continue;
@@ -626,7 +645,7 @@ match_wildcards:
 
 		if (!fnmatch(match, base->buf + base_offset, 0)) {
 			strbuf_setlen(base, base_offset + baselen);
-			return 1;
+			return item->to_exclude ? 0 : 1;
 		}
 		strbuf_setlen(base, base_offset + baselen);
 
-- 
1.7.3.1.256.g2539c.dirty
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 2 of 20 in “support --exclude for diff/log commands”
  1. 0/5 support --exclude for diff/log commandsNguyễn Thái Ngọc Duy, Mar 10, 2011
  2. 1/5 tree-walk: support negative pathspecNguyễn Thái Ngọc Duy, Mar 10, 2011
  3. 2/5 match_pathspec_depth: support negative pathspecNguyễn Thái Ngọc Duy, Mar 10, 2011
  4. 3/5 revision.c: get rid of struct rev_info.prune_dataNguyễn Thái Ngọc Duy, Mar 10, 2011
  5. Junio C HamanoMar 10, 2011
  6. Nguyen Thai Ngoc DuyMar 10, 2011
  7. 4/5 diff: refactor init/release APINguyễn Thái Ngọc Duy, Mar 10, 2011
  8. 5/5 diff: support --excludeNguyễn Thái Ngọc Duy, Mar 10, 2011
  9. Junio C HamanoMar 10, 2011
  10. Nguyen Thai Ngoc DuyMar 10, 2011
  11. Junio C HamanoMar 10, 2011
  12. Nguyen Thai Ngoc DuyMar 10, 2011
  13. Nguyen Thai Ngoc DuyMar 21, 2011
  14. Michael J GruberMar 21, 2011
  15. Nguyen Thai Ngoc DuyMar 21, 2011
  16. Junio C HamanoMar 22, 2011
  17. Nguyen Thai Ngoc DuyMar 23, 2011
  18. Nguyen Thai Ngoc DuyMar 23, 2011
  19. Michael J GruberMar 23, 2011
  20. Nguyen Thai Ngoc DuyMar 23, 2011

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.