Add git_tree_cache_get Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
diff --git a/src/tree-cache.c b/src/tree-cache.c
index 026ede0..5a32575 100644
--- a/src/tree-cache.c
+++ b/src/tree-cache.c
@@ -7,7 +7,7 @@
#include "tree-cache.h"
-static git_tree_cache *find_child(git_tree_cache *tree, const char *path)
+static git_tree_cache *find_child(const git_tree_cache *tree, const char *path)
{
size_t i, dirlen;
const char *end;
@@ -53,6 +53,29 @@ void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path)
}
}
+const git_tree_cache *git_tree_cache_get(const git_tree_cache *tree, const char *path)
+{
+ const char *ptr = path, *end;
+
+ if (tree == NULL) {
+ return NULL;
+ }
+
+ while (1) {
+ end = strchr(ptr, '/');
+
+ tree = find_child(tree, ptr);
+ if (tree == NULL) { /* Can't find it */
+ return NULL;
+ }
+
+ if (end == NULL || end + 1 == '\0')
+ return tree;
+
+ ptr = end + 1;
+ }
+}
+
static int read_tree_internal(git_tree_cache **out,
const char **buffer_in, const char *buffer_end, git_tree_cache *parent)
{
diff --git a/src/tree-cache.h b/src/tree-cache.h
index 450b038..0d93291 100644
--- a/src/tree-cache.h
+++ b/src/tree-cache.h
@@ -25,6 +25,7 @@ typedef struct git_tree_cache git_tree_cache;
int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size);
void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path);
+const git_tree_cache *git_tree_cache_get(const git_tree_cache *tree, const char *path);
void git_tree_cache_free(git_tree_cache *tree);
#endif