Sync with 'maint'
[git.git] / cache-tree.c
blobc595e8612044c7c909e87dbaf193416f3d6b1ca6
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "lockfile.h"
7 #include "tree.h"
8 #include "tree-walk.h"
9 #include "cache-tree.h"
10 #include "bulk-checkin.h"
11 #include "object-file.h"
12 #include "object-store-ll.h"
13 #include "read-cache-ll.h"
14 #include "replace-object.h"
15 #include "repository.h"
16 #include "promisor-remote.h"
17 #include "trace.h"
18 #include "trace2.h"
20 #ifndef DEBUG_CACHE_TREE
21 #define DEBUG_CACHE_TREE 0
22 #endif
24 struct cache_tree *cache_tree(void)
26 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
27 it->entry_count = -1;
28 return it;
31 void cache_tree_free(struct cache_tree **it_p)
33 int i;
34 struct cache_tree *it = *it_p;
36 if (!it)
37 return;
38 for (i = 0; i < it->subtree_nr; i++)
39 if (it->down[i]) {
40 cache_tree_free(&it->down[i]->cache_tree);
41 free(it->down[i]);
43 free(it->down);
44 free(it);
45 *it_p = NULL;
48 static int subtree_name_cmp(const char *one, int onelen,
49 const char *two, int twolen)
51 if (onelen < twolen)
52 return -1;
53 if (twolen < onelen)
54 return 1;
55 return memcmp(one, two, onelen);
58 int cache_tree_subtree_pos(struct cache_tree *it, const char *path, int pathlen)
60 struct cache_tree_sub **down = it->down;
61 int lo, hi;
62 lo = 0;
63 hi = it->subtree_nr;
64 while (lo < hi) {
65 int mi = lo + (hi - lo) / 2;
66 struct cache_tree_sub *mdl = down[mi];
67 int cmp = subtree_name_cmp(path, pathlen,
68 mdl->name, mdl->namelen);
69 if (!cmp)
70 return mi;
71 if (cmp < 0)
72 hi = mi;
73 else
74 lo = mi + 1;
76 return -lo-1;
79 static struct cache_tree_sub *find_subtree(struct cache_tree *it,
80 const char *path,
81 int pathlen,
82 int create)
84 struct cache_tree_sub *down;
85 int pos = cache_tree_subtree_pos(it, path, pathlen);
86 if (0 <= pos)
87 return it->down[pos];
88 if (!create)
89 return NULL;
91 pos = -pos-1;
92 ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
93 it->subtree_nr++;
95 FLEX_ALLOC_MEM(down, name, path, pathlen);
96 down->cache_tree = NULL;
97 down->namelen = pathlen;
99 if (pos < it->subtree_nr)
100 MOVE_ARRAY(it->down + pos + 1, it->down + pos,
101 it->subtree_nr - pos - 1);
102 it->down[pos] = down;
103 return down;
106 struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
108 int pathlen = strlen(path);
109 return find_subtree(it, path, pathlen, 1);
112 static int do_invalidate_path(struct cache_tree *it, const char *path)
114 /* a/b/c
115 * ==> invalidate self
116 * ==> find "a", have it invalidate "b/c"
118 * ==> invalidate self
119 * ==> if "a" exists as a subtree, remove it.
121 const char *slash;
122 int namelen;
123 struct cache_tree_sub *down;
125 #if DEBUG_CACHE_TREE
126 fprintf(stderr, "cache-tree invalidate <%s>\n", path);
127 #endif
129 if (!it)
130 return 0;
131 slash = strchrnul(path, '/');
132 namelen = slash - path;
133 it->entry_count = -1;
134 if (!*slash) {
135 int pos;
136 pos = cache_tree_subtree_pos(it, path, namelen);
137 if (0 <= pos) {
138 cache_tree_free(&it->down[pos]->cache_tree);
139 free(it->down[pos]);
140 /* 0 1 2 3 4 5
141 * ^ ^subtree_nr = 6
142 * pos
143 * move 4 and 5 up one place (2 entries)
144 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
146 MOVE_ARRAY(it->down + pos, it->down + pos + 1,
147 it->subtree_nr - pos - 1);
148 it->subtree_nr--;
150 return 1;
152 down = find_subtree(it, path, namelen, 0);
153 if (down)
154 do_invalidate_path(down->cache_tree, slash + 1);
155 return 1;
158 void cache_tree_invalidate_path(struct index_state *istate, const char *path)
160 if (do_invalidate_path(istate->cache_tree, path))
161 istate->cache_changed |= CACHE_TREE_CHANGED;
164 static int verify_cache(struct index_state *istate, int flags)
166 unsigned i, funny;
167 int silent = flags & WRITE_TREE_SILENT;
169 /* Verify that the tree is merged */
170 funny = 0;
171 for (i = 0; i < istate->cache_nr; i++) {
172 const struct cache_entry *ce = istate->cache[i];
173 if (ce_stage(ce)) {
174 if (silent)
175 return -1;
176 if (10 < ++funny) {
177 fprintf(stderr, "...\n");
178 break;
180 fprintf(stderr, "%s: unmerged (%s)\n",
181 ce->name, oid_to_hex(&ce->oid));
184 if (funny)
185 return -1;
187 /* Also verify that the cache does not have path and path/file
188 * at the same time. At this point we know the cache has only
189 * stage 0 entries.
191 funny = 0;
192 for (i = 0; i + 1 < istate->cache_nr; i++) {
193 /* path/file always comes after path because of the way
194 * the cache is sorted. Also path can appear only once,
195 * which means conflicting one would immediately follow.
197 const struct cache_entry *this_ce = istate->cache[i];
198 const struct cache_entry *next_ce = istate->cache[i + 1];
199 const char *this_name = this_ce->name;
200 const char *next_name = next_ce->name;
201 int this_len = ce_namelen(this_ce);
202 if (this_len < ce_namelen(next_ce) &&
203 next_name[this_len] == '/' &&
204 strncmp(this_name, next_name, this_len) == 0) {
205 if (10 < ++funny) {
206 fprintf(stderr, "...\n");
207 break;
209 fprintf(stderr, "You have both %s and %s\n",
210 this_name, next_name);
213 if (funny)
214 return -1;
215 return 0;
218 static void discard_unused_subtrees(struct cache_tree *it)
220 struct cache_tree_sub **down = it->down;
221 int nr = it->subtree_nr;
222 int dst, src;
223 for (dst = src = 0; src < nr; src++) {
224 struct cache_tree_sub *s = down[src];
225 if (s->used)
226 down[dst++] = s;
227 else {
228 cache_tree_free(&s->cache_tree);
229 free(s);
230 it->subtree_nr--;
235 int cache_tree_fully_valid(struct cache_tree *it)
237 int i;
238 if (!it)
239 return 0;
240 if (it->entry_count < 0 || !repo_has_object_file(the_repository, &it->oid))
241 return 0;
242 for (i = 0; i < it->subtree_nr; i++) {
243 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
244 return 0;
246 return 1;
249 static int must_check_existence(const struct cache_entry *ce)
251 return !(repo_has_promisor_remote(the_repository) && ce_skip_worktree(ce));
254 static int update_one(struct cache_tree *it,
255 struct cache_entry **cache,
256 int entries,
257 const char *base,
258 int baselen,
259 int *skip_count,
260 int flags)
262 struct strbuf buffer;
263 int missing_ok = flags & WRITE_TREE_MISSING_OK;
264 int dryrun = flags & WRITE_TREE_DRY_RUN;
265 int repair = flags & WRITE_TREE_REPAIR;
266 int to_invalidate = 0;
267 int i;
269 assert(!(dryrun && repair));
271 *skip_count = 0;
274 * If the first entry of this region is a sparse directory
275 * entry corresponding exactly to 'base', then this cache_tree
276 * struct is a "leaf" in the data structure, pointing to the
277 * tree OID specified in the entry.
279 if (entries > 0) {
280 const struct cache_entry *ce = cache[0];
282 if (S_ISSPARSEDIR(ce->ce_mode) &&
283 ce->ce_namelen == baselen &&
284 !strncmp(ce->name, base, baselen)) {
285 it->entry_count = 1;
286 oidcpy(&it->oid, &ce->oid);
287 return 1;
291 if (0 <= it->entry_count && repo_has_object_file(the_repository, &it->oid))
292 return it->entry_count;
295 * We first scan for subtrees and update them; we start by
296 * marking existing subtrees -- the ones that are unmarked
297 * should not be in the result.
299 for (i = 0; i < it->subtree_nr; i++)
300 it->down[i]->used = 0;
303 * Find the subtrees and update them.
305 i = 0;
306 while (i < entries) {
307 const struct cache_entry *ce = cache[i];
308 struct cache_tree_sub *sub;
309 const char *path, *slash;
310 int pathlen, sublen, subcnt, subskip;
312 path = ce->name;
313 pathlen = ce_namelen(ce);
314 if (pathlen <= baselen || memcmp(base, path, baselen))
315 break; /* at the end of this level */
317 slash = strchr(path + baselen, '/');
318 if (!slash) {
319 i++;
320 continue;
323 * a/bbb/c (base = a/, slash = /c)
324 * ==>
325 * path+baselen = bbb/c, sublen = 3
327 sublen = slash - (path + baselen);
328 sub = find_subtree(it, path + baselen, sublen, 1);
329 if (!sub->cache_tree)
330 sub->cache_tree = cache_tree();
331 subcnt = update_one(sub->cache_tree,
332 cache + i, entries - i,
333 path,
334 baselen + sublen + 1,
335 &subskip,
336 flags);
337 if (subcnt < 0)
338 return subcnt;
339 if (!subcnt)
340 die("index cache-tree records empty sub-tree");
341 i += subcnt;
342 sub->count = subcnt; /* to be used in the next loop */
343 *skip_count += subskip;
344 sub->used = 1;
347 discard_unused_subtrees(it);
350 * Then write out the tree object for this level.
352 strbuf_init(&buffer, 8192);
354 i = 0;
355 while (i < entries) {
356 const struct cache_entry *ce = cache[i];
357 struct cache_tree_sub *sub = NULL;
358 const char *path, *slash;
359 int pathlen, entlen;
360 const struct object_id *oid;
361 unsigned mode;
362 int expected_missing = 0;
363 int contains_ita = 0;
364 int ce_missing_ok;
366 path = ce->name;
367 pathlen = ce_namelen(ce);
368 if (pathlen <= baselen || memcmp(base, path, baselen))
369 break; /* at the end of this level */
371 slash = strchr(path + baselen, '/');
372 if (slash) {
373 entlen = slash - (path + baselen);
374 sub = find_subtree(it, path + baselen, entlen, 0);
375 if (!sub)
376 die("cache-tree.c: '%.*s' in '%s' not found",
377 entlen, path + baselen, path);
378 i += sub->count;
379 oid = &sub->cache_tree->oid;
380 mode = S_IFDIR;
381 contains_ita = sub->cache_tree->entry_count < 0;
382 if (contains_ita) {
383 to_invalidate = 1;
384 expected_missing = 1;
387 else {
388 oid = &ce->oid;
389 mode = ce->ce_mode;
390 entlen = pathlen - baselen;
391 i++;
394 ce_missing_ok = mode == S_IFGITLINK || missing_ok ||
395 !must_check_existence(ce);
396 if (is_null_oid(oid) ||
397 (!ce_missing_ok && !repo_has_object_file(the_repository, oid))) {
398 strbuf_release(&buffer);
399 if (expected_missing)
400 return -1;
401 return error("invalid object %06o %s for '%.*s'",
402 mode, oid_to_hex(oid), entlen+baselen, path);
406 * CE_REMOVE entries are removed before the index is
407 * written to disk. Skip them to remain consistent
408 * with the future on-disk index.
410 if (ce->ce_flags & CE_REMOVE) {
411 *skip_count = *skip_count + 1;
412 continue;
416 * CE_INTENT_TO_ADD entries exist in on-disk index but
417 * they are not part of generated trees. Invalidate up
418 * to root to force cache-tree users to read elsewhere.
420 if (!sub && ce_intent_to_add(ce)) {
421 to_invalidate = 1;
422 continue;
426 * "sub" can be an empty tree if all subentries are i-t-a.
428 if (contains_ita && is_empty_tree_oid(oid, the_repository->hash_algo))
429 continue;
431 strbuf_grow(&buffer, entlen + 100);
432 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
433 strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz);
435 #if DEBUG_CACHE_TREE
436 fprintf(stderr, "cache-tree update-one %o %.*s\n",
437 mode, entlen, path + baselen);
438 #endif
441 if (repair) {
442 struct object_id oid;
443 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
444 OBJ_TREE, &oid);
445 if (repo_has_object_file_with_flags(the_repository, &oid, OBJECT_INFO_SKIP_FETCH_OBJECT))
446 oidcpy(&it->oid, &oid);
447 else
448 to_invalidate = 1;
449 } else if (dryrun) {
450 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
451 OBJ_TREE, &it->oid);
452 } else if (write_object_file_flags(buffer.buf, buffer.len, OBJ_TREE,
453 &it->oid, NULL, flags & WRITE_TREE_SILENT
454 ? HASH_SILENT : 0)) {
455 strbuf_release(&buffer);
456 return -1;
459 strbuf_release(&buffer);
460 it->entry_count = to_invalidate ? -1 : i - *skip_count;
461 #if DEBUG_CACHE_TREE
462 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
463 it->entry_count, it->subtree_nr,
464 oid_to_hex(&it->oid));
465 #endif
466 return i;
469 int cache_tree_update(struct index_state *istate, int flags)
471 int skip, i;
473 i = verify_cache(istate, flags);
475 if (i)
476 return i;
478 if (!istate->cache_tree)
479 istate->cache_tree = cache_tree();
481 if (!(flags & WRITE_TREE_MISSING_OK) && repo_has_promisor_remote(the_repository))
482 prefetch_cache_entries(istate, must_check_existence);
484 trace_performance_enter();
485 trace2_region_enter("cache_tree", "update", the_repository);
486 begin_odb_transaction();
487 i = update_one(istate->cache_tree, istate->cache, istate->cache_nr,
488 "", 0, &skip, flags);
489 end_odb_transaction();
490 trace2_region_leave("cache_tree", "update", the_repository);
491 trace_performance_leave("cache_tree_update");
492 if (i < 0)
493 return i;
494 istate->cache_changed |= CACHE_TREE_CHANGED;
495 return 0;
498 static void write_one(struct strbuf *buffer, struct cache_tree *it,
499 const char *path, int pathlen)
501 int i;
503 /* One "cache-tree" entry consists of the following:
504 * path (NUL terminated)
505 * entry_count, subtree_nr ("%d %d\n")
506 * tree-sha1 (missing if invalid)
507 * subtree_nr "cache-tree" entries for subtrees.
509 strbuf_grow(buffer, pathlen + 100);
510 strbuf_add(buffer, path, pathlen);
511 strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
513 #if DEBUG_CACHE_TREE
514 if (0 <= it->entry_count)
515 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
516 pathlen, path, it->entry_count, it->subtree_nr,
517 oid_to_hex(&it->oid));
518 else
519 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
520 pathlen, path, it->subtree_nr);
521 #endif
523 if (0 <= it->entry_count) {
524 strbuf_add(buffer, it->oid.hash, the_hash_algo->rawsz);
526 for (i = 0; i < it->subtree_nr; i++) {
527 struct cache_tree_sub *down = it->down[i];
528 if (i) {
529 struct cache_tree_sub *prev = it->down[i-1];
530 if (subtree_name_cmp(down->name, down->namelen,
531 prev->name, prev->namelen) <= 0)
532 die("fatal - unsorted cache subtree");
534 write_one(buffer, down->cache_tree, down->name, down->namelen);
538 void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
540 trace2_region_enter("cache_tree", "write", the_repository);
541 write_one(sb, root, "", 0);
542 trace2_region_leave("cache_tree", "write", the_repository);
545 static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
547 const char *buf = *buffer;
548 unsigned long size = *size_p;
549 const char *cp;
550 char *ep;
551 struct cache_tree *it;
552 int i, subtree_nr;
553 const unsigned rawsz = the_hash_algo->rawsz;
555 it = NULL;
556 /* skip name, but make sure name exists */
557 while (size && *buf) {
558 size--;
559 buf++;
561 if (!size)
562 goto free_return;
563 buf++; size--;
564 it = cache_tree();
566 cp = buf;
567 it->entry_count = strtol(cp, &ep, 10);
568 if (cp == ep)
569 goto free_return;
570 cp = ep;
571 subtree_nr = strtol(cp, &ep, 10);
572 if (cp == ep)
573 goto free_return;
574 while (size && *buf && *buf != '\n') {
575 size--;
576 buf++;
578 if (!size)
579 goto free_return;
580 buf++; size--;
581 if (0 <= it->entry_count) {
582 if (size < rawsz)
583 goto free_return;
584 oidread(&it->oid, (const unsigned char *)buf,
585 the_repository->hash_algo);
586 buf += rawsz;
587 size -= rawsz;
590 #if DEBUG_CACHE_TREE
591 if (0 <= it->entry_count)
592 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
593 *buffer, it->entry_count, subtree_nr,
594 oid_to_hex(&it->oid));
595 else
596 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
597 *buffer, subtree_nr);
598 #endif
601 * Just a heuristic -- we do not add directories that often but
602 * we do not want to have to extend it immediately when we do,
603 * hence +2.
605 it->subtree_alloc = subtree_nr + 2;
606 CALLOC_ARRAY(it->down, it->subtree_alloc);
607 for (i = 0; i < subtree_nr; i++) {
608 /* read each subtree */
609 struct cache_tree *sub;
610 struct cache_tree_sub *subtree;
611 const char *name = buf;
613 sub = read_one(&buf, &size);
614 if (!sub)
615 goto free_return;
616 subtree = cache_tree_sub(it, name);
617 subtree->cache_tree = sub;
619 if (subtree_nr != it->subtree_nr)
620 die("cache-tree: internal error");
621 *buffer = buf;
622 *size_p = size;
623 return it;
625 free_return:
626 cache_tree_free(&it);
627 return NULL;
630 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
632 struct cache_tree *result;
634 if (buffer[0])
635 return NULL; /* not the whole tree */
637 trace2_region_enter("cache_tree", "read", the_repository);
638 result = read_one(&buffer, &size);
639 trace2_region_leave("cache_tree", "read", the_repository);
641 return result;
644 static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
646 if (!it)
647 return NULL;
648 while (*path) {
649 const char *slash;
650 struct cache_tree_sub *sub;
652 slash = strchrnul(path, '/');
654 * Between path and slash is the name of the subtree
655 * to look for.
657 sub = find_subtree(it, path, slash - path, 0);
658 if (!sub)
659 return NULL;
660 it = sub->cache_tree;
662 path = slash;
663 while (*path == '/')
664 path++;
666 return it;
669 static int write_index_as_tree_internal(struct object_id *oid,
670 struct index_state *index_state,
671 int cache_tree_valid,
672 int flags,
673 const char *prefix)
675 if (flags & WRITE_TREE_IGNORE_CACHE_TREE) {
676 cache_tree_free(&index_state->cache_tree);
677 cache_tree_valid = 0;
680 if (!cache_tree_valid && cache_tree_update(index_state, flags) < 0)
681 return WRITE_TREE_UNMERGED_INDEX;
683 if (prefix) {
684 struct cache_tree *subtree;
685 subtree = cache_tree_find(index_state->cache_tree, prefix);
686 if (!subtree)
687 return WRITE_TREE_PREFIX_ERROR;
688 oidcpy(oid, &subtree->oid);
690 else
691 oidcpy(oid, &index_state->cache_tree->oid);
693 return 0;
696 struct tree* write_in_core_index_as_tree(struct repository *repo) {
697 struct object_id o;
698 int was_valid, ret;
700 struct index_state *index_state = repo->index;
701 was_valid = index_state->cache_tree &&
702 cache_tree_fully_valid(index_state->cache_tree);
704 ret = write_index_as_tree_internal(&o, index_state, was_valid, 0, NULL);
705 if (ret == WRITE_TREE_UNMERGED_INDEX) {
706 int i;
707 bug("there are unmerged index entries:");
708 for (i = 0; i < index_state->cache_nr; i++) {
709 const struct cache_entry *ce = index_state->cache[i];
710 if (ce_stage(ce))
711 bug("%d %.*s", ce_stage(ce),
712 (int)ce_namelen(ce), ce->name);
714 BUG("unmerged index entries when writing in-core index");
717 return lookup_tree(repo, &index_state->cache_tree->oid);
721 int write_index_as_tree(struct object_id *oid, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
723 int entries, was_valid;
724 struct lock_file lock_file = LOCK_INIT;
725 int ret;
727 hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
729 entries = read_index_from(index_state, index_path,
730 repo_get_git_dir(the_repository));
731 if (entries < 0) {
732 ret = WRITE_TREE_UNREADABLE_INDEX;
733 goto out;
736 was_valid = !(flags & WRITE_TREE_IGNORE_CACHE_TREE) &&
737 index_state->cache_tree &&
738 cache_tree_fully_valid(index_state->cache_tree);
740 ret = write_index_as_tree_internal(oid, index_state, was_valid, flags,
741 prefix);
742 if (!ret && !was_valid) {
743 write_locked_index(index_state, &lock_file, COMMIT_LOCK);
744 /* Not being able to write is fine -- we are only interested
745 * in updating the cache-tree part, and if the next caller
746 * ends up using the old index with unupdated cache-tree part
747 * it misses the work we did here, but that is just a
748 * performance penalty and not a big deal.
752 out:
753 rollback_lock_file(&lock_file);
754 return ret;
757 static void prime_cache_tree_sparse_dir(struct cache_tree *it,
758 struct tree *tree)
761 oidcpy(&it->oid, &tree->object.oid);
762 it->entry_count = 1;
765 static void prime_cache_tree_rec(struct repository *r,
766 struct cache_tree *it,
767 struct tree *tree,
768 struct strbuf *tree_path)
770 struct tree_desc desc;
771 struct name_entry entry;
772 int cnt;
773 size_t base_path_len = tree_path->len;
775 oidcpy(&it->oid, &tree->object.oid);
777 init_tree_desc(&desc, &tree->object.oid, tree->buffer, tree->size);
778 cnt = 0;
779 while (tree_entry(&desc, &entry)) {
780 if (!S_ISDIR(entry.mode))
781 cnt++;
782 else {
783 struct cache_tree_sub *sub;
784 struct tree *subtree = lookup_tree(r, &entry.oid);
786 if (parse_tree(subtree) < 0)
787 exit(128);
788 sub = cache_tree_sub(it, entry.path);
789 sub->cache_tree = cache_tree();
792 * Recursively-constructed subtree path is only needed when working
793 * in a sparse index (where it's used to determine whether the
794 * subtree is a sparse directory in the index).
796 if (r->index->sparse_index) {
797 strbuf_setlen(tree_path, base_path_len);
798 strbuf_add(tree_path, entry.path, entry.pathlen);
799 strbuf_addch(tree_path, '/');
803 * If a sparse index is in use, the directory being processed may be
804 * sparse. To confirm that, we can check whether an entry with that
805 * exact name exists in the index. If it does, the created subtree
806 * should be sparse. Otherwise, cache tree expansion should continue
807 * as normal.
809 if (r->index->sparse_index &&
810 index_entry_exists(r->index, tree_path->buf, tree_path->len))
811 prime_cache_tree_sparse_dir(sub->cache_tree, subtree);
812 else
813 prime_cache_tree_rec(r, sub->cache_tree, subtree, tree_path);
814 cnt += sub->cache_tree->entry_count;
818 it->entry_count = cnt;
821 void prime_cache_tree(struct repository *r,
822 struct index_state *istate,
823 struct tree *tree)
825 struct strbuf tree_path = STRBUF_INIT;
827 trace2_region_enter("cache-tree", "prime_cache_tree", r);
828 cache_tree_free(&istate->cache_tree);
829 istate->cache_tree = cache_tree();
831 prime_cache_tree_rec(r, istate->cache_tree, tree, &tree_path);
832 strbuf_release(&tree_path);
833 istate->cache_changed |= CACHE_TREE_CHANGED;
834 trace2_region_leave("cache-tree", "prime_cache_tree", r);
838 * find the cache_tree that corresponds to the current level without
839 * exploding the full path into textual form. The root of the
840 * cache tree is given as "root", and our current level is "info".
841 * (1) When at root level, info->prev is NULL, so it is "root" itself.
842 * (2) Otherwise, find the cache_tree that corresponds to one level
843 * above us, and find ourselves in there.
845 static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
846 struct traverse_info *info)
848 struct cache_tree *our_parent;
850 if (!info->prev)
851 return root;
852 our_parent = find_cache_tree_from_traversal(root, info->prev);
853 return cache_tree_find(our_parent, info->name);
856 int cache_tree_matches_traversal(struct cache_tree *root,
857 struct name_entry *ent,
858 struct traverse_info *info)
860 struct cache_tree *it;
862 it = find_cache_tree_from_traversal(root, info);
863 it = cache_tree_find(it, ent->path);
864 if (it && it->entry_count > 0 && oideq(&ent->oid, &it->oid))
865 return it->entry_count;
866 return 0;
869 static int verify_one_sparse(struct index_state *istate,
870 struct strbuf *path,
871 int pos)
873 struct cache_entry *ce = istate->cache[pos];
874 if (!S_ISSPARSEDIR(ce->ce_mode))
875 return error(_("directory '%s' is present in index, but not sparse"),
876 path->buf);
877 return 0;
881 * Returns:
882 * 0 - Verification completed.
883 * 1 - Restart verification - a call to ensure_full_index() freed the cache
884 * tree that is being verified and verification needs to be restarted from
885 * the new toplevel cache tree.
886 * -1 - Verification failed.
888 static int verify_one(struct repository *r,
889 struct index_state *istate,
890 struct cache_tree *it,
891 struct strbuf *path)
893 int i, pos, len = path->len;
894 struct strbuf tree_buf = STRBUF_INIT;
895 struct object_id new_oid;
896 int ret;
898 for (i = 0; i < it->subtree_nr; i++) {
899 strbuf_addf(path, "%s/", it->down[i]->name);
900 ret = verify_one(r, istate, it->down[i]->cache_tree, path);
901 if (ret)
902 goto out;
904 strbuf_setlen(path, len);
907 if (it->entry_count < 0 ||
908 /* no verification on tests (t7003) that replace trees */
909 lookup_replace_object(r, &it->oid) != &it->oid) {
910 ret = 0;
911 goto out;
914 if (path->len) {
916 * If the index is sparse and the cache tree is not
917 * index_name_pos() may trigger ensure_full_index() which will
918 * free the tree that is being verified.
920 int is_sparse = istate->sparse_index;
921 pos = index_name_pos(istate, path->buf, path->len);
922 if (is_sparse && !istate->sparse_index) {
923 ret = 1;
924 goto out;
927 if (pos >= 0) {
928 ret = verify_one_sparse(istate, path, pos);
929 goto out;
932 pos = -pos - 1;
933 } else {
934 pos = 0;
937 if (it->entry_count + pos > istate->cache_nr) {
938 ret = error(_("corrupted cache-tree has entries not present in index"));
939 goto out;
942 i = 0;
943 while (i < it->entry_count) {
944 struct cache_entry *ce = istate->cache[pos + i];
945 const char *slash;
946 struct cache_tree_sub *sub = NULL;
947 const struct object_id *oid;
948 const char *name;
949 unsigned mode;
950 int entlen;
952 if (ce->ce_flags & (CE_STAGEMASK | CE_INTENT_TO_ADD | CE_REMOVE)) {
953 ret = error(_("%s with flags 0x%x should not be in cache-tree"),
954 ce->name, ce->ce_flags);
955 goto out;
958 name = ce->name + path->len;
959 slash = strchr(name, '/');
960 if (slash) {
961 entlen = slash - name;
963 sub = find_subtree(it, ce->name + path->len, entlen, 0);
964 if (!sub || sub->cache_tree->entry_count < 0) {
965 ret = error(_("bad subtree '%.*s'"), entlen, name);
966 goto out;
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++;
978 strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0');
979 strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz);
982 hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, OBJ_TREE,
983 &new_oid);
985 if (!oideq(&new_oid, &it->oid)) {
986 ret = error(_("cache-tree for path %.*s does not match. "
987 "Expected %s got %s"), len, path->buf,
988 oid_to_hex(&new_oid), oid_to_hex(&it->oid));
989 goto out;
992 ret = 0;
993 out:
994 strbuf_setlen(path, len);
995 strbuf_release(&tree_buf);
996 return ret;
999 int cache_tree_verify(struct repository *r, struct index_state *istate)
1001 struct strbuf path = STRBUF_INIT;
1002 int ret;
1004 if (!istate->cache_tree) {
1005 ret = 0;
1006 goto out;
1009 ret = verify_one(r, istate, istate->cache_tree, &path);
1010 if (ret < 0)
1011 goto out;
1012 if (ret > 0) {
1013 strbuf_reset(&path);
1015 ret = verify_one(r, istate, istate->cache_tree, &path);
1016 if (ret < 0)
1017 goto out;
1018 if (ret > 0)
1019 BUG("ensure_full_index() called twice while verifying cache tree");
1022 ret = 0;
1024 out:
1025 strbuf_release(&path);
1026 return ret;