treentry: no need for manual size book-keeping We can simply ask the hasmap.
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
diff --git a/src/tree.c b/src/tree.c
index e7357dd..e0e2dbe 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -367,7 +367,8 @@ size_t git_tree_entrycount(const git_tree *tree)
unsigned int git_treebuilder_entrycount(git_treebuilder *bld)
{
assert(bld);
- return (unsigned int)bld->entrycount;
+
+ return git_strmap_num_entries(bld->map);
}
static int tree_error(const char *str, const char *path)
@@ -469,7 +470,6 @@ static int append_entry(
return -1;
}
- bld->entrycount++;
return 0;
}
@@ -680,7 +680,6 @@ int git_treebuilder_insert(
}
}
- bld->entrycount++;
git_oid_cpy(&entry->oid, id);
entry->attr = filemode;
@@ -719,14 +718,13 @@ int git_treebuilder_remove(git_treebuilder *bld, const char *filename)
git_strmap_delete(bld->map, filename);
git_tree_entry_free(entry);
- bld->entrycount--;
return 0;
}
int git_treebuilder_write(git_oid *oid, git_repository *repo, git_treebuilder *bld)
{
int error = 0;
- size_t i;
+ size_t i, entrycount;
git_buf tree = GIT_BUF_INIT;
git_odb *odb;
git_tree_entry *entry;
@@ -734,7 +732,8 @@ int git_treebuilder_write(git_oid *oid, git_repository *repo, git_treebuilder *b
assert(bld);
- if (git_vector_init(&entries, bld->entrycount, entry_sort_cmp) < 0)
+ entrycount = git_strmap_num_entries(bld->map);
+ if (git_vector_init(&entries, entrycount, entry_sort_cmp) < 0)
return -1;
git_strmap_foreach_value(bld->map, entry, {
@@ -745,7 +744,7 @@ int git_treebuilder_write(git_oid *oid, git_repository *repo, git_treebuilder *b
git_vector_sort(&entries);
/* Grow the buffer beforehand to an estimated size */
- error = git_buf_grow(&tree, bld->entrycount * 72);
+ error = git_buf_grow(&tree, entrycount * 72);
for (i = 0; i < entries.length && !error; ++i) {
git_tree_entry *entry = git_vector_get(&entries, i);
@@ -781,7 +780,6 @@ void git_treebuilder_filter(
git_strmap_foreach(bld->map, filename, entry, {
if (filter(entry, payload)) {
git_strmap_delete(bld->map, filename);
- bld->entrycount--;
git_tree_entry_free(entry);
}
});
@@ -795,7 +793,6 @@ void git_treebuilder_clear(git_treebuilder *bld)
git_strmap_foreach_value(bld->map, e, git_tree_entry_free(e));
git_strmap_clear(bld->map);
- bld->entrycount = 0;
}
void git_treebuilder_free(git_treebuilder *bld)
diff --git a/src/tree.h b/src/tree.h
index 81508a6..5d27eb7 100644
--- a/src/tree.h
+++ b/src/tree.h
@@ -26,7 +26,6 @@ struct git_tree {
};
struct git_treebuilder {
- size_t entrycount; /* vector may contain "removed" entries */
git_strmap *map;
};