Start adding interfaces to read in partial trees
[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);
b155725d 21 return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
94537c78
LT
22}
23
0ca14a57
LT
24static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, char **paths)
25{
26 char *match;
27 int pathlen;
28
29 if (!paths)
30 return 1;
31 pathlen = strlen(path);
32 while ((match = *paths++) != NULL) {
33 int matchlen = strlen(match);
34
35 if (baselen >= matchlen) {
36 /* If it doesn't match, move along... */
37 if (strncmp(base, match, matchlen))
38 continue;
39 /* The base is a subdirectory of a path which was specified. */
40 return 1;
41 }
42
43 /* Does the base match? */
44 if (strncmp(base, match, baselen))
45 continue;
46
47 match += baselen;
48 matchlen -= baselen;
49
50 if (pathlen > matchlen)
51 continue;
52
53 if (matchlen > pathlen) {
54 if (match[pathlen] != '/')
55 continue;
56 if (!S_ISDIR(mode))
57 continue;
58 }
59
60 if (strncmp(path, match, pathlen))
61 continue;
62 }
63 return 0;
64}
65
94537c78 66static int read_tree_recursive(void *buffer, unsigned long size,
0ca14a57
LT
67 const char *base, int baselen,
68 int stage, char **match)
94537c78
LT
69{
70 while (size) {
71 int len = strlen(buffer)+1;
72 unsigned char *sha1 = buffer + len;
73 char *path = strchr(buffer, ' ')+1;
74 unsigned int mode;
75
76 if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
77 return -1;
78
79 buffer = sha1 + 20;
80 size -= len + 20;
81
0ca14a57
LT
82 if (!match_tree_entry(base, baselen, path, mode, match))
83 continue;
84
94537c78
LT
85 if (S_ISDIR(mode)) {
86 int retval;
87 int pathlen = strlen(path);
1c9da46d 88 char *newbase;
94537c78
LT
89 void *eltbuf;
90 char elttype[20];
91 unsigned long eltsize;
92
93 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
1c9da46d
JF
94 if (!eltbuf || strcmp(elttype, "tree")) {
95 if (eltbuf) free(eltbuf);
94537c78 96 return -1;
1c9da46d
JF
97 }
98 newbase = xmalloc(baselen + 1 + pathlen);
94537c78
LT
99 memcpy(newbase, base, baselen);
100 memcpy(newbase + baselen, path, pathlen);
101 newbase[baselen + pathlen] = '/';
102 retval = read_tree_recursive(eltbuf, eltsize,
103 newbase,
0ca14a57
LT
104 baselen + pathlen + 1,
105 stage, match);
94537c78
LT
106 free(eltbuf);
107 free(newbase);
108 if (retval)
109 return -1;
110 continue;
111 }
112 if (read_one_entry(sha1, base, baselen, path, mode, stage) < 0)
113 return -1;
114 }
115 return 0;
116}
117
0ca14a57 118int read_tree(void *buffer, unsigned long size, int stage, char **match)
94537c78 119{
0ca14a57 120 return read_tree_recursive(buffer, size, "", 0, stage, match);
94537c78
LT
121}
122
5d6ccf5c 123struct tree *lookup_tree(const unsigned char *sha1)
175785e5
DB
124{
125 struct object *obj = lookup_object(sha1);
126 if (!obj) {
812666c8 127 struct tree *ret = xmalloc(sizeof(struct tree));
175785e5
DB
128 memset(ret, 0, sizeof(struct tree));
129 created_object(sha1, &ret->object);
d32987be 130 ret->object.type = tree_type;
175785e5
DB
131 return ret;
132 }
d1af002d
NP
133 if (!obj->type)
134 obj->type = tree_type;
c35dfe85 135 if (obj->type != tree_type) {
175785e5
DB
136 error("Object %s is a %s, not a tree",
137 sha1_to_hex(sha1), obj->type);
138 return NULL;
139 }
140 return (struct tree *) obj;
141}
142
bd2c39f5 143int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
175785e5 144{
bd2c39f5 145 void *bufptr = buffer;
08692164 146 struct tree_entry_list **list_p;
bd2c39f5 147
175785e5
DB
148 if (item->object.parsed)
149 return 0;
150 item->object.parsed = 1;
08692164 151 list_p = &item->entries;
175785e5
DB
152 while (size) {
153 struct object *obj;
08692164 154 struct tree_entry_list *entry;
175785e5
DB
155 int len = 1+strlen(bufptr);
156 unsigned char *file_sha1 = bufptr + len;
157 char *path = strchr(bufptr, ' ');
158 unsigned int mode;
159 if (size < len + 20 || !path ||
bd2c39f5 160 sscanf(bufptr, "%o", &mode) != 1)
175785e5
DB
161 return -1;
162
812666c8 163 entry = xmalloc(sizeof(struct tree_entry_list));
08692164 164 entry->name = strdup(path + 1);
42ea9cb2
LT
165 entry->directory = S_ISDIR(mode) != 0;
166 entry->executable = (mode & S_IXUSR) != 0;
167 entry->symlink = S_ISLNK(mode) != 0;
168 entry->mode = mode;
08692164
DB
169 entry->next = NULL;
170
175785e5
DB
171 bufptr += len + 20;
172 size -= len + 20;
173
08692164
DB
174 if (entry->directory) {
175 entry->item.tree = lookup_tree(file_sha1);
176 obj = &entry->item.tree->object;
175785e5 177 } else {
08692164
DB
178 entry->item.blob = lookup_blob(file_sha1);
179 obj = &entry->item.blob->object;
175785e5 180 }
235ac407
LT
181 if (obj)
182 add_ref(&item->object, obj);
6af1f019 183 entry->parent = NULL; /* needs to be filled by the user */
08692164
DB
184 *list_p = entry;
185 list_p = &entry->next;
175785e5
DB
186 }
187 return 0;
188}
bd2c39f5
NP
189
190int parse_tree(struct tree *item)
191{
192 char type[20];
193 void *buffer;
194 unsigned long size;
195 int ret;
196
197 if (item->object.parsed)
198 return 0;
199 buffer = read_sha1_file(item->object.sha1, type, &size);
200 if (!buffer)
201 return error("Could not read %s",
202 sha1_to_hex(item->object.sha1));
203 if (strcmp(type, tree_type)) {
204 free(buffer);
205 return error("Object %s not a tree",
206 sha1_to_hex(item->object.sha1));
207 }
208 ret = parse_tree_buffer(item, buffer, size);
209 free(buffer);
210 return ret;
211}