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