@matheusbsbs / matheus-bagliano / commits / bc96cc87db
Fork 0

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>

Nguyễn Thái Ngọc Duy committed Dec 15, 2010 at 22:02 UTC bc96cc87dbb229cbdabfd93391e24ef168713a74
5 files changed +38 -3
cache.h
+2
@@ -503,6 +503,8 @@ extern int ie_modified(const struct index_state *, struct cache_entry *, struct
503 struct pathspec {
504 const char **raw; /* get_pathspec() result, not freed by free_pathspec() */
505 int nr;
506 + int recursive:1;
507 + int max_depth;
508 struct pathspec_item {
509 const char *match;
510 int len;
dir.c
+15
@@ -87,6 +87,21 @@ int fill_directory(struct dir_struct *dir, const char **pathspec)
87 return len;
88 }
89
90 +int within_depth(const char *name, int namelen,
91 + int depth, int max_depth)
92 +{
93 + const char *cp = name, *cpe = name + namelen;
94 +
95 + while (cp < cpe) {
96 + if (*cp++ != '/')
97 + continue;
98 + depth++;
99 + if (depth > max_depth)
100 + return 0;
101 + }
102 + return 1;
103 +}
104 +
105 /*
106 * Does 'match' match the given name?
107 * A match is found if
dir.h
+1
@@ -65,6 +65,7 @@ struct dir_struct {
65 #define MATCHED_FNMATCH 2
66 #define MATCHED_EXACTLY 3
67 extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen);
68 +extern int within_depth(const char *name, int namelen, int depth, int max_depth);
69
70 extern int fill_directory(struct dir_struct *dir, const char **pathspec);
71 extern int read_directory(struct dir_struct *, const char *path, int len, const char **pathspec);
tree-diff.c
+4
@@ -146,6 +146,10 @@ int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
146 int all_t1_interesting = 0;
147 int all_t2_interesting = 0;
148
149 + /* Enable recursion indefinitely */
150 + opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
151 + opt->pathspec.max_depth = -1;
152 +
153 strbuf_init(&base, PATH_MAX);
154 strbuf_add(&base, base_str, baselen);
155
tree-walk.c
+16 -3
@@ -1,6 +1,7 @@
1 #include "cache.h"
2 #include "tree-walk.h"
3 #include "unpack-trees.h"
4 +#include "dir.h"
5 #include "tree.h"
6
7 static const char *get_mode(const char *str, unsigned int *modep)
@@ -559,8 +560,13 @@ int tree_entry_interesting(const struct name_entry *entry,
560 int pathlen, baselen = base->len;
561 int never_interesting = -1;
562
562 - if (!ps || !ps->nr)
563 - return 2;
563 + if (!ps->nr) {
564 + if (!ps->recursive || ps->max_depth == -1)
565 + return 2;
566 + return !!within_depth(base->buf, baselen,
567 + !!S_ISDIR(entry->mode),
568 + ps->max_depth);
569 + }
570
571 pathlen = tree_entry_len(entry->path, entry->sha1);
572
@@ -573,7 +579,14 @@ int tree_entry_interesting(const struct name_entry *entry,
579 /* If it doesn't match, move along... */
580 if (!match_dir_prefix(base->buf, baselen, match, matchlen))
581 continue;
576 - return 2;
582 +
583 + if (!ps->recursive || ps->max_depth == -1)
584 + return 2;
585 +
586 + return !!within_depth(base->buf + matchlen + 1,
587 + baselen - matchlen - 1,
588 + !!S_ISDIR(entry->mode),
589 + ps->max_depth);
590 }
591
592 /* Does the base match? */