Subject: [PATCH 09/17] cache-tree: only cache tree within narrow area

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Sun, 5 Sep 2010 16:47:36 +1000

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 cache-tree.c |   41 +++++++++++++++++++++++++++++++++++++----
 1 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/cache-tree.c b/cache-tree.c
index c60cf91..6aaf265 100644
--- a/cache-tree.c
+++ b/cache-tree.c
@@ -602,7 +602,8 @@ int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
 	return 0;
 }
 
-static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
+static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree,
+				 char *base, int baselen)
 {
 	struct tree_desc desc;
 	struct name_entry entry;
@@ -611,17 +612,48 @@ static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
 	hashcpy(it->sha1, tree->object.sha1);
 	init_tree_desc(&desc, tree->buffer, tree->size);
 	cnt = 0;
+	if (base && baselen)
+		base[baselen++] = '/';
 	while (tree_entry(&desc, &entry)) {
 		if (!S_ISDIR(entry.mode))
 			cnt++;
 		else {
 			struct cache_tree_sub *sub;
-			struct tree *subtree = lookup_tree(entry.sha1);
+			struct tree *subtree;
+			int found = 2; /* all interesting */
+			const char **p = get_narrow_prefix();
+			int len = 0;
+
+			if (p) {
+				found = 0; /* not interesting */
+				len = strlen(entry.path);
+				if (base)
+					memcpy(base+baselen, entry.path, len+1);
+				while (*p) {
+					if (!prefixcmp(entry.path, *p)) {
+						found = 2;
+						break;
+					}
+					if (!prefixcmp(*p, entry.path)) {
+						found = 1;
+						break;
+					}
+					p++;
+				}
+				if (!found) {
+					cnt++;
+					continue;
+				}
+			}
+
+			subtree = lookup_tree(entry.sha1);
 			if (!subtree->object.parsed)
 				parse_tree(subtree);
 			sub = cache_tree_sub(it, entry.path);
 			sub->cache_tree = cache_tree();
-			prime_cache_tree_rec(sub->cache_tree, subtree);
+			prime_cache_tree_rec(sub->cache_tree, subtree,
+					     found == 1 ? base : NULL,
+					     baselen+len);
 			cnt += sub->cache_tree->entry_count;
 		}
 	}
@@ -630,9 +662,10 @@ static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
 
 void prime_cache_tree(struct cache_tree **it, struct tree *tree)
 {
+	char path[PATH_MAX];
 	cache_tree_free(it);
 	*it = cache_tree();
-	prime_cache_tree_rec(*it, tree);
+	prime_cache_tree_rec(*it, tree, get_narrow_prefix() ? path : NULL, 0);
 }
 
 /*
-- 
1.7.1.rc1.69.g24c2f7