Skip to content

Commit ecfafff

Browse files
committed
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. This new method is not currently used, but will be in an upcoming change. Signed-off-by: Derrick Stolee <[email protected]>
1 parent 0e3b630 commit ecfafff

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

cache-tree.c

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

104+
struct cache_tree *cache_tree_find_path(struct cache_tree *it, const char *path)
105+
{
106+
const char *slash;
107+
int namelen;
108+
struct cache_tree_sub *down;
109+
110+
if (!it)
111+
return NULL;
112+
slash = strchrnul(path, '/');
113+
namelen = slash - path;
114+
it->entry_count = -1;
115+
if (!*slash) {
116+
int pos;
117+
pos = cache_tree_subtree_pos(it, path, namelen);
118+
if (0 <= pos) {
119+
return it->down[pos]->cache_tree;
120+
}
121+
return NULL;
122+
}
123+
down = find_subtree(it, path, namelen, 0);
124+
if (down)
125+
return cache_tree_find_path(down->cache_tree, slash + 1);
126+
return NULL;
127+
}
128+
104129
static int do_invalidate_path(struct cache_tree *it, const char *path)
105130
{
106131
/* 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)