l10n: Update Catalan translation
[alt-git.git] / cache-tree.c
blobc97111cccf2eda3a53be9dfce637fa2c35464f4d
1 #include "cache.h"
2 #include "lockfile.h"
3 #include "tree.h"
4 #include "tree-walk.h"
5 #include "cache-tree.h"
6 #include "bulk-checkin.h"
7 #include "object-store.h"
8 #include "replace-object.h"
9 #include "promisor-remote.h"
10 #include "sparse-index.h"
12 #ifndef DEBUG_CACHE_TREE
13 #define DEBUG_CACHE_TREE 0
14 #endif
16 struct cache_tree *cache_tree(void)
18 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
19 it->entry_count = -1;
20 return it;
23 void cache_tree_free(struct cache_tree **it_p)
25 int i;
26 struct cache_tree *it = *it_p;
28 if (!it)
29 return;
30 for (i = 0; i < it->subtree_nr; i++)
31 if (it->down[i]) {
32 cache_tree_free(&it->down[i]->cache_tree);
33 free(it->down[i]);
35 free(it->down);
36 free(it);
37 *it_p = NULL;
40 static int subtree_name_cmp(const char *one, int onelen,
41 const char *two, int twolen)
43 if (onelen < twolen)
44 return -1;
45 if (twolen < onelen)
46 return 1;
47 return memcmp(one, two, onelen);
50 int cache_tree_subtree_pos(struct cache_tree *it, const char *path, int pathlen)
52 struct cache_tree_sub **down = it->down;
53 int lo, hi;
54 lo = 0;
55 hi = it->subtree_nr;
56 while (lo < hi) {
57 int mi = lo + (hi - lo) / 2;
58 struct cache_tree_sub *mdl = down[mi];
59 int cmp = subtree_name_cmp(path, pathlen,
60 mdl->name, mdl->namelen);
61 if (!cmp)
62 return mi;
63 if (cmp < 0)
64 hi = mi;
65 else
66 lo = mi + 1;
68 return -lo-1;
71 static struct cache_tree_sub *find_subtree(struct cache_tree *it,
72 const char *path,
73 int pathlen,
74 int create)
76 struct cache_tree_sub *down;
77 int pos = cache_tree_subtree_pos(it, path, pathlen);
78 if (0 <= pos)
79 return it->down[pos];
80 if (!create)
81 return NULL;
83 pos = -pos-1;
84 ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
85 it->subtree_nr++;
87 FLEX_ALLOC_MEM(down, name, path, pathlen);
88 down->cache_tree = NULL;
89 down->namelen = pathlen;
91 if (pos < it->subtree_nr)
92 MOVE_ARRAY(it->down + pos + 1, it->down + pos,
93 it->subtree_nr - pos - 1);
94 it->down[pos] = down;
95 return down;
98 struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
100 int pathlen = strlen(path);
101 return find_subtree(it, path, pathlen, 1);
104 static int do_invalidate_path(struct cache_tree *it, const char *path)
106 /* a/b/c
107 * ==> invalidate self
108 * ==> find "a", have it invalidate "b/c"
110 * ==> invalidate self
111 * ==> if "a" exists as a subtree, remove it.
113 const char *slash;
114 int namelen;
115 struct cache_tree_sub *down;
117 #if DEBUG_CACHE_TREE
118 fprintf(stderr, "cache-tree invalidate <%s>\n", path);
119 #endif
121 if (!it)
122 return 0;
123 slash = strchrnul(path, '/');
124 namelen = slash - path;
125 it->entry_count = -1;
126 if (!*slash) {
127 int pos;
128 pos = cache_tree_subtree_pos(it, path, namelen);
129 if (0 <= pos) {
130 cache_tree_free(&it->down[pos]->cache_tree);
131 free(it->down[pos]);
132 /* 0 1 2 3 4 5
133 * ^ ^subtree_nr = 6
134 * pos
135 * move 4 and 5 up one place (2 entries)
136 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
138 MOVE_ARRAY(it->down + pos, it->down + pos + 1,
139 it->subtree_nr - pos - 1);
140 it->subtree_nr--;
142 return 1;
144 down = find_subtree(it, path, namelen, 0);
145 if (down)
146 do_invalidate_path(down->cache_tree, slash + 1);
147 return 1;
150 void cache_tree_invalidate_path(struct index_state *istate, const char *path)
152 if (do_invalidate_path(istate->cache_tree, path))
153 istate->cache_changed |= CACHE_TREE_CHANGED;
156 static int verify_cache(struct index_state *istate, int flags)
158 unsigned i, funny;
159 int silent = flags & WRITE_TREE_SILENT;
161 /* Verify that the tree is merged */
162 funny = 0;
163 for (i = 0; i < istate->cache_nr; i++) {
164 const struct cache_entry *ce = istate->cache[i];
165 if (ce_stage(ce)) {
166 if (silent)
167 return -1;
168 if (10 < ++funny) {
169 fprintf(stderr, "...\n");
170 break;
172 fprintf(stderr, "%s: unmerged (%s)\n",
173 ce->name, oid_to_hex(&ce->oid));
176 if (funny)
177 return -1;
179 /* Also verify that the cache does not have path and path/file
180 * at the same time. At this point we know the cache has only
181 * stage 0 entries.
183 funny = 0;
184 for (i = 0; i + 1 < istate->cache_nr; i++) {
185 /* path/file always comes after path because of the way
186 * the cache is sorted. Also path can appear only once,
187 * which means conflicting one would immediately follow.
189 const struct cache_entry *this_ce = istate->cache[i];
190 const struct cache_entry *next_ce = istate->cache[i + 1];
191 const char *this_name = this_ce->name;
192 const char *next_name = next_ce->name;
193 int this_len = ce_namelen(this_ce);
194 if (this_len < ce_namelen(next_ce) &&
195 next_name[this_len] == '/' &&
196 strncmp(this_name, next_name, this_len) == 0) {
197 if (10 < ++funny) {
198 fprintf(stderr, "...\n");
199 break;
201 fprintf(stderr, "You have both %s and %s\n",
202 this_name, next_name);
205 if (funny)
206 return -1;
207 return 0;
210 static void discard_unused_subtrees(struct cache_tree *it)
212 struct cache_tree_sub **down = it->down;
213 int nr = it->subtree_nr;
214 int dst, src;
215 for (dst = src = 0; src < nr; src++) {
216 struct cache_tree_sub *s = down[src];
217 if (s->used)
218 down[dst++] = s;
219 else {
220 cache_tree_free(&s->cache_tree);
221 free(s);
222 it->subtree_nr--;
227 int cache_tree_fully_valid(struct cache_tree *it)
229 int i;
230 if (!it)
231 return 0;
232 if (it->entry_count < 0 || !has_object_file(&it->oid))
233 return 0;
234 for (i = 0; i < it->subtree_nr; i++) {
235 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
236 return 0;
238 return 1;
241 static int must_check_existence(const struct cache_entry *ce)
243 return !(has_promisor_remote() && ce_skip_worktree(ce));
246 static int update_one(struct cache_tree *it,
247 struct cache_entry **cache,
248 int entries,
249 const char *base,
250 int baselen,
251 int *skip_count,
252 int flags)
254 struct strbuf buffer;
255 int missing_ok = flags & WRITE_TREE_MISSING_OK;
256 int dryrun = flags & WRITE_TREE_DRY_RUN;
257 int repair = flags & WRITE_TREE_REPAIR;
258 int to_invalidate = 0;
259 int i;
261 assert(!(dryrun && repair));
263 *skip_count = 0;
266 * If the first entry of this region is a sparse directory
267 * entry corresponding exactly to 'base', then this cache_tree
268 * struct is a "leaf" in the data structure, pointing to the
269 * tree OID specified in the entry.
271 if (entries > 0) {
272 const struct cache_entry *ce = cache[0];
274 if (S_ISSPARSEDIR(ce->ce_mode) &&
275 ce->ce_namelen == baselen &&
276 !strncmp(ce->name, base, baselen)) {
277 it->entry_count = 1;
278 oidcpy(&it->oid, &ce->oid);
279 return 1;
283 if (0 <= it->entry_count && has_object_file(&it->oid))
284 return it->entry_count;
287 * We first scan for subtrees and update them; we start by
288 * marking existing subtrees -- the ones that are unmarked
289 * should not be in the result.
291 for (i = 0; i < it->subtree_nr; i++)
292 it->down[i]->used = 0;
295 * Find the subtrees and update them.
297 i = 0;
298 while (i < entries) {
299 const struct cache_entry *ce = cache[i];
300 struct cache_tree_sub *sub;
301 const char *path, *slash;
302 int pathlen, sublen, subcnt, subskip;
304 path = ce->name;
305 pathlen = ce_namelen(ce);
306 if (pathlen <= baselen || memcmp(base, path, baselen))
307 break; /* at the end of this level */
309 slash = strchr(path + baselen, '/');
310 if (!slash) {
311 i++;
312 continue;
315 * a/bbb/c (base = a/, slash = /c)
316 * ==>
317 * path+baselen = bbb/c, sublen = 3
319 sublen = slash - (path + baselen);
320 sub = find_subtree(it, path + baselen, sublen, 1);
321 if (!sub->cache_tree)
322 sub->cache_tree = cache_tree();
323 subcnt = update_one(sub->cache_tree,
324 cache + i, entries - i,
325 path,
326 baselen + sublen + 1,
327 &subskip,
328 flags);
329 if (subcnt < 0)
330 return subcnt;
331 if (!subcnt)
332 die("index cache-tree records empty sub-tree");
333 i += subcnt;
334 sub->count = subcnt; /* to be used in the next loop */
335 *skip_count += subskip;
336 sub->used = 1;
339 discard_unused_subtrees(it);
342 * Then write out the tree object for this level.
344 strbuf_init(&buffer, 8192);
346 i = 0;
347 while (i < entries) {
348 const struct cache_entry *ce = cache[i];
349 struct cache_tree_sub *sub = NULL;
350 const char *path, *slash;
351 int pathlen, entlen;
352 const struct object_id *oid;
353 unsigned mode;
354 int expected_missing = 0;
355 int contains_ita = 0;
356 int ce_missing_ok;
358 path = ce->name;
359 pathlen = ce_namelen(ce);
360 if (pathlen <= baselen || memcmp(base, path, baselen))
361 break; /* at the end of this level */
363 slash = strchr(path + baselen, '/');
364 if (slash) {
365 entlen = slash - (path + baselen);
366 sub = find_subtree(it, path + baselen, entlen, 0);
367 if (!sub)
368 die("cache-tree.c: '%.*s' in '%s' not found",
369 entlen, path + baselen, path);
370 i += sub->count;
371 oid = &sub->cache_tree->oid;
372 mode = S_IFDIR;
373 contains_ita = sub->cache_tree->entry_count < 0;
374 if (contains_ita) {
375 to_invalidate = 1;
376 expected_missing = 1;
379 else {
380 oid = &ce->oid;
381 mode = ce->ce_mode;
382 entlen = pathlen - baselen;
383 i++;
386 ce_missing_ok = mode == S_IFGITLINK || missing_ok ||
387 !must_check_existence(ce);
388 if (is_null_oid(oid) ||
389 (!ce_missing_ok && !has_object_file(oid))) {
390 strbuf_release(&buffer);
391 if (expected_missing)
392 return -1;
393 return error("invalid object %06o %s for '%.*s'",
394 mode, oid_to_hex(oid), entlen+baselen, path);
398 * CE_REMOVE entries are removed before the index is
399 * written to disk. Skip them to remain consistent
400 * with the future on-disk index.
402 if (ce->ce_flags & CE_REMOVE) {
403 *skip_count = *skip_count + 1;
404 continue;
408 * CE_INTENT_TO_ADD entries exist on on-disk index but
409 * they are not part of generated trees. Invalidate up
410 * to root to force cache-tree users to read elsewhere.
412 if (!sub && ce_intent_to_add(ce)) {
413 to_invalidate = 1;
414 continue;
418 * "sub" can be an empty tree if all subentries are i-t-a.
420 if (contains_ita && is_empty_tree_oid(oid))
421 continue;
423 strbuf_grow(&buffer, entlen + 100);
424 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
425 strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz);
427 #if DEBUG_CACHE_TREE
428 fprintf(stderr, "cache-tree update-one %o %.*s\n",
429 mode, entlen, path + baselen);
430 #endif
433 if (repair) {
434 struct object_id oid;
435 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
436 OBJ_TREE, &oid);
437 if (has_object_file_with_flags(&oid, OBJECT_INFO_SKIP_FETCH_OBJECT))
438 oidcpy(&it->oid, &oid);
439 else
440 to_invalidate = 1;
441 } else if (dryrun) {
442 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
443 OBJ_TREE, &it->oid);
444 } else if (write_object_file_flags(buffer.buf, buffer.len, OBJ_TREE,
445 &it->oid, flags & WRITE_TREE_SILENT
446 ? HASH_SILENT : 0)) {
447 strbuf_release(&buffer);
448 return -1;
451 strbuf_release(&buffer);
452 it->entry_count = to_invalidate ? -1 : i - *skip_count;
453 #if DEBUG_CACHE_TREE
454 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
455 it->entry_count, it->subtree_nr,
456 oid_to_hex(&it->oid));
457 #endif
458 return i;
461 int cache_tree_update(struct index_state *istate, int flags)
463 int skip, i;
465 i = verify_cache(istate, flags);
467 if (i)
468 return i;
470 if (!istate->cache_tree)
471 istate->cache_tree = cache_tree();
473 if (!(flags & WRITE_TREE_MISSING_OK) && has_promisor_remote())
474 prefetch_cache_entries(istate, must_check_existence);
476 trace_performance_enter();
477 trace2_region_enter("cache_tree", "update", the_repository);
478 begin_odb_transaction();
479 i = update_one(istate->cache_tree, istate->cache, istate->cache_nr,
480 "", 0, &skip, flags);
481 end_odb_transaction();
482 trace2_region_leave("cache_tree", "update", the_repository);
483 trace_performance_leave("cache_tree_update");
484 if (i < 0)
485 return i;
486 istate->cache_changed |= CACHE_TREE_CHANGED;
487 return 0;
490 static void write_one(struct strbuf *buffer, struct cache_tree *it,
491 const char *path, int pathlen)
493 int i;
495 /* One "cache-tree" entry consists of the following:
496 * path (NUL terminated)
497 * entry_count, subtree_nr ("%d %d\n")
498 * tree-sha1 (missing if invalid)
499 * subtree_nr "cache-tree" entries for subtrees.
501 strbuf_grow(buffer, pathlen + 100);
502 strbuf_add(buffer, path, pathlen);
503 strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
505 #if DEBUG_CACHE_TREE
506 if (0 <= it->entry_count)
507 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
508 pathlen, path, it->entry_count, it->subtree_nr,
509 oid_to_hex(&it->oid));
510 else
511 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
512 pathlen, path, it->subtree_nr);
513 #endif
515 if (0 <= it->entry_count) {
516 strbuf_add(buffer, it->oid.hash, the_hash_algo->rawsz);
518 for (i = 0; i < it->subtree_nr; i++) {
519 struct cache_tree_sub *down = it->down[i];
520 if (i) {
521 struct cache_tree_sub *prev = it->down[i-1];
522 if (subtree_name_cmp(down->name, down->namelen,
523 prev->name, prev->namelen) <= 0)
524 die("fatal - unsorted cache subtree");
526 write_one(buffer, down->cache_tree, down->name, down->namelen);
530 void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
532 trace2_region_enter("cache_tree", "write", the_repository);
533 write_one(sb, root, "", 0);
534 trace2_region_leave("cache_tree", "write", the_repository);
537 static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
539 const char *buf = *buffer;
540 unsigned long size = *size_p;
541 const char *cp;
542 char *ep;
543 struct cache_tree *it;
544 int i, subtree_nr;
545 const unsigned rawsz = the_hash_algo->rawsz;
547 it = NULL;
548 /* skip name, but make sure name exists */
549 while (size && *buf) {
550 size--;
551 buf++;
553 if (!size)
554 goto free_return;
555 buf++; size--;
556 it = cache_tree();
558 cp = buf;
559 it->entry_count = strtol(cp, &ep, 10);
560 if (cp == ep)
561 goto free_return;
562 cp = ep;
563 subtree_nr = strtol(cp, &ep, 10);
564 if (cp == ep)
565 goto free_return;
566 while (size && *buf && *buf != '\n') {
567 size--;
568 buf++;
570 if (!size)
571 goto free_return;
572 buf++; size--;
573 if (0 <= it->entry_count) {
574 if (size < rawsz)
575 goto free_return;
576 oidread(&it->oid, (const unsigned char *)buf);
577 buf += rawsz;
578 size -= rawsz;
581 #if DEBUG_CACHE_TREE
582 if (0 <= it->entry_count)
583 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
584 *buffer, it->entry_count, subtree_nr,
585 oid_to_hex(&it->oid));
586 else
587 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
588 *buffer, subtree_nr);
589 #endif
592 * Just a heuristic -- we do not add directories that often but
593 * we do not want to have to extend it immediately when we do,
594 * hence +2.
596 it->subtree_alloc = subtree_nr + 2;
597 CALLOC_ARRAY(it->down, it->subtree_alloc);
598 for (i = 0; i < subtree_nr; i++) {
599 /* read each subtree */
600 struct cache_tree *sub;
601 struct cache_tree_sub *subtree;
602 const char *name = buf;
604 sub = read_one(&buf, &size);
605 if (!sub)
606 goto free_return;
607 subtree = cache_tree_sub(it, name);
608 subtree->cache_tree = sub;
610 if (subtree_nr != it->subtree_nr)
611 die("cache-tree: internal error");
612 *buffer = buf;
613 *size_p = size;
614 return it;
616 free_return:
617 cache_tree_free(&it);
618 return NULL;
621 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
623 struct cache_tree *result;
625 if (buffer[0])
626 return NULL; /* not the whole tree */
628 trace2_region_enter("cache_tree", "read", the_repository);
629 result = read_one(&buffer, &size);
630 trace2_region_leave("cache_tree", "read", the_repository);
632 return result;
635 static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
637 if (!it)
638 return NULL;
639 while (*path) {
640 const char *slash;
641 struct cache_tree_sub *sub;
643 slash = strchrnul(path, '/');
645 * Between path and slash is the name of the subtree
646 * to look for.
648 sub = find_subtree(it, path, slash - path, 0);
649 if (!sub)
650 return NULL;
651 it = sub->cache_tree;
653 path = slash;
654 while (*path == '/')
655 path++;
657 return it;
660 static int write_index_as_tree_internal(struct object_id *oid,
661 struct index_state *index_state,
662 int cache_tree_valid,
663 int flags,
664 const char *prefix)
666 if (flags & WRITE_TREE_IGNORE_CACHE_TREE) {
667 cache_tree_free(&index_state->cache_tree);
668 cache_tree_valid = 0;
671 if (!cache_tree_valid && cache_tree_update(index_state, flags) < 0)
672 return WRITE_TREE_UNMERGED_INDEX;
674 if (prefix) {
675 struct cache_tree *subtree;
676 subtree = cache_tree_find(index_state->cache_tree, prefix);
677 if (!subtree)
678 return WRITE_TREE_PREFIX_ERROR;
679 oidcpy(oid, &subtree->oid);
681 else
682 oidcpy(oid, &index_state->cache_tree->oid);
684 return 0;
687 struct tree* write_in_core_index_as_tree(struct repository *repo) {
688 struct object_id o;
689 int was_valid, ret;
691 struct index_state *index_state = repo->index;
692 was_valid = index_state->cache_tree &&
693 cache_tree_fully_valid(index_state->cache_tree);
695 ret = write_index_as_tree_internal(&o, index_state, was_valid, 0, NULL);
696 if (ret == WRITE_TREE_UNMERGED_INDEX) {
697 int i;
698 bug("there are unmerged index entries:");
699 for (i = 0; i < index_state->cache_nr; i++) {
700 const struct cache_entry *ce = index_state->cache[i];
701 if (ce_stage(ce))
702 bug("%d %.*s", ce_stage(ce),
703 (int)ce_namelen(ce), ce->name);
705 BUG("unmerged index entries when writing in-core index");
708 return lookup_tree(repo, &index_state->cache_tree->oid);
712 int write_index_as_tree(struct object_id *oid, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
714 int entries, was_valid;
715 struct lock_file lock_file = LOCK_INIT;
716 int ret;
718 hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
720 entries = read_index_from(index_state, index_path, get_git_dir());
721 if (entries < 0) {
722 ret = WRITE_TREE_UNREADABLE_INDEX;
723 goto out;
726 was_valid = !(flags & WRITE_TREE_IGNORE_CACHE_TREE) &&
727 index_state->cache_tree &&
728 cache_tree_fully_valid(index_state->cache_tree);
730 ret = write_index_as_tree_internal(oid, index_state, was_valid, flags,
731 prefix);
732 if (!ret && !was_valid) {
733 write_locked_index(index_state, &lock_file, COMMIT_LOCK);
734 /* Not being able to write is fine -- we are only interested
735 * in updating the cache-tree part, and if the next caller
736 * ends up using the old index with unupdated cache-tree part
737 * it misses the work we did here, but that is just a
738 * performance penalty and not a big deal.
742 out:
743 rollback_lock_file(&lock_file);
744 return ret;
747 static void prime_cache_tree_sparse_dir(struct cache_tree *it,
748 struct tree *tree)
751 oidcpy(&it->oid, &tree->object.oid);
752 it->entry_count = 1;
755 static void prime_cache_tree_rec(struct repository *r,
756 struct cache_tree *it,
757 struct tree *tree,
758 struct strbuf *tree_path)
760 struct tree_desc desc;
761 struct name_entry entry;
762 int cnt;
763 int base_path_len = tree_path->len;
765 oidcpy(&it->oid, &tree->object.oid);
767 init_tree_desc(&desc, tree->buffer, tree->size);
768 cnt = 0;
769 while (tree_entry(&desc, &entry)) {
770 if (!S_ISDIR(entry.mode))
771 cnt++;
772 else {
773 struct cache_tree_sub *sub;
774 struct tree *subtree = lookup_tree(r, &entry.oid);
776 if (!subtree->object.parsed)
777 parse_tree(subtree);
778 sub = cache_tree_sub(it, entry.path);
779 sub->cache_tree = cache_tree();
782 * Recursively-constructed subtree path is only needed when working
783 * in a sparse index (where it's used to determine whether the
784 * subtree is a sparse directory in the index).
786 if (r->index->sparse_index) {
787 strbuf_setlen(tree_path, base_path_len);
788 strbuf_grow(tree_path, base_path_len + entry.pathlen + 1);
789 strbuf_add(tree_path, entry.path, entry.pathlen);
790 strbuf_addch(tree_path, '/');
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.
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);
805 cnt += sub->cache_tree->entry_count;
809 it->entry_count = cnt;
812 void prime_cache_tree(struct repository *r,
813 struct index_state *istate,
814 struct tree *tree)
816 struct strbuf tree_path = STRBUF_INIT;
818 trace2_region_enter("cache-tree", "prime_cache_tree", the_repository);
819 cache_tree_free(&istate->cache_tree);
820 istate->cache_tree = cache_tree();
822 prime_cache_tree_rec(r, istate->cache_tree, tree, &tree_path);
823 strbuf_release(&tree_path);
824 istate->cache_changed |= CACHE_TREE_CHANGED;
825 trace2_region_leave("cache-tree", "prime_cache_tree", the_repository);
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.
836 static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
837 struct traverse_info *info)
839 struct cache_tree *our_parent;
841 if (!info->prev)
842 return root;
843 our_parent = find_cache_tree_from_traversal(root, info->prev);
844 return cache_tree_find(our_parent, info->name);
847 int cache_tree_matches_traversal(struct cache_tree *root,
848 struct name_entry *ent,
849 struct traverse_info *info)
851 struct cache_tree *it;
853 it = find_cache_tree_from_traversal(root, info);
854 it = cache_tree_find(it, ent->path);
855 if (it && it->entry_count > 0 && oideq(&ent->oid, &it->oid))
856 return it->entry_count;
857 return 0;
860 static void verify_one_sparse(struct index_state *istate,
861 struct strbuf *path,
862 int pos)
864 struct cache_entry *ce = istate->cache[pos];
866 if (!S_ISSPARSEDIR(ce->ce_mode))
867 BUG("directory '%s' is present in index, but not sparse",
868 path->buf);
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.
878 static int verify_one(struct repository *r,
879 struct index_state *istate,
880 struct cache_tree *it,
881 struct strbuf *path)
883 int i, pos, len = path->len;
884 struct strbuf tree_buf = STRBUF_INIT;
885 struct object_id new_oid;
887 for (i = 0; i < it->subtree_nr; i++) {
888 strbuf_addf(path, "%s/", it->down[i]->name);
889 if (verify_one(r, istate, it->down[i]->cache_tree, path))
890 return 1;
891 strbuf_setlen(path, len);
894 if (it->entry_count < 0 ||
895 /* no verification on tests (t7003) that replace trees */
896 lookup_replace_object(r, &it->oid) != &it->oid)
897 return 0;
899 if (path->len) {
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.
905 int is_sparse = istate->sparse_index;
906 pos = index_name_pos(istate, path->buf, path->len);
907 if (is_sparse && !istate->sparse_index)
908 return 1;
910 if (pos >= 0) {
911 verify_one_sparse(istate, path, pos);
912 return 0;
915 pos = -pos - 1;
916 } else {
917 pos = 0;
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;
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++;
949 strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0');
950 strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz);
952 hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, OBJ_TREE,
953 &new_oid);
954 if (!oideq(&new_oid, &it->oid))
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);
960 return 0;
963 void cache_tree_verify(struct repository *r, struct index_state *istate)
965 struct strbuf path = STRBUF_INIT;
967 if (!istate->cache_tree)
968 return;
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");
974 strbuf_release(&path);