rebase: deprecate --rebase-merges=""
[git/debian.git] / cache-tree.c
blob9d46ecef09162a2d01d6f4ee508b2b1cdb1c2304
1 #include "git-compat-util.h"
2 #include "alloc.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-store.h"
10 #include "replace-object.h"
11 #include "promisor-remote.h"
12 #include "sparse-index.h"
14 #ifndef DEBUG_CACHE_TREE
15 #define DEBUG_CACHE_TREE 0
16 #endif
18 struct cache_tree *cache_tree(void)
20 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
21 it->entry_count = -1;
22 return it;
25 void cache_tree_free(struct cache_tree **it_p)
27 int i;
28 struct cache_tree *it = *it_p;
30 if (!it)
31 return;
32 for (i = 0; i < it->subtree_nr; i++)
33 if (it->down[i]) {
34 cache_tree_free(&it->down[i]->cache_tree);
35 free(it->down[i]);
37 free(it->down);
38 free(it);
39 *it_p = NULL;
42 static int subtree_name_cmp(const char *one, int onelen,
43 const char *two, int twolen)
45 if (onelen < twolen)
46 return -1;
47 if (twolen < onelen)
48 return 1;
49 return memcmp(one, two, onelen);
52 int cache_tree_subtree_pos(struct cache_tree *it, const char *path, int pathlen)
54 struct cache_tree_sub **down = it->down;
55 int lo, hi;
56 lo = 0;
57 hi = it->subtree_nr;
58 while (lo < hi) {
59 int mi = lo + (hi - lo) / 2;
60 struct cache_tree_sub *mdl = down[mi];
61 int cmp = subtree_name_cmp(path, pathlen,
62 mdl->name, mdl->namelen);
63 if (!cmp)
64 return mi;
65 if (cmp < 0)
66 hi = mi;
67 else
68 lo = mi + 1;
70 return -lo-1;
73 static struct cache_tree_sub *find_subtree(struct cache_tree *it,
74 const char *path,
75 int pathlen,
76 int create)
78 struct cache_tree_sub *down;
79 int pos = cache_tree_subtree_pos(it, path, pathlen);
80 if (0 <= pos)
81 return it->down[pos];
82 if (!create)
83 return NULL;
85 pos = -pos-1;
86 ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
87 it->subtree_nr++;
89 FLEX_ALLOC_MEM(down, name, path, pathlen);
90 down->cache_tree = NULL;
91 down->namelen = pathlen;
93 if (pos < it->subtree_nr)
94 MOVE_ARRAY(it->down + pos + 1, it->down + pos,
95 it->subtree_nr - pos - 1);
96 it->down[pos] = down;
97 return down;
100 struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
102 int pathlen = strlen(path);
103 return find_subtree(it, path, pathlen, 1);
106 static int do_invalidate_path(struct cache_tree *it, const char *path)
108 /* a/b/c
109 * ==> invalidate self
110 * ==> find "a", have it invalidate "b/c"
112 * ==> invalidate self
113 * ==> if "a" exists as a subtree, remove it.
115 const char *slash;
116 int namelen;
117 struct cache_tree_sub *down;
119 #if DEBUG_CACHE_TREE
120 fprintf(stderr, "cache-tree invalidate <%s>\n", path);
121 #endif
123 if (!it)
124 return 0;
125 slash = strchrnul(path, '/');
126 namelen = slash - path;
127 it->entry_count = -1;
128 if (!*slash) {
129 int pos;
130 pos = cache_tree_subtree_pos(it, path, namelen);
131 if (0 <= pos) {
132 cache_tree_free(&it->down[pos]->cache_tree);
133 free(it->down[pos]);
134 /* 0 1 2 3 4 5
135 * ^ ^subtree_nr = 6
136 * pos
137 * move 4 and 5 up one place (2 entries)
138 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
140 MOVE_ARRAY(it->down + pos, it->down + pos + 1,
141 it->subtree_nr - pos - 1);
142 it->subtree_nr--;
144 return 1;
146 down = find_subtree(it, path, namelen, 0);
147 if (down)
148 do_invalidate_path(down->cache_tree, slash + 1);
149 return 1;
152 void cache_tree_invalidate_path(struct index_state *istate, const char *path)
154 if (do_invalidate_path(istate->cache_tree, path))
155 istate->cache_changed |= CACHE_TREE_CHANGED;
158 static int verify_cache(struct index_state *istate, int flags)
160 unsigned i, funny;
161 int silent = flags & WRITE_TREE_SILENT;
163 /* Verify that the tree is merged */
164 funny = 0;
165 for (i = 0; i < istate->cache_nr; i++) {
166 const struct cache_entry *ce = istate->cache[i];
167 if (ce_stage(ce)) {
168 if (silent)
169 return -1;
170 if (10 < ++funny) {
171 fprintf(stderr, "...\n");
172 break;
174 fprintf(stderr, "%s: unmerged (%s)\n",
175 ce->name, oid_to_hex(&ce->oid));
178 if (funny)
179 return -1;
181 /* Also verify that the cache does not have path and path/file
182 * at the same time. At this point we know the cache has only
183 * stage 0 entries.
185 funny = 0;
186 for (i = 0; i + 1 < istate->cache_nr; i++) {
187 /* path/file always comes after path because of the way
188 * the cache is sorted. Also path can appear only once,
189 * which means conflicting one would immediately follow.
191 const struct cache_entry *this_ce = istate->cache[i];
192 const struct cache_entry *next_ce = istate->cache[i + 1];
193 const char *this_name = this_ce->name;
194 const char *next_name = next_ce->name;
195 int this_len = ce_namelen(this_ce);
196 if (this_len < ce_namelen(next_ce) &&
197 next_name[this_len] == '/' &&
198 strncmp(this_name, next_name, this_len) == 0) {
199 if (10 < ++funny) {
200 fprintf(stderr, "...\n");
201 break;
203 fprintf(stderr, "You have both %s and %s\n",
204 this_name, next_name);
207 if (funny)
208 return -1;
209 return 0;
212 static void discard_unused_subtrees(struct cache_tree *it)
214 struct cache_tree_sub **down = it->down;
215 int nr = it->subtree_nr;
216 int dst, src;
217 for (dst = src = 0; src < nr; src++) {
218 struct cache_tree_sub *s = down[src];
219 if (s->used)
220 down[dst++] = s;
221 else {
222 cache_tree_free(&s->cache_tree);
223 free(s);
224 it->subtree_nr--;
229 int cache_tree_fully_valid(struct cache_tree *it)
231 int i;
232 if (!it)
233 return 0;
234 if (it->entry_count < 0 || !has_object_file(&it->oid))
235 return 0;
236 for (i = 0; i < it->subtree_nr; i++) {
237 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
238 return 0;
240 return 1;
243 static int must_check_existence(const struct cache_entry *ce)
245 return !(has_promisor_remote() && ce_skip_worktree(ce));
248 static int update_one(struct cache_tree *it,
249 struct cache_entry **cache,
250 int entries,
251 const char *base,
252 int baselen,
253 int *skip_count,
254 int flags)
256 struct strbuf buffer;
257 int missing_ok = flags & WRITE_TREE_MISSING_OK;
258 int dryrun = flags & WRITE_TREE_DRY_RUN;
259 int repair = flags & WRITE_TREE_REPAIR;
260 int to_invalidate = 0;
261 int i;
263 assert(!(dryrun && repair));
265 *skip_count = 0;
268 * If the first entry of this region is a sparse directory
269 * entry corresponding exactly to 'base', then this cache_tree
270 * struct is a "leaf" in the data structure, pointing to the
271 * tree OID specified in the entry.
273 if (entries > 0) {
274 const struct cache_entry *ce = cache[0];
276 if (S_ISSPARSEDIR(ce->ce_mode) &&
277 ce->ce_namelen == baselen &&
278 !strncmp(ce->name, base, baselen)) {
279 it->entry_count = 1;
280 oidcpy(&it->oid, &ce->oid);
281 return 1;
285 if (0 <= it->entry_count && has_object_file(&it->oid))
286 return it->entry_count;
289 * We first scan for subtrees and update them; we start by
290 * marking existing subtrees -- the ones that are unmarked
291 * should not be in the result.
293 for (i = 0; i < it->subtree_nr; i++)
294 it->down[i]->used = 0;
297 * Find the subtrees and update them.
299 i = 0;
300 while (i < entries) {
301 const struct cache_entry *ce = cache[i];
302 struct cache_tree_sub *sub;
303 const char *path, *slash;
304 int pathlen, sublen, subcnt, subskip;
306 path = ce->name;
307 pathlen = ce_namelen(ce);
308 if (pathlen <= baselen || memcmp(base, path, baselen))
309 break; /* at the end of this level */
311 slash = strchr(path + baselen, '/');
312 if (!slash) {
313 i++;
314 continue;
317 * a/bbb/c (base = a/, slash = /c)
318 * ==>
319 * path+baselen = bbb/c, sublen = 3
321 sublen = slash - (path + baselen);
322 sub = find_subtree(it, path + baselen, sublen, 1);
323 if (!sub->cache_tree)
324 sub->cache_tree = cache_tree();
325 subcnt = update_one(sub->cache_tree,
326 cache + i, entries - i,
327 path,
328 baselen + sublen + 1,
329 &subskip,
330 flags);
331 if (subcnt < 0)
332 return subcnt;
333 if (!subcnt)
334 die("index cache-tree records empty sub-tree");
335 i += subcnt;
336 sub->count = subcnt; /* to be used in the next loop */
337 *skip_count += subskip;
338 sub->used = 1;
341 discard_unused_subtrees(it);
344 * Then write out the tree object for this level.
346 strbuf_init(&buffer, 8192);
348 i = 0;
349 while (i < entries) {
350 const struct cache_entry *ce = cache[i];
351 struct cache_tree_sub *sub = NULL;
352 const char *path, *slash;
353 int pathlen, entlen;
354 const struct object_id *oid;
355 unsigned mode;
356 int expected_missing = 0;
357 int contains_ita = 0;
358 int ce_missing_ok;
360 path = ce->name;
361 pathlen = ce_namelen(ce);
362 if (pathlen <= baselen || memcmp(base, path, baselen))
363 break; /* at the end of this level */
365 slash = strchr(path + baselen, '/');
366 if (slash) {
367 entlen = slash - (path + baselen);
368 sub = find_subtree(it, path + baselen, entlen, 0);
369 if (!sub)
370 die("cache-tree.c: '%.*s' in '%s' not found",
371 entlen, path + baselen, path);
372 i += sub->count;
373 oid = &sub->cache_tree->oid;
374 mode = S_IFDIR;
375 contains_ita = sub->cache_tree->entry_count < 0;
376 if (contains_ita) {
377 to_invalidate = 1;
378 expected_missing = 1;
381 else {
382 oid = &ce->oid;
383 mode = ce->ce_mode;
384 entlen = pathlen - baselen;
385 i++;
388 ce_missing_ok = mode == S_IFGITLINK || missing_ok ||
389 !must_check_existence(ce);
390 if (is_null_oid(oid) ||
391 (!ce_missing_ok && !has_object_file(oid))) {
392 strbuf_release(&buffer);
393 if (expected_missing)
394 return -1;
395 return error("invalid object %06o %s for '%.*s'",
396 mode, oid_to_hex(oid), entlen+baselen, path);
400 * CE_REMOVE entries are removed before the index is
401 * written to disk. Skip them to remain consistent
402 * with the future on-disk index.
404 if (ce->ce_flags & CE_REMOVE) {
405 *skip_count = *skip_count + 1;
406 continue;
410 * CE_INTENT_TO_ADD entries exist in on-disk index but
411 * they are not part of generated trees. Invalidate up
412 * to root to force cache-tree users to read elsewhere.
414 if (!sub && ce_intent_to_add(ce)) {
415 to_invalidate = 1;
416 continue;
420 * "sub" can be an empty tree if all subentries are i-t-a.
422 if (contains_ita && is_empty_tree_oid(oid))
423 continue;
425 strbuf_grow(&buffer, entlen + 100);
426 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
427 strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz);
429 #if DEBUG_CACHE_TREE
430 fprintf(stderr, "cache-tree update-one %o %.*s\n",
431 mode, entlen, path + baselen);
432 #endif
435 if (repair) {
436 struct object_id oid;
437 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
438 OBJ_TREE, &oid);
439 if (has_object_file_with_flags(&oid, OBJECT_INFO_SKIP_FETCH_OBJECT))
440 oidcpy(&it->oid, &oid);
441 else
442 to_invalidate = 1;
443 } else if (dryrun) {
444 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
445 OBJ_TREE, &it->oid);
446 } else if (write_object_file_flags(buffer.buf, buffer.len, OBJ_TREE,
447 &it->oid, flags & WRITE_TREE_SILENT
448 ? HASH_SILENT : 0)) {
449 strbuf_release(&buffer);
450 return -1;
453 strbuf_release(&buffer);
454 it->entry_count = to_invalidate ? -1 : i - *skip_count;
455 #if DEBUG_CACHE_TREE
456 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
457 it->entry_count, it->subtree_nr,
458 oid_to_hex(&it->oid));
459 #endif
460 return i;
463 int cache_tree_update(struct index_state *istate, int flags)
465 int skip, i;
467 i = verify_cache(istate, flags);
469 if (i)
470 return i;
472 if (!istate->cache_tree)
473 istate->cache_tree = cache_tree();
475 if (!(flags & WRITE_TREE_MISSING_OK) && has_promisor_remote())
476 prefetch_cache_entries(istate, must_check_existence);
478 trace_performance_enter();
479 trace2_region_enter("cache_tree", "update", the_repository);
480 begin_odb_transaction();
481 i = update_one(istate->cache_tree, istate->cache, istate->cache_nr,
482 "", 0, &skip, flags);
483 end_odb_transaction();
484 trace2_region_leave("cache_tree", "update", the_repository);
485 trace_performance_leave("cache_tree_update");
486 if (i < 0)
487 return i;
488 istate->cache_changed |= CACHE_TREE_CHANGED;
489 return 0;
492 static void write_one(struct strbuf *buffer, struct cache_tree *it,
493 const char *path, int pathlen)
495 int i;
497 /* One "cache-tree" entry consists of the following:
498 * path (NUL terminated)
499 * entry_count, subtree_nr ("%d %d\n")
500 * tree-sha1 (missing if invalid)
501 * subtree_nr "cache-tree" entries for subtrees.
503 strbuf_grow(buffer, pathlen + 100);
504 strbuf_add(buffer, path, pathlen);
505 strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
507 #if DEBUG_CACHE_TREE
508 if (0 <= it->entry_count)
509 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
510 pathlen, path, it->entry_count, it->subtree_nr,
511 oid_to_hex(&it->oid));
512 else
513 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
514 pathlen, path, it->subtree_nr);
515 #endif
517 if (0 <= it->entry_count) {
518 strbuf_add(buffer, it->oid.hash, the_hash_algo->rawsz);
520 for (i = 0; i < it->subtree_nr; i++) {
521 struct cache_tree_sub *down = it->down[i];
522 if (i) {
523 struct cache_tree_sub *prev = it->down[i-1];
524 if (subtree_name_cmp(down->name, down->namelen,
525 prev->name, prev->namelen) <= 0)
526 die("fatal - unsorted cache subtree");
528 write_one(buffer, down->cache_tree, down->name, down->namelen);
532 void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
534 trace2_region_enter("cache_tree", "write", the_repository);
535 write_one(sb, root, "", 0);
536 trace2_region_leave("cache_tree", "write", the_repository);
539 static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
541 const char *buf = *buffer;
542 unsigned long size = *size_p;
543 const char *cp;
544 char *ep;
545 struct cache_tree *it;
546 int i, subtree_nr;
547 const unsigned rawsz = the_hash_algo->rawsz;
549 it = NULL;
550 /* skip name, but make sure name exists */
551 while (size && *buf) {
552 size--;
553 buf++;
555 if (!size)
556 goto free_return;
557 buf++; size--;
558 it = cache_tree();
560 cp = buf;
561 it->entry_count = strtol(cp, &ep, 10);
562 if (cp == ep)
563 goto free_return;
564 cp = ep;
565 subtree_nr = strtol(cp, &ep, 10);
566 if (cp == ep)
567 goto free_return;
568 while (size && *buf && *buf != '\n') {
569 size--;
570 buf++;
572 if (!size)
573 goto free_return;
574 buf++; size--;
575 if (0 <= it->entry_count) {
576 if (size < rawsz)
577 goto free_return;
578 oidread(&it->oid, (const unsigned char *)buf);
579 buf += rawsz;
580 size -= rawsz;
583 #if DEBUG_CACHE_TREE
584 if (0 <= it->entry_count)
585 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
586 *buffer, it->entry_count, subtree_nr,
587 oid_to_hex(&it->oid));
588 else
589 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
590 *buffer, subtree_nr);
591 #endif
594 * Just a heuristic -- we do not add directories that often but
595 * we do not want to have to extend it immediately when we do,
596 * hence +2.
598 it->subtree_alloc = subtree_nr + 2;
599 CALLOC_ARRAY(it->down, it->subtree_alloc);
600 for (i = 0; i < subtree_nr; i++) {
601 /* read each subtree */
602 struct cache_tree *sub;
603 struct cache_tree_sub *subtree;
604 const char *name = buf;
606 sub = read_one(&buf, &size);
607 if (!sub)
608 goto free_return;
609 subtree = cache_tree_sub(it, name);
610 subtree->cache_tree = sub;
612 if (subtree_nr != it->subtree_nr)
613 die("cache-tree: internal error");
614 *buffer = buf;
615 *size_p = size;
616 return it;
618 free_return:
619 cache_tree_free(&it);
620 return NULL;
623 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
625 struct cache_tree *result;
627 if (buffer[0])
628 return NULL; /* not the whole tree */
630 trace2_region_enter("cache_tree", "read", the_repository);
631 result = read_one(&buffer, &size);
632 trace2_region_leave("cache_tree", "read", the_repository);
634 return result;
637 static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
639 if (!it)
640 return NULL;
641 while (*path) {
642 const char *slash;
643 struct cache_tree_sub *sub;
645 slash = strchrnul(path, '/');
647 * Between path and slash is the name of the subtree
648 * to look for.
650 sub = find_subtree(it, path, slash - path, 0);
651 if (!sub)
652 return NULL;
653 it = sub->cache_tree;
655 path = slash;
656 while (*path == '/')
657 path++;
659 return it;
662 static int write_index_as_tree_internal(struct object_id *oid,
663 struct index_state *index_state,
664 int cache_tree_valid,
665 int flags,
666 const char *prefix)
668 if (flags & WRITE_TREE_IGNORE_CACHE_TREE) {
669 cache_tree_free(&index_state->cache_tree);
670 cache_tree_valid = 0;
673 if (!cache_tree_valid && cache_tree_update(index_state, flags) < 0)
674 return WRITE_TREE_UNMERGED_INDEX;
676 if (prefix) {
677 struct cache_tree *subtree;
678 subtree = cache_tree_find(index_state->cache_tree, prefix);
679 if (!subtree)
680 return WRITE_TREE_PREFIX_ERROR;
681 oidcpy(oid, &subtree->oid);
683 else
684 oidcpy(oid, &index_state->cache_tree->oid);
686 return 0;
689 struct tree* write_in_core_index_as_tree(struct repository *repo) {
690 struct object_id o;
691 int was_valid, ret;
693 struct index_state *index_state = repo->index;
694 was_valid = index_state->cache_tree &&
695 cache_tree_fully_valid(index_state->cache_tree);
697 ret = write_index_as_tree_internal(&o, index_state, was_valid, 0, NULL);
698 if (ret == WRITE_TREE_UNMERGED_INDEX) {
699 int i;
700 bug("there are unmerged index entries:");
701 for (i = 0; i < index_state->cache_nr; i++) {
702 const struct cache_entry *ce = index_state->cache[i];
703 if (ce_stage(ce))
704 bug("%d %.*s", ce_stage(ce),
705 (int)ce_namelen(ce), ce->name);
707 BUG("unmerged index entries when writing in-core index");
710 return lookup_tree(repo, &index_state->cache_tree->oid);
714 int write_index_as_tree(struct object_id *oid, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
716 int entries, was_valid;
717 struct lock_file lock_file = LOCK_INIT;
718 int ret;
720 hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
722 entries = read_index_from(index_state, index_path, get_git_dir());
723 if (entries < 0) {
724 ret = WRITE_TREE_UNREADABLE_INDEX;
725 goto out;
728 was_valid = !(flags & WRITE_TREE_IGNORE_CACHE_TREE) &&
729 index_state->cache_tree &&
730 cache_tree_fully_valid(index_state->cache_tree);
732 ret = write_index_as_tree_internal(oid, index_state, was_valid, flags,
733 prefix);
734 if (!ret && !was_valid) {
735 write_locked_index(index_state, &lock_file, COMMIT_LOCK);
736 /* Not being able to write is fine -- we are only interested
737 * in updating the cache-tree part, and if the next caller
738 * ends up using the old index with unupdated cache-tree part
739 * it misses the work we did here, but that is just a
740 * performance penalty and not a big deal.
744 out:
745 rollback_lock_file(&lock_file);
746 return ret;
749 static void prime_cache_tree_sparse_dir(struct cache_tree *it,
750 struct tree *tree)
753 oidcpy(&it->oid, &tree->object.oid);
754 it->entry_count = 1;
757 static void prime_cache_tree_rec(struct repository *r,
758 struct cache_tree *it,
759 struct tree *tree,
760 struct strbuf *tree_path)
762 struct tree_desc desc;
763 struct name_entry entry;
764 int cnt;
765 size_t base_path_len = tree_path->len;
767 oidcpy(&it->oid, &tree->object.oid);
769 init_tree_desc(&desc, tree->buffer, tree->size);
770 cnt = 0;
771 while (tree_entry(&desc, &entry)) {
772 if (!S_ISDIR(entry.mode))
773 cnt++;
774 else {
775 struct cache_tree_sub *sub;
776 struct tree *subtree = lookup_tree(r, &entry.oid);
778 if (!subtree->object.parsed)
779 parse_tree(subtree);
780 sub = cache_tree_sub(it, entry.path);
781 sub->cache_tree = cache_tree();
784 * Recursively-constructed subtree path is only needed when working
785 * in a sparse index (where it's used to determine whether the
786 * subtree is a sparse directory in the index).
788 if (r->index->sparse_index) {
789 strbuf_setlen(tree_path, base_path_len);
790 strbuf_add(tree_path, entry.path, entry.pathlen);
791 strbuf_addch(tree_path, '/');
795 * If a sparse index is in use, the directory being processed may be
796 * sparse. To confirm that, we can check whether an entry with that
797 * exact name exists in the index. If it does, the created subtree
798 * should be sparse. Otherwise, cache tree expansion should continue
799 * as normal.
801 if (r->index->sparse_index &&
802 index_entry_exists(r->index, tree_path->buf, tree_path->len))
803 prime_cache_tree_sparse_dir(sub->cache_tree, subtree);
804 else
805 prime_cache_tree_rec(r, sub->cache_tree, subtree, tree_path);
806 cnt += sub->cache_tree->entry_count;
810 it->entry_count = cnt;
813 void prime_cache_tree(struct repository *r,
814 struct index_state *istate,
815 struct tree *tree)
817 struct strbuf tree_path = STRBUF_INIT;
819 trace2_region_enter("cache-tree", "prime_cache_tree", the_repository);
820 cache_tree_free(&istate->cache_tree);
821 istate->cache_tree = cache_tree();
823 prime_cache_tree_rec(r, istate->cache_tree, tree, &tree_path);
824 strbuf_release(&tree_path);
825 istate->cache_changed |= CACHE_TREE_CHANGED;
826 trace2_region_leave("cache-tree", "prime_cache_tree", the_repository);
830 * find the cache_tree that corresponds to the current level without
831 * exploding the full path into textual form. The root of the
832 * cache tree is given as "root", and our current level is "info".
833 * (1) When at root level, info->prev is NULL, so it is "root" itself.
834 * (2) Otherwise, find the cache_tree that corresponds to one level
835 * above us, and find ourselves in there.
837 static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
838 struct traverse_info *info)
840 struct cache_tree *our_parent;
842 if (!info->prev)
843 return root;
844 our_parent = find_cache_tree_from_traversal(root, info->prev);
845 return cache_tree_find(our_parent, info->name);
848 int cache_tree_matches_traversal(struct cache_tree *root,
849 struct name_entry *ent,
850 struct traverse_info *info)
852 struct cache_tree *it;
854 it = find_cache_tree_from_traversal(root, info);
855 it = cache_tree_find(it, ent->path);
856 if (it && it->entry_count > 0 && oideq(&ent->oid, &it->oid))
857 return it->entry_count;
858 return 0;
861 static void verify_one_sparse(struct index_state *istate,
862 struct strbuf *path,
863 int pos)
865 struct cache_entry *ce = istate->cache[pos];
867 if (!S_ISSPARSEDIR(ce->ce_mode))
868 BUG("directory '%s' is present in index, but not sparse",
869 path->buf);
873 * Returns:
874 * 0 - Verification completed.
875 * 1 - Restart verification - a call to ensure_full_index() freed the cache
876 * tree that is being verified and verification needs to be restarted from
877 * the new toplevel cache tree.
879 static int verify_one(struct repository *r,
880 struct index_state *istate,
881 struct cache_tree *it,
882 struct strbuf *path)
884 int i, pos, len = path->len;
885 struct strbuf tree_buf = STRBUF_INIT;
886 struct object_id new_oid;
888 for (i = 0; i < it->subtree_nr; i++) {
889 strbuf_addf(path, "%s/", it->down[i]->name);
890 if (verify_one(r, istate, it->down[i]->cache_tree, path))
891 return 1;
892 strbuf_setlen(path, len);
895 if (it->entry_count < 0 ||
896 /* no verification on tests (t7003) that replace trees */
897 lookup_replace_object(r, &it->oid) != &it->oid)
898 return 0;
900 if (path->len) {
902 * If the index is sparse and the cache tree is not
903 * index_name_pos() may trigger ensure_full_index() which will
904 * free the tree that is being verified.
906 int is_sparse = istate->sparse_index;
907 pos = index_name_pos(istate, path->buf, path->len);
908 if (is_sparse && !istate->sparse_index)
909 return 1;
911 if (pos >= 0) {
912 verify_one_sparse(istate, path, pos);
913 return 0;
916 pos = -pos - 1;
917 } else {
918 pos = 0;
921 i = 0;
922 while (i < it->entry_count) {
923 struct cache_entry *ce = istate->cache[pos + i];
924 const char *slash;
925 struct cache_tree_sub *sub = NULL;
926 const struct object_id *oid;
927 const char *name;
928 unsigned mode;
929 int entlen;
931 if (ce->ce_flags & (CE_STAGEMASK | CE_INTENT_TO_ADD | CE_REMOVE))
932 BUG("%s with flags 0x%x should not be in cache-tree",
933 ce->name, ce->ce_flags);
934 name = ce->name + path->len;
935 slash = strchr(name, '/');
936 if (slash) {
937 entlen = slash - name;
938 sub = find_subtree(it, ce->name + path->len, entlen, 0);
939 if (!sub || sub->cache_tree->entry_count < 0)
940 BUG("bad subtree '%.*s'", entlen, name);
941 oid = &sub->cache_tree->oid;
942 mode = S_IFDIR;
943 i += sub->cache_tree->entry_count;
944 } else {
945 oid = &ce->oid;
946 mode = ce->ce_mode;
947 entlen = ce_namelen(ce) - path->len;
948 i++;
950 strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0');
951 strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz);
953 hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, OBJ_TREE,
954 &new_oid);
955 if (!oideq(&new_oid, &it->oid))
956 BUG("cache-tree for path %.*s does not match. "
957 "Expected %s got %s", len, path->buf,
958 oid_to_hex(&new_oid), oid_to_hex(&it->oid));
959 strbuf_setlen(path, len);
960 strbuf_release(&tree_buf);
961 return 0;
964 void cache_tree_verify(struct repository *r, struct index_state *istate)
966 struct strbuf path = STRBUF_INIT;
968 if (!istate->cache_tree)
969 return;
970 if (verify_one(r, istate, istate->cache_tree, &path)) {
971 strbuf_reset(&path);
972 if (verify_one(r, istate, istate->cache_tree, &path))
973 BUG("ensure_full_index() called twice while verifying cache tree");
975 strbuf_release(&path);