Subject: [PATCH 7/8] setup_tree_pathspec(): interpret '^' as negative pathspec

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Thu, 9 Sep 2010 01:50:22 +1000

This patch does preparation work for tree exclusion in
tree_entry_interesting(). '^' has similar meaning to '!' in
gitexcludes. '!' is not used because bash does not like arguments with
a leading '!'.

Eventually, "git diff -- foo ^foo/bar" should show differences in foo,
except foo/bar. If "git diff -- ^foo" is given, then it implies
everything except foo, which could surprise users that
"bar" in "git diff -- bar ^foo" has no effect at all.

NOTE: pathspec in diff machinery is also used by ce_path_match() and
read_index_preload(), which currently do not understand '^' at all.

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 tree-walk.c |   28 ++++++++++++++++++++++++++++
 tree-walk.h |    3 +++
 2 files changed, 31 insertions(+), 0 deletions(-)

diff --git a/tree-walk.c b/tree-walk.c
index e7041d7..a2f4a00 100644
--- a/tree-walk.c
+++ b/tree-walk.c
@@ -479,7 +479,35 @@ int setup_tree_pathspec(const char **paths, struct tree_pathspec_list *ps)
 	for (i=0; i < ps->nr; i++) {
 		struct tree_pathspec *exc = ps->info+i;
 		exc->path = ps->paths[i];
+		if (*exc->path == '^') {
+			exc->path++;
+			exc->to_exclude = 1;
+		}
 		exc->pathlen = strlen(exc->path);
+		if (exc->to_exclude) {
+			int j, found = 0;
+
+			for (j = i-1; j >= 0; j--) {
+				int len = strlen(ps->info[j].path);
+				if (len < exc->pathlen &&
+				    !strncmp(ps->info[j].path, exc->path, len) &&
+				    exc->path[len] == '/') {
+					ps->info[j].has_sub_pathspec = 1;
+					found = 1;
+				}
+			}
+
+			/*
+			 * If a negative pathspec has nothing to
+			 * negate from, include everything so it can
+			 * negate from that.  This way is not
+			 * perfect. You may be surprised to find out
+			 * "^Documentation t" does not take "t" into
+			 * account at all
+			 */
+			if (!found)
+				ps->include_by_default = 1;
+		}
 	}
 	return 0;
 }
diff --git a/tree-walk.h b/tree-walk.h
index bb55656..6be1e6c 100644
--- a/tree-walk.h
+++ b/tree-walk.h
@@ -16,9 +16,12 @@ struct tree_desc {
 struct tree_pathspec_list {
 	const char **paths;
 	int nr;
+	int include_by_default:1;
 	struct tree_pathspec {
 		const char *path;
 		int pathlen;
+		int to_exclude:1;
+		int has_sub_pathspec:1;
 	} *info;
 };
 
-- 
1.7.1.rc1.70.g13aff