midx: make some functions non-static
[alt-git.git] / cache-tree.c
blob2fb483d3c0838998677d53327100db7bc134e2f3
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"
10 #ifndef DEBUG_CACHE_TREE
11 #define DEBUG_CACHE_TREE 0
12 #endif
14 struct cache_tree *cache_tree(void)
16 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
17 it->entry_count = -1;
18 return it;
21 void cache_tree_free(struct cache_tree **it_p)
23 int i;
24 struct cache_tree *it = *it_p;
26 if (!it)
27 return;
28 for (i = 0; i < it->subtree_nr; i++)
29 if (it->down[i]) {
30 cache_tree_free(&it->down[i]->cache_tree);
31 free(it->down[i]);
33 free(it->down);
34 free(it);
35 *it_p = NULL;
38 static int subtree_name_cmp(const char *one, int onelen,
39 const char *two, int twolen)
41 if (onelen < twolen)
42 return -1;
43 if (twolen < onelen)
44 return 1;
45 return memcmp(one, two, onelen);
48 int cache_tree_subtree_pos(struct cache_tree *it, const char *path, int pathlen)
50 struct cache_tree_sub **down = it->down;
51 int lo, hi;
52 lo = 0;
53 hi = it->subtree_nr;
54 while (lo < hi) {
55 int mi = lo + (hi - lo) / 2;
56 struct cache_tree_sub *mdl = down[mi];
57 int cmp = subtree_name_cmp(path, pathlen,
58 mdl->name, mdl->namelen);
59 if (!cmp)
60 return mi;
61 if (cmp < 0)
62 hi = mi;
63 else
64 lo = mi + 1;
66 return -lo-1;
69 static struct cache_tree_sub *find_subtree(struct cache_tree *it,
70 const char *path,
71 int pathlen,
72 int create)
74 struct cache_tree_sub *down;
75 int pos = cache_tree_subtree_pos(it, path, pathlen);
76 if (0 <= pos)
77 return it->down[pos];
78 if (!create)
79 return NULL;
81 pos = -pos-1;
82 ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
83 it->subtree_nr++;
85 FLEX_ALLOC_MEM(down, name, path, pathlen);
86 down->cache_tree = NULL;
87 down->namelen = pathlen;
89 if (pos < it->subtree_nr)
90 MOVE_ARRAY(it->down + pos + 1, it->down + pos,
91 it->subtree_nr - pos - 1);
92 it->down[pos] = down;
93 return down;
96 struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
98 int pathlen = strlen(path);
99 return find_subtree(it, path, pathlen, 1);
102 static int do_invalidate_path(struct cache_tree *it, const char *path)
104 /* a/b/c
105 * ==> invalidate self
106 * ==> find "a", have it invalidate "b/c"
108 * ==> invalidate self
109 * ==> if "a" exists as a subtree, remove it.
111 const char *slash;
112 int namelen;
113 struct cache_tree_sub *down;
115 #if DEBUG_CACHE_TREE
116 fprintf(stderr, "cache-tree invalidate <%s>\n", path);
117 #endif
119 if (!it)
120 return 0;
121 slash = strchrnul(path, '/');
122 namelen = slash - path;
123 it->entry_count = -1;
124 if (!*slash) {
125 int pos;
126 pos = cache_tree_subtree_pos(it, path, namelen);
127 if (0 <= pos) {
128 cache_tree_free(&it->down[pos]->cache_tree);
129 free(it->down[pos]);
130 /* 0 1 2 3 4 5
131 * ^ ^subtree_nr = 6
132 * pos
133 * move 4 and 5 up one place (2 entries)
134 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
136 MOVE_ARRAY(it->down + pos, it->down + pos + 1,
137 it->subtree_nr - pos - 1);
138 it->subtree_nr--;
140 return 1;
142 down = find_subtree(it, path, namelen, 0);
143 if (down)
144 do_invalidate_path(down->cache_tree, slash + 1);
145 return 1;
148 void cache_tree_invalidate_path(struct index_state *istate, const char *path)
150 if (do_invalidate_path(istate->cache_tree, path))
151 istate->cache_changed |= CACHE_TREE_CHANGED;
154 static int verify_cache(struct index_state *istate, int flags)
156 unsigned i, funny;
157 int silent = flags & WRITE_TREE_SILENT;
159 /* Verify that the tree is merged */
160 funny = 0;
161 for (i = 0; i < istate->cache_nr; i++) {
162 const struct cache_entry *ce = istate->cache[i];
163 if (ce_stage(ce)) {
164 if (silent)
165 return -1;
166 if (10 < ++funny) {
167 fprintf(stderr, "...\n");
168 break;
170 fprintf(stderr, "%s: unmerged (%s)\n",
171 ce->name, oid_to_hex(&ce->oid));
174 if (funny)
175 return -1;
177 /* Also verify that the cache does not have path and path/file
178 * at the same time. At this point we know the cache has only
179 * stage 0 entries.
181 funny = 0;
182 for (i = 0; i + 1 < istate->cache_nr; i++) {
183 /* path/file always comes after path because of the way
184 * the cache is sorted. Also path can appear only once,
185 * which means conflicting one would immediately follow.
187 const struct cache_entry *this_ce = istate->cache[i];
188 const struct cache_entry *next_ce = istate->cache[i + 1];
189 const char *this_name = this_ce->name;
190 const char *next_name = next_ce->name;
191 int this_len = ce_namelen(this_ce);
192 if (this_len < ce_namelen(next_ce) &&
193 next_name[this_len] == '/' &&
194 strncmp(this_name, next_name, this_len) == 0) {
195 if (10 < ++funny) {
196 fprintf(stderr, "...\n");
197 break;
199 fprintf(stderr, "You have both %s and %s\n",
200 this_name, next_name);
203 if (funny)
204 return -1;
205 return 0;
208 static void discard_unused_subtrees(struct cache_tree *it)
210 struct cache_tree_sub **down = it->down;
211 int nr = it->subtree_nr;
212 int dst, src;
213 for (dst = src = 0; src < nr; src++) {
214 struct cache_tree_sub *s = down[src];
215 if (s->used)
216 down[dst++] = s;
217 else {
218 cache_tree_free(&s->cache_tree);
219 free(s);
220 it->subtree_nr--;
225 int cache_tree_fully_valid(struct cache_tree *it)
227 int i;
228 if (!it)
229 return 0;
230 if (it->entry_count < 0 || !has_object_file(&it->oid))
231 return 0;
232 for (i = 0; i < it->subtree_nr; i++) {
233 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
234 return 0;
236 return 1;
239 static int update_one(struct cache_tree *it,
240 struct cache_entry **cache,
241 int entries,
242 const char *base,
243 int baselen,
244 int *skip_count,
245 int flags)
247 struct strbuf buffer;
248 int missing_ok = flags & WRITE_TREE_MISSING_OK;
249 int dryrun = flags & WRITE_TREE_DRY_RUN;
250 int repair = flags & WRITE_TREE_REPAIR;
251 int to_invalidate = 0;
252 int i;
254 assert(!(dryrun && repair));
256 *skip_count = 0;
258 if (0 <= it->entry_count && has_object_file(&it->oid))
259 return it->entry_count;
262 * We first scan for subtrees and update them; we start by
263 * marking existing subtrees -- the ones that are unmarked
264 * should not be in the result.
266 for (i = 0; i < it->subtree_nr; i++)
267 it->down[i]->used = 0;
270 * Find the subtrees and update them.
272 i = 0;
273 while (i < entries) {
274 const struct cache_entry *ce = cache[i];
275 struct cache_tree_sub *sub;
276 const char *path, *slash;
277 int pathlen, sublen, subcnt, subskip;
279 path = ce->name;
280 pathlen = ce_namelen(ce);
281 if (pathlen <= baselen || memcmp(base, path, baselen))
282 break; /* at the end of this level */
284 slash = strchr(path + baselen, '/');
285 if (!slash) {
286 i++;
287 continue;
290 * a/bbb/c (base = a/, slash = /c)
291 * ==>
292 * path+baselen = bbb/c, sublen = 3
294 sublen = slash - (path + baselen);
295 sub = find_subtree(it, path + baselen, sublen, 1);
296 if (!sub->cache_tree)
297 sub->cache_tree = cache_tree();
298 subcnt = update_one(sub->cache_tree,
299 cache + i, entries - i,
300 path,
301 baselen + sublen + 1,
302 &subskip,
303 flags);
304 if (subcnt < 0)
305 return subcnt;
306 if (!subcnt)
307 die("index cache-tree records empty sub-tree");
308 i += subcnt;
309 sub->count = subcnt; /* to be used in the next loop */
310 *skip_count += subskip;
311 sub->used = 1;
314 discard_unused_subtrees(it);
317 * Then write out the tree object for this level.
319 strbuf_init(&buffer, 8192);
321 i = 0;
322 while (i < entries) {
323 const struct cache_entry *ce = cache[i];
324 struct cache_tree_sub *sub = NULL;
325 const char *path, *slash;
326 int pathlen, entlen;
327 const struct object_id *oid;
328 unsigned mode;
329 int expected_missing = 0;
330 int contains_ita = 0;
331 int ce_missing_ok;
333 path = ce->name;
334 pathlen = ce_namelen(ce);
335 if (pathlen <= baselen || memcmp(base, path, baselen))
336 break; /* at the end of this level */
338 slash = strchr(path + baselen, '/');
339 if (slash) {
340 entlen = slash - (path + baselen);
341 sub = find_subtree(it, path + baselen, entlen, 0);
342 if (!sub)
343 die("cache-tree.c: '%.*s' in '%s' not found",
344 entlen, path + baselen, path);
345 i += sub->count;
346 oid = &sub->cache_tree->oid;
347 mode = S_IFDIR;
348 contains_ita = sub->cache_tree->entry_count < 0;
349 if (contains_ita) {
350 to_invalidate = 1;
351 expected_missing = 1;
354 else {
355 oid = &ce->oid;
356 mode = ce->ce_mode;
357 entlen = pathlen - baselen;
358 i++;
361 ce_missing_ok = mode == S_IFGITLINK || missing_ok ||
362 (has_promisor_remote() &&
363 ce_skip_worktree(ce));
364 if (is_null_oid(oid) ||
365 (!ce_missing_ok && !has_object_file(oid))) {
366 strbuf_release(&buffer);
367 if (expected_missing)
368 return -1;
369 return error("invalid object %06o %s for '%.*s'",
370 mode, oid_to_hex(oid), entlen+baselen, path);
374 * CE_REMOVE entries are removed before the index is
375 * written to disk. Skip them to remain consistent
376 * with the future on-disk index.
378 if (ce->ce_flags & CE_REMOVE) {
379 *skip_count = *skip_count + 1;
380 continue;
384 * CE_INTENT_TO_ADD entries exist on on-disk index but
385 * they are not part of generated trees. Invalidate up
386 * to root to force cache-tree users to read elsewhere.
388 if (!sub && ce_intent_to_add(ce)) {
389 to_invalidate = 1;
390 continue;
394 * "sub" can be an empty tree if all subentries are i-t-a.
396 if (contains_ita && is_empty_tree_oid(oid))
397 continue;
399 strbuf_grow(&buffer, entlen + 100);
400 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
401 strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz);
403 #if DEBUG_CACHE_TREE
404 fprintf(stderr, "cache-tree update-one %o %.*s\n",
405 mode, entlen, path + baselen);
406 #endif
409 if (repair) {
410 struct object_id oid;
411 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
412 tree_type, &oid);
413 if (has_object_file_with_flags(&oid, OBJECT_INFO_SKIP_FETCH_OBJECT))
414 oidcpy(&it->oid, &oid);
415 else
416 to_invalidate = 1;
417 } else if (dryrun) {
418 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
419 tree_type, &it->oid);
420 } else if (write_object_file(buffer.buf, buffer.len, tree_type,
421 &it->oid)) {
422 strbuf_release(&buffer);
423 return -1;
426 strbuf_release(&buffer);
427 it->entry_count = to_invalidate ? -1 : i - *skip_count;
428 #if DEBUG_CACHE_TREE
429 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
430 it->entry_count, it->subtree_nr,
431 oid_to_hex(&it->oid));
432 #endif
433 return i;
436 int cache_tree_update(struct index_state *istate, int flags)
438 int skip, i;
440 i = verify_cache(istate, flags);
442 if (i)
443 return i;
445 if (!istate->cache_tree)
446 istate->cache_tree = cache_tree();
448 trace_performance_enter();
449 trace2_region_enter("cache_tree", "update", the_repository);
450 i = update_one(istate->cache_tree, istate->cache, istate->cache_nr,
451 "", 0, &skip, flags);
452 trace2_region_leave("cache_tree", "update", the_repository);
453 trace_performance_leave("cache_tree_update");
454 if (i < 0)
455 return i;
456 istate->cache_changed |= CACHE_TREE_CHANGED;
457 return 0;
460 static void write_one(struct strbuf *buffer, struct cache_tree *it,
461 const char *path, int pathlen)
463 int i;
465 /* One "cache-tree" entry consists of the following:
466 * path (NUL terminated)
467 * entry_count, subtree_nr ("%d %d\n")
468 * tree-sha1 (missing if invalid)
469 * subtree_nr "cache-tree" entries for subtrees.
471 strbuf_grow(buffer, pathlen + 100);
472 strbuf_add(buffer, path, pathlen);
473 strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
475 #if DEBUG_CACHE_TREE
476 if (0 <= it->entry_count)
477 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
478 pathlen, path, it->entry_count, it->subtree_nr,
479 oid_to_hex(&it->oid));
480 else
481 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
482 pathlen, path, it->subtree_nr);
483 #endif
485 if (0 <= it->entry_count) {
486 strbuf_add(buffer, it->oid.hash, the_hash_algo->rawsz);
488 for (i = 0; i < it->subtree_nr; i++) {
489 struct cache_tree_sub *down = it->down[i];
490 if (i) {
491 struct cache_tree_sub *prev = it->down[i-1];
492 if (subtree_name_cmp(down->name, down->namelen,
493 prev->name, prev->namelen) <= 0)
494 die("fatal - unsorted cache subtree");
496 write_one(buffer, down->cache_tree, down->name, down->namelen);
500 void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
502 trace2_region_enter("cache_tree", "write", the_repository);
503 write_one(sb, root, "", 0);
504 trace2_region_leave("cache_tree", "write", the_repository);
507 static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
509 const char *buf = *buffer;
510 unsigned long size = *size_p;
511 const char *cp;
512 char *ep;
513 struct cache_tree *it;
514 int i, subtree_nr;
515 const unsigned rawsz = the_hash_algo->rawsz;
517 it = NULL;
518 /* skip name, but make sure name exists */
519 while (size && *buf) {
520 size--;
521 buf++;
523 if (!size)
524 goto free_return;
525 buf++; size--;
526 it = cache_tree();
528 cp = buf;
529 it->entry_count = strtol(cp, &ep, 10);
530 if (cp == ep)
531 goto free_return;
532 cp = ep;
533 subtree_nr = strtol(cp, &ep, 10);
534 if (cp == ep)
535 goto free_return;
536 while (size && *buf && *buf != '\n') {
537 size--;
538 buf++;
540 if (!size)
541 goto free_return;
542 buf++; size--;
543 if (0 <= it->entry_count) {
544 if (size < rawsz)
545 goto free_return;
546 oidread(&it->oid, (const unsigned char *)buf);
547 buf += rawsz;
548 size -= rawsz;
551 #if DEBUG_CACHE_TREE
552 if (0 <= it->entry_count)
553 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
554 *buffer, it->entry_count, subtree_nr,
555 oid_to_hex(&it->oid));
556 else
557 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
558 *buffer, subtree_nr);
559 #endif
562 * Just a heuristic -- we do not add directories that often but
563 * we do not want to have to extend it immediately when we do,
564 * hence +2.
566 it->subtree_alloc = subtree_nr + 2;
567 it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
568 for (i = 0; i < subtree_nr; i++) {
569 /* read each subtree */
570 struct cache_tree *sub;
571 struct cache_tree_sub *subtree;
572 const char *name = buf;
574 sub = read_one(&buf, &size);
575 if (!sub)
576 goto free_return;
577 subtree = cache_tree_sub(it, name);
578 subtree->cache_tree = sub;
580 if (subtree_nr != it->subtree_nr)
581 die("cache-tree: internal error");
582 *buffer = buf;
583 *size_p = size;
584 return it;
586 free_return:
587 cache_tree_free(&it);
588 return NULL;
591 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
593 struct cache_tree *result;
595 if (buffer[0])
596 return NULL; /* not the whole tree */
598 trace2_region_enter("cache_tree", "read", the_repository);
599 result = read_one(&buffer, &size);
600 trace2_region_leave("cache_tree", "read", the_repository);
602 return result;
605 static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
607 if (!it)
608 return NULL;
609 while (*path) {
610 const char *slash;
611 struct cache_tree_sub *sub;
613 slash = strchrnul(path, '/');
615 * Between path and slash is the name of the subtree
616 * to look for.
618 sub = find_subtree(it, path, slash - path, 0);
619 if (!sub)
620 return NULL;
621 it = sub->cache_tree;
623 path = slash;
624 while (*path == '/')
625 path++;
627 return it;
630 static int write_index_as_tree_internal(struct object_id *oid,
631 struct index_state *index_state,
632 int cache_tree_valid,
633 int flags,
634 const char *prefix)
636 if (flags & WRITE_TREE_IGNORE_CACHE_TREE) {
637 cache_tree_free(&index_state->cache_tree);
638 cache_tree_valid = 0;
641 if (!cache_tree_valid && cache_tree_update(index_state, flags) < 0)
642 return WRITE_TREE_UNMERGED_INDEX;
644 if (prefix) {
645 struct cache_tree *subtree;
646 subtree = cache_tree_find(index_state->cache_tree, prefix);
647 if (!subtree)
648 return WRITE_TREE_PREFIX_ERROR;
649 oidcpy(oid, &subtree->oid);
651 else
652 oidcpy(oid, &index_state->cache_tree->oid);
654 return 0;
657 struct tree* write_in_core_index_as_tree(struct repository *repo) {
658 struct object_id o;
659 int was_valid, ret;
661 struct index_state *index_state = repo->index;
662 was_valid = index_state->cache_tree &&
663 cache_tree_fully_valid(index_state->cache_tree);
665 ret = write_index_as_tree_internal(&o, index_state, was_valid, 0, NULL);
666 if (ret == WRITE_TREE_UNMERGED_INDEX) {
667 int i;
668 fprintf(stderr, "BUG: There are unmerged index entries:\n");
669 for (i = 0; i < index_state->cache_nr; i++) {
670 const struct cache_entry *ce = index_state->cache[i];
671 if (ce_stage(ce))
672 fprintf(stderr, "BUG: %d %.*s\n", ce_stage(ce),
673 (int)ce_namelen(ce), ce->name);
675 BUG("unmerged index entries when writing inmemory index");
678 return lookup_tree(repo, &index_state->cache_tree->oid);
682 int write_index_as_tree(struct object_id *oid, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
684 int entries, was_valid;
685 struct lock_file lock_file = LOCK_INIT;
686 int ret;
688 hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
690 entries = read_index_from(index_state, index_path, get_git_dir());
691 if (entries < 0) {
692 ret = WRITE_TREE_UNREADABLE_INDEX;
693 goto out;
696 was_valid = !(flags & WRITE_TREE_IGNORE_CACHE_TREE) &&
697 index_state->cache_tree &&
698 cache_tree_fully_valid(index_state->cache_tree);
700 ret = write_index_as_tree_internal(oid, index_state, was_valid, flags,
701 prefix);
702 if (!ret && !was_valid) {
703 write_locked_index(index_state, &lock_file, COMMIT_LOCK);
704 /* Not being able to write is fine -- we are only interested
705 * in updating the cache-tree part, and if the next caller
706 * ends up using the old index with unupdated cache-tree part
707 * it misses the work we did here, but that is just a
708 * performance penalty and not a big deal.
712 out:
713 rollback_lock_file(&lock_file);
714 return ret;
717 static void prime_cache_tree_rec(struct repository *r,
718 struct cache_tree *it,
719 struct tree *tree)
721 struct tree_desc desc;
722 struct name_entry entry;
723 int cnt;
725 oidcpy(&it->oid, &tree->object.oid);
726 init_tree_desc(&desc, tree->buffer, tree->size);
727 cnt = 0;
728 while (tree_entry(&desc, &entry)) {
729 if (!S_ISDIR(entry.mode))
730 cnt++;
731 else {
732 struct cache_tree_sub *sub;
733 struct tree *subtree = lookup_tree(r, &entry.oid);
734 if (!subtree->object.parsed)
735 parse_tree(subtree);
736 sub = cache_tree_sub(it, entry.path);
737 sub->cache_tree = cache_tree();
738 prime_cache_tree_rec(r, sub->cache_tree, subtree);
739 cnt += sub->cache_tree->entry_count;
742 it->entry_count = cnt;
745 void prime_cache_tree(struct repository *r,
746 struct index_state *istate,
747 struct tree *tree)
749 trace2_region_enter("cache-tree", "prime_cache_tree", the_repository);
750 cache_tree_free(&istate->cache_tree);
751 istate->cache_tree = cache_tree();
753 prime_cache_tree_rec(r, istate->cache_tree, tree);
754 istate->cache_changed |= CACHE_TREE_CHANGED;
755 trace2_region_leave("cache-tree", "prime_cache_tree", the_repository);
759 * find the cache_tree that corresponds to the current level without
760 * exploding the full path into textual form. The root of the
761 * cache tree is given as "root", and our current level is "info".
762 * (1) When at root level, info->prev is NULL, so it is "root" itself.
763 * (2) Otherwise, find the cache_tree that corresponds to one level
764 * above us, and find ourselves in there.
766 static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
767 struct traverse_info *info)
769 struct cache_tree *our_parent;
771 if (!info->prev)
772 return root;
773 our_parent = find_cache_tree_from_traversal(root, info->prev);
774 return cache_tree_find(our_parent, info->name);
777 int cache_tree_matches_traversal(struct cache_tree *root,
778 struct name_entry *ent,
779 struct traverse_info *info)
781 struct cache_tree *it;
783 it = find_cache_tree_from_traversal(root, info);
784 it = cache_tree_find(it, ent->path);
785 if (it && it->entry_count > 0 && oideq(&ent->oid, &it->oid))
786 return it->entry_count;
787 return 0;
790 static void verify_one(struct repository *r,
791 struct index_state *istate,
792 struct cache_tree *it,
793 struct strbuf *path)
795 int i, pos, len = path->len;
796 struct strbuf tree_buf = STRBUF_INIT;
797 struct object_id new_oid;
799 for (i = 0; i < it->subtree_nr; i++) {
800 strbuf_addf(path, "%s/", it->down[i]->name);
801 verify_one(r, istate, it->down[i]->cache_tree, path);
802 strbuf_setlen(path, len);
805 if (it->entry_count < 0 ||
806 /* no verification on tests (t7003) that replace trees */
807 lookup_replace_object(r, &it->oid) != &it->oid)
808 return;
810 if (path->len) {
811 pos = index_name_pos(istate, path->buf, path->len);
812 pos = -pos - 1;
813 } else {
814 pos = 0;
817 i = 0;
818 while (i < it->entry_count) {
819 struct cache_entry *ce = istate->cache[pos + i];
820 const char *slash;
821 struct cache_tree_sub *sub = NULL;
822 const struct object_id *oid;
823 const char *name;
824 unsigned mode;
825 int entlen;
827 if (ce->ce_flags & (CE_STAGEMASK | CE_INTENT_TO_ADD | CE_REMOVE))
828 BUG("%s with flags 0x%x should not be in cache-tree",
829 ce->name, ce->ce_flags);
830 name = ce->name + path->len;
831 slash = strchr(name, '/');
832 if (slash) {
833 entlen = slash - name;
834 sub = find_subtree(it, ce->name + path->len, entlen, 0);
835 if (!sub || sub->cache_tree->entry_count < 0)
836 BUG("bad subtree '%.*s'", entlen, name);
837 oid = &sub->cache_tree->oid;
838 mode = S_IFDIR;
839 i += sub->cache_tree->entry_count;
840 } else {
841 oid = &ce->oid;
842 mode = ce->ce_mode;
843 entlen = ce_namelen(ce) - path->len;
844 i++;
846 strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0');
847 strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz);
849 hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, tree_type,
850 &new_oid);
851 if (!oideq(&new_oid, &it->oid))
852 BUG("cache-tree for path %.*s does not match. "
853 "Expected %s got %s", len, path->buf,
854 oid_to_hex(&new_oid), oid_to_hex(&it->oid));
855 strbuf_setlen(path, len);
856 strbuf_release(&tree_buf);
859 void cache_tree_verify(struct repository *r, struct index_state *istate)
861 struct strbuf path = STRBUF_INIT;
863 if (!istate->cache_tree)
864 return;
865 verify_one(r, istate, istate->cache_tree, &path);
866 strbuf_release(&path);