debian: new upstream release
[git/debian.git] / cache-tree.c
blob641427ed410af39be80be22460a103dbad2d3d35
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 "sparse-index.h"
15 #include "trace.h"
16 #include "trace2.h"
18 #ifndef DEBUG_CACHE_TREE
19 #define DEBUG_CACHE_TREE 0
20 #endif
22 struct cache_tree *cache_tree(void)
24 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
25 it->entry_count = -1;
26 return it;
29 void cache_tree_free(struct cache_tree **it_p)
31 int i;
32 struct cache_tree *it = *it_p;
34 if (!it)
35 return;
36 for (i = 0; i < it->subtree_nr; i++)
37 if (it->down[i]) {
38 cache_tree_free(&it->down[i]->cache_tree);
39 free(it->down[i]);
41 free(it->down);
42 free(it);
43 *it_p = NULL;
46 static int subtree_name_cmp(const char *one, int onelen,
47 const char *two, int twolen)
49 if (onelen < twolen)
50 return -1;
51 if (twolen < onelen)
52 return 1;
53 return memcmp(one, two, onelen);
56 int cache_tree_subtree_pos(struct cache_tree *it, const char *path, int pathlen)
58 struct cache_tree_sub **down = it->down;
59 int lo, hi;
60 lo = 0;
61 hi = it->subtree_nr;
62 while (lo < hi) {
63 int mi = lo + (hi - lo) / 2;
64 struct cache_tree_sub *mdl = down[mi];
65 int cmp = subtree_name_cmp(path, pathlen,
66 mdl->name, mdl->namelen);
67 if (!cmp)
68 return mi;
69 if (cmp < 0)
70 hi = mi;
71 else
72 lo = mi + 1;
74 return -lo-1;
77 static struct cache_tree_sub *find_subtree(struct cache_tree *it,
78 const char *path,
79 int pathlen,
80 int create)
82 struct cache_tree_sub *down;
83 int pos = cache_tree_subtree_pos(it, path, pathlen);
84 if (0 <= pos)
85 return it->down[pos];
86 if (!create)
87 return NULL;
89 pos = -pos-1;
90 ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
91 it->subtree_nr++;
93 FLEX_ALLOC_MEM(down, name, path, pathlen);
94 down->cache_tree = NULL;
95 down->namelen = pathlen;
97 if (pos < it->subtree_nr)
98 MOVE_ARRAY(it->down + pos + 1, it->down + pos,
99 it->subtree_nr - pos - 1);
100 it->down[pos] = down;
101 return down;
104 struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
106 int pathlen = strlen(path);
107 return find_subtree(it, path, pathlen, 1);
110 static int do_invalidate_path(struct cache_tree *it, const char *path)
112 /* a/b/c
113 * ==> invalidate self
114 * ==> find "a", have it invalidate "b/c"
116 * ==> invalidate self
117 * ==> if "a" exists as a subtree, remove it.
119 const char *slash;
120 int namelen;
121 struct cache_tree_sub *down;
123 #if DEBUG_CACHE_TREE
124 fprintf(stderr, "cache-tree invalidate <%s>\n", path);
125 #endif
127 if (!it)
128 return 0;
129 slash = strchrnul(path, '/');
130 namelen = slash - path;
131 it->entry_count = -1;
132 if (!*slash) {
133 int pos;
134 pos = cache_tree_subtree_pos(it, path, namelen);
135 if (0 <= pos) {
136 cache_tree_free(&it->down[pos]->cache_tree);
137 free(it->down[pos]);
138 /* 0 1 2 3 4 5
139 * ^ ^subtree_nr = 6
140 * pos
141 * move 4 and 5 up one place (2 entries)
142 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
144 MOVE_ARRAY(it->down + pos, it->down + pos + 1,
145 it->subtree_nr - pos - 1);
146 it->subtree_nr--;
148 return 1;
150 down = find_subtree(it, path, namelen, 0);
151 if (down)
152 do_invalidate_path(down->cache_tree, slash + 1);
153 return 1;
156 void cache_tree_invalidate_path(struct index_state *istate, const char *path)
158 if (do_invalidate_path(istate->cache_tree, path))
159 istate->cache_changed |= CACHE_TREE_CHANGED;
162 static int verify_cache(struct index_state *istate, int flags)
164 unsigned i, funny;
165 int silent = flags & WRITE_TREE_SILENT;
167 /* Verify that the tree is merged */
168 funny = 0;
169 for (i = 0; i < istate->cache_nr; i++) {
170 const struct cache_entry *ce = istate->cache[i];
171 if (ce_stage(ce)) {
172 if (silent)
173 return -1;
174 if (10 < ++funny) {
175 fprintf(stderr, "...\n");
176 break;
178 fprintf(stderr, "%s: unmerged (%s)\n",
179 ce->name, oid_to_hex(&ce->oid));
182 if (funny)
183 return -1;
185 /* Also verify that the cache does not have path and path/file
186 * at the same time. At this point we know the cache has only
187 * stage 0 entries.
189 funny = 0;
190 for (i = 0; i + 1 < istate->cache_nr; i++) {
191 /* path/file always comes after path because of the way
192 * the cache is sorted. Also path can appear only once,
193 * which means conflicting one would immediately follow.
195 const struct cache_entry *this_ce = istate->cache[i];
196 const struct cache_entry *next_ce = istate->cache[i + 1];
197 const char *this_name = this_ce->name;
198 const char *next_name = next_ce->name;
199 int this_len = ce_namelen(this_ce);
200 if (this_len < ce_namelen(next_ce) &&
201 next_name[this_len] == '/' &&
202 strncmp(this_name, next_name, this_len) == 0) {
203 if (10 < ++funny) {
204 fprintf(stderr, "...\n");
205 break;
207 fprintf(stderr, "You have both %s and %s\n",
208 this_name, next_name);
211 if (funny)
212 return -1;
213 return 0;
216 static void discard_unused_subtrees(struct cache_tree *it)
218 struct cache_tree_sub **down = it->down;
219 int nr = it->subtree_nr;
220 int dst, src;
221 for (dst = src = 0; src < nr; src++) {
222 struct cache_tree_sub *s = down[src];
223 if (s->used)
224 down[dst++] = s;
225 else {
226 cache_tree_free(&s->cache_tree);
227 free(s);
228 it->subtree_nr--;
233 int cache_tree_fully_valid(struct cache_tree *it)
235 int i;
236 if (!it)
237 return 0;
238 if (it->entry_count < 0 || !repo_has_object_file(the_repository, &it->oid))
239 return 0;
240 for (i = 0; i < it->subtree_nr; i++) {
241 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
242 return 0;
244 return 1;
247 static int must_check_existence(const struct cache_entry *ce)
249 return !(repo_has_promisor_remote(the_repository) && ce_skip_worktree(ce));
252 static int update_one(struct cache_tree *it,
253 struct cache_entry **cache,
254 int entries,
255 const char *base,
256 int baselen,
257 int *skip_count,
258 int flags)
260 struct strbuf buffer;
261 int missing_ok = flags & WRITE_TREE_MISSING_OK;
262 int dryrun = flags & WRITE_TREE_DRY_RUN;
263 int repair = flags & WRITE_TREE_REPAIR;
264 int to_invalidate = 0;
265 int i;
267 assert(!(dryrun && repair));
269 *skip_count = 0;
272 * If the first entry of this region is a sparse directory
273 * entry corresponding exactly to 'base', then this cache_tree
274 * struct is a "leaf" in the data structure, pointing to the
275 * tree OID specified in the entry.
277 if (entries > 0) {
278 const struct cache_entry *ce = cache[0];
280 if (S_ISSPARSEDIR(ce->ce_mode) &&
281 ce->ce_namelen == baselen &&
282 !strncmp(ce->name, base, baselen)) {
283 it->entry_count = 1;
284 oidcpy(&it->oid, &ce->oid);
285 return 1;
289 if (0 <= it->entry_count && repo_has_object_file(the_repository, &it->oid))
290 return it->entry_count;
293 * We first scan for subtrees and update them; we start by
294 * marking existing subtrees -- the ones that are unmarked
295 * should not be in the result.
297 for (i = 0; i < it->subtree_nr; i++)
298 it->down[i]->used = 0;
301 * Find the subtrees and update them.
303 i = 0;
304 while (i < entries) {
305 const struct cache_entry *ce = cache[i];
306 struct cache_tree_sub *sub;
307 const char *path, *slash;
308 int pathlen, sublen, subcnt, subskip;
310 path = ce->name;
311 pathlen = ce_namelen(ce);
312 if (pathlen <= baselen || memcmp(base, path, baselen))
313 break; /* at the end of this level */
315 slash = strchr(path + baselen, '/');
316 if (!slash) {
317 i++;
318 continue;
321 * a/bbb/c (base = a/, slash = /c)
322 * ==>
323 * path+baselen = bbb/c, sublen = 3
325 sublen = slash - (path + baselen);
326 sub = find_subtree(it, path + baselen, sublen, 1);
327 if (!sub->cache_tree)
328 sub->cache_tree = cache_tree();
329 subcnt = update_one(sub->cache_tree,
330 cache + i, entries - i,
331 path,
332 baselen + sublen + 1,
333 &subskip,
334 flags);
335 if (subcnt < 0)
336 return subcnt;
337 if (!subcnt)
338 die("index cache-tree records empty sub-tree");
339 i += subcnt;
340 sub->count = subcnt; /* to be used in the next loop */
341 *skip_count += subskip;
342 sub->used = 1;
345 discard_unused_subtrees(it);
348 * Then write out the tree object for this level.
350 strbuf_init(&buffer, 8192);
352 i = 0;
353 while (i < entries) {
354 const struct cache_entry *ce = cache[i];
355 struct cache_tree_sub *sub = NULL;
356 const char *path, *slash;
357 int pathlen, entlen;
358 const struct object_id *oid;
359 unsigned mode;
360 int expected_missing = 0;
361 int contains_ita = 0;
362 int ce_missing_ok;
364 path = ce->name;
365 pathlen = ce_namelen(ce);
366 if (pathlen <= baselen || memcmp(base, path, baselen))
367 break; /* at the end of this level */
369 slash = strchr(path + baselen, '/');
370 if (slash) {
371 entlen = slash - (path + baselen);
372 sub = find_subtree(it, path + baselen, entlen, 0);
373 if (!sub)
374 die("cache-tree.c: '%.*s' in '%s' not found",
375 entlen, path + baselen, path);
376 i += sub->count;
377 oid = &sub->cache_tree->oid;
378 mode = S_IFDIR;
379 contains_ita = sub->cache_tree->entry_count < 0;
380 if (contains_ita) {
381 to_invalidate = 1;
382 expected_missing = 1;
385 else {
386 oid = &ce->oid;
387 mode = ce->ce_mode;
388 entlen = pathlen - baselen;
389 i++;
392 ce_missing_ok = mode == S_IFGITLINK || missing_ok ||
393 !must_check_existence(ce);
394 if (is_null_oid(oid) ||
395 (!ce_missing_ok && !repo_has_object_file(the_repository, oid))) {
396 strbuf_release(&buffer);
397 if (expected_missing)
398 return -1;
399 return error("invalid object %06o %s for '%.*s'",
400 mode, oid_to_hex(oid), entlen+baselen, path);
404 * CE_REMOVE entries are removed before the index is
405 * written to disk. Skip them to remain consistent
406 * with the future on-disk index.
408 if (ce->ce_flags & CE_REMOVE) {
409 *skip_count = *skip_count + 1;
410 continue;
414 * CE_INTENT_TO_ADD entries exist in on-disk index but
415 * they are not part of generated trees. Invalidate up
416 * to root to force cache-tree users to read elsewhere.
418 if (!sub && ce_intent_to_add(ce)) {
419 to_invalidate = 1;
420 continue;
424 * "sub" can be an empty tree if all subentries are i-t-a.
426 if (contains_ita && is_empty_tree_oid(oid))
427 continue;
429 strbuf_grow(&buffer, entlen + 100);
430 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
431 strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz);
433 #if DEBUG_CACHE_TREE
434 fprintf(stderr, "cache-tree update-one %o %.*s\n",
435 mode, entlen, path + baselen);
436 #endif
439 if (repair) {
440 struct object_id oid;
441 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
442 OBJ_TREE, &oid);
443 if (repo_has_object_file_with_flags(the_repository, &oid, OBJECT_INFO_SKIP_FETCH_OBJECT))
444 oidcpy(&it->oid, &oid);
445 else
446 to_invalidate = 1;
447 } else if (dryrun) {
448 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
449 OBJ_TREE, &it->oid);
450 } else if (write_object_file_flags(buffer.buf, buffer.len, OBJ_TREE,
451 &it->oid, flags & WRITE_TREE_SILENT
452 ? HASH_SILENT : 0)) {
453 strbuf_release(&buffer);
454 return -1;
457 strbuf_release(&buffer);
458 it->entry_count = to_invalidate ? -1 : i - *skip_count;
459 #if DEBUG_CACHE_TREE
460 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
461 it->entry_count, it->subtree_nr,
462 oid_to_hex(&it->oid));
463 #endif
464 return i;
467 int cache_tree_update(struct index_state *istate, int flags)
469 int skip, i;
471 i = verify_cache(istate, flags);
473 if (i)
474 return i;
476 if (!istate->cache_tree)
477 istate->cache_tree = cache_tree();
479 if (!(flags & WRITE_TREE_MISSING_OK) && repo_has_promisor_remote(the_repository))
480 prefetch_cache_entries(istate, must_check_existence);
482 trace_performance_enter();
483 trace2_region_enter("cache_tree", "update", the_repository);
484 begin_odb_transaction();
485 i = update_one(istate->cache_tree, istate->cache, istate->cache_nr,
486 "", 0, &skip, flags);
487 end_odb_transaction();
488 trace2_region_leave("cache_tree", "update", the_repository);
489 trace_performance_leave("cache_tree_update");
490 if (i < 0)
491 return i;
492 istate->cache_changed |= CACHE_TREE_CHANGED;
493 return 0;
496 static void write_one(struct strbuf *buffer, struct cache_tree *it,
497 const char *path, int pathlen)
499 int i;
501 /* One "cache-tree" entry consists of the following:
502 * path (NUL terminated)
503 * entry_count, subtree_nr ("%d %d\n")
504 * tree-sha1 (missing if invalid)
505 * subtree_nr "cache-tree" entries for subtrees.
507 strbuf_grow(buffer, pathlen + 100);
508 strbuf_add(buffer, path, pathlen);
509 strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
511 #if DEBUG_CACHE_TREE
512 if (0 <= it->entry_count)
513 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
514 pathlen, path, it->entry_count, it->subtree_nr,
515 oid_to_hex(&it->oid));
516 else
517 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
518 pathlen, path, it->subtree_nr);
519 #endif
521 if (0 <= it->entry_count) {
522 strbuf_add(buffer, it->oid.hash, the_hash_algo->rawsz);
524 for (i = 0; i < it->subtree_nr; i++) {
525 struct cache_tree_sub *down = it->down[i];
526 if (i) {
527 struct cache_tree_sub *prev = it->down[i-1];
528 if (subtree_name_cmp(down->name, down->namelen,
529 prev->name, prev->namelen) <= 0)
530 die("fatal - unsorted cache subtree");
532 write_one(buffer, down->cache_tree, down->name, down->namelen);
536 void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
538 trace2_region_enter("cache_tree", "write", the_repository);
539 write_one(sb, root, "", 0);
540 trace2_region_leave("cache_tree", "write", the_repository);
543 static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
545 const char *buf = *buffer;
546 unsigned long size = *size_p;
547 const char *cp;
548 char *ep;
549 struct cache_tree *it;
550 int i, subtree_nr;
551 const unsigned rawsz = the_hash_algo->rawsz;
553 it = NULL;
554 /* skip name, but make sure name exists */
555 while (size && *buf) {
556 size--;
557 buf++;
559 if (!size)
560 goto free_return;
561 buf++; size--;
562 it = cache_tree();
564 cp = buf;
565 it->entry_count = strtol(cp, &ep, 10);
566 if (cp == ep)
567 goto free_return;
568 cp = ep;
569 subtree_nr = strtol(cp, &ep, 10);
570 if (cp == ep)
571 goto free_return;
572 while (size && *buf && *buf != '\n') {
573 size--;
574 buf++;
576 if (!size)
577 goto free_return;
578 buf++; size--;
579 if (0 <= it->entry_count) {
580 if (size < rawsz)
581 goto free_return;
582 oidread(&it->oid, (const unsigned char *)buf);
583 buf += rawsz;
584 size -= rawsz;
587 #if DEBUG_CACHE_TREE
588 if (0 <= it->entry_count)
589 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
590 *buffer, it->entry_count, subtree_nr,
591 oid_to_hex(&it->oid));
592 else
593 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
594 *buffer, subtree_nr);
595 #endif
598 * Just a heuristic -- we do not add directories that often but
599 * we do not want to have to extend it immediately when we do,
600 * hence +2.
602 it->subtree_alloc = subtree_nr + 2;
603 CALLOC_ARRAY(it->down, it->subtree_alloc);
604 for (i = 0; i < subtree_nr; i++) {
605 /* read each subtree */
606 struct cache_tree *sub;
607 struct cache_tree_sub *subtree;
608 const char *name = buf;
610 sub = read_one(&buf, &size);
611 if (!sub)
612 goto free_return;
613 subtree = cache_tree_sub(it, name);
614 subtree->cache_tree = sub;
616 if (subtree_nr != it->subtree_nr)
617 die("cache-tree: internal error");
618 *buffer = buf;
619 *size_p = size;
620 return it;
622 free_return:
623 cache_tree_free(&it);
624 return NULL;
627 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
629 struct cache_tree *result;
631 if (buffer[0])
632 return NULL; /* not the whole tree */
634 trace2_region_enter("cache_tree", "read", the_repository);
635 result = read_one(&buffer, &size);
636 trace2_region_leave("cache_tree", "read", the_repository);
638 return result;
641 static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
643 if (!it)
644 return NULL;
645 while (*path) {
646 const char *slash;
647 struct cache_tree_sub *sub;
649 slash = strchrnul(path, '/');
651 * Between path and slash is the name of the subtree
652 * to look for.
654 sub = find_subtree(it, path, slash - path, 0);
655 if (!sub)
656 return NULL;
657 it = sub->cache_tree;
659 path = slash;
660 while (*path == '/')
661 path++;
663 return it;
666 static int write_index_as_tree_internal(struct object_id *oid,
667 struct index_state *index_state,
668 int cache_tree_valid,
669 int flags,
670 const char *prefix)
672 if (flags & WRITE_TREE_IGNORE_CACHE_TREE) {
673 cache_tree_free(&index_state->cache_tree);
674 cache_tree_valid = 0;
677 if (!cache_tree_valid && cache_tree_update(index_state, flags) < 0)
678 return WRITE_TREE_UNMERGED_INDEX;
680 if (prefix) {
681 struct cache_tree *subtree;
682 subtree = cache_tree_find(index_state->cache_tree, prefix);
683 if (!subtree)
684 return WRITE_TREE_PREFIX_ERROR;
685 oidcpy(oid, &subtree->oid);
687 else
688 oidcpy(oid, &index_state->cache_tree->oid);
690 return 0;
693 struct tree* write_in_core_index_as_tree(struct repository *repo) {
694 struct object_id o;
695 int was_valid, ret;
697 struct index_state *index_state = repo->index;
698 was_valid = index_state->cache_tree &&
699 cache_tree_fully_valid(index_state->cache_tree);
701 ret = write_index_as_tree_internal(&o, index_state, was_valid, 0, NULL);
702 if (ret == WRITE_TREE_UNMERGED_INDEX) {
703 int i;
704 bug("there are unmerged index entries:");
705 for (i = 0; i < index_state->cache_nr; i++) {
706 const struct cache_entry *ce = index_state->cache[i];
707 if (ce_stage(ce))
708 bug("%d %.*s", ce_stage(ce),
709 (int)ce_namelen(ce), ce->name);
711 BUG("unmerged index entries when writing in-core index");
714 return lookup_tree(repo, &index_state->cache_tree->oid);
718 int write_index_as_tree(struct object_id *oid, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
720 int entries, was_valid;
721 struct lock_file lock_file = LOCK_INIT;
722 int ret;
724 hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
726 entries = read_index_from(index_state, index_path, get_git_dir());
727 if (entries < 0) {
728 ret = WRITE_TREE_UNREADABLE_INDEX;
729 goto out;
732 was_valid = !(flags & WRITE_TREE_IGNORE_CACHE_TREE) &&
733 index_state->cache_tree &&
734 cache_tree_fully_valid(index_state->cache_tree);
736 ret = write_index_as_tree_internal(oid, index_state, was_valid, flags,
737 prefix);
738 if (!ret && !was_valid) {
739 write_locked_index(index_state, &lock_file, COMMIT_LOCK);
740 /* Not being able to write is fine -- we are only interested
741 * in updating the cache-tree part, and if the next caller
742 * ends up using the old index with unupdated cache-tree part
743 * it misses the work we did here, but that is just a
744 * performance penalty and not a big deal.
748 out:
749 rollback_lock_file(&lock_file);
750 return ret;
753 static void prime_cache_tree_sparse_dir(struct cache_tree *it,
754 struct tree *tree)
757 oidcpy(&it->oid, &tree->object.oid);
758 it->entry_count = 1;
761 static void prime_cache_tree_rec(struct repository *r,
762 struct cache_tree *it,
763 struct tree *tree,
764 struct strbuf *tree_path)
766 struct tree_desc desc;
767 struct name_entry entry;
768 int cnt;
769 size_t base_path_len = tree_path->len;
771 oidcpy(&it->oid, &tree->object.oid);
773 init_tree_desc(&desc, tree->buffer, tree->size);
774 cnt = 0;
775 while (tree_entry(&desc, &entry)) {
776 if (!S_ISDIR(entry.mode))
777 cnt++;
778 else {
779 struct cache_tree_sub *sub;
780 struct tree *subtree = lookup_tree(r, &entry.oid);
782 if (!subtree->object.parsed)
783 parse_tree(subtree);
784 sub = cache_tree_sub(it, entry.path);
785 sub->cache_tree = cache_tree();
788 * Recursively-constructed subtree path is only needed when working
789 * in a sparse index (where it's used to determine whether the
790 * subtree is a sparse directory in the index).
792 if (r->index->sparse_index) {
793 strbuf_setlen(tree_path, base_path_len);
794 strbuf_add(tree_path, entry.path, entry.pathlen);
795 strbuf_addch(tree_path, '/');
799 * If a sparse index is in use, the directory being processed may be
800 * sparse. To confirm that, we can check whether an entry with that
801 * exact name exists in the index. If it does, the created subtree
802 * should be sparse. Otherwise, cache tree expansion should continue
803 * as normal.
805 if (r->index->sparse_index &&
806 index_entry_exists(r->index, tree_path->buf, tree_path->len))
807 prime_cache_tree_sparse_dir(sub->cache_tree, subtree);
808 else
809 prime_cache_tree_rec(r, sub->cache_tree, subtree, tree_path);
810 cnt += sub->cache_tree->entry_count;
814 it->entry_count = cnt;
817 void prime_cache_tree(struct repository *r,
818 struct index_state *istate,
819 struct tree *tree)
821 struct strbuf tree_path = STRBUF_INIT;
823 trace2_region_enter("cache-tree", "prime_cache_tree", r);
824 cache_tree_free(&istate->cache_tree);
825 istate->cache_tree = cache_tree();
827 prime_cache_tree_rec(r, istate->cache_tree, tree, &tree_path);
828 strbuf_release(&tree_path);
829 istate->cache_changed |= CACHE_TREE_CHANGED;
830 trace2_region_leave("cache-tree", "prime_cache_tree", r);
834 * find the cache_tree that corresponds to the current level without
835 * exploding the full path into textual form. The root of the
836 * cache tree is given as "root", and our current level is "info".
837 * (1) When at root level, info->prev is NULL, so it is "root" itself.
838 * (2) Otherwise, find the cache_tree that corresponds to one level
839 * above us, and find ourselves in there.
841 static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
842 struct traverse_info *info)
844 struct cache_tree *our_parent;
846 if (!info->prev)
847 return root;
848 our_parent = find_cache_tree_from_traversal(root, info->prev);
849 return cache_tree_find(our_parent, info->name);
852 int cache_tree_matches_traversal(struct cache_tree *root,
853 struct name_entry *ent,
854 struct traverse_info *info)
856 struct cache_tree *it;
858 it = find_cache_tree_from_traversal(root, info);
859 it = cache_tree_find(it, ent->path);
860 if (it && it->entry_count > 0 && oideq(&ent->oid, &it->oid))
861 return it->entry_count;
862 return 0;
865 static void verify_one_sparse(struct index_state *istate,
866 struct strbuf *path,
867 int pos)
869 struct cache_entry *ce = istate->cache[pos];
871 if (!S_ISSPARSEDIR(ce->ce_mode))
872 BUG("directory '%s' is present in index, but not sparse",
873 path->buf);
877 * Returns:
878 * 0 - Verification completed.
879 * 1 - Restart verification - a call to ensure_full_index() freed the cache
880 * tree that is being verified and verification needs to be restarted from
881 * the new toplevel cache tree.
883 static int verify_one(struct repository *r,
884 struct index_state *istate,
885 struct cache_tree *it,
886 struct strbuf *path)
888 int i, pos, len = path->len;
889 struct strbuf tree_buf = STRBUF_INIT;
890 struct object_id new_oid;
892 for (i = 0; i < it->subtree_nr; i++) {
893 strbuf_addf(path, "%s/", it->down[i]->name);
894 if (verify_one(r, istate, it->down[i]->cache_tree, path))
895 return 1;
896 strbuf_setlen(path, len);
899 if (it->entry_count < 0 ||
900 /* no verification on tests (t7003) that replace trees */
901 lookup_replace_object(r, &it->oid) != &it->oid)
902 return 0;
904 if (path->len) {
906 * If the index is sparse and the cache tree is not
907 * index_name_pos() may trigger ensure_full_index() which will
908 * free the tree that is being verified.
910 int is_sparse = istate->sparse_index;
911 pos = index_name_pos(istate, path->buf, path->len);
912 if (is_sparse && !istate->sparse_index)
913 return 1;
915 if (pos >= 0) {
916 verify_one_sparse(istate, path, pos);
917 return 0;
920 pos = -pos - 1;
921 } else {
922 pos = 0;
925 i = 0;
926 while (i < it->entry_count) {
927 struct cache_entry *ce = istate->cache[pos + i];
928 const char *slash;
929 struct cache_tree_sub *sub = NULL;
930 const struct object_id *oid;
931 const char *name;
932 unsigned mode;
933 int entlen;
935 if (ce->ce_flags & (CE_STAGEMASK | CE_INTENT_TO_ADD | CE_REMOVE))
936 BUG("%s with flags 0x%x should not be in cache-tree",
937 ce->name, ce->ce_flags);
938 name = ce->name + path->len;
939 slash = strchr(name, '/');
940 if (slash) {
941 entlen = slash - name;
942 sub = find_subtree(it, ce->name + path->len, entlen, 0);
943 if (!sub || sub->cache_tree->entry_count < 0)
944 BUG("bad subtree '%.*s'", entlen, name);
945 oid = &sub->cache_tree->oid;
946 mode = S_IFDIR;
947 i += sub->cache_tree->entry_count;
948 } else {
949 oid = &ce->oid;
950 mode = ce->ce_mode;
951 entlen = ce_namelen(ce) - path->len;
952 i++;
954 strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0');
955 strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz);
957 hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, OBJ_TREE,
958 &new_oid);
959 if (!oideq(&new_oid, &it->oid))
960 BUG("cache-tree for path %.*s does not match. "
961 "Expected %s got %s", len, path->buf,
962 oid_to_hex(&new_oid), oid_to_hex(&it->oid));
963 strbuf_setlen(path, len);
964 strbuf_release(&tree_buf);
965 return 0;
968 void cache_tree_verify(struct repository *r, struct index_state *istate)
970 struct strbuf path = STRBUF_INIT;
972 if (!istate->cache_tree)
973 return;
974 if (verify_one(r, istate, istate->cache_tree, &path)) {
975 strbuf_reset(&path);
976 if (verify_one(r, istate, istate->cache_tree, &path))
977 BUG("ensure_full_index() called twice while verifying cache tree");
979 strbuf_release(&path);