[PATCH] don't load and decompress objects twice with parse_object()
[git/git.git] / tree.c
CommitLineData
175785e5
DB
1#include "tree.h"
2#include "blob.h"
3#include "cache.h"
4#include <stdlib.h>
5
6const char *tree_type = "tree";
7
94537c78
LT
8static int read_one_entry(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
9{
10 int len = strlen(pathname);
11 unsigned int size = cache_entry_size(baselen + len);
812666c8 12 struct cache_entry *ce = xmalloc(size);
94537c78
LT
13
14 memset(ce, 0, size);
15
16 ce->ce_mode = create_ce_mode(mode);
17 ce->ce_flags = create_ce_flags(baselen + len, stage);
18 memcpy(ce->name, base, baselen);
19 memcpy(ce->name + baselen, pathname, len+1);
20 memcpy(ce->sha1, sha1, 20);
21 return add_cache_entry(ce, 1);
22}
23
24static int read_tree_recursive(void *buffer, unsigned long size,
25 const char *base, int baselen, int stage)
26{
27 while (size) {
28 int len = strlen(buffer)+1;
29 unsigned char *sha1 = buffer + len;
30 char *path = strchr(buffer, ' ')+1;
31 unsigned int mode;
32
33 if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
34 return -1;
35
36 buffer = sha1 + 20;
37 size -= len + 20;
38
39 if (S_ISDIR(mode)) {
40 int retval;
41 int pathlen = strlen(path);
812666c8 42 char *newbase = xmalloc(baselen + 1 + pathlen);
94537c78
LT
43 void *eltbuf;
44 char elttype[20];
45 unsigned long eltsize;
46
47 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
48 if (!eltbuf || strcmp(elttype, "tree"))
49 return -1;
50 memcpy(newbase, base, baselen);
51 memcpy(newbase + baselen, path, pathlen);
52 newbase[baselen + pathlen] = '/';
53 retval = read_tree_recursive(eltbuf, eltsize,
54 newbase,
55 baselen + pathlen + 1, stage);
56 free(eltbuf);
57 free(newbase);
58 if (retval)
59 return -1;
60 continue;
61 }
62 if (read_one_entry(sha1, base, baselen, path, mode, stage) < 0)
63 return -1;
64 }
65 return 0;
66}
67
68int read_tree(void *buffer, unsigned long size, int stage)
69{
70 return read_tree_recursive(buffer, size, "", 0, stage);
71}
72
175785e5
DB
73struct tree *lookup_tree(unsigned char *sha1)
74{
75 struct object *obj = lookup_object(sha1);
76 if (!obj) {
812666c8 77 struct tree *ret = xmalloc(sizeof(struct tree));
175785e5
DB
78 memset(ret, 0, sizeof(struct tree));
79 created_object(sha1, &ret->object);
d32987be 80 ret->object.type = tree_type;
175785e5
DB
81 return ret;
82 }
c35dfe85 83 if (obj->type != tree_type) {
175785e5
DB
84 error("Object %s is a %s, not a tree",
85 sha1_to_hex(sha1), obj->type);
86 return NULL;
87 }
88 return (struct tree *) obj;
89}
90
bd2c39f5 91int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
175785e5 92{
bd2c39f5 93 void *bufptr = buffer;
08692164 94 struct tree_entry_list **list_p;
bd2c39f5 95
175785e5
DB
96 if (item->object.parsed)
97 return 0;
98 item->object.parsed = 1;
08692164 99 list_p = &item->entries;
175785e5
DB
100 while (size) {
101 struct object *obj;
08692164 102 struct tree_entry_list *entry;
175785e5
DB
103 int len = 1+strlen(bufptr);
104 unsigned char *file_sha1 = bufptr + len;
105 char *path = strchr(bufptr, ' ');
106 unsigned int mode;
107 if (size < len + 20 || !path ||
bd2c39f5 108 sscanf(bufptr, "%o", &mode) != 1)
175785e5
DB
109 return -1;
110
812666c8 111 entry = xmalloc(sizeof(struct tree_entry_list));
08692164 112 entry->name = strdup(path + 1);
42ea9cb2
LT
113 entry->directory = S_ISDIR(mode) != 0;
114 entry->executable = (mode & S_IXUSR) != 0;
115 entry->symlink = S_ISLNK(mode) != 0;
116 entry->mode = mode;
08692164
DB
117 entry->next = NULL;
118
175785e5
DB
119 bufptr += len + 20;
120 size -= len + 20;
121
08692164
DB
122 if (entry->directory) {
123 entry->item.tree = lookup_tree(file_sha1);
124 obj = &entry->item.tree->object;
175785e5 125 } else {
08692164
DB
126 entry->item.blob = lookup_blob(file_sha1);
127 obj = &entry->item.blob->object;
175785e5 128 }
235ac407
LT
129 if (obj)
130 add_ref(&item->object, obj);
08692164
DB
131
132 *list_p = entry;
133 list_p = &entry->next;
175785e5
DB
134 }
135 return 0;
136}
bd2c39f5
NP
137
138int parse_tree(struct tree *item)
139{
140 char type[20];
141 void *buffer;
142 unsigned long size;
143 int ret;
144
145 if (item->object.parsed)
146 return 0;
147 buffer = read_sha1_file(item->object.sha1, type, &size);
148 if (!buffer)
149 return error("Could not read %s",
150 sha1_to_hex(item->object.sha1));
151 if (strcmp(type, tree_type)) {
152 free(buffer);
153 return error("Object %s not a tree",
154 sha1_to_hex(item->object.sha1));
155 }
156 ret = parse_tree_buffer(item, buffer, size);
157 free(buffer);
158 return ret;
159}