tree: Use an internal append functiont to add new entries
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
diff --git a/src/tree.c b/src/tree.c
index 6ad7139..227da3c 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -222,6 +222,28 @@ static unsigned int find_next_dir(const char *dirname, git_index *index, unsigne
return i;
}
+static int append_entry(git_treebuilder *bld, const char *filename, const git_oid *id, unsigned int attributes)
+{
+ git_tree_entry *entry;
+
+ if ((entry = git__malloc(sizeof(git_tree_entry))) == NULL)
+ return GIT_ENOMEM;
+
+ memset(entry, 0x0, sizeof(git_tree_entry));
+ entry->filename = git__strdup(filename);
+ entry->filename_len = strlen(entry->filename);
+
+ bld->entry_count++;
+
+ git_oid_cpy(&entry->oid, id);
+ entry->attr = attributes;
+
+ if (git_vector_insert(&bld->entries, entry) < 0)
+ return GIT_ENOMEM;
+
+ return GIT_SUCCESS;
+}
+
static int write_tree(git_oid *oid, git_index *index, const char *dirname, unsigned int start)
{
git_treebuilder *bld = NULL;
@@ -299,14 +321,14 @@ static int write_tree(git_oid *oid, git_index *index, const char *dirname, unsig
} else {
last_comp = subdir;
}
- error = git_treebuilder_insert(NULL, bld, last_comp, &sub_oid, S_IFDIR);
+ error = append_entry(bld, last_comp, &sub_oid, S_IFDIR);
free(subdir);
if (error < GIT_SUCCESS) {
error = git__rethrow(error, "Failed to insert dir");
goto cleanup;
}
} else {
- error = git_treebuilder_insert(NULL, bld, filename, &entry->oid, entry->mode);
+ error = append_entry(bld, filename, &entry->oid, entry->mode);
if (error < GIT_SUCCESS) {
error = git__rethrow(error, "Failed to insert file");
}
@@ -368,29 +390,13 @@ int git_treebuilder_create(git_treebuilder **builder_p, const git_tree *source)
}
if (source != NULL) {
- bld->entry_count = source_entries;
for (i = 0; i < source->entries.length; ++i) {
git_tree_entry *entry_src = source->entries.contents[i];
- git_tree_entry *entry = git__calloc(1, sizeof(git_tree_entry));
- if (entry == NULL) {
+ if (append_entry(bld, entry_src->filename, &entry_src->oid, entry_src->attr) < 0) {
git_treebuilder_free(bld);
return GIT_ENOMEM;
}
-
- entry->filename = git__strdup(entry_src->filename);
-
- if (entry->filename == NULL) {
- free(entry);
- git_treebuilder_free(bld);
- return GIT_ENOMEM;
- }
-
- entry->filename_len = entry_src->filename_len;
- git_oid_cpy(&entry->oid, &entry_src->oid);
- entry->attr = entry_src->attr;
-
- git_vector_insert(&bld->entries, entry);
}
}