]>
Commit | Line | Data |
---|---|---|
85ab50f9 | 1 | #define NO_THE_INDEX_COMPATIBILITY_MACROS |
8f1d2e6f | 2 | #include "cache.h" |
af3785dc | 3 | #include "cache-tree.h" |
175785e5 DB |
4 | #include "tree.h" |
5 | #include "blob.h" | |
77675e2a DB |
6 | #include "commit.h" |
7 | #include "tag.h" | |
136f2e54 | 8 | #include "tree-walk.h" |
175785e5 DB |
9 | |
10 | const char *tree_type = "tree"; | |
11 | ||
85ab50f9 BW |
12 | static int read_one_entry_opt(struct index_state *istate, |
13 | const unsigned char *sha1, | |
14 | const char *base, int baselen, | |
15 | const char *pathname, | |
16 | unsigned mode, int stage, int opt) | |
94537c78 | 17 | { |
3c5e8468 LT |
18 | int len; |
19 | unsigned int size; | |
20 | struct cache_entry *ce; | |
21 | ||
22 | if (S_ISDIR(mode)) | |
23 | return READ_TREE_RECURSIVE; | |
24 | ||
25 | len = strlen(pathname); | |
26 | size = cache_entry_size(baselen + len); | |
90321c10 | 27 | ce = xcalloc(1, size); |
94537c78 LT |
28 | |
29 | ce->ce_mode = create_ce_mode(mode); | |
b60e188c TG |
30 | ce->ce_flags = create_ce_flags(stage); |
31 | ce->ce_namelen = baselen + len; | |
94537c78 LT |
32 | memcpy(ce->name, base, baselen); |
33 | memcpy(ce->name + baselen, pathname, len+1); | |
99d1a986 | 34 | hashcpy(ce->oid.hash, sha1); |
85ab50f9 | 35 | return add_index_entry(istate, ce, opt); |
af3785dc JH |
36 | } |
37 | ||
6a0b0b6d NTND |
38 | static int read_one_entry(const unsigned char *sha1, struct strbuf *base, |
39 | const char *pathname, unsigned mode, int stage, | |
40 | void *context) | |
af3785dc | 41 | { |
85ab50f9 BW |
42 | struct index_state *istate = context; |
43 | return read_one_entry_opt(istate, sha1, base->buf, base->len, pathname, | |
6a0b0b6d | 44 | mode, stage, |
af3785dc JH |
45 | ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK); |
46 | } | |
47 | ||
48 | /* | |
49 | * This is used when the caller knows there is no existing entries at | |
50 | * the stage that will conflict with the entry being added. | |
51 | */ | |
6a0b0b6d NTND |
52 | static int read_one_entry_quick(const unsigned char *sha1, struct strbuf *base, |
53 | const char *pathname, unsigned mode, int stage, | |
54 | void *context) | |
af3785dc | 55 | { |
85ab50f9 BW |
56 | struct index_state *istate = context; |
57 | return read_one_entry_opt(istate, sha1, base->buf, base->len, pathname, | |
6a0b0b6d | 58 | mode, stage, |
af3785dc | 59 | ADD_CACHE_JUST_APPEND); |
94537c78 LT |
60 | } |
61 | ||
ffd31f66 | 62 | static int read_tree_1(struct tree *tree, struct strbuf *base, |
18e4f405 | 63 | int stage, const struct pathspec *pathspec, |
ffd31f66 | 64 | read_tree_fn_t fn, void *context) |
94537c78 | 65 | { |
0790a42a | 66 | struct tree_desc desc; |
4c068a98 | 67 | struct name_entry entry; |
f26efc58 | 68 | struct object_id oid; |
d688cf07 NTND |
69 | int len, oldlen = base->len; |
70 | enum interesting retval = entry_not_interesting; | |
0790a42a | 71 | |
521698b1 DB |
72 | if (parse_tree(tree)) |
73 | return -1; | |
0790a42a | 74 | |
6fda5e51 | 75 | init_tree_desc(&desc, tree->buffer, tree->size); |
0790a42a | 76 | |
4c068a98 | 77 | while (tree_entry(&desc, &entry)) { |
d688cf07 | 78 | if (retval != all_entries_interesting) { |
ffd31f66 | 79 | retval = tree_entry_interesting(&entry, base, 0, pathspec); |
d688cf07 | 80 | if (retval == all_entries_not_interesting) |
ffd31f66 | 81 | break; |
d688cf07 | 82 | if (retval == entry_not_interesting) |
ffd31f66 NTND |
83 | continue; |
84 | } | |
0ca14a57 | 85 | |
7d924c91 | 86 | switch (fn(entry.oid->hash, base, |
ffd31f66 | 87 | entry.path, entry.mode, stage, context)) { |
3c5e8468 LT |
88 | case 0: |
89 | continue; | |
90 | case READ_TREE_RECURSIVE: | |
ba19a808 | 91 | break; |
3c5e8468 LT |
92 | default: |
93 | return -1; | |
94 | } | |
d3bee161 | 95 | |
ffd31f66 | 96 | if (S_ISDIR(entry.mode)) |
f26efc58 | 97 | oidcpy(&oid, entry.oid); |
ffd31f66 NTND |
98 | else if (S_ISGITLINK(entry.mode)) { |
99 | struct commit *commit; | |
d3bee161 | 100 | |
bc83266a | 101 | commit = lookup_commit(entry.oid); |
d3bee161 | 102 | if (!commit) |
ffd31f66 | 103 | die("Commit %s in submodule path %s%s not found", |
7d924c91 | 104 | oid_to_hex(entry.oid), |
ffd31f66 | 105 | base->buf, entry.path); |
d3bee161 LH |
106 | |
107 | if (parse_commit(commit)) | |
ffd31f66 | 108 | die("Invalid commit %s in submodule path %s%s", |
7d924c91 | 109 | oid_to_hex(entry.oid), |
ffd31f66 NTND |
110 | base->buf, entry.path); |
111 | ||
f26efc58 | 112 | oidcpy(&oid, &commit->tree->object.oid); |
94537c78 | 113 | } |
ffd31f66 NTND |
114 | else |
115 | continue; | |
116 | ||
0de16337 | 117 | len = tree_entry_len(&entry); |
ffd31f66 NTND |
118 | strbuf_add(base, entry.path, len); |
119 | strbuf_addch(base, '/'); | |
740ee055 | 120 | retval = read_tree_1(lookup_tree(&oid), |
ffd31f66 NTND |
121 | base, stage, pathspec, |
122 | fn, context); | |
123 | strbuf_setlen(base, oldlen); | |
124 | if (retval) | |
125 | return -1; | |
94537c78 LT |
126 | } |
127 | return 0; | |
128 | } | |
129 | ||
ffd31f66 NTND |
130 | int read_tree_recursive(struct tree *tree, |
131 | const char *base, int baselen, | |
18e4f405 | 132 | int stage, const struct pathspec *pathspec, |
ffd31f66 NTND |
133 | read_tree_fn_t fn, void *context) |
134 | { | |
135 | struct strbuf sb = STRBUF_INIT; | |
f0096c06 | 136 | int ret; |
ffd31f66 | 137 | |
ffd31f66 | 138 | strbuf_add(&sb, base, baselen); |
f0096c06 | 139 | ret = read_tree_1(tree, &sb, stage, pathspec, fn, context); |
ffd31f66 | 140 | strbuf_release(&sb); |
ffd31f66 NTND |
141 | return ret; |
142 | } | |
143 | ||
af3785dc JH |
144 | static int cmp_cache_name_compare(const void *a_, const void *b_) |
145 | { | |
146 | const struct cache_entry *ce1, *ce2; | |
147 | ||
148 | ce1 = *((const struct cache_entry **)a_); | |
149 | ce2 = *((const struct cache_entry **)b_); | |
b60e188c TG |
150 | return cache_name_stage_compare(ce1->name, ce1->ce_namelen, ce_stage(ce1), |
151 | ce2->name, ce2->ce_namelen, ce_stage(ce2)); | |
af3785dc JH |
152 | } |
153 | ||
85ab50f9 BW |
154 | int read_tree(struct tree *tree, int stage, struct pathspec *match, |
155 | struct index_state *istate) | |
94537c78 | 156 | { |
af3785dc JH |
157 | read_tree_fn_t fn = NULL; |
158 | int i, err; | |
159 | ||
160 | /* | |
161 | * Currently the only existing callers of this function all | |
162 | * call it with stage=1 and after making sure there is nothing | |
163 | * at that stage; we could always use read_one_entry_quick(). | |
164 | * | |
165 | * But when we decide to straighten out git-read-tree not to | |
166 | * use unpack_trees() in some cases, this will probably start | |
167 | * to matter. | |
168 | */ | |
169 | ||
170 | /* | |
171 | * See if we have cache entry at the stage. If so, | |
172 | * do it the original slow way, otherwise, append and then | |
173 | * sort at the end. | |
174 | */ | |
85ab50f9 BW |
175 | for (i = 0; !fn && i < istate->cache_nr; i++) { |
176 | const struct cache_entry *ce = istate->cache[i]; | |
af3785dc JH |
177 | if (ce_stage(ce) == stage) |
178 | fn = read_one_entry; | |
179 | } | |
180 | ||
181 | if (!fn) | |
182 | fn = read_one_entry_quick; | |
85ab50f9 | 183 | err = read_tree_recursive(tree, "", 0, stage, match, fn, istate); |
af3785dc JH |
184 | if (fn == read_one_entry || err) |
185 | return err; | |
186 | ||
187 | /* | |
188 | * Sort the cache entry -- we need to nuke the cache tree, though. | |
189 | */ | |
85ab50f9 BW |
190 | cache_tree_free(&istate->cache_tree); |
191 | QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare); | |
af3785dc | 192 | return 0; |
94537c78 LT |
193 | } |
194 | ||
740ee055 | 195 | struct tree *lookup_tree(const struct object_id *oid) |
175785e5 | 196 | { |
740ee055 | 197 | struct object *obj = lookup_object(oid->hash); |
100c5f3b | 198 | if (!obj) |
740ee055 | 199 | return create_object(oid->hash, alloc_tree_node()); |
8ff226a9 | 200 | return object_as_type(obj, OBJ_TREE, 0); |
175785e5 DB |
201 | } |
202 | ||
2d9c58c6 LT |
203 | int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size) |
204 | { | |
175785e5 DB |
205 | if (item->object.parsed) |
206 | return 0; | |
207 | item->object.parsed = 1; | |
136f2e54 LT |
208 | item->buffer = buffer; |
209 | item->size = size; | |
210 | ||
2d9c58c6 LT |
211 | return 0; |
212 | } | |
213 | ||
9cc2b07a | 214 | int parse_tree_gently(struct tree *item, int quiet_on_missing) |
bd2c39f5 | 215 | { |
21666f1a | 216 | enum object_type type; |
bd2c39f5 NP |
217 | void *buffer; |
218 | unsigned long size; | |
bd2c39f5 NP |
219 | |
220 | if (item->object.parsed) | |
221 | return 0; | |
ed1c9977 | 222 | buffer = read_sha1_file(item->object.oid.hash, &type, &size); |
bd2c39f5 | 223 | if (!buffer) |
9cc2b07a JK |
224 | return quiet_on_missing ? -1 : |
225 | error("Could not read %s", | |
f2fd0760 | 226 | oid_to_hex(&item->object.oid)); |
21666f1a | 227 | if (type != OBJ_TREE) { |
bd2c39f5 NP |
228 | free(buffer); |
229 | return error("Object %s not a tree", | |
f2fd0760 | 230 | oid_to_hex(&item->object.oid)); |
bd2c39f5 | 231 | } |
136f2e54 | 232 | return parse_tree_buffer(item, buffer, size); |
bd2c39f5 | 233 | } |
77675e2a | 234 | |
6e454b9a JK |
235 | void free_tree_buffer(struct tree *tree) |
236 | { | |
6a83d902 | 237 | FREE_AND_NULL(tree->buffer); |
6e454b9a JK |
238 | tree->size = 0; |
239 | tree->object.parsed = 0; | |
240 | } | |
241 | ||
a9dbc179 | 242 | struct tree *parse_tree_indirect(const struct object_id *oid) |
77675e2a | 243 | { |
c251c83d | 244 | struct object *obj = parse_object(oid); |
77675e2a DB |
245 | do { |
246 | if (!obj) | |
247 | return NULL; | |
1974632c | 248 | if (obj->type == OBJ_TREE) |
77675e2a | 249 | return (struct tree *) obj; |
1974632c | 250 | else if (obj->type == OBJ_COMMIT) |
77675e2a | 251 | obj = &(((struct commit *) obj)->tree->object); |
1974632c | 252 | else if (obj->type == OBJ_TAG) |
77675e2a DB |
253 | obj = ((struct tag *) obj)->tagged; |
254 | else | |
255 | return NULL; | |
256 | if (!obj->parsed) | |
c251c83d | 257 | parse_object(&obj->oid); |
77675e2a DB |
258 | } while (1); |
259 | } |