]>
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 | |
8 | const char *tree_type = "tree"; | |
9 | ||
3a7c352b | 10 | static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage) |
94537c78 | 11 | { |
3c5e8468 LT |
12 | int len; |
13 | unsigned int size; | |
14 | struct cache_entry *ce; | |
15 | ||
16 | if (S_ISDIR(mode)) | |
17 | return READ_TREE_RECURSIVE; | |
18 | ||
19 | len = strlen(pathname); | |
20 | size = cache_entry_size(baselen + len); | |
90321c10 | 21 | ce = xcalloc(1, size); |
94537c78 LT |
22 | |
23 | ce->ce_mode = create_ce_mode(mode); | |
24 | ce->ce_flags = create_ce_flags(baselen + len, stage); | |
25 | memcpy(ce->name, base, baselen); | |
26 | memcpy(ce->name + baselen, pathname, len+1); | |
e702496e | 27 | hashcpy(ce->sha1, sha1); |
b155725d | 28 | return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK); |
94537c78 LT |
29 | } |
30 | ||
3e587635 | 31 | static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths) |
0ca14a57 | 32 | { |
3e587635 | 33 | const char *match; |
0ca14a57 LT |
34 | int pathlen; |
35 | ||
36 | if (!paths) | |
37 | return 1; | |
38 | pathlen = strlen(path); | |
39 | while ((match = *paths++) != NULL) { | |
40 | int matchlen = strlen(match); | |
41 | ||
42 | if (baselen >= matchlen) { | |
43 | /* If it doesn't match, move along... */ | |
44 | if (strncmp(base, match, matchlen)) | |
45 | continue; | |
46 | /* The base is a subdirectory of a path which was specified. */ | |
47 | return 1; | |
48 | } | |
49 | ||
50 | /* Does the base match? */ | |
51 | if (strncmp(base, match, baselen)) | |
52 | continue; | |
53 | ||
54 | match += baselen; | |
55 | matchlen -= baselen; | |
56 | ||
57 | if (pathlen > matchlen) | |
58 | continue; | |
59 | ||
60 | if (matchlen > pathlen) { | |
61 | if (match[pathlen] != '/') | |
62 | continue; | |
63 | if (!S_ISDIR(mode)) | |
64 | continue; | |
65 | } | |
66 | ||
67 | if (strncmp(path, match, pathlen)) | |
68 | continue; | |
3e587635 LT |
69 | |
70 | return 1; | |
0ca14a57 LT |
71 | } |
72 | return 0; | |
73 | } | |
74 | ||
521698b1 | 75 | int read_tree_recursive(struct tree *tree, |
3c5e8468 LT |
76 | const char *base, int baselen, |
77 | int stage, const char **match, | |
78 | read_tree_fn_t fn) | |
94537c78 | 79 | { |
0790a42a | 80 | struct tree_desc desc; |
4c068a98 | 81 | struct name_entry entry; |
0790a42a | 82 | |
521698b1 DB |
83 | if (parse_tree(tree)) |
84 | return -1; | |
0790a42a | 85 | |
6fda5e51 | 86 | init_tree_desc(&desc, tree->buffer, tree->size); |
0790a42a | 87 | |
4c068a98 LT |
88 | while (tree_entry(&desc, &entry)) { |
89 | if (!match_tree_entry(base, baselen, entry.path, entry.mode, match)) | |
0ca14a57 LT |
90 | continue; |
91 | ||
4c068a98 | 92 | switch (fn(entry.sha1, base, baselen, entry.path, entry.mode, stage)) { |
3c5e8468 LT |
93 | case 0: |
94 | continue; | |
95 | case READ_TREE_RECURSIVE: | |
96 | break;; | |
97 | default: | |
98 | return -1; | |
99 | } | |
4c068a98 | 100 | if (S_ISDIR(entry.mode)) { |
94537c78 | 101 | int retval; |
1c9da46d | 102 | char *newbase; |
a8c40471 | 103 | unsigned int pathlen = tree_entry_len(entry.path, entry.sha1); |
94537c78 | 104 | |
a8c40471 | 105 | newbase = xmalloc(baselen + 1 + pathlen); |
94537c78 | 106 | memcpy(newbase, base, baselen); |
a8c40471 LT |
107 | memcpy(newbase + baselen, entry.path, pathlen); |
108 | newbase[baselen + pathlen] = '/'; | |
4c068a98 | 109 | retval = read_tree_recursive(lookup_tree(entry.sha1), |
94537c78 | 110 | newbase, |
a8c40471 | 111 | baselen + pathlen + 1, |
3c5e8468 | 112 | stage, match, fn); |
94537c78 LT |
113 | free(newbase); |
114 | if (retval) | |
115 | return -1; | |
116 | continue; | |
117 | } | |
94537c78 LT |
118 | } |
119 | return 0; | |
120 | } | |
121 | ||
521698b1 | 122 | int read_tree(struct tree *tree, int stage, const char **match) |
94537c78 | 123 | { |
521698b1 | 124 | return read_tree_recursive(tree, "", 0, stage, match, read_one_entry); |
94537c78 LT |
125 | } |
126 | ||
5d6ccf5c | 127 | struct tree *lookup_tree(const unsigned char *sha1) |
175785e5 DB |
128 | { |
129 | struct object *obj = lookup_object(sha1); | |
100c5f3b LT |
130 | if (!obj) |
131 | return create_object(sha1, OBJ_TREE, alloc_tree_node()); | |
d1af002d | 132 | if (!obj->type) |
1974632c LT |
133 | obj->type = OBJ_TREE; |
134 | if (obj->type != OBJ_TREE) { | |
885a86ab LT |
135 | error("Object %s is a %s, not a tree", |
136 | sha1_to_hex(sha1), typename(obj->type)); | |
175785e5 DB |
137 | return NULL; |
138 | } | |
139 | return (struct tree *) obj; | |
140 | } | |
141 | ||
8d9721c8 LT |
142 | /* |
143 | * NOTE! Tree refs to external git repositories | |
144 | * (ie gitlinks) do not count as real references. | |
145 | * | |
146 | * You don't have to have those repositories | |
147 | * available at all, much less have the objects | |
148 | * accessible from the current repository. | |
149 | */ | |
74b504f6 | 150 | static void track_tree_refs(struct tree *item) |
175785e5 | 151 | { |
2d9c58c6 LT |
152 | int n_refs = 0, i; |
153 | struct object_refs *refs; | |
136f2e54 | 154 | struct tree_desc desc; |
4c068a98 | 155 | struct name_entry entry; |
bd2c39f5 | 156 | |
2d9c58c6 | 157 | /* Count how many entries there are.. */ |
6fda5e51 | 158 | init_tree_desc(&desc, item->buffer, item->size); |
8d9721c8 LT |
159 | while (tree_entry(&desc, &entry)) { |
160 | if (S_ISDIRLNK(entry.mode)) | |
161 | continue; | |
2d9c58c6 | 162 | n_refs++; |
8d9721c8 | 163 | } |
2d9c58c6 LT |
164 | |
165 | /* Allocate object refs and walk it again.. */ | |
166 | i = 0; | |
167 | refs = alloc_object_refs(n_refs); | |
6fda5e51 | 168 | init_tree_desc(&desc, item->buffer, item->size); |
4c068a98 | 169 | while (tree_entry(&desc, &entry)) { |
2d9c58c6 LT |
170 | struct object *obj; |
171 | ||
8d9721c8 LT |
172 | if (S_ISDIRLNK(entry.mode)) |
173 | continue; | |
4c068a98 LT |
174 | if (S_ISDIR(entry.mode)) |
175 | obj = &lookup_tree(entry.sha1)->object; | |
2d9c58c6 | 176 | else |
4c068a98 | 177 | obj = &lookup_blob(entry.sha1)->object; |
2d9c58c6 LT |
178 | refs->ref[i++] = obj; |
179 | } | |
180 | set_object_refs(&item->object, refs); | |
2d9c58c6 LT |
181 | } |
182 | ||
183 | int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size) | |
184 | { | |
175785e5 DB |
185 | if (item->object.parsed) |
186 | return 0; | |
187 | item->object.parsed = 1; | |
136f2e54 LT |
188 | item->buffer = buffer; |
189 | item->size = size; | |
190 | ||
2d9c58c6 LT |
191 | if (track_object_refs) |
192 | track_tree_refs(item); | |
193 | return 0; | |
194 | } | |
195 | ||
bd2c39f5 NP |
196 | int parse_tree(struct tree *item) |
197 | { | |
21666f1a | 198 | enum object_type type; |
bd2c39f5 NP |
199 | void *buffer; |
200 | unsigned long size; | |
bd2c39f5 NP |
201 | |
202 | if (item->object.parsed) | |
203 | return 0; | |
21666f1a | 204 | buffer = read_sha1_file(item->object.sha1, &type, &size); |
bd2c39f5 NP |
205 | if (!buffer) |
206 | return error("Could not read %s", | |
207 | sha1_to_hex(item->object.sha1)); | |
21666f1a | 208 | if (type != OBJ_TREE) { |
bd2c39f5 NP |
209 | free(buffer); |
210 | return error("Object %s not a tree", | |
211 | sha1_to_hex(item->object.sha1)); | |
212 | } | |
136f2e54 | 213 | return parse_tree_buffer(item, buffer, size); |
bd2c39f5 | 214 | } |
77675e2a DB |
215 | |
216 | struct tree *parse_tree_indirect(const unsigned char *sha1) | |
217 | { | |
218 | struct object *obj = parse_object(sha1); | |
219 | do { | |
220 | if (!obj) | |
221 | return NULL; | |
1974632c | 222 | if (obj->type == OBJ_TREE) |
77675e2a | 223 | return (struct tree *) obj; |
1974632c | 224 | else if (obj->type == OBJ_COMMIT) |
77675e2a | 225 | obj = &(((struct commit *) obj)->tree->object); |
1974632c | 226 | else if (obj->type == OBJ_TAG) |
77675e2a DB |
227 | obj = ((struct tag *) obj)->tagged; |
228 | else | |
229 | return NULL; | |
230 | if (!obj->parsed) | |
231 | parse_object(obj->sha1); | |
232 | } while (1); | |
233 | } |