]>
Commit | Line | Data |
---|---|---|
74986462 | 1 | #include "cache.h" |
697cc8ef | 2 | #include "lockfile.h" |
74986462 | 3 | #include "tree.h" |
b9d37a54 | 4 | #include "tree-walk.h" |
74986462 | 5 | #include "cache-tree.h" |
4d33e2ba | 6 | #include "bulk-checkin.h" |
cbd53a21 | 7 | #include "object-store.h" |
4592e608 | 8 | #include "replace-object.h" |
b14ed5ad | 9 | #include "promisor-remote.h" |
6e773527 | 10 | #include "sparse-index.h" |
74986462 | 11 | |
e9b9cc56 JH |
12 | #ifndef DEBUG_CACHE_TREE |
13 | #define DEBUG_CACHE_TREE 0 | |
4903161f | 14 | #endif |
74986462 JH |
15 | |
16 | struct cache_tree *cache_tree(void) | |
17 | { | |
18 | struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree)); | |
19 | it->entry_count = -1; | |
20 | return it; | |
21 | } | |
22 | ||
bad68ec9 | 23 | void cache_tree_free(struct cache_tree **it_p) |
74986462 JH |
24 | { |
25 | int i; | |
bad68ec9 | 26 | struct cache_tree *it = *it_p; |
74986462 JH |
27 | |
28 | if (!it) | |
29 | return; | |
30 | for (i = 0; i < it->subtree_nr; i++) | |
e92fa514 | 31 | if (it->down[i]) { |
61fa3097 | 32 | cache_tree_free(&it->down[i]->cache_tree); |
e92fa514 EN |
33 | free(it->down[i]); |
34 | } | |
74986462 JH |
35 | free(it->down); |
36 | free(it); | |
bad68ec9 | 37 | *it_p = NULL; |
74986462 JH |
38 | } |
39 | ||
61fa3097 JH |
40 | static int subtree_name_cmp(const char *one, int onelen, |
41 | const char *two, int twolen) | |
42 | { | |
43 | if (onelen < twolen) | |
44 | return -1; | |
45 | if (twolen < onelen) | |
46 | return 1; | |
47 | return memcmp(one, two, onelen); | |
48 | } | |
49 | ||
c80dd396 | 50 | int cache_tree_subtree_pos(struct cache_tree *it, const char *path, int pathlen) |
61fa3097 JH |
51 | { |
52 | struct cache_tree_sub **down = it->down; | |
53 | int lo, hi; | |
54 | lo = 0; | |
55 | hi = it->subtree_nr; | |
56 | while (lo < hi) { | |
19716b21 | 57 | int mi = lo + (hi - lo) / 2; |
61fa3097 JH |
58 | struct cache_tree_sub *mdl = down[mi]; |
59 | int cmp = subtree_name_cmp(path, pathlen, | |
60 | mdl->name, mdl->namelen); | |
61 | if (!cmp) | |
62 | return mi; | |
63 | if (cmp < 0) | |
64 | hi = mi; | |
65 | else | |
66 | lo = mi + 1; | |
67 | } | |
68 | return -lo-1; | |
69 | } | |
70 | ||
74986462 JH |
71 | static struct cache_tree_sub *find_subtree(struct cache_tree *it, |
72 | const char *path, | |
73 | int pathlen, | |
74 | int create) | |
75 | { | |
74986462 | 76 | struct cache_tree_sub *down; |
c80dd396 | 77 | int pos = cache_tree_subtree_pos(it, path, pathlen); |
61fa3097 JH |
78 | if (0 <= pos) |
79 | return it->down[pos]; | |
74986462 JH |
80 | if (!create) |
81 | return NULL; | |
61fa3097 JH |
82 | |
83 | pos = -pos-1; | |
bcc7a032 | 84 | ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc); |
61fa3097 JH |
85 | it->subtree_nr++; |
86 | ||
96ffc06f | 87 | FLEX_ALLOC_MEM(down, name, path, pathlen); |
61fa3097 | 88 | down->cache_tree = NULL; |
74986462 | 89 | down->namelen = pathlen; |
61fa3097 JH |
90 | |
91 | if (pos < it->subtree_nr) | |
f919ffeb SG |
92 | MOVE_ARRAY(it->down + pos + 1, it->down + pos, |
93 | it->subtree_nr - pos - 1); | |
61fa3097 | 94 | it->down[pos] = down; |
74986462 JH |
95 | return down; |
96 | } | |
97 | ||
7927a55d JH |
98 | struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path) |
99 | { | |
100 | int pathlen = strlen(path); | |
101 | return find_subtree(it, path, pathlen, 1); | |
102 | } | |
103 | ||
080ab56a DS |
104 | struct cache_tree *cache_tree_find_path(struct cache_tree *it, const char *path) |
105 | { | |
106 | const char *slash; | |
107 | int namelen; | |
108 | struct cache_tree_sub it_sub = { | |
109 | .cache_tree = it, | |
110 | }; | |
111 | struct cache_tree_sub *down = &it_sub; | |
112 | ||
113 | while (down) { | |
114 | slash = strchrnul(path, '/'); | |
115 | namelen = slash - path; | |
116 | down->cache_tree->entry_count = -1; | |
117 | if (!*slash) { | |
118 | int pos; | |
119 | pos = cache_tree_subtree_pos(down->cache_tree, path, namelen); | |
120 | if (0 <= pos) | |
121 | return down->cache_tree->down[pos]->cache_tree; | |
122 | return NULL; | |
123 | } | |
124 | down = find_subtree(it, path, namelen, 0); | |
125 | path = slash + 1; | |
126 | } | |
127 | ||
128 | return NULL; | |
129 | } | |
130 | ||
a5400efe | 131 | static int do_invalidate_path(struct cache_tree *it, const char *path) |
74986462 JH |
132 | { |
133 | /* a/b/c | |
134 | * ==> invalidate self | |
135 | * ==> find "a", have it invalidate "b/c" | |
136 | * a | |
137 | * ==> invalidate self | |
138 | * ==> if "a" exists as a subtree, remove it. | |
139 | */ | |
140 | const char *slash; | |
141 | int namelen; | |
142 | struct cache_tree_sub *down; | |
143 | ||
e9b9cc56 | 144 | #if DEBUG_CACHE_TREE |
00703e6d JH |
145 | fprintf(stderr, "cache-tree invalidate <%s>\n", path); |
146 | #endif | |
147 | ||
74986462 | 148 | if (!it) |
a5400efe | 149 | return 0; |
2c5495f7 RM |
150 | slash = strchrnul(path, '/'); |
151 | namelen = slash - path; | |
74986462 | 152 | it->entry_count = -1; |
2c5495f7 | 153 | if (!*slash) { |
61fa3097 | 154 | int pos; |
c80dd396 | 155 | pos = cache_tree_subtree_pos(it, path, namelen); |
61fa3097 JH |
156 | if (0 <= pos) { |
157 | cache_tree_free(&it->down[pos]->cache_tree); | |
158 | free(it->down[pos]); | |
74986462 JH |
159 | /* 0 1 2 3 4 5 |
160 | * ^ ^subtree_nr = 6 | |
61fa3097 | 161 | * pos |
74986462 | 162 | * move 4 and 5 up one place (2 entries) |
61fa3097 | 163 | * 2 = 6 - 3 - 1 = subtree_nr - pos - 1 |
74986462 | 164 | */ |
f331ab9d RS |
165 | MOVE_ARRAY(it->down + pos, it->down + pos + 1, |
166 | it->subtree_nr - pos - 1); | |
74986462 JH |
167 | it->subtree_nr--; |
168 | } | |
a5400efe | 169 | return 1; |
74986462 | 170 | } |
74986462 JH |
171 | down = find_subtree(it, path, namelen, 0); |
172 | if (down) | |
a5400efe NTND |
173 | do_invalidate_path(down->cache_tree, slash + 1); |
174 | return 1; | |
175 | } | |
176 | ||
177 | void cache_tree_invalidate_path(struct index_state *istate, const char *path) | |
178 | { | |
179 | if (do_invalidate_path(istate->cache_tree, path)) | |
180 | istate->cache_changed |= CACHE_TREE_CHANGED; | |
74986462 JH |
181 | } |
182 | ||
8d87e338 | 183 | static int verify_cache(struct index_state *istate, int flags) |
74986462 | 184 | { |
8d87e338 | 185 | unsigned i, funny; |
e859c69b | 186 | int silent = flags & WRITE_TREE_SILENT; |
74986462 JH |
187 | |
188 | /* Verify that the tree is merged */ | |
189 | funny = 0; | |
8d87e338 DS |
190 | for (i = 0; i < istate->cache_nr; i++) { |
191 | const struct cache_entry *ce = istate->cache[i]; | |
3f6d56de | 192 | if (ce_stage(ce)) { |
996277c5 TR |
193 | if (silent) |
194 | return -1; | |
74986462 JH |
195 | if (10 < ++funny) { |
196 | fprintf(stderr, "...\n"); | |
197 | break; | |
198 | } | |
dbc3904e | 199 | fprintf(stderr, "%s: unmerged (%s)\n", |
99d1a986 | 200 | ce->name, oid_to_hex(&ce->oid)); |
74986462 JH |
201 | } |
202 | } | |
203 | if (funny) | |
204 | return -1; | |
205 | ||
206 | /* Also verify that the cache does not have path and path/file | |
207 | * at the same time. At this point we know the cache has only | |
208 | * stage 0 entries. | |
209 | */ | |
210 | funny = 0; | |
8d87e338 | 211 | for (i = 0; i + 1 < istate->cache_nr; i++) { |
74986462 JH |
212 | /* path/file always comes after path because of the way |
213 | * the cache is sorted. Also path can appear only once, | |
214 | * which means conflicting one would immediately follow. | |
215 | */ | |
8d87e338 DS |
216 | const struct cache_entry *this_ce = istate->cache[i]; |
217 | const struct cache_entry *next_ce = istate->cache[i + 1]; | |
0b72536a RS |
218 | const char *this_name = this_ce->name; |
219 | const char *next_name = next_ce->name; | |
220 | int this_len = ce_namelen(this_ce); | |
221 | if (this_len < ce_namelen(next_ce) && | |
a4b6d202 DS |
222 | next_name[this_len] == '/' && |
223 | strncmp(this_name, next_name, this_len) == 0) { | |
74986462 JH |
224 | if (10 < ++funny) { |
225 | fprintf(stderr, "...\n"); | |
226 | break; | |
227 | } | |
228 | fprintf(stderr, "You have both %s and %s\n", | |
229 | this_name, next_name); | |
230 | } | |
231 | } | |
232 | if (funny) | |
233 | return -1; | |
234 | return 0; | |
235 | } | |
236 | ||
237 | static void discard_unused_subtrees(struct cache_tree *it) | |
238 | { | |
239 | struct cache_tree_sub **down = it->down; | |
240 | int nr = it->subtree_nr; | |
241 | int dst, src; | |
242 | for (dst = src = 0; src < nr; src++) { | |
243 | struct cache_tree_sub *s = down[src]; | |
244 | if (s->used) | |
245 | down[dst++] = s; | |
246 | else { | |
bad68ec9 | 247 | cache_tree_free(&s->cache_tree); |
74986462 JH |
248 | free(s); |
249 | it->subtree_nr--; | |
250 | } | |
251 | } | |
252 | } | |
253 | ||
bad68ec9 JH |
254 | int cache_tree_fully_valid(struct cache_tree *it) |
255 | { | |
256 | int i; | |
257 | if (!it) | |
258 | return 0; | |
98374a07 | 259 | if (it->entry_count < 0 || !has_object_file(&it->oid)) |
bad68ec9 JH |
260 | return 0; |
261 | for (i = 0; i < it->subtree_nr; i++) { | |
262 | if (!cache_tree_fully_valid(it->down[i]->cache_tree)) | |
263 | return 0; | |
264 | } | |
265 | return 1; | |
266 | } | |
267 | ||
d3da223f JT |
268 | static int must_check_existence(const struct cache_entry *ce) |
269 | { | |
270 | return !(has_promisor_remote() && ce_skip_worktree(ce)); | |
271 | } | |
272 | ||
74986462 | 273 | static int update_one(struct cache_tree *it, |
d0cfc3e8 | 274 | struct cache_entry **cache, |
74986462 JH |
275 | int entries, |
276 | const char *base, | |
277 | int baselen, | |
3cf773e4 | 278 | int *skip_count, |
e859c69b | 279 | int flags) |
74986462 | 280 | { |
5242bcbb | 281 | struct strbuf buffer; |
e859c69b NTND |
282 | int missing_ok = flags & WRITE_TREE_MISSING_OK; |
283 | int dryrun = flags & WRITE_TREE_DRY_RUN; | |
aecf567c | 284 | int repair = flags & WRITE_TREE_REPAIR; |
eec3e7e4 | 285 | int to_invalidate = 0; |
74986462 JH |
286 | int i; |
287 | ||
aecf567c DT |
288 | assert(!(dryrun && repair)); |
289 | ||
3cf773e4 NTND |
290 | *skip_count = 0; |
291 | ||
2de37c53 DS |
292 | /* |
293 | * If the first entry of this region is a sparse directory | |
294 | * entry corresponding exactly to 'base', then this cache_tree | |
295 | * struct is a "leaf" in the data structure, pointing to the | |
296 | * tree OID specified in the entry. | |
297 | */ | |
298 | if (entries > 0) { | |
299 | const struct cache_entry *ce = cache[0]; | |
300 | ||
301 | if (S_ISSPARSEDIR(ce->ce_mode) && | |
302 | ce->ce_namelen == baselen && | |
303 | !strncmp(ce->name, base, baselen)) { | |
304 | it->entry_count = 1; | |
305 | oidcpy(&it->oid, &ce->oid); | |
306 | return 1; | |
307 | } | |
308 | } | |
309 | ||
98374a07 | 310 | if (0 <= it->entry_count && has_object_file(&it->oid)) |
74986462 JH |
311 | return it->entry_count; |
312 | ||
313 | /* | |
314 | * We first scan for subtrees and update them; we start by | |
315 | * marking existing subtrees -- the ones that are unmarked | |
316 | * should not be in the result. | |
317 | */ | |
318 | for (i = 0; i < it->subtree_nr; i++) | |
319 | it->down[i]->used = 0; | |
320 | ||
321 | /* | |
322 | * Find the subtrees and update them. | |
323 | */ | |
386cc8b0 NTND |
324 | i = 0; |
325 | while (i < entries) { | |
9c5e6c80 | 326 | const struct cache_entry *ce = cache[i]; |
74986462 JH |
327 | struct cache_tree_sub *sub; |
328 | const char *path, *slash; | |
3cf773e4 | 329 | int pathlen, sublen, subcnt, subskip; |
74986462 JH |
330 | |
331 | path = ce->name; | |
332 | pathlen = ce_namelen(ce); | |
333 | if (pathlen <= baselen || memcmp(base, path, baselen)) | |
334 | break; /* at the end of this level */ | |
335 | ||
336 | slash = strchr(path + baselen, '/'); | |
386cc8b0 NTND |
337 | if (!slash) { |
338 | i++; | |
74986462 | 339 | continue; |
386cc8b0 | 340 | } |
74986462 JH |
341 | /* |
342 | * a/bbb/c (base = a/, slash = /c) | |
343 | * ==> | |
344 | * path+baselen = bbb/c, sublen = 3 | |
345 | */ | |
346 | sublen = slash - (path + baselen); | |
347 | sub = find_subtree(it, path + baselen, sublen, 1); | |
348 | if (!sub->cache_tree) | |
349 | sub->cache_tree = cache_tree(); | |
350 | subcnt = update_one(sub->cache_tree, | |
351 | cache + i, entries - i, | |
352 | path, | |
353 | baselen + sublen + 1, | |
3cf773e4 | 354 | &subskip, |
e859c69b | 355 | flags); |
3d12d0cf JS |
356 | if (subcnt < 0) |
357 | return subcnt; | |
729dbbd9 JK |
358 | if (!subcnt) |
359 | die("index cache-tree records empty sub-tree"); | |
386cc8b0 | 360 | i += subcnt; |
3cf773e4 NTND |
361 | sub->count = subcnt; /* to be used in the next loop */ |
362 | *skip_count += subskip; | |
74986462 JH |
363 | sub->used = 1; |
364 | } | |
365 | ||
366 | discard_unused_subtrees(it); | |
367 | ||
368 | /* | |
369 | * Then write out the tree object for this level. | |
370 | */ | |
f1696ee3 | 371 | strbuf_init(&buffer, 8192); |
74986462 | 372 | |
386cc8b0 NTND |
373 | i = 0; |
374 | while (i < entries) { | |
9c5e6c80 | 375 | const struct cache_entry *ce = cache[i]; |
c041d54a | 376 | struct cache_tree_sub *sub = NULL; |
74986462 JH |
377 | const char *path, *slash; |
378 | int pathlen, entlen; | |
6dcb4625 | 379 | const struct object_id *oid; |
74986462 | 380 | unsigned mode; |
4ed115e9 | 381 | int expected_missing = 0; |
6d6a782f | 382 | int contains_ita = 0; |
2f215ff1 | 383 | int ce_missing_ok; |
74986462 JH |
384 | |
385 | path = ce->name; | |
386 | pathlen = ce_namelen(ce); | |
387 | if (pathlen <= baselen || memcmp(base, path, baselen)) | |
388 | break; /* at the end of this level */ | |
389 | ||
390 | slash = strchr(path + baselen, '/'); | |
391 | if (slash) { | |
392 | entlen = slash - (path + baselen); | |
393 | sub = find_subtree(it, path + baselen, entlen, 0); | |
394 | if (!sub) | |
395 | die("cache-tree.c: '%.*s' in '%s' not found", | |
396 | entlen, path + baselen, path); | |
3cf773e4 | 397 | i += sub->count; |
6dcb4625 | 398 | oid = &sub->cache_tree->oid; |
74986462 | 399 | mode = S_IFDIR; |
6d6a782f NTND |
400 | contains_ita = sub->cache_tree->entry_count < 0; |
401 | if (contains_ita) { | |
eec3e7e4 | 402 | to_invalidate = 1; |
4ed115e9 JH |
403 | expected_missing = 1; |
404 | } | |
74986462 JH |
405 | } |
406 | else { | |
6dcb4625 | 407 | oid = &ce->oid; |
7a51ed66 | 408 | mode = ce->ce_mode; |
74986462 | 409 | entlen = pathlen - baselen; |
386cc8b0 | 410 | i++; |
74986462 | 411 | } |
a96d3cc3 | 412 | |
2f215ff1 | 413 | ce_missing_ok = mode == S_IFGITLINK || missing_ok || |
d3da223f | 414 | !must_check_existence(ce); |
6dcb4625 | 415 | if (is_null_oid(oid) || |
2f215ff1 | 416 | (!ce_missing_ok && !has_object_file(oid))) { |
b6b56ace | 417 | strbuf_release(&buffer); |
4ed115e9 JH |
418 | if (expected_missing) |
419 | return -1; | |
a3883734 | 420 | return error("invalid object %06o %s for '%.*s'", |
6dcb4625 | 421 | mode, oid_to_hex(oid), entlen+baselen, path); |
b6b56ace | 422 | } |
74986462 | 423 | |
3cf773e4 NTND |
424 | /* |
425 | * CE_REMOVE entries are removed before the index is | |
426 | * written to disk. Skip them to remain consistent | |
427 | * with the future on-disk index. | |
428 | */ | |
429 | if (ce->ce_flags & CE_REMOVE) { | |
430 | *skip_count = *skip_count + 1; | |
431 | continue; | |
432 | } | |
433 | ||
eec3e7e4 NTND |
434 | /* |
435 | * CE_INTENT_TO_ADD entries exist on on-disk index but | |
436 | * they are not part of generated trees. Invalidate up | |
437 | * to root to force cache-tree users to read elsewhere. | |
438 | */ | |
c041d54a | 439 | if (!sub && ce_intent_to_add(ce)) { |
eec3e7e4 | 440 | to_invalidate = 1; |
3cf773e4 | 441 | continue; |
eec3e7e4 | 442 | } |
74986462 | 443 | |
6d6a782f NTND |
444 | /* |
445 | * "sub" can be an empty tree if all subentries are i-t-a. | |
446 | */ | |
a0554934 | 447 | if (contains_ita && is_empty_tree_oid(oid)) |
6d6a782f NTND |
448 | continue; |
449 | ||
5242bcbb PH |
450 | strbuf_grow(&buffer, entlen + 100); |
451 | strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0'); | |
6dcb4625 | 452 | strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz); |
74986462 | 453 | |
e9b9cc56 | 454 | #if DEBUG_CACHE_TREE |
00703e6d | 455 | fprintf(stderr, "cache-tree update-one %o %.*s\n", |
74986462 JH |
456 | mode, entlen, path + baselen); |
457 | #endif | |
458 | } | |
459 | ||
aecf567c | 460 | if (repair) { |
f070facc | 461 | struct object_id oid; |
2dcde20e | 462 | hash_object_file(the_hash_algo, buffer.buf, buffer.len, |
44439c1c | 463 | OBJ_TREE, &oid); |
f981ec18 | 464 | if (has_object_file_with_flags(&oid, OBJECT_INFO_SKIP_FETCH_OBJECT)) |
f070facc | 465 | oidcpy(&it->oid, &oid); |
aecf567c DT |
466 | else |
467 | to_invalidate = 1; | |
a09c985e | 468 | } else if (dryrun) { |
2dcde20e | 469 | hash_object_file(the_hash_algo, buffer.buf, buffer.len, |
44439c1c | 470 | OBJ_TREE, &it->oid); |
c80d226a | 471 | } else if (write_object_file_flags(buffer.buf, buffer.len, OBJ_TREE, |
4ef91a2d ÆAB |
472 | &it->oid, flags & WRITE_TREE_SILENT |
473 | ? HASH_SILENT : 0)) { | |
edae5f0c JH |
474 | strbuf_release(&buffer); |
475 | return -1; | |
476 | } | |
477 | ||
5242bcbb | 478 | strbuf_release(&buffer); |
eec3e7e4 | 479 | it->entry_count = to_invalidate ? -1 : i - *skip_count; |
e9b9cc56 | 480 | #if DEBUG_CACHE_TREE |
00703e6d | 481 | fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n", |
74986462 | 482 | it->entry_count, it->subtree_nr, |
e0a92804 | 483 | oid_to_hex(&it->oid)); |
74986462 JH |
484 | #endif |
485 | return i; | |
486 | } | |
487 | ||
d0cfc3e8 | 488 | int cache_tree_update(struct index_state *istate, int flags) |
74986462 | 489 | { |
fb088264 DS |
490 | int skip, i; |
491 | ||
8d87e338 | 492 | i = verify_cache(istate, flags); |
d0cfc3e8 | 493 | |
74986462 JH |
494 | if (i) |
495 | return i; | |
fb088264 DS |
496 | |
497 | if (!istate->cache_tree) | |
498 | istate->cache_tree = cache_tree(); | |
499 | ||
d3da223f JT |
500 | if (!(flags & WRITE_TREE_MISSING_OK) && has_promisor_remote()) |
501 | prefetch_cache_entries(istate, must_check_existence); | |
502 | ||
0d1ed596 | 503 | trace_performance_enter(); |
fa7ca5d4 | 504 | trace2_region_enter("cache_tree", "update", the_repository); |
4d33e2ba | 505 | begin_odb_transaction(); |
fb088264 DS |
506 | i = update_one(istate->cache_tree, istate->cache, istate->cache_nr, |
507 | "", 0, &skip, flags); | |
4d33e2ba | 508 | end_odb_transaction(); |
fa7ca5d4 | 509 | trace2_region_leave("cache_tree", "update", the_repository); |
0d1ed596 | 510 | trace_performance_leave("cache_tree_update"); |
74986462 JH |
511 | if (i < 0) |
512 | return i; | |
d0cfc3e8 | 513 | istate->cache_changed |= CACHE_TREE_CHANGED; |
74986462 JH |
514 | return 0; |
515 | } | |
516 | ||
1dffb8fa | 517 | static void write_one(struct strbuf *buffer, struct cache_tree *it, |
ec36c42a | 518 | const char *path, int pathlen) |
74986462 JH |
519 | { |
520 | int i; | |
521 | ||
522 | /* One "cache-tree" entry consists of the following: | |
523 | * path (NUL terminated) | |
524 | * entry_count, subtree_nr ("%d %d\n") | |
525 | * tree-sha1 (missing if invalid) | |
526 | * subtree_nr "cache-tree" entries for subtrees. | |
527 | */ | |
5242bcbb PH |
528 | strbuf_grow(buffer, pathlen + 100); |
529 | strbuf_add(buffer, path, pathlen); | |
530 | strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr); | |
74986462 | 531 | |
e9b9cc56 | 532 | #if DEBUG_CACHE_TREE |
74986462 JH |
533 | if (0 <= it->entry_count) |
534 | fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n", | |
535 | pathlen, path, it->entry_count, it->subtree_nr, | |
e0a92804 | 536 | oid_to_hex(&it->oid)); |
74986462 JH |
537 | else |
538 | fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n", | |
539 | pathlen, path, it->subtree_nr); | |
540 | #endif | |
541 | ||
542 | if (0 <= it->entry_count) { | |
6dcb4625 | 543 | strbuf_add(buffer, it->oid.hash, the_hash_algo->rawsz); |
74986462 JH |
544 | } |
545 | for (i = 0; i < it->subtree_nr; i++) { | |
546 | struct cache_tree_sub *down = it->down[i]; | |
61fa3097 JH |
547 | if (i) { |
548 | struct cache_tree_sub *prev = it->down[i-1]; | |
549 | if (subtree_name_cmp(down->name, down->namelen, | |
550 | prev->name, prev->namelen) <= 0) | |
551 | die("fatal - unsorted cache subtree"); | |
552 | } | |
1dffb8fa | 553 | write_one(buffer, down->cache_tree, down->name, down->namelen); |
74986462 | 554 | } |
74986462 JH |
555 | } |
556 | ||
1dffb8fa | 557 | void cache_tree_write(struct strbuf *sb, struct cache_tree *root) |
74986462 | 558 | { |
4c3e1872 | 559 | trace2_region_enter("cache_tree", "write", the_repository); |
1dffb8fa | 560 | write_one(sb, root, "", 0); |
4c3e1872 | 561 | trace2_region_leave("cache_tree", "write", the_repository); |
74986462 JH |
562 | } |
563 | ||
564 | static struct cache_tree *read_one(const char **buffer, unsigned long *size_p) | |
565 | { | |
566 | const char *buf = *buffer; | |
567 | unsigned long size = *size_p; | |
0111ea38 JS |
568 | const char *cp; |
569 | char *ep; | |
74986462 JH |
570 | struct cache_tree *it; |
571 | int i, subtree_nr; | |
6dcb4625 | 572 | const unsigned rawsz = the_hash_algo->rawsz; |
74986462 JH |
573 | |
574 | it = NULL; | |
575 | /* skip name, but make sure name exists */ | |
576 | while (size && *buf) { | |
577 | size--; | |
578 | buf++; | |
579 | } | |
580 | if (!size) | |
581 | goto free_return; | |
582 | buf++; size--; | |
583 | it = cache_tree(); | |
0111ea38 JS |
584 | |
585 | cp = buf; | |
586 | it->entry_count = strtol(cp, &ep, 10); | |
587 | if (cp == ep) | |
588 | goto free_return; | |
589 | cp = ep; | |
590 | subtree_nr = strtol(cp, &ep, 10); | |
591 | if (cp == ep) | |
74986462 JH |
592 | goto free_return; |
593 | while (size && *buf && *buf != '\n') { | |
594 | size--; | |
595 | buf++; | |
596 | } | |
597 | if (!size) | |
598 | goto free_return; | |
599 | buf++; size--; | |
600 | if (0 <= it->entry_count) { | |
6dcb4625 | 601 | if (size < rawsz) |
74986462 | 602 | goto free_return; |
69d12425 | 603 | oidread(&it->oid, (const unsigned char *)buf); |
6dcb4625 | 604 | buf += rawsz; |
605 | size -= rawsz; | |
74986462 JH |
606 | } |
607 | ||
e9b9cc56 | 608 | #if DEBUG_CACHE_TREE |
74986462 JH |
609 | if (0 <= it->entry_count) |
610 | fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n", | |
611 | *buffer, it->entry_count, subtree_nr, | |
e0a92804 | 612 | oid_to_hex(&it->oid)); |
74986462 JH |
613 | else |
614 | fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n", | |
615 | *buffer, subtree_nr); | |
616 | #endif | |
617 | ||
618 | /* | |
619 | * Just a heuristic -- we do not add directories that often but | |
620 | * we do not want to have to extend it immediately when we do, | |
621 | * hence +2. | |
622 | */ | |
623 | it->subtree_alloc = subtree_nr + 2; | |
ca56dadb | 624 | CALLOC_ARRAY(it->down, it->subtree_alloc); |
74986462 JH |
625 | for (i = 0; i < subtree_nr; i++) { |
626 | /* read each subtree */ | |
627 | struct cache_tree *sub; | |
61fa3097 | 628 | struct cache_tree_sub *subtree; |
74986462 | 629 | const char *name = buf; |
7927a55d | 630 | |
74986462 JH |
631 | sub = read_one(&buf, &size); |
632 | if (!sub) | |
633 | goto free_return; | |
7927a55d | 634 | subtree = cache_tree_sub(it, name); |
61fa3097 | 635 | subtree->cache_tree = sub; |
74986462 JH |
636 | } |
637 | if (subtree_nr != it->subtree_nr) | |
638 | die("cache-tree: internal error"); | |
639 | *buffer = buf; | |
640 | *size_p = size; | |
641 | return it; | |
642 | ||
643 | free_return: | |
bad68ec9 | 644 | cache_tree_free(&it); |
74986462 JH |
645 | return NULL; |
646 | } | |
647 | ||
bad68ec9 | 648 | struct cache_tree *cache_tree_read(const char *buffer, unsigned long size) |
74986462 | 649 | { |
4c3e1872 DS |
650 | struct cache_tree *result; |
651 | ||
bad68ec9 | 652 | if (buffer[0]) |
74986462 | 653 | return NULL; /* not the whole tree */ |
4c3e1872 DS |
654 | |
655 | trace2_region_enter("cache_tree", "read", the_repository); | |
656 | result = read_one(&buffer, &size); | |
657 | trace2_region_leave("cache_tree", "read", the_repository); | |
658 | ||
659 | return result; | |
74986462 | 660 | } |
6bd20358 | 661 | |
7ba04d9f | 662 | static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path) |
6bd20358 | 663 | { |
b87fc964 JH |
664 | if (!it) |
665 | return NULL; | |
6bd20358 JH |
666 | while (*path) { |
667 | const char *slash; | |
668 | struct cache_tree_sub *sub; | |
669 | ||
17e22ddc | 670 | slash = strchrnul(path, '/'); |
79192b87 MH |
671 | /* |
672 | * Between path and slash is the name of the subtree | |
673 | * to look for. | |
6bd20358 JH |
674 | */ |
675 | sub = find_subtree(it, path, slash - path, 0); | |
676 | if (!sub) | |
677 | return NULL; | |
678 | it = sub->cache_tree; | |
3491047e | 679 | |
6bd20358 | 680 | path = slash; |
3491047e MH |
681 | while (*path == '/') |
682 | path++; | |
6bd20358 JH |
683 | } |
684 | return it; | |
685 | } | |
45525bd0 | 686 | |
724dd767 EN |
687 | static int write_index_as_tree_internal(struct object_id *oid, |
688 | struct index_state *index_state, | |
689 | int cache_tree_valid, | |
690 | int flags, | |
691 | const char *prefix) | |
692 | { | |
693 | if (flags & WRITE_TREE_IGNORE_CACHE_TREE) { | |
694 | cache_tree_free(&index_state->cache_tree); | |
695 | cache_tree_valid = 0; | |
696 | } | |
697 | ||
724dd767 EN |
698 | if (!cache_tree_valid && cache_tree_update(index_state, flags) < 0) |
699 | return WRITE_TREE_UNMERGED_INDEX; | |
700 | ||
701 | if (prefix) { | |
702 | struct cache_tree *subtree; | |
703 | subtree = cache_tree_find(index_state->cache_tree, prefix); | |
704 | if (!subtree) | |
705 | return WRITE_TREE_PREFIX_ERROR; | |
706 | oidcpy(oid, &subtree->oid); | |
707 | } | |
708 | else | |
709 | oidcpy(oid, &index_state->cache_tree->oid); | |
710 | ||
711 | return 0; | |
712 | } | |
713 | ||
714 | struct tree* write_in_core_index_as_tree(struct repository *repo) { | |
715 | struct object_id o; | |
716 | int was_valid, ret; | |
717 | ||
718 | struct index_state *index_state = repo->index; | |
719 | was_valid = index_state->cache_tree && | |
720 | cache_tree_fully_valid(index_state->cache_tree); | |
721 | ||
722 | ret = write_index_as_tree_internal(&o, index_state, was_valid, 0, NULL); | |
723 | if (ret == WRITE_TREE_UNMERGED_INDEX) { | |
724 | int i; | |
6d40f0ad | 725 | bug("there are unmerged index entries:"); |
724dd767 EN |
726 | for (i = 0; i < index_state->cache_nr; i++) { |
727 | const struct cache_entry *ce = index_state->cache[i]; | |
728 | if (ce_stage(ce)) | |
6d40f0ad ÆAB |
729 | bug("%d %.*s", ce_stage(ce), |
730 | (int)ce_namelen(ce), ce->name); | |
724dd767 | 731 | } |
6d40f0ad | 732 | BUG("unmerged index entries when writing in-core index"); |
724dd767 EN |
733 | } |
734 | ||
735 | return lookup_tree(repo, &index_state->cache_tree->oid); | |
736 | } | |
737 | ||
738 | ||
fc5cb99f | 739 | int write_index_as_tree(struct object_id *oid, struct index_state *index_state, const char *index_path, int flags, const char *prefix) |
45525bd0 | 740 | { |
2954e5ec | 741 | int entries, was_valid; |
bfffb48c | 742 | struct lock_file lock_file = LOCK_INIT; |
724dd767 | 743 | int ret; |
45525bd0 | 744 | |
2954e5ec | 745 | hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR); |
45525bd0 | 746 | |
a125a223 | 747 | entries = read_index_from(index_state, index_path, get_git_dir()); |
c82c75b9 JK |
748 | if (entries < 0) { |
749 | ret = WRITE_TREE_UNREADABLE_INDEX; | |
750 | goto out; | |
751 | } | |
45525bd0 | 752 | |
724dd767 EN |
753 | was_valid = !(flags & WRITE_TREE_IGNORE_CACHE_TREE) && |
754 | index_state->cache_tree && | |
755 | cache_tree_fully_valid(index_state->cache_tree); | |
45525bd0 | 756 | |
724dd767 EN |
757 | ret = write_index_as_tree_internal(oid, index_state, was_valid, flags, |
758 | prefix); | |
759 | if (!ret && !was_valid) { | |
2954e5ec | 760 | write_locked_index(index_state, &lock_file, COMMIT_LOCK); |
45525bd0 JH |
761 | /* Not being able to write is fine -- we are only interested |
762 | * in updating the cache-tree part, and if the next caller | |
763 | * ends up using the old index with unupdated cache-tree part | |
764 | * it misses the work we did here, but that is just a | |
765 | * performance penalty and not a big deal. | |
766 | */ | |
767 | } | |
768 | ||
c82c75b9 | 769 | out: |
2954e5ec | 770 | rollback_lock_file(&lock_file); |
c82c75b9 | 771 | return ret; |
45525bd0 | 772 | } |
b9d37a54 | 773 | |
20ec2d03 VD |
774 | static void prime_cache_tree_sparse_dir(struct cache_tree *it, |
775 | struct tree *tree) | |
776 | { | |
777 | ||
778 | oidcpy(&it->oid, &tree->object.oid); | |
779 | it->entry_count = 1; | |
780 | } | |
781 | ||
c207e9e1 NTND |
782 | static void prime_cache_tree_rec(struct repository *r, |
783 | struct cache_tree *it, | |
20ec2d03 VD |
784 | struct tree *tree, |
785 | struct strbuf *tree_path) | |
b9d37a54 JH |
786 | { |
787 | struct tree_desc desc; | |
788 | struct name_entry entry; | |
789 | int cnt; | |
20ec2d03 | 790 | int base_path_len = tree_path->len; |
b9d37a54 | 791 | |
e0a92804 | 792 | oidcpy(&it->oid, &tree->object.oid); |
20ec2d03 | 793 | |
b9d37a54 JH |
794 | init_tree_desc(&desc, tree->buffer, tree->size); |
795 | cnt = 0; | |
796 | while (tree_entry(&desc, &entry)) { | |
797 | if (!S_ISDIR(entry.mode)) | |
798 | cnt++; | |
799 | else { | |
800 | struct cache_tree_sub *sub; | |
ea82b2a0 | 801 | struct tree *subtree = lookup_tree(r, &entry.oid); |
20ec2d03 | 802 | |
b9d37a54 JH |
803 | if (!subtree->object.parsed) |
804 | parse_tree(subtree); | |
805 | sub = cache_tree_sub(it, entry.path); | |
806 | sub->cache_tree = cache_tree(); | |
20ec2d03 VD |
807 | |
808 | /* | |
809 | * Recursively-constructed subtree path is only needed when working | |
810 | * in a sparse index (where it's used to determine whether the | |
811 | * subtree is a sparse directory in the index). | |
812 | */ | |
813 | if (r->index->sparse_index) { | |
814 | strbuf_setlen(tree_path, base_path_len); | |
815 | strbuf_grow(tree_path, base_path_len + entry.pathlen + 1); | |
816 | strbuf_add(tree_path, entry.path, entry.pathlen); | |
817 | strbuf_addch(tree_path, '/'); | |
818 | } | |
819 | ||
820 | /* | |
821 | * If a sparse index is in use, the directory being processed may be | |
822 | * sparse. To confirm that, we can check whether an entry with that | |
823 | * exact name exists in the index. If it does, the created subtree | |
824 | * should be sparse. Otherwise, cache tree expansion should continue | |
825 | * as normal. | |
826 | */ | |
827 | if (r->index->sparse_index && | |
828 | index_entry_exists(r->index, tree_path->buf, tree_path->len)) | |
829 | prime_cache_tree_sparse_dir(sub->cache_tree, subtree); | |
830 | else | |
831 | prime_cache_tree_rec(r, sub->cache_tree, subtree, tree_path); | |
b9d37a54 JH |
832 | cnt += sub->cache_tree->entry_count; |
833 | } | |
834 | } | |
20ec2d03 | 835 | |
b9d37a54 JH |
836 | it->entry_count = cnt; |
837 | } | |
838 | ||
c207e9e1 NTND |
839 | void prime_cache_tree(struct repository *r, |
840 | struct index_state *istate, | |
841 | struct tree *tree) | |
b9d37a54 | 842 | { |
20ec2d03 VD |
843 | struct strbuf tree_path = STRBUF_INIT; |
844 | ||
0e5c9502 | 845 | trace2_region_enter("cache-tree", "prime_cache_tree", the_repository); |
e6c286e8 NTND |
846 | cache_tree_free(&istate->cache_tree); |
847 | istate->cache_tree = cache_tree(); | |
0e5c9502 | 848 | |
20ec2d03 VD |
849 | prime_cache_tree_rec(r, istate->cache_tree, tree, &tree_path); |
850 | strbuf_release(&tree_path); | |
e6c286e8 | 851 | istate->cache_changed |= CACHE_TREE_CHANGED; |
0e5c9502 | 852 | trace2_region_leave("cache-tree", "prime_cache_tree", the_repository); |
b9d37a54 | 853 | } |
b65982b6 JH |
854 | |
855 | /* | |
856 | * find the cache_tree that corresponds to the current level without | |
857 | * exploding the full path into textual form. The root of the | |
858 | * cache tree is given as "root", and our current level is "info". | |
859 | * (1) When at root level, info->prev is NULL, so it is "root" itself. | |
860 | * (2) Otherwise, find the cache_tree that corresponds to one level | |
861 | * above us, and find ourselves in there. | |
862 | */ | |
863 | static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root, | |
864 | struct traverse_info *info) | |
865 | { | |
866 | struct cache_tree *our_parent; | |
867 | ||
868 | if (!info->prev) | |
869 | return root; | |
870 | our_parent = find_cache_tree_from_traversal(root, info->prev); | |
90553847 | 871 | return cache_tree_find(our_parent, info->name); |
b65982b6 JH |
872 | } |
873 | ||
874 | int cache_tree_matches_traversal(struct cache_tree *root, | |
875 | struct name_entry *ent, | |
876 | struct traverse_info *info) | |
877 | { | |
878 | struct cache_tree *it; | |
879 | ||
880 | it = find_cache_tree_from_traversal(root, info); | |
881 | it = cache_tree_find(it, ent->path); | |
ea82b2a0 | 882 | if (it && it->entry_count > 0 && oideq(&ent->oid, &it->oid)) |
b65982b6 JH |
883 | return it->entry_count; |
884 | return 0; | |
885 | } | |
996277c5 | 886 | |
9ad2d5ea DS |
887 | static void verify_one_sparse(struct repository *r, |
888 | struct index_state *istate, | |
889 | struct cache_tree *it, | |
890 | struct strbuf *path, | |
891 | int pos) | |
892 | { | |
893 | struct cache_entry *ce = istate->cache[pos]; | |
894 | ||
895 | if (!S_ISSPARSEDIR(ce->ce_mode)) | |
896 | BUG("directory '%s' is present in index, but not sparse", | |
897 | path->buf); | |
898 | } | |
899 | ||
f751097b PW |
900 | /* |
901 | * Returns: | |
902 | * 0 - Verification completed. | |
903 | * 1 - Restart verification - a call to ensure_full_index() freed the cache | |
904 | * tree that is being verified and verification needs to be restarted from | |
905 | * the new toplevel cache tree. | |
906 | */ | |
907 | static int verify_one(struct repository *r, | |
908 | struct index_state *istate, | |
909 | struct cache_tree *it, | |
910 | struct strbuf *path) | |
4592e608 NTND |
911 | { |
912 | int i, pos, len = path->len; | |
913 | struct strbuf tree_buf = STRBUF_INIT; | |
914 | struct object_id new_oid; | |
915 | ||
916 | for (i = 0; i < it->subtree_nr; i++) { | |
917 | strbuf_addf(path, "%s/", it->down[i]->name); | |
f751097b PW |
918 | if (verify_one(r, istate, it->down[i]->cache_tree, path)) |
919 | return 1; | |
4592e608 NTND |
920 | strbuf_setlen(path, len); |
921 | } | |
922 | ||
923 | if (it->entry_count < 0 || | |
924 | /* no verification on tests (t7003) that replace trees */ | |
c207e9e1 | 925 | lookup_replace_object(r, &it->oid) != &it->oid) |
f751097b | 926 | return 0; |
4592e608 NTND |
927 | |
928 | if (path->len) { | |
f751097b PW |
929 | /* |
930 | * If the index is sparse and the cache tree is not | |
931 | * index_name_pos() may trigger ensure_full_index() which will | |
932 | * free the tree that is being verified. | |
933 | */ | |
934 | int is_sparse = istate->sparse_index; | |
4592e608 | 935 | pos = index_name_pos(istate, path->buf, path->len); |
f751097b PW |
936 | if (is_sparse && !istate->sparse_index) |
937 | return 1; | |
9ad2d5ea DS |
938 | |
939 | if (pos >= 0) { | |
940 | verify_one_sparse(r, istate, it, path, pos); | |
f751097b | 941 | return 0; |
9ad2d5ea DS |
942 | } |
943 | ||
4592e608 NTND |
944 | pos = -pos - 1; |
945 | } else { | |
946 | pos = 0; | |
947 | } | |
948 | ||
949 | i = 0; | |
950 | while (i < it->entry_count) { | |
951 | struct cache_entry *ce = istate->cache[pos + i]; | |
952 | const char *slash; | |
953 | struct cache_tree_sub *sub = NULL; | |
954 | const struct object_id *oid; | |
955 | const char *name; | |
956 | unsigned mode; | |
957 | int entlen; | |
958 | ||
959 | if (ce->ce_flags & (CE_STAGEMASK | CE_INTENT_TO_ADD | CE_REMOVE)) | |
960 | BUG("%s with flags 0x%x should not be in cache-tree", | |
961 | ce->name, ce->ce_flags); | |
962 | name = ce->name + path->len; | |
963 | slash = strchr(name, '/'); | |
964 | if (slash) { | |
965 | entlen = slash - name; | |
966 | sub = find_subtree(it, ce->name + path->len, entlen, 0); | |
967 | if (!sub || sub->cache_tree->entry_count < 0) | |
968 | BUG("bad subtree '%.*s'", entlen, name); | |
969 | oid = &sub->cache_tree->oid; | |
970 | mode = S_IFDIR; | |
971 | i += sub->cache_tree->entry_count; | |
972 | } else { | |
973 | oid = &ce->oid; | |
974 | mode = ce->ce_mode; | |
975 | entlen = ce_namelen(ce) - path->len; | |
976 | i++; | |
977 | } | |
978 | strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0'); | |
a6519467 | 979 | strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz); |
4592e608 | 980 | } |
44439c1c | 981 | hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, OBJ_TREE, |
2dcde20e | 982 | &new_oid); |
e43d2dcc | 983 | if (!oideq(&new_oid, &it->oid)) |
4592e608 NTND |
984 | BUG("cache-tree for path %.*s does not match. " |
985 | "Expected %s got %s", len, path->buf, | |
986 | oid_to_hex(&new_oid), oid_to_hex(&it->oid)); | |
987 | strbuf_setlen(path, len); | |
988 | strbuf_release(&tree_buf); | |
f751097b | 989 | return 0; |
4592e608 NTND |
990 | } |
991 | ||
c207e9e1 | 992 | void cache_tree_verify(struct repository *r, struct index_state *istate) |
4592e608 NTND |
993 | { |
994 | struct strbuf path = STRBUF_INIT; | |
995 | ||
996 | if (!istate->cache_tree) | |
997 | return; | |
f751097b PW |
998 | if (verify_one(r, istate, istate->cache_tree, &path)) { |
999 | strbuf_reset(&path); | |
1000 | if (verify_one(r, istate, istate->cache_tree, &path)) | |
1001 | BUG("ensure_full_index() called twice while verifying cache tree"); | |
1002 | } | |
4592e608 NTND |
1003 | strbuf_release(&path); |
1004 | } |