Make "struct tree" contain the pointer to the tree buffer
[gitweb.git] / tree.c
diff --git a/tree.c b/tree.c
index 87e0d743f85605b8a77eda57f28ad1b25489905a..1e76d9cc11dcbda655e40f19fbd82d34212383b2 100644 (file)
--- a/tree.c
+++ b/tree.c
@@ -3,6 +3,7 @@
 #include "blob.h"
 #include "commit.h"
 #include "tag.h"
+#include "tree-walk.h"
 #include <stdlib.h>
 
 const char *tree_type = "tree";
@@ -18,9 +19,7 @@ static int read_one_entry(unsigned char *sha1, const char *base, int baselen, co
 
        len = strlen(pathname);
        size = cache_entry_size(baselen + len);
-       ce = xmalloc(size);
-
-       memset(ce, 0, size);
+       ce = xcalloc(1, size);
 
        ce->ce_mode = create_ce_mode(mode);
        ce->ce_flags = create_ce_flags(baselen + len, stage);
@@ -130,8 +129,7 @@ struct tree *lookup_tree(const unsigned char *sha1)
 {
        struct object *obj = lookup_object(sha1);
        if (!obj) {
-               struct tree *ret = xmalloc(sizeof(struct tree));
-               memset(ret, 0, sizeof(struct tree));
+               struct tree *ret = xcalloc(1, sizeof(struct tree));
                created_object(sha1, &ret->object);
                ret->object.type = tree_type;
                return ret;
@@ -148,46 +146,45 @@ struct tree *lookup_tree(const unsigned char *sha1)
 
 int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
 {
-       void *bufptr = buffer;
+       struct tree_desc desc;
        struct tree_entry_list **list_p;
        int n_refs = 0;
 
        if (item->object.parsed)
                return 0;
        item->object.parsed = 1;
+       item->buffer = buffer;
+       item->size = size;
+
+       desc.buf = buffer;
+       desc.size = size;
+
        list_p = &item->entries;
-       while (size) {
-               struct object *obj;
+       while (desc.size) {
+               unsigned mode;
+               const char *path;
+               const unsigned char *sha1;
                struct tree_entry_list *entry;
-               int len = 1+strlen(bufptr);
-               unsigned char *file_sha1 = bufptr + len;
-               char *path = strchr(bufptr, ' ');
-               unsigned int mode;
-               if (size < len + 20 || !path || 
-                   sscanf(bufptr, "%o", &mode) != 1)
-                       return -1;
+
+               sha1 = tree_entry_extract(&desc, &path, &mode);
 
                entry = xmalloc(sizeof(struct tree_entry_list));
-               entry->name = strdup(path + 1);
+               entry->name = path;
+               entry->mode = mode;
                entry->directory = S_ISDIR(mode) != 0;
                entry->executable = (mode & S_IXUSR) != 0;
                entry->symlink = S_ISLNK(mode) != 0;
-               entry->zeropad = *(char *)bufptr == '0';
-               entry->mode = mode;
+               entry->zeropad = *(const char *)(desc.buf) == '0';
                entry->next = NULL;
 
-               bufptr += len + 20;
-               size -= len + 20;
+               update_tree_entry(&desc);
 
                if (entry->directory) {
-                       entry->item.tree = lookup_tree(file_sha1);
-                       obj = &entry->item.tree->object;
+                       entry->item.tree = lookup_tree(sha1);
                } else {
-                       entry->item.blob = lookup_blob(file_sha1);
-                       obj = &entry->item.blob->object;
+                       entry->item.blob = lookup_blob(sha1);
                }
-               if (obj)
-                       n_refs++;
+               n_refs++;
                *list_p = entry;
                list_p = &entry->next;
        }
@@ -209,7 +206,6 @@ int parse_tree(struct tree *item)
         char type[20];
         void *buffer;
         unsigned long size;
-        int ret;
 
        if (item->object.parsed)
                return 0;
@@ -222,9 +218,7 @@ int parse_tree(struct tree *item)
                return error("Object %s not a tree",
                             sha1_to_hex(item->object.sha1));
        }
-       ret = parse_tree_buffer(item, buffer, size);
-       free(buffer);
-       return ret;
+       return parse_tree_buffer(item, buffer, size);
 }
 
 struct tree *parse_tree_indirect(const unsigned char *sha1)