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