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