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