Skip to content

Commit bc96cc8

Browse files
pcloudsgitster
authored andcommitted
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>
1 parent 58c4d66 commit bc96cc8

File tree

5 files changed

+38
-3
lines changed

5 files changed

+38
-3
lines changed

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,8 @@ extern int ie_modified(const struct index_state *, struct cache_entry *, struct
503503
struct pathspec {
504504
const char **raw; /* get_pathspec() result, not freed by free_pathspec() */
505505
int nr;
506+
int recursive:1;
507+
int max_depth;
506508
struct pathspec_item {
507509
const char *match;
508510
int len;

dir.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,21 @@ int fill_directory(struct dir_struct *dir, const char **pathspec)
8787
return len;
8888
}
8989

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+
90105
/*
91106
* Does 'match' match the given name?
92107
* A match is found if

dir.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ struct dir_struct {
6565
#define MATCHED_FNMATCH 2
6666
#define MATCHED_EXACTLY 3
6767
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);
6869

6970
extern int fill_directory(struct dir_struct *dir, const char **pathspec);
7071
extern int read_directory(struct dir_struct *, const char *path, int len, const char **pathspec);

tree-diff.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
146146
int all_t1_interesting = 0;
147147
int all_t2_interesting = 0;
148148

149+
/* Enable recursion indefinitely */
150+
opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
151+
opt->pathspec.max_depth = -1;
152+
149153
strbuf_init(&base, PATH_MAX);
150154
strbuf_add(&base, base_str, baselen);
151155

tree-walk.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "cache.h"
22
#include "tree-walk.h"
33
#include "unpack-trees.h"
4+
#include "dir.h"
45
#include "tree.h"
56

67
static const char *get_mode(const char *str, unsigned int *modep)
@@ -559,8 +560,13 @@ int tree_entry_interesting(const struct name_entry *entry,
559560
int pathlen, baselen = base->len;
560561
int never_interesting = -1;
561562

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+
}
564570

565571
pathlen = tree_entry_len(entry->path, entry->sha1);
566572

@@ -573,7 +579,14 @@ int tree_entry_interesting(const struct name_entry *entry,
573579
/* If it doesn't match, move along... */
574580
if (!match_dir_prefix(base->buf, baselen, match, matchlen))
575581
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);
577590
}
578591

579592
/* Does the base match? */

0 commit comments

Comments
 (0)