Add git_tree_cache_invalidate_path Whenever a file is updated in the index, each tree leading towards it needs to be invalidated. Provide the supporting function. 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
diff --git a/src/tree-cache.c b/src/tree-cache.c
index 2e6e5f1..026ede0 100644
--- a/src/tree-cache.c
+++ b/src/tree-cache.c
@@ -7,10 +7,56 @@
#include "tree-cache.h"
+static git_tree_cache *find_child(git_tree_cache *tree, const char *path)
+{
+ size_t i, dirlen;
+ const char *end;
+
+ end = strchr(path, '/');
+ if (end == NULL) {
+ end = strrchr(path, '\0');
+ }
+
+ dirlen = end - path;
+
+ for (i = 0; i < tree->children_count; ++i) {
+ const char *childname = tree->children[i]->name;
+
+ if (strlen(childname) == dirlen && !memcmp(path, childname, dirlen))
+ return tree->children[i];
+ }
+
+ return NULL;
+}
+
+void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path)
+{
+ const char *ptr = path, *end;
+
+ if (tree == NULL)
+ return;
+
+ tree->entries = -1;
+
+ while (ptr != NULL) {
+ end = strchr(ptr, '/');
+
+ if (end == NULL) /* End of path */
+ break;
+
+ tree = find_child(tree, ptr);
+ if (tree == NULL) /* We don't have that tree */
+ return;
+
+ tree->entries = -1;
+ ptr = end + 1;
+ }
+}
+
static int read_tree_internal(git_tree_cache **out,
const char **buffer_in, const char *buffer_end, git_tree_cache *parent)
{
- git_tree_cache *tree;
+ git_tree_cache *tree = NULL;
const char *name_start, *buffer;
int count;
int error = GIT_SUCCESS;
diff --git a/src/tree-cache.h b/src/tree-cache.h
index 1a5d406..450b038 100644
--- a/src/tree-cache.h
+++ b/src/tree-cache.h
@@ -24,6 +24,7 @@ struct git_tree_cache {
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);
void git_tree_cache_free(git_tree_cache *tree);
#endif