summaryrefslogtreecommitdiff
path: root/src/tree.c
diff options
context:
space:
mode:
authorVicent Marti <tanoku@gmail.com>2011-01-20 14:43:27 -0800
committerVicent Marti <tanoku@gmail.com>2011-01-20 14:43:27 -0800
commitc8f5ff8f65617e6e097dd779492bd7af47232d4f (patch)
treed7ec9ee0fa5c4d9b1dcda5320c7d40795e9616c0 /src/tree.c
parente16c2f6a4cfff50907699d73daad2c10565ec686 (diff)
downloadlibgit2-c8f5ff8f65617e6e097dd779492bd7af47232d4f.tar.gz
Fix initialization of in-memory trees
In-memory tree objects were not being properly initialized, because the internal entries vector was created on the 'parse' method. Signed-off-by: Vicent Marti <tanoku@gmail.com>
Diffstat (limited to 'src/tree.c')
-rw-r--r--src/tree.c33
1 files changed, 27 insertions, 6 deletions
diff --git a/src/tree.c b/src/tree.c
index 4655dc760..d160a79a4 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -30,6 +30,8 @@
#include "git2/repository.h"
#include "git2/object.h"
+#define DEFAULT_TREE_SIZE 16
+
int entry_search_cmp(const void *key, const void *array_member)
{
const char *filename = (const char *)key;
@@ -46,7 +48,7 @@ int entry_sort_cmp(const void *a, const void *b)
return strcmp(entry_a->filename, entry_b->filename);
}
-static void free_tree_entries(git_tree *tree)
+static void clear_entries(git_tree *tree)
{
unsigned int i;
@@ -61,14 +63,35 @@ static void free_tree_entries(git_tree *tree)
free(e);
}
- git_vector_free(&tree->entries);
+ git_vector_clear(&tree->entries);
}
+git_tree *git_tree__new(void)
+{
+ git_tree *tree;
+
+ tree = git__malloc(sizeof(struct git_tree));
+ if (tree == NULL)
+ return NULL;
+
+ memset(tree, 0x0, sizeof(struct git_tree));
+
+ if (git_vector_init(&tree->entries,
+ DEFAULT_TREE_SIZE,
+ entry_sort_cmp,
+ entry_search_cmp) < GIT_SUCCESS) {
+ free(tree);
+ return NULL;
+ }
+
+ return tree;
+}
void git_tree__free(git_tree *tree)
{
- free_tree_entries(tree);
+ clear_entries(tree);
+ git_vector_free(&tree->entries);
free(tree);
}
@@ -243,9 +266,7 @@ static int tree_parse_buffer(git_tree *tree, char *buffer, char *buffer_end)
expected_size = (tree->object.source.raw.len / avg_entry_size) + 1;
- free_tree_entries(tree);
- if (git_vector_init(&tree->entries, expected_size, entry_sort_cmp, entry_search_cmp) < GIT_SUCCESS)
- return GIT_ENOMEM;
+ clear_entries(tree);
while (buffer < buffer_end) {
git_tree_entry *entry;