]> git.proxmox.com Git - libgit2.git/commitdiff
tree-cache: extract the allocation
authorCarlos Martín Nieto <cmn@dwim.me>
Thu, 10 Jul 2014 10:21:28 +0000 (12:21 +0200)
committerCarlos Martín Nieto <cmn@dwim.me>
Fri, 10 Oct 2014 17:35:18 +0000 (19:35 +0200)
src/tree-cache.c
src/tree-cache.h

index 49afd6e49182544c9d427a2a818b9653d7e5bdd2..aec94938d215fb61a0e4a1c856869529e9d5dc0e 100644 (file)
@@ -74,7 +74,6 @@ static int read_tree_internal(git_tree_cache **out,
        git_tree_cache *tree = NULL;
        const char *name_start, *buffer;
        int count;
-       size_t name_len;
 
        buffer = name_start = *buffer_in;
 
@@ -84,17 +83,8 @@ static int read_tree_internal(git_tree_cache **out,
        if (++buffer >= buffer_end)
                goto corrupted;
 
-       name_len = strlen(name_start);
-       tree = git__malloc(sizeof(git_tree_cache) + name_len + 1);
-       GITERR_CHECK_ALLOC(tree);
-
-       memset(tree, 0x0, sizeof(git_tree_cache));
-       tree->parent = parent;
-
-       /* NUL-terminated tree name */
-       tree->namelen = name_len;
-       memcpy(tree->name, name_start, name_len);
-       tree->name[name_len] = '\0';
+       if (git_tree_cache_new(&tree, name_start, parent) < 0)
+               return -1;
 
        /* Blank-terminated ASCII decimal number of entries in this tree */
        if (git__strtol32(&count, buffer, &buffer, 10) < 0)
@@ -164,6 +154,26 @@ int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer
        return 0;
 }
 
+int git_tree_cache_new(git_tree_cache **out, const char *name, git_tree_cache *parent)
+{
+       size_t name_len;
+       git_tree_cache *tree;
+
+       name_len = strlen(name);
+       tree = git__malloc(sizeof(git_tree_cache) + name_len + 1);
+       GITERR_CHECK_ALLOC(tree);
+
+       memset(tree, 0x0, sizeof(git_tree_cache));
+       tree->parent = parent;
+       /* NUL-terminated tree name */
+       tree->namelen = name_len;
+       memcpy(tree->name, name, name_len);
+       tree->name[name_len] = '\0';
+
+       *out = tree;
+       return 0;
+}
+
 void git_tree_cache_free(git_tree_cache *tree)
 {
        unsigned int i;
index 78017127c9d407abe9972f1fdf80825d1ec1d26e..592a352dbf6eb671e6fe570d2cf9f46eab61d39c 100644 (file)
@@ -25,6 +25,7 @@ typedef struct {
 int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size);
 void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path);
 const git_tree_cache *git_tree_cache_get(const git_tree_cache *tree, const char *path);
+int git_tree_cache_new(git_tree_cache **out, const char *name, git_tree_cache *parent);
 void git_tree_cache_free(git_tree_cache *tree);
 
 #endif