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