]>
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 | { |
521698b1 DB |
81 | struct tree_entry_list *list; |
82 | if (parse_tree(tree)) | |
83 | return -1; | |
84 | list = tree->entries; | |
85 | while (list) { | |
86 | struct tree_entry_list *current = list; | |
87 | list = list->next; | |
88 | if (!match_tree_entry(base, baselen, current->name, | |
89 | current->mode, match)) | |
0ca14a57 LT |
90 | continue; |
91 | ||
3a7c352b | 92 | switch (fn(current->sha1, base, baselen, |
521698b1 | 93 | current->name, current->mode, stage)) { |
3c5e8468 LT |
94 | case 0: |
95 | continue; | |
96 | case READ_TREE_RECURSIVE: | |
97 | break;; | |
98 | default: | |
99 | return -1; | |
100 | } | |
521698b1 | 101 | if (current->directory) { |
94537c78 | 102 | int retval; |
521698b1 | 103 | int pathlen = strlen(current->name); |
1c9da46d | 104 | char *newbase; |
94537c78 | 105 | |
1c9da46d | 106 | newbase = xmalloc(baselen + 1 + pathlen); |
94537c78 | 107 | memcpy(newbase, base, baselen); |
521698b1 | 108 | memcpy(newbase + baselen, current->name, pathlen); |
94537c78 | 109 | newbase[baselen + pathlen] = '/'; |
3a7c352b | 110 | retval = read_tree_recursive(lookup_tree(current->sha1), |
94537c78 | 111 | newbase, |
0ca14a57 | 112 | baselen + pathlen + 1, |
3c5e8468 | 113 | stage, match, fn); |
94537c78 LT |
114 | free(newbase); |
115 | if (retval) | |
116 | return -1; | |
117 | continue; | |
118 | } | |
94537c78 LT |
119 | } |
120 | return 0; | |
121 | } | |
122 | ||
521698b1 | 123 | int read_tree(struct tree *tree, int stage, const char **match) |
94537c78 | 124 | { |
521698b1 | 125 | return read_tree_recursive(tree, "", 0, stage, match, read_one_entry); |
94537c78 LT |
126 | } |
127 | ||
5d6ccf5c | 128 | struct tree *lookup_tree(const unsigned char *sha1) |
175785e5 DB |
129 | { |
130 | struct object *obj = lookup_object(sha1); | |
131 | if (!obj) { | |
90321c10 | 132 | struct tree *ret = xcalloc(1, sizeof(struct tree)); |
175785e5 | 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 | 147 | int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size) |
175785e5 | 148 | { |
136f2e54 | 149 | struct tree_desc desc; |
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; | |
136f2e54 LT |
156 | item->buffer = buffer; |
157 | item->size = size; | |
158 | ||
159 | desc.buf = buffer; | |
160 | desc.size = size; | |
161 | ||
08692164 | 162 | list_p = &item->entries; |
136f2e54 LT |
163 | while (desc.size) { |
164 | unsigned mode; | |
165 | const char *path; | |
166 | const unsigned char *sha1; | |
08692164 | 167 | struct tree_entry_list *entry; |
136f2e54 LT |
168 | |
169 | sha1 = tree_entry_extract(&desc, &path, &mode); | |
175785e5 | 170 | |
812666c8 | 171 | entry = xmalloc(sizeof(struct tree_entry_list)); |
136f2e54 | 172 | entry->name = path; |
3a7c352b | 173 | entry->sha1 = sha1; |
136f2e54 | 174 | entry->mode = mode; |
42ea9cb2 LT |
175 | entry->directory = S_ISDIR(mode) != 0; |
176 | entry->executable = (mode & S_IXUSR) != 0; | |
177 | entry->symlink = S_ISLNK(mode) != 0; | |
136f2e54 | 178 | entry->zeropad = *(const char *)(desc.buf) == '0'; |
08692164 DB |
179 | entry->next = NULL; |
180 | ||
136f2e54 | 181 | update_tree_entry(&desc); |
136f2e54 | 182 | n_refs++; |
08692164 DB |
183 | *list_p = entry; |
184 | list_p = &entry->next; | |
175785e5 | 185 | } |
4a4e6fd7 SV |
186 | |
187 | if (track_object_refs) { | |
188 | struct tree_entry_list *entry; | |
189 | unsigned i = 0; | |
190 | struct object_refs *refs = alloc_object_refs(n_refs); | |
3a7c352b LT |
191 | for (entry = item->entries; entry; entry = entry->next) { |
192 | struct object *obj; | |
193 | ||
194 | if (entry->directory) | |
195 | obj = &lookup_tree(entry->sha1)->object; | |
196 | else | |
197 | obj = &lookup_blob(entry->sha1)->object; | |
198 | refs->ref[i++] = obj; | |
199 | } | |
200 | ||
4a4e6fd7 SV |
201 | set_object_refs(&item->object, refs); |
202 | } | |
203 | ||
175785e5 DB |
204 | return 0; |
205 | } | |
bd2c39f5 NP |
206 | |
207 | int parse_tree(struct tree *item) | |
208 | { | |
209 | char type[20]; | |
210 | void *buffer; | |
211 | unsigned long size; | |
bd2c39f5 NP |
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 | } | |
136f2e54 | 224 | return parse_tree_buffer(item, buffer, size); |
bd2c39f5 | 225 | } |
77675e2a DB |
226 | |
227 | struct tree *parse_tree_indirect(const unsigned char *sha1) | |
228 | { | |
229 | struct object *obj = parse_object(sha1); | |
230 | do { | |
231 | if (!obj) | |
232 | return NULL; | |
233 | if (obj->type == tree_type) | |
234 | return (struct tree *) obj; | |
235 | else if (obj->type == commit_type) | |
236 | obj = &(((struct commit *) obj)->tree->object); | |
237 | else if (obj->type == tag_type) | |
238 | obj = ((struct tag *) obj)->tagged; | |
239 | else | |
240 | return NULL; | |
241 | if (!obj->parsed) | |
242 | parse_object(obj->sha1); | |
243 | } while (1); | |
244 | } |