Skip to content

Commit 080ab56

Browse files
derrickstoleegitster
authored andcommitted
cache-tree: implement cache_tree_find_path()
Given a 'struct cache_tree', it may be beneficial to navigate directly to a node within that corresponds to a given path name. Create cache_tree_find_path() for this function. It returns NULL when no such path exists. The implementation is adapted from do_invalidate_path() which does a similar search but also modifies the nodes it finds along the way. The method could be implemented simply using tail-recursion, but this while loop does the same thing. This new method is not currently used, but will be in an upcoming change. Helped-by: Junio C Hamano <[email protected]> Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9fadb37 commit 080ab56

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

cache-tree.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,33 @@ struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
100100
return find_subtree(it, path, pathlen, 1);
101101
}
102102

103+
struct cache_tree *cache_tree_find_path(struct cache_tree *it, const char *path)
104+
{
105+
const char *slash;
106+
int namelen;
107+
struct cache_tree_sub it_sub = {
108+
.cache_tree = it,
109+
};
110+
struct cache_tree_sub *down = &it_sub;
111+
112+
while (down) {
113+
slash = strchrnul(path, '/');
114+
namelen = slash - path;
115+
down->cache_tree->entry_count = -1;
116+
if (!*slash) {
117+
int pos;
118+
pos = cache_tree_subtree_pos(down->cache_tree, path, namelen);
119+
if (0 <= pos)
120+
return down->cache_tree->down[pos]->cache_tree;
121+
return NULL;
122+
}
123+
down = find_subtree(it, path, namelen, 0);
124+
path = slash + 1;
125+
}
126+
127+
return NULL;
128+
}
129+
103130
static int do_invalidate_path(struct cache_tree *it, const char *path)
104131
{
105132
/* a/b/c

cache-tree.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ struct cache_tree_sub *cache_tree_sub(struct cache_tree *, const char *);
2929

3030
int cache_tree_subtree_pos(struct cache_tree *it, const char *path, int pathlen);
3131

32+
struct cache_tree *cache_tree_find_path(struct cache_tree *it, const char *path);
33+
3234
void cache_tree_write(struct strbuf *, struct cache_tree *root);
3335
struct cache_tree *cache_tree_read(const char *buffer, unsigned long size);
3436

0 commit comments

Comments
 (0)