tree_entry_interesting(): support depth limit

This is needed to replace pathspec_matches() in builtin/grep.c.

max_depth == -1 means infinite depth. Depth limit is only effective
when pathspec.recursive == 1. When pathspec.recursive == 0, the
behavior depends on match functions: non-recursive for
tree_entry_interesting() and recursive for match_pathspec{,_depth}

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
diff --git a/cache.h b/cache.h
index a645614..0cf0bac 100644
--- a/cache.h
+++ b/cache.h
@@ -503,6 +503,8 @@
 struct pathspec {
 	const char **raw; /* get_pathspec() result, not freed by free_pathspec() */
 	int nr;
+	int recursive:1;
+	int max_depth;
 	struct pathspec_item {
 		const char *match;
 		int len;
diff --git a/dir.c b/dir.c
index 70d10bc..c3bddb6 100644
--- a/dir.c
+++ b/dir.c
@@ -87,6 +87,21 @@
 	return len;
 }
 
+int within_depth(const char *name, int namelen,
+			int depth, int max_depth)
+{
+	const char *cp = name, *cpe = name + namelen;
+
+	while (cp < cpe) {
+		if (*cp++ != '/')
+			continue;
+		depth++;
+		if (depth > max_depth)
+			return 0;
+	}
+	return 1;
+}
+
 /*
  * Does 'match' match the given name?
  * A match is found if
diff --git a/dir.h b/dir.h
index 72a764e..5fa3fbe 100644
--- a/dir.h
+++ b/dir.h
@@ -65,6 +65,7 @@
 #define MATCHED_FNMATCH 2
 #define MATCHED_EXACTLY 3
 extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen);
+extern int within_depth(const char *name, int namelen, int depth, int max_depth);
 
 extern int fill_directory(struct dir_struct *dir, const char **pathspec);
 extern int read_directory(struct dir_struct *, const char *path, int len, const char **pathspec);
diff --git a/tree-diff.c b/tree-diff.c
index 45a3845..03dc5c8 100644
--- a/tree-diff.c
+++ b/tree-diff.c
@@ -146,6 +146,10 @@
 	int all_t1_interesting = 0;
 	int all_t2_interesting = 0;
 
+	/* Enable recursion indefinitely */
+	opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
+	opt->pathspec.max_depth = -1;
+
 	strbuf_init(&base, PATH_MAX);
 	strbuf_add(&base, base_str, baselen);
 
diff --git a/tree-walk.c b/tree-walk.c
index 83bede9..33feafa 100644
--- a/tree-walk.c
+++ b/tree-walk.c
@@ -1,6 +1,7 @@
 #include "cache.h"
 #include "tree-walk.h"
 #include "unpack-trees.h"
+#include "dir.h"
 #include "tree.h"
 
 static const char *get_mode(const char *str, unsigned int *modep)
@@ -559,8 +560,13 @@
 	int pathlen, baselen = base->len;
 	int never_interesting = -1;
 
-	if (!ps || !ps->nr)
-		return 2;
+	if (!ps->nr) {
+		if (!ps->recursive || ps->max_depth == -1)
+			return 2;
+		return !!within_depth(base->buf, baselen,
+				      !!S_ISDIR(entry->mode),
+				      ps->max_depth);
+	}
 
 	pathlen = tree_entry_len(entry->path, entry->sha1);
 
@@ -573,7 +579,14 @@
 			/* If it doesn't match, move along... */
 			if (!match_dir_prefix(base->buf, baselen, match, matchlen))
 				continue;
-			return 2;
+
+			if (!ps->recursive || ps->max_depth == -1)
+				return 2;
+
+			return !!within_depth(base->buf + matchlen + 1,
+					      baselen - matchlen - 1,
+					      !!S_ISDIR(entry->mode),
+					      ps->max_depth);
 		}
 
 		/* Does the base match? */