summaryrefslogtreecommitdiff
path: root/src/tree-cache.c
diff options
context:
space:
mode:
authorCarlos Martín Nieto <cmn@dwim.me>2014-10-22 21:09:31 +0200
committerCarlos Martín Nieto <cmn@dwim.me>2014-10-22 21:25:08 +0200
commitbb0757d56c7a2f0d337f1e22f9796f4090866480 (patch)
tree193a59f31bf3c257bc6af4ba4a756adbce4a98b6 /src/tree-cache.c
parent12e1803188407e6ee18b9bd22186dd5d02c8ba78 (diff)
downloadlibgit2-bb0757d56c7a2f0d337f1e22f9796f4090866480.tar.gz
tree-cache: correct the entry_count calculation
The entry_count field is the amount of index entries covered by a particular cache entry, that is how many files are there (recursively) under a particular directory. The current code that attemps to do this is severely defincient and is trying to count the amount of children, which always comes up to zero. We don't even need to recount, since we have the information during the cache creation. We can take that number and keep it, as we only ever invalidate or replace.
Diffstat (limited to 'src/tree-cache.c')
-rw-r--r--src/tree-cache.c35
1 files changed, 4 insertions, 31 deletions
diff --git a/src/tree-cache.c b/src/tree-cache.c
index aaf8a132c..b37be0f0d 100644
--- a/src/tree-cache.c
+++ b/src/tree-cache.c
@@ -191,8 +191,10 @@ static int read_tree_recursive(git_tree_cache *cache, const git_tree *tree, git_
git_tree *subtree;
entry = git_tree_entry_byindex(tree, i);
- if (git_tree_entry_filemode(entry) != GIT_FILEMODE_TREE)
+ if (git_tree_entry_filemode(entry) != GIT_FILEMODE_TREE) {
+ cache->entry_count++;
continue;
+ }
if ((error = git_tree_cache_new(&cache->children[j], git_tree_entry_name(entry), pool)) < 0)
return error;
@@ -202,6 +204,7 @@ static int read_tree_recursive(git_tree_cache *cache, const git_tree *tree, git_
error = read_tree_recursive(cache->children[j], subtree, pool);
git_tree_free(subtree);
+ cache->entry_count += cache->children[j]->entry_count;
j++;
if (error < 0)
@@ -245,35 +248,6 @@ int git_tree_cache_new(git_tree_cache **out, const char *name, git_pool *pool)
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;
@@ -289,7 +263,6 @@ static void write_tree(git_buf *out, git_tree_cache *tree)
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;