summaryrefslogtreecommitdiff
path: root/src/tree-cache.c
diff options
context:
space:
mode:
authorCarlos Martín Nieto <cmn@dwim.me>2014-09-28 07:00:49 +0200
committerCarlos Martín Nieto <cmn@dwim.me>2014-10-10 19:43:42 +0200
commitc2f8b215937ed6f17dce94a4bde26d5c83b42533 (patch)
treea8ec9796153969a8b857430fc1c04061026a731a /src/tree-cache.c
parent46bb00673013a7dae1ee81c484ac05c533ed1c4d (diff)
downloadlibgit2-c2f8b215937ed6f17dce94a4bde26d5c83b42533.tar.gz
index: write out the tree cache extension
Keeping the cache around after read-tree is only one part of the optimisation opportunities. In order to share the cache between program instances, we need to write the TREE extension to the index. Do so, taking the opportunity to rename 'entries' to 'entry_count' to match the name given in the format description. The included test is rather trivial, but works as a sanity check.
Diffstat (limited to 'src/tree-cache.c')
-rw-r--r--src/tree-cache.c60
1 files changed, 56 insertions, 4 deletions
diff --git a/src/tree-cache.c b/src/tree-cache.c
index 1eb046d33..48dc46a40 100644
--- a/src/tree-cache.c
+++ b/src/tree-cache.c
@@ -31,7 +31,7 @@ void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path)
if (tree == NULL)
return;
- tree->entries = -1;
+ tree->entry_count = -1;
while (ptr != NULL) {
end = strchr(ptr, '/');
@@ -43,7 +43,7 @@ void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path)
if (tree == NULL) /* We don't have that tree */
return;
- tree->entries = -1;
+ tree->entry_count = -1;
ptr = end + 1;
}
}
@@ -93,7 +93,7 @@ static int read_tree_internal(git_tree_cache **out,
if (git__strtol32(&count, buffer, &buffer, 10) < 0)
goto corrupted;
- tree->entries = count;
+ tree->entry_count = count;
if (*buffer != ' ' || ++buffer >= buffer_end)
goto corrupted;
@@ -108,7 +108,7 @@ static int read_tree_internal(git_tree_cache **out,
goto corrupted;
/* The SHA1 is only there if it's not invalidated */
- if (tree->entries >= 0) {
+ if (tree->entry_count >= 0) {
/* 160-bit SHA-1 for this tree and it's children */
if (buffer + GIT_OID_RAWSZ > buffer_end)
goto corrupted;
@@ -244,3 +244,55 @@ int git_tree_cache_new(git_tree_cache **out, const char *name, git_pool *pool)
*out = tree;
return 0;
}
+
+/**
+ * Recursively recalculate the total entry count, which we need to
+ * write out to the index
+ */
+static void recount_entries(git_tree_cache *tree)
+{
+ size_t i;
+ ssize_t entry_count;
+ git_tree_cache *child;
+
+ for (i = 0; i < tree->children_count; i++)
+ recount_entries(tree->children[i]);
+
+ if (tree->entry_count == -1)
+ return;
+
+ entry_count = 0;
+ for (i = 0; i < tree->children_count; i++) {
+ child = tree->children[i];
+
+ if (child->entry_count == -1)
+ continue;
+
+ entry_count += tree->children[i]->children_count;
+ }
+
+ tree->entry_count = entry_count;
+}
+
+static void write_tree(git_buf *out, git_tree_cache *tree)
+{
+ size_t i;
+
+ git_buf_printf(out, "%s%c%zd %"PRIuZ"\n", tree->name, 0, tree->entry_count, tree->children_count);
+
+ if (tree->entry_count == -1)
+ return;
+
+ git_buf_put(out, (const char *) &tree->oid, GIT_OID_RAWSZ);
+
+ for (i = 0; i < tree->children_count; i++)
+ write_tree(out, tree->children[i]);
+}
+
+int git_tree_cache_write(git_buf *out, git_tree_cache *tree)
+{
+ recount_entries(tree);
+ write_tree(out, tree);
+
+ return git_buf_oom(out) ? -1 : 0;
+}