Doc: fix Asciidoctor css workaround
[alt-git.git] / cache-tree.c
blob80ca8324773d2dc352bf4d47cfc4d0036316180e
1 #include "git-compat-util.h"
2 #include "environment.h"
3 #include "hex.h"
4 #include "lockfile.h"
5 #include "tree.h"
6 #include "tree-walk.h"
7 #include "cache-tree.h"
8 #include "bulk-checkin.h"
9 #include "object-file.h"
10 #include "object-store-ll.h"
11 #include "read-cache-ll.h"
12 #include "replace-object.h"
13 #include "promisor-remote.h"
14 #include "trace.h"
15 #include "trace2.h"
17 #ifndef DEBUG_CACHE_TREE
18 #define DEBUG_CACHE_TREE 0
19 #endif
21 struct cache_tree *cache_tree(void)
23 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
24 it->entry_count = -1;
25 return it;
28 void cache_tree_free(struct cache_tree **it_p)
30 int i;
31 struct cache_tree *it = *it_p;
33 if (!it)
34 return;
35 for (i = 0; i < it->subtree_nr; i++)
36 if (it->down[i]) {
37 cache_tree_free(&it->down[i]->cache_tree);
38 free(it->down[i]);
40 free(it->down);
41 free(it);
42 *it_p = NULL;
45 static int subtree_name_cmp(const char *one, int onelen,
46 const char *two, int twolen)
48 if (onelen < twolen)
49 return -1;
50 if (twolen < onelen)
51 return 1;
52 return memcmp(one, two, onelen);
55 int cache_tree_subtree_pos(struct cache_tree *it, const char *path, int pathlen)
57 struct cache_tree_sub **down = it->down;
58 int lo, hi;
59 lo = 0;
60 hi = it->subtree_nr;
61 while (lo < hi) {
62 int mi = lo + (hi - lo) / 2;
63 struct cache_tree_sub *mdl = down[mi];
64 int cmp = subtree_name_cmp(path, pathlen,
65 mdl->name, mdl->namelen);
66 if (!cmp)
67 return mi;
68 if (cmp < 0)
69 hi = mi;
70 else
71 lo = mi + 1;
73 return -lo-1;
76 static struct cache_tree_sub *find_subtree(struct cache_tree *it,
77 const char *path,
78 int pathlen,
79 int create)
81 struct cache_tree_sub *down;
82 int pos = cache_tree_subtree_pos(it, path, pathlen);
83 if (0 <= pos)
84 return it->down[pos];
85 if (!create)
86 return NULL;
88 pos = -pos-1;
89 ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
90 it->subtree_nr++;
92 FLEX_ALLOC_MEM(down, name, path, pathlen);
93 down->cache_tree = NULL;
94 down->namelen = pathlen;
96 if (pos < it->subtree_nr)
97 MOVE_ARRAY(it->down + pos + 1, it->down + pos,
98 it->subtree_nr - pos - 1);
99 it->down[pos] = down;
100 return down;
103 struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
105 int pathlen = strlen(path);
106 return find_subtree(it, path, pathlen, 1);
109 static int do_invalidate_path(struct cache_tree *it, const char *path)
111 /* a/b/c
112 * ==> invalidate self
113 * ==> find "a", have it invalidate "b/c"
115 * ==> invalidate self
116 * ==> if "a" exists as a subtree, remove it.
118 const char *slash;
119 int namelen;
120 struct cache_tree_sub *down;
122 #if DEBUG_CACHE_TREE
123 fprintf(stderr, "cache-tree invalidate <%s>\n", path);
124 #endif
126 if (!it)
127 return 0;
128 slash = strchrnul(path, '/');
129 namelen = slash - path;
130 it->entry_count = -1;
131 if (!*slash) {
132 int pos;
133 pos = cache_tree_subtree_pos(it, path, namelen);
134 if (0 <= pos) {
135 cache_tree_free(&it->down[pos]->cache_tree);
136 free(it->down[pos]);
137 /* 0 1 2 3 4 5
138 * ^ ^subtree_nr = 6
139 * pos
140 * move 4 and 5 up one place (2 entries)
141 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
143 MOVE_ARRAY(it->down + pos, it->down + pos + 1,
144 it->subtree_nr - pos - 1);
145 it->subtree_nr--;
147 return 1;
149 down = find_subtree(it, path, namelen, 0);
150 if (down)
151 do_invalidate_path(down->cache_tree, slash + 1);
152 return 1;
155 void cache_tree_invalidate_path(struct index_state *istate, const char *path)
157 if (do_invalidate_path(istate->cache_tree, path))
158 istate->cache_changed |= CACHE_TREE_CHANGED;
161 static int verify_cache(struct index_state *istate, int flags)
163 unsigned i, funny;
164 int silent = flags & WRITE_TREE_SILENT;
166 /* Verify that the tree is merged */
167 funny = 0;
168 for (i = 0; i < istate->cache_nr; i++) {
169 const struct cache_entry *ce = istate->cache[i];
170 if (ce_stage(ce)) {
171 if (silent)
172 return -1;
173 if (10 < ++funny) {
174 fprintf(stderr, "...\n");
175 break;
177 fprintf(stderr, "%s: unmerged (%s)\n",
178 ce->name, oid_to_hex(&ce->oid));
181 if (funny)
182 return -1;
184 /* Also verify that the cache does not have path and path/file
185 * at the same time. At this point we know the cache has only
186 * stage 0 entries.
188 funny = 0;
189 for (i = 0; i + 1 < istate->cache_nr; i++) {
190 /* path/file always comes after path because of the way
191 * the cache is sorted. Also path can appear only once,
192 * which means conflicting one would immediately follow.
194 const struct cache_entry *this_ce = istate->cache[i];
195 const struct cache_entry *next_ce = istate->cache[i + 1];
196 const char *this_name = this_ce->name;
197 const char *next_name = next_ce->name;
198 int this_len = ce_namelen(this_ce);
199 if (this_len < ce_namelen(next_ce) &&
200 next_name[this_len] == '/' &&
201 strncmp(this_name, next_name, this_len) == 0) {
202 if (10 < ++funny) {
203 fprintf(stderr, "...\n");
204 break;
206 fprintf(stderr, "You have both %s and %s\n",
207 this_name, next_name);
210 if (funny)
211 return -1;
212 return 0;
215 static void discard_unused_subtrees(struct cache_tree *it)
217 struct cache_tree_sub **down = it->down;
218 int nr = it->subtree_nr;
219 int dst, src;
220 for (dst = src = 0; src < nr; src++) {
221 struct cache_tree_sub *s = down[src];
222 if (s->used)
223 down[dst++] = s;
224 else {
225 cache_tree_free(&s->cache_tree);
226 free(s);
227 it->subtree_nr--;
232 int cache_tree_fully_valid(struct cache_tree *it)
234 int i;
235 if (!it)
236 return 0;
237 if (it->entry_count < 0 || !repo_has_object_file(the_repository, &it->oid))
238 return 0;
239 for (i = 0; i < it->subtree_nr; i++) {
240 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
241 return 0;
243 return 1;
246 static int must_check_existence(const struct cache_entry *ce)
248 return !(repo_has_promisor_remote(the_repository) && ce_skip_worktree(ce));
251 static int update_one(struct cache_tree *it,
252 struct cache_entry **cache,
253 int entries,
254 const char *base,
255 int baselen,
256 int *skip_count,
257 int flags)
259 struct strbuf buffer;
260 int missing_ok = flags & WRITE_TREE_MISSING_OK;
261 int dryrun = flags & WRITE_TREE_DRY_RUN;
262 int repair = flags & WRITE_TREE_REPAIR;
263 int to_invalidate = 0;
264 int i;
266 assert(!(dryrun && repair));
268 *skip_count = 0;
271 * If the first entry of this region is a sparse directory
272 * entry corresponding exactly to 'base', then this cache_tree
273 * struct is a "leaf" in the data structure, pointing to the
274 * tree OID specified in the entry.
276 if (entries > 0) {
277 const struct cache_entry *ce = cache[0];
279 if (S_ISSPARSEDIR(ce->ce_mode) &&
280 ce->ce_namelen == baselen &&
281 !strncmp(ce->name, base, baselen)) {
282 it->entry_count = 1;
283 oidcpy(&it->oid, &ce->oid);
284 return 1;
288 if (0 <= it->entry_count && repo_has_object_file(the_repository, &it->oid))
289 return it->entry_count;
292 * We first scan for subtrees and update them; we start by
293 * marking existing subtrees -- the ones that are unmarked
294 * should not be in the result.
296 for (i = 0; i < it->subtree_nr; i++)
297 it->down[i]->used = 0;
300 * Find the subtrees and update them.
302 i = 0;
303 while (i < entries) {
304 const struct cache_entry *ce = cache[i];
305 struct cache_tree_sub *sub;
306 const char *path, *slash;
307 int pathlen, sublen, subcnt, subskip;
309 path = ce->name;
310 pathlen = ce_namelen(ce);
311 if (pathlen <= baselen || memcmp(base, path, baselen))
312 break; /* at the end of this level */
314 slash = strchr(path + baselen, '/');
315 if (!slash) {
316 i++;
317 continue;
320 * a/bbb/c (base = a/, slash = /c)
321 * ==>
322 * path+baselen = bbb/c, sublen = 3
324 sublen = slash - (path + baselen);
325 sub = find_subtree(it, path + baselen, sublen, 1);
326 if (!sub->cache_tree)
327 sub->cache_tree = cache_tree();
328 subcnt = update_one(sub->cache_tree,
329 cache + i, entries - i,
330 path,
331 baselen + sublen + 1,
332 &subskip,
333 flags);
334 if (subcnt < 0)
335 return subcnt;
336 if (!subcnt)
337 die("index cache-tree records empty sub-tree");
338 i += subcnt;
339 sub->count = subcnt; /* to be used in the next loop */
340 *skip_count += subskip;
341 sub->used = 1;
344 discard_unused_subtrees(it);
347 * Then write out the tree object for this level.
349 strbuf_init(&buffer, 8192);
351 i = 0;
352 while (i < entries) {
353 const struct cache_entry *ce = cache[i];
354 struct cache_tree_sub *sub = NULL;
355 const char *path, *slash;
356 int pathlen, entlen;
357 const struct object_id *oid;
358 unsigned mode;
359 int expected_missing = 0;
360 int contains_ita = 0;
361 int ce_missing_ok;
363 path = ce->name;
364 pathlen = ce_namelen(ce);
365 if (pathlen <= baselen || memcmp(base, path, baselen))
366 break; /* at the end of this level */
368 slash = strchr(path + baselen, '/');
369 if (slash) {
370 entlen = slash - (path + baselen);
371 sub = find_subtree(it, path + baselen, entlen, 0);
372 if (!sub)
373 die("cache-tree.c: '%.*s' in '%s' not found",
374 entlen, path + baselen, path);
375 i += sub->count;
376 oid = &sub->cache_tree->oid;
377 mode = S_IFDIR;
378 contains_ita = sub->cache_tree->entry_count < 0;
379 if (contains_ita) {
380 to_invalidate = 1;
381 expected_missing = 1;
384 else {
385 oid = &ce->oid;
386 mode = ce->ce_mode;
387 entlen = pathlen - baselen;
388 i++;
391 ce_missing_ok = mode == S_IFGITLINK || missing_ok ||
392 !must_check_existence(ce);
393 if (is_null_oid(oid) ||
394 (!ce_missing_ok && !repo_has_object_file(the_repository, oid))) {
395 strbuf_release(&buffer);
396 if (expected_missing)
397 return -1;
398 return error("invalid object %06o %s for '%.*s'",
399 mode, oid_to_hex(oid), entlen+baselen, path);
403 * CE_REMOVE entries are removed before the index is
404 * written to disk. Skip them to remain consistent
405 * with the future on-disk index.
407 if (ce->ce_flags & CE_REMOVE) {
408 *skip_count = *skip_count + 1;
409 continue;
413 * CE_INTENT_TO_ADD entries exist in on-disk index but
414 * they are not part of generated trees. Invalidate up
415 * to root to force cache-tree users to read elsewhere.
417 if (!sub && ce_intent_to_add(ce)) {
418 to_invalidate = 1;
419 continue;
423 * "sub" can be an empty tree if all subentries are i-t-a.
425 if (contains_ita && is_empty_tree_oid(oid))
426 continue;
428 strbuf_grow(&buffer, entlen + 100);
429 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
430 strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz);
432 #if DEBUG_CACHE_TREE
433 fprintf(stderr, "cache-tree update-one %o %.*s\n",
434 mode, entlen, path + baselen);
435 #endif
438 if (repair) {
439 struct object_id oid;
440 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
441 OBJ_TREE, &oid);
442 if (repo_has_object_file_with_flags(the_repository, &oid, OBJECT_INFO_SKIP_FETCH_OBJECT))
443 oidcpy(&it->oid, &oid);
444 else
445 to_invalidate = 1;
446 } else if (dryrun) {
447 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
448 OBJ_TREE, &it->oid);
449 } else if (write_object_file_flags(buffer.buf, buffer.len, OBJ_TREE,
450 &it->oid, flags & WRITE_TREE_SILENT
451 ? HASH_SILENT : 0)) {
452 strbuf_release(&buffer);
453 return -1;
456 strbuf_release(&buffer);
457 it->entry_count = to_invalidate ? -1 : i - *skip_count;
458 #if DEBUG_CACHE_TREE
459 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
460 it->entry_count, it->subtree_nr,
461 oid_to_hex(&it->oid));
462 #endif
463 return i;
466 int cache_tree_update(struct index_state *istate, int flags)
468 int skip, i;
470 i = verify_cache(istate, flags);
472 if (i)
473 return i;
475 if (!istate->cache_tree)
476 istate->cache_tree = cache_tree();
478 if (!(flags & WRITE_TREE_MISSING_OK) && repo_has_promisor_remote(the_repository))
479 prefetch_cache_entries(istate, must_check_existence);
481 trace_performance_enter();
482 trace2_region_enter("cache_tree", "update", the_repository);
483 begin_odb_transaction();
484 i = update_one(istate->cache_tree, istate->cache, istate->cache_nr,
485 "", 0, &skip, flags);
486 end_odb_transaction();
487 trace2_region_leave("cache_tree", "update", the_repository);
488 trace_performance_leave("cache_tree_update");
489 if (i < 0)
490 return i;
491 istate->cache_changed |= CACHE_TREE_CHANGED;
492 return 0;
495 static void write_one(struct strbuf *buffer, struct cache_tree *it,
496 const char *path, int pathlen)
498 int i;
500 /* One "cache-tree" entry consists of the following:
501 * path (NUL terminated)
502 * entry_count, subtree_nr ("%d %d\n")
503 * tree-sha1 (missing if invalid)
504 * subtree_nr "cache-tree" entries for subtrees.
506 strbuf_grow(buffer, pathlen + 100);
507 strbuf_add(buffer, path, pathlen);
508 strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
510 #if DEBUG_CACHE_TREE
511 if (0 <= it->entry_count)
512 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
513 pathlen, path, it->entry_count, it->subtree_nr,
514 oid_to_hex(&it->oid));
515 else
516 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
517 pathlen, path, it->subtree_nr);
518 #endif
520 if (0 <= it->entry_count) {
521 strbuf_add(buffer, it->oid.hash, the_hash_algo->rawsz);
523 for (i = 0; i < it->subtree_nr; i++) {
524 struct cache_tree_sub *down = it->down[i];
525 if (i) {
526 struct cache_tree_sub *prev = it->down[i-1];
527 if (subtree_name_cmp(down->name, down->namelen,
528 prev->name, prev->namelen) <= 0)
529 die("fatal - unsorted cache subtree");
531 write_one(buffer, down->cache_tree, down->name, down->namelen);
535 void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
537 trace2_region_enter("cache_tree", "write", the_repository);
538 write_one(sb, root, "", 0);
539 trace2_region_leave("cache_tree", "write", the_repository);
542 static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
544 const char *buf = *buffer;
545 unsigned long size = *size_p;
546 const char *cp;
547 char *ep;
548 struct cache_tree *it;
549 int i, subtree_nr;
550 const unsigned rawsz = the_hash_algo->rawsz;
552 it = NULL;
553 /* skip name, but make sure name exists */
554 while (size && *buf) {
555 size--;
556 buf++;
558 if (!size)
559 goto free_return;
560 buf++; size--;
561 it = cache_tree();
563 cp = buf;
564 it->entry_count = strtol(cp, &ep, 10);
565 if (cp == ep)
566 goto free_return;
567 cp = ep;
568 subtree_nr = strtol(cp, &ep, 10);
569 if (cp == ep)
570 goto free_return;
571 while (size && *buf && *buf != '\n') {
572 size--;
573 buf++;
575 if (!size)
576 goto free_return;
577 buf++; size--;
578 if (0 <= it->entry_count) {
579 if (size < rawsz)
580 goto free_return;
581 oidread(&it->oid, (const unsigned char *)buf);
582 buf += rawsz;
583 size -= rawsz;
586 #if DEBUG_CACHE_TREE
587 if (0 <= it->entry_count)
588 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
589 *buffer, it->entry_count, subtree_nr,
590 oid_to_hex(&it->oid));
591 else
592 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
593 *buffer, subtree_nr);
594 #endif
597 * Just a heuristic -- we do not add directories that often but
598 * we do not want to have to extend it immediately when we do,
599 * hence +2.
601 it->subtree_alloc = subtree_nr + 2;
602 CALLOC_ARRAY(it->down, it->subtree_alloc);
603 for (i = 0; i < subtree_nr; i++) {
604 /* read each subtree */
605 struct cache_tree *sub;
606 struct cache_tree_sub *subtree;
607 const char *name = buf;
609 sub = read_one(&buf, &size);
610 if (!sub)
611 goto free_return;
612 subtree = cache_tree_sub(it, name);
613 subtree->cache_tree = sub;
615 if (subtree_nr != it->subtree_nr)
616 die("cache-tree: internal error");
617 *buffer = buf;
618 *size_p = size;
619 return it;
621 free_return:
622 cache_tree_free(&it);
623 return NULL;
626 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
628 struct cache_tree *result;
630 if (buffer[0])
631 return NULL; /* not the whole tree */
633 trace2_region_enter("cache_tree", "read", the_repository);
634 result = read_one(&buffer, &size);
635 trace2_region_leave("cache_tree", "read", the_repository);
637 return result;
640 static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
642 if (!it)
643 return NULL;
644 while (*path) {
645 const char *slash;
646 struct cache_tree_sub *sub;
648 slash = strchrnul(path, '/');
650 * Between path and slash is the name of the subtree
651 * to look for.
653 sub = find_subtree(it, path, slash - path, 0);
654 if (!sub)
655 return NULL;
656 it = sub->cache_tree;
658 path = slash;
659 while (*path == '/')
660 path++;
662 return it;
665 static int write_index_as_tree_internal(struct object_id *oid,
666 struct index_state *index_state,
667 int cache_tree_valid,
668 int flags,
669 const char *prefix)
671 if (flags & WRITE_TREE_IGNORE_CACHE_TREE) {
672 cache_tree_free(&index_state->cache_tree);
673 cache_tree_valid = 0;
676 if (!cache_tree_valid && cache_tree_update(index_state, flags) < 0)
677 return WRITE_TREE_UNMERGED_INDEX;
679 if (prefix) {
680 struct cache_tree *subtree;
681 subtree = cache_tree_find(index_state->cache_tree, prefix);
682 if (!subtree)
683 return WRITE_TREE_PREFIX_ERROR;
684 oidcpy(oid, &subtree->oid);
686 else
687 oidcpy(oid, &index_state->cache_tree->oid);
689 return 0;
692 struct tree* write_in_core_index_as_tree(struct repository *repo) {
693 struct object_id o;
694 int was_valid, ret;
696 struct index_state *index_state = repo->index;
697 was_valid = index_state->cache_tree &&
698 cache_tree_fully_valid(index_state->cache_tree);
700 ret = write_index_as_tree_internal(&o, index_state, was_valid, 0, NULL);
701 if (ret == WRITE_TREE_UNMERGED_INDEX) {
702 int i;
703 bug("there are unmerged index entries:");
704 for (i = 0; i < index_state->cache_nr; i++) {
705 const struct cache_entry *ce = index_state->cache[i];
706 if (ce_stage(ce))
707 bug("%d %.*s", ce_stage(ce),
708 (int)ce_namelen(ce), ce->name);
710 BUG("unmerged index entries when writing in-core index");
713 return lookup_tree(repo, &index_state->cache_tree->oid);
717 int write_index_as_tree(struct object_id *oid, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
719 int entries, was_valid;
720 struct lock_file lock_file = LOCK_INIT;
721 int ret;
723 hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
725 entries = read_index_from(index_state, index_path, get_git_dir());
726 if (entries < 0) {
727 ret = WRITE_TREE_UNREADABLE_INDEX;
728 goto out;
731 was_valid = !(flags & WRITE_TREE_IGNORE_CACHE_TREE) &&
732 index_state->cache_tree &&
733 cache_tree_fully_valid(index_state->cache_tree);
735 ret = write_index_as_tree_internal(oid, index_state, was_valid, flags,
736 prefix);
737 if (!ret && !was_valid) {
738 write_locked_index(index_state, &lock_file, COMMIT_LOCK);
739 /* Not being able to write is fine -- we are only interested
740 * in updating the cache-tree part, and if the next caller
741 * ends up using the old index with unupdated cache-tree part
742 * it misses the work we did here, but that is just a
743 * performance penalty and not a big deal.
747 out:
748 rollback_lock_file(&lock_file);
749 return ret;
752 static void prime_cache_tree_sparse_dir(struct cache_tree *it,
753 struct tree *tree)
756 oidcpy(&it->oid, &tree->object.oid);
757 it->entry_count = 1;
760 static void prime_cache_tree_rec(struct repository *r,
761 struct cache_tree *it,
762 struct tree *tree,
763 struct strbuf *tree_path)
765 struct tree_desc desc;
766 struct name_entry entry;
767 int cnt;
768 size_t base_path_len = tree_path->len;
770 oidcpy(&it->oid, &tree->object.oid);
772 init_tree_desc(&desc, tree->buffer, tree->size);
773 cnt = 0;
774 while (tree_entry(&desc, &entry)) {
775 if (!S_ISDIR(entry.mode))
776 cnt++;
777 else {
778 struct cache_tree_sub *sub;
779 struct tree *subtree = lookup_tree(r, &entry.oid);
781 if (parse_tree(subtree) < 0)
782 exit(128);
783 sub = cache_tree_sub(it, entry.path);
784 sub->cache_tree = cache_tree();
787 * Recursively-constructed subtree path is only needed when working
788 * in a sparse index (where it's used to determine whether the
789 * subtree is a sparse directory in the index).
791 if (r->index->sparse_index) {
792 strbuf_setlen(tree_path, base_path_len);
793 strbuf_add(tree_path, entry.path, entry.pathlen);
794 strbuf_addch(tree_path, '/');
798 * If a sparse index is in use, the directory being processed may be
799 * sparse. To confirm that, we can check whether an entry with that
800 * exact name exists in the index. If it does, the created subtree
801 * should be sparse. Otherwise, cache tree expansion should continue
802 * as normal.
804 if (r->index->sparse_index &&
805 index_entry_exists(r->index, tree_path->buf, tree_path->len))
806 prime_cache_tree_sparse_dir(sub->cache_tree, subtree);
807 else
808 prime_cache_tree_rec(r, sub->cache_tree, subtree, tree_path);
809 cnt += sub->cache_tree->entry_count;
813 it->entry_count = cnt;
816 void prime_cache_tree(struct repository *r,
817 struct index_state *istate,
818 struct tree *tree)
820 struct strbuf tree_path = STRBUF_INIT;
822 trace2_region_enter("cache-tree", "prime_cache_tree", r);
823 cache_tree_free(&istate->cache_tree);
824 istate->cache_tree = cache_tree();
826 prime_cache_tree_rec(r, istate->cache_tree, tree, &tree_path);
827 strbuf_release(&tree_path);
828 istate->cache_changed |= CACHE_TREE_CHANGED;
829 trace2_region_leave("cache-tree", "prime_cache_tree", r);
833 * find the cache_tree that corresponds to the current level without
834 * exploding the full path into textual form. The root of the
835 * cache tree is given as "root", and our current level is "info".
836 * (1) When at root level, info->prev is NULL, so it is "root" itself.
837 * (2) Otherwise, find the cache_tree that corresponds to one level
838 * above us, and find ourselves in there.
840 static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
841 struct traverse_info *info)
843 struct cache_tree *our_parent;
845 if (!info->prev)
846 return root;
847 our_parent = find_cache_tree_from_traversal(root, info->prev);
848 return cache_tree_find(our_parent, info->name);
851 int cache_tree_matches_traversal(struct cache_tree *root,
852 struct name_entry *ent,
853 struct traverse_info *info)
855 struct cache_tree *it;
857 it = find_cache_tree_from_traversal(root, info);
858 it = cache_tree_find(it, ent->path);
859 if (it && it->entry_count > 0 && oideq(&ent->oid, &it->oid))
860 return it->entry_count;
861 return 0;
864 static void verify_one_sparse(struct index_state *istate,
865 struct strbuf *path,
866 int pos)
868 struct cache_entry *ce = istate->cache[pos];
870 if (!S_ISSPARSEDIR(ce->ce_mode))
871 BUG("directory '%s' is present in index, but not sparse",
872 path->buf);
876 * Returns:
877 * 0 - Verification completed.
878 * 1 - Restart verification - a call to ensure_full_index() freed the cache
879 * tree that is being verified and verification needs to be restarted from
880 * the new toplevel cache tree.
882 static int verify_one(struct repository *r,
883 struct index_state *istate,
884 struct cache_tree *it,
885 struct strbuf *path)
887 int i, pos, len = path->len;
888 struct strbuf tree_buf = STRBUF_INIT;
889 struct object_id new_oid;
891 for (i = 0; i < it->subtree_nr; i++) {
892 strbuf_addf(path, "%s/", it->down[i]->name);
893 if (verify_one(r, istate, it->down[i]->cache_tree, path))
894 return 1;
895 strbuf_setlen(path, len);
898 if (it->entry_count < 0 ||
899 /* no verification on tests (t7003) that replace trees */
900 lookup_replace_object(r, &it->oid) != &it->oid)
901 return 0;
903 if (path->len) {
905 * If the index is sparse and the cache tree is not
906 * index_name_pos() may trigger ensure_full_index() which will
907 * free the tree that is being verified.
909 int is_sparse = istate->sparse_index;
910 pos = index_name_pos(istate, path->buf, path->len);
911 if (is_sparse && !istate->sparse_index)
912 return 1;
914 if (pos >= 0) {
915 verify_one_sparse(istate, path, pos);
916 return 0;
919 pos = -pos - 1;
920 } else {
921 pos = 0;
924 i = 0;
925 while (i < it->entry_count) {
926 struct cache_entry *ce = istate->cache[pos + i];
927 const char *slash;
928 struct cache_tree_sub *sub = NULL;
929 const struct object_id *oid;
930 const char *name;
931 unsigned mode;
932 int entlen;
934 if (ce->ce_flags & (CE_STAGEMASK | CE_INTENT_TO_ADD | CE_REMOVE))
935 BUG("%s with flags 0x%x should not be in cache-tree",
936 ce->name, ce->ce_flags);
937 name = ce->name + path->len;
938 slash = strchr(name, '/');
939 if (slash) {
940 entlen = slash - name;
941 sub = find_subtree(it, ce->name + path->len, entlen, 0);
942 if (!sub || sub->cache_tree->entry_count < 0)
943 BUG("bad subtree '%.*s'", entlen, name);
944 oid = &sub->cache_tree->oid;
945 mode = S_IFDIR;
946 i += sub->cache_tree->entry_count;
947 } else {
948 oid = &ce->oid;
949 mode = ce->ce_mode;
950 entlen = ce_namelen(ce) - path->len;
951 i++;
953 strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0');
954 strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz);
956 hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, OBJ_TREE,
957 &new_oid);
958 if (!oideq(&new_oid, &it->oid))
959 BUG("cache-tree for path %.*s does not match. "
960 "Expected %s got %s", len, path->buf,
961 oid_to_hex(&new_oid), oid_to_hex(&it->oid));
962 strbuf_setlen(path, len);
963 strbuf_release(&tree_buf);
964 return 0;
967 void cache_tree_verify(struct repository *r, struct index_state *istate)
969 struct strbuf path = STRBUF_INIT;
971 if (!istate->cache_tree)
972 return;
973 if (verify_one(r, istate, istate->cache_tree, &path)) {
974 strbuf_reset(&path);
975 if (verify_one(r, istate, istate->cache_tree, &path))
976 BUG("ensure_full_index() called twice while verifying cache tree");
978 strbuf_release(&path);