cache-tree: extract subtree_pos()

This method will be helpful to use outside of cache-tree.c in a later feature. The implementation is subtle due to subtree_name_cmp() sorting by length and then lexicographically. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Jan 23, 2021 at 19:58 UTC c80dd3967f28527dab49c8e9525524c7f33baa22
2 files changed +5 -3
cache-tree.c
+3 -3
@@ -45,7 +45,7 @@ static int subtree_name_cmp(const char *one, int onelen,
45 return memcmp(one, two, onelen);
46 }
47
48 -static int subtree_pos(struct cache_tree *it, const char *path, int pathlen)
48 +int cache_tree_subtree_pos(struct cache_tree *it, const char *path, int pathlen)
49 {
50 struct cache_tree_sub **down = it->down;
51 int lo, hi;
@@ -72,7 +72,7 @@ static struct cache_tree_sub *find_subtree(struct cache_tree *it,
72 int create)
73 {
74 struct cache_tree_sub *down;
75 - int pos = subtree_pos(it, path, pathlen);
75 + int pos = cache_tree_subtree_pos(it, path, pathlen);
76 if (0 <= pos)
77 return it->down[pos];
78 if (!create)
@@ -123,7 +123,7 @@ static int do_invalidate_path(struct cache_tree *it, const char *path)
123 it->entry_count = -1;
124 if (!*slash) {
125 int pos;
126 - pos = subtree_pos(it, path, namelen);
126 + pos = cache_tree_subtree_pos(it, path, namelen);
127 if (0 <= pos) {
128 cache_tree_free(&it->down[pos]->cache_tree);
129 free(it->down[pos]);
cache-tree.h
+2
@@ -27,6 +27,8 @@ void cache_tree_free(struct cache_tree **);
27 void cache_tree_invalidate_path(struct index_state *, const char *);
28 struct cache_tree_sub *cache_tree_sub(struct cache_tree *, const char *);
29
30 +int cache_tree_subtree_pos(struct cache_tree *it, const char *path, int pathlen);
31 +
32 void cache_tree_write(struct strbuf *, struct cache_tree *root);
33 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size);
34