write_name_quoted(): make one of the path a counted string.
[git/git.git] / tree.c
CommitLineData
175785e5
DB
1#include "tree.h"
2#include "blob.h"
77675e2a
DB
3#include "commit.h"
4#include "tag.h"
175785e5
DB
5#include "cache.h"
6#include <stdlib.h>
7
8const char *tree_type = "tree";
9
94537c78
LT
10static int read_one_entry(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
11{
12 int len = strlen(pathname);
13 unsigned int size = cache_entry_size(baselen + len);
812666c8 14 struct cache_entry *ce = xmalloc(size);
94537c78
LT
15
16 memset(ce, 0, size);
17
18 ce->ce_mode = create_ce_mode(mode);
19 ce->ce_flags = create_ce_flags(baselen + len, stage);
20 memcpy(ce->name, base, baselen);
21 memcpy(ce->name + baselen, pathname, len+1);
22 memcpy(ce->sha1, sha1, 20);
b155725d 23 return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
94537c78
LT
24}
25
3e587635 26static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths)
0ca14a57 27{
3e587635 28 const char *match;
0ca14a57
LT
29 int pathlen;
30
31 if (!paths)
32 return 1;
33 pathlen = strlen(path);
34 while ((match = *paths++) != NULL) {
35 int matchlen = strlen(match);
36
37 if (baselen >= matchlen) {
38 /* If it doesn't match, move along... */
39 if (strncmp(base, match, matchlen))
40 continue;
41 /* The base is a subdirectory of a path which was specified. */
42 return 1;
43 }
44
45 /* Does the base match? */
46 if (strncmp(base, match, baselen))
47 continue;
48
49 match += baselen;
50 matchlen -= baselen;
51
52 if (pathlen > matchlen)
53 continue;
54
55 if (matchlen > pathlen) {
56 if (match[pathlen] != '/')
57 continue;
58 if (!S_ISDIR(mode))
59 continue;
60 }
61
62 if (strncmp(path, match, pathlen))
63 continue;
3e587635
LT
64
65 return 1;
0ca14a57
LT
66 }
67 return 0;
68}
69
94537c78 70static int read_tree_recursive(void *buffer, unsigned long size,
0ca14a57 71 const char *base, int baselen,
3e587635 72 int stage, const char **match)
94537c78
LT
73{
74 while (size) {
75 int len = strlen(buffer)+1;
76 unsigned char *sha1 = buffer + len;
77 char *path = strchr(buffer, ' ')+1;
78 unsigned int mode;
79
80 if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
81 return -1;
82
83 buffer = sha1 + 20;
84 size -= len + 20;
85
0ca14a57
LT
86 if (!match_tree_entry(base, baselen, path, mode, match))
87 continue;
88
94537c78
LT
89 if (S_ISDIR(mode)) {
90 int retval;
91 int pathlen = strlen(path);
1c9da46d 92 char *newbase;
94537c78
LT
93 void *eltbuf;
94 char elttype[20];
95 unsigned long eltsize;
96
97 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
1c9da46d
JF
98 if (!eltbuf || strcmp(elttype, "tree")) {
99 if (eltbuf) free(eltbuf);
94537c78 100 return -1;
1c9da46d
JF
101 }
102 newbase = xmalloc(baselen + 1 + pathlen);
94537c78
LT
103 memcpy(newbase, base, baselen);
104 memcpy(newbase + baselen, path, pathlen);
105 newbase[baselen + pathlen] = '/';
106 retval = read_tree_recursive(eltbuf, eltsize,
107 newbase,
0ca14a57
LT
108 baselen + pathlen + 1,
109 stage, match);
94537c78
LT
110 free(eltbuf);
111 free(newbase);
112 if (retval)
113 return -1;
114 continue;
115 }
116 if (read_one_entry(sha1, base, baselen, path, mode, stage) < 0)
117 return -1;
118 }
119 return 0;
120}
121
3e587635 122int read_tree(void *buffer, unsigned long size, int stage, const char **match)
94537c78 123{
0ca14a57 124 return read_tree_recursive(buffer, size, "", 0, stage, match);
94537c78
LT
125}
126
5d6ccf5c 127struct tree *lookup_tree(const unsigned char *sha1)
175785e5
DB
128{
129 struct object *obj = lookup_object(sha1);
130 if (!obj) {
812666c8 131 struct tree *ret = xmalloc(sizeof(struct tree));
175785e5
DB
132 memset(ret, 0, sizeof(struct tree));
133 created_object(sha1, &ret->object);
d32987be 134 ret->object.type = tree_type;
175785e5
DB
135 return ret;
136 }
d1af002d
NP
137 if (!obj->type)
138 obj->type = tree_type;
c35dfe85 139 if (obj->type != tree_type) {
175785e5
DB
140 error("Object %s is a %s, not a tree",
141 sha1_to_hex(sha1), obj->type);
142 return NULL;
143 }
144 return (struct tree *) obj;
145}
146
bd2c39f5 147int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
175785e5 148{
bd2c39f5 149 void *bufptr = buffer;
08692164 150 struct tree_entry_list **list_p;
4a4e6fd7 151 int n_refs = 0;
bd2c39f5 152
175785e5
DB
153 if (item->object.parsed)
154 return 0;
155 item->object.parsed = 1;
08692164 156 list_p = &item->entries;
175785e5
DB
157 while (size) {
158 struct object *obj;
08692164 159 struct tree_entry_list *entry;
175785e5
DB
160 int len = 1+strlen(bufptr);
161 unsigned char *file_sha1 = bufptr + len;
162 char *path = strchr(bufptr, ' ');
163 unsigned int mode;
164 if (size < len + 20 || !path ||
bd2c39f5 165 sscanf(bufptr, "%o", &mode) != 1)
175785e5
DB
166 return -1;
167
812666c8 168 entry = xmalloc(sizeof(struct tree_entry_list));
08692164 169 entry->name = strdup(path + 1);
42ea9cb2
LT
170 entry->directory = S_ISDIR(mode) != 0;
171 entry->executable = (mode & S_IXUSR) != 0;
172 entry->symlink = S_ISLNK(mode) != 0;
64071805 173 entry->zeropad = *(char *)bufptr == '0';
42ea9cb2 174 entry->mode = mode;
08692164
DB
175 entry->next = NULL;
176
175785e5
DB
177 bufptr += len + 20;
178 size -= len + 20;
179
08692164
DB
180 if (entry->directory) {
181 entry->item.tree = lookup_tree(file_sha1);
182 obj = &entry->item.tree->object;
175785e5 183 } else {
08692164
DB
184 entry->item.blob = lookup_blob(file_sha1);
185 obj = &entry->item.blob->object;
175785e5 186 }
235ac407 187 if (obj)
4a4e6fd7 188 n_refs++;
6af1f019 189 entry->parent = NULL; /* needs to be filled by the user */
08692164
DB
190 *list_p = entry;
191 list_p = &entry->next;
175785e5 192 }
4a4e6fd7
SV
193
194 if (track_object_refs) {
195 struct tree_entry_list *entry;
196 unsigned i = 0;
197 struct object_refs *refs = alloc_object_refs(n_refs);
198 for (entry = item->entries; entry; entry = entry->next)
199 refs->ref[i++] = entry->item.any;
200 set_object_refs(&item->object, refs);
201 }
202
175785e5
DB
203 return 0;
204}
bd2c39f5
NP
205
206int parse_tree(struct tree *item)
207{
208 char type[20];
209 void *buffer;
210 unsigned long size;
211 int ret;
212
213 if (item->object.parsed)
214 return 0;
215 buffer = read_sha1_file(item->object.sha1, type, &size);
216 if (!buffer)
217 return error("Could not read %s",
218 sha1_to_hex(item->object.sha1));
219 if (strcmp(type, tree_type)) {
220 free(buffer);
221 return error("Object %s not a tree",
222 sha1_to_hex(item->object.sha1));
223 }
224 ret = parse_tree_buffer(item, buffer, size);
225 free(buffer);
226 return ret;
227}
77675e2a
DB
228
229struct tree *parse_tree_indirect(const unsigned char *sha1)
230{
231 struct object *obj = parse_object(sha1);
232 do {
233 if (!obj)
234 return NULL;
235 if (obj->type == tree_type)
236 return (struct tree *) obj;
237 else if (obj->type == commit_type)
238 obj = &(((struct commit *) obj)->tree->object);
239 else if (obj->type == tag_type)
240 obj = ((struct tag *) obj)->tagged;
241 else
242 return NULL;
243 if (!obj->parsed)
244 parse_object(obj->sha1);
245 } while (1);
246}