t/helper: fix leaking buffer in "dump-untracked-cache"
[git/gitster.git] / cache-tree.c
blobb482167a69ae6acaee05a500850a529e6ae425f9
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "hex.h"
5 #include "lockfile.h"
6 #include "tree.h"
7 #include "tree-walk.h"
8 #include "cache-tree.h"
9 #include "bulk-checkin.h"
10 #include "object-file.h"
11 #include "object-store-ll.h"
12 #include "read-cache-ll.h"
13 #include "replace-object.h"
14 #include "repository.h"
15 #include "promisor-remote.h"
16 #include "trace.h"
17 #include "trace2.h"
19 #ifndef DEBUG_CACHE_TREE
20 #define DEBUG_CACHE_TREE 0
21 #endif
23 struct cache_tree *cache_tree(void)
25 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
26 it->entry_count = -1;
27 return it;
30 void cache_tree_free(struct cache_tree **it_p)
32 int i;
33 struct cache_tree *it = *it_p;
35 if (!it)
36 return;
37 for (i = 0; i < it->subtree_nr; i++)
38 if (it->down[i]) {
39 cache_tree_free(&it->down[i]->cache_tree);
40 free(it->down[i]);
42 free(it->down);
43 free(it);
44 *it_p = NULL;
47 static int subtree_name_cmp(const char *one, int onelen,
48 const char *two, int twolen)
50 if (onelen < twolen)
51 return -1;
52 if (twolen < onelen)
53 return 1;
54 return memcmp(one, two, onelen);
57 int cache_tree_subtree_pos(struct cache_tree *it, const char *path, int pathlen)
59 struct cache_tree_sub **down = it->down;
60 int lo, hi;
61 lo = 0;
62 hi = it->subtree_nr;
63 while (lo < hi) {
64 int mi = lo + (hi - lo) / 2;
65 struct cache_tree_sub *mdl = down[mi];
66 int cmp = subtree_name_cmp(path, pathlen,
67 mdl->name, mdl->namelen);
68 if (!cmp)
69 return mi;
70 if (cmp < 0)
71 hi = mi;
72 else
73 lo = mi + 1;
75 return -lo-1;
78 static struct cache_tree_sub *find_subtree(struct cache_tree *it,
79 const char *path,
80 int pathlen,
81 int create)
83 struct cache_tree_sub *down;
84 int pos = cache_tree_subtree_pos(it, path, pathlen);
85 if (0 <= pos)
86 return it->down[pos];
87 if (!create)
88 return NULL;
90 pos = -pos-1;
91 ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
92 it->subtree_nr++;
94 FLEX_ALLOC_MEM(down, name, path, pathlen);
95 down->cache_tree = NULL;
96 down->namelen = pathlen;
98 if (pos < it->subtree_nr)
99 MOVE_ARRAY(it->down + pos + 1, it->down + pos,
100 it->subtree_nr - pos - 1);
101 it->down[pos] = down;
102 return down;
105 struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
107 int pathlen = strlen(path);
108 return find_subtree(it, path, pathlen, 1);
111 static int do_invalidate_path(struct cache_tree *it, const char *path)
113 /* a/b/c
114 * ==> invalidate self
115 * ==> find "a", have it invalidate "b/c"
117 * ==> invalidate self
118 * ==> if "a" exists as a subtree, remove it.
120 const char *slash;
121 int namelen;
122 struct cache_tree_sub *down;
124 #if DEBUG_CACHE_TREE
125 fprintf(stderr, "cache-tree invalidate <%s>\n", path);
126 #endif
128 if (!it)
129 return 0;
130 slash = strchrnul(path, '/');
131 namelen = slash - path;
132 it->entry_count = -1;
133 if (!*slash) {
134 int pos;
135 pos = cache_tree_subtree_pos(it, path, namelen);
136 if (0 <= pos) {
137 cache_tree_free(&it->down[pos]->cache_tree);
138 free(it->down[pos]);
139 /* 0 1 2 3 4 5
140 * ^ ^subtree_nr = 6
141 * pos
142 * move 4 and 5 up one place (2 entries)
143 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
145 MOVE_ARRAY(it->down + pos, it->down + pos + 1,
146 it->subtree_nr - pos - 1);
147 it->subtree_nr--;
149 return 1;
151 down = find_subtree(it, path, namelen, 0);
152 if (down)
153 do_invalidate_path(down->cache_tree, slash + 1);
154 return 1;
157 void cache_tree_invalidate_path(struct index_state *istate, const char *path)
159 if (do_invalidate_path(istate->cache_tree, path))
160 istate->cache_changed |= CACHE_TREE_CHANGED;
163 static int verify_cache(struct index_state *istate, int flags)
165 unsigned i, funny;
166 int silent = flags & WRITE_TREE_SILENT;
168 /* Verify that the tree is merged */
169 funny = 0;
170 for (i = 0; i < istate->cache_nr; i++) {
171 const struct cache_entry *ce = istate->cache[i];
172 if (ce_stage(ce)) {
173 if (silent)
174 return -1;
175 if (10 < ++funny) {
176 fprintf(stderr, "...\n");
177 break;
179 fprintf(stderr, "%s: unmerged (%s)\n",
180 ce->name, oid_to_hex(&ce->oid));
183 if (funny)
184 return -1;
186 /* Also verify that the cache does not have path and path/file
187 * at the same time. At this point we know the cache has only
188 * stage 0 entries.
190 funny = 0;
191 for (i = 0; i + 1 < istate->cache_nr; i++) {
192 /* path/file always comes after path because of the way
193 * the cache is sorted. Also path can appear only once,
194 * which means conflicting one would immediately follow.
196 const struct cache_entry *this_ce = istate->cache[i];
197 const struct cache_entry *next_ce = istate->cache[i + 1];
198 const char *this_name = this_ce->name;
199 const char *next_name = next_ce->name;
200 int this_len = ce_namelen(this_ce);
201 if (this_len < ce_namelen(next_ce) &&
202 next_name[this_len] == '/' &&
203 strncmp(this_name, next_name, this_len) == 0) {
204 if (10 < ++funny) {
205 fprintf(stderr, "...\n");
206 break;
208 fprintf(stderr, "You have both %s and %s\n",
209 this_name, next_name);
212 if (funny)
213 return -1;
214 return 0;
217 static void discard_unused_subtrees(struct cache_tree *it)
219 struct cache_tree_sub **down = it->down;
220 int nr = it->subtree_nr;
221 int dst, src;
222 for (dst = src = 0; src < nr; src++) {
223 struct cache_tree_sub *s = down[src];
224 if (s->used)
225 down[dst++] = s;
226 else {
227 cache_tree_free(&s->cache_tree);
228 free(s);
229 it->subtree_nr--;
234 int cache_tree_fully_valid(struct cache_tree *it)
236 int i;
237 if (!it)
238 return 0;
239 if (it->entry_count < 0 || !repo_has_object_file(the_repository, &it->oid))
240 return 0;
241 for (i = 0; i < it->subtree_nr; i++) {
242 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
243 return 0;
245 return 1;
248 static int must_check_existence(const struct cache_entry *ce)
250 return !(repo_has_promisor_remote(the_repository) && ce_skip_worktree(ce));
253 static int update_one(struct cache_tree *it,
254 struct cache_entry **cache,
255 int entries,
256 const char *base,
257 int baselen,
258 int *skip_count,
259 int flags)
261 struct strbuf buffer;
262 int missing_ok = flags & WRITE_TREE_MISSING_OK;
263 int dryrun = flags & WRITE_TREE_DRY_RUN;
264 int repair = flags & WRITE_TREE_REPAIR;
265 int to_invalidate = 0;
266 int i;
268 assert(!(dryrun && repair));
270 *skip_count = 0;
273 * If the first entry of this region is a sparse directory
274 * entry corresponding exactly to 'base', then this cache_tree
275 * struct is a "leaf" in the data structure, pointing to the
276 * tree OID specified in the entry.
278 if (entries > 0) {
279 const struct cache_entry *ce = cache[0];
281 if (S_ISSPARSEDIR(ce->ce_mode) &&
282 ce->ce_namelen == baselen &&
283 !strncmp(ce->name, base, baselen)) {
284 it->entry_count = 1;
285 oidcpy(&it->oid, &ce->oid);
286 return 1;
290 if (0 <= it->entry_count && repo_has_object_file(the_repository, &it->oid))
291 return it->entry_count;
294 * We first scan for subtrees and update them; we start by
295 * marking existing subtrees -- the ones that are unmarked
296 * should not be in the result.
298 for (i = 0; i < it->subtree_nr; i++)
299 it->down[i]->used = 0;
302 * Find the subtrees and update them.
304 i = 0;
305 while (i < entries) {
306 const struct cache_entry *ce = cache[i];
307 struct cache_tree_sub *sub;
308 const char *path, *slash;
309 int pathlen, sublen, subcnt, subskip;
311 path = ce->name;
312 pathlen = ce_namelen(ce);
313 if (pathlen <= baselen || memcmp(base, path, baselen))
314 break; /* at the end of this level */
316 slash = strchr(path + baselen, '/');
317 if (!slash) {
318 i++;
319 continue;
322 * a/bbb/c (base = a/, slash = /c)
323 * ==>
324 * path+baselen = bbb/c, sublen = 3
326 sublen = slash - (path + baselen);
327 sub = find_subtree(it, path + baselen, sublen, 1);
328 if (!sub->cache_tree)
329 sub->cache_tree = cache_tree();
330 subcnt = update_one(sub->cache_tree,
331 cache + i, entries - i,
332 path,
333 baselen + sublen + 1,
334 &subskip,
335 flags);
336 if (subcnt < 0)
337 return subcnt;
338 if (!subcnt)
339 die("index cache-tree records empty sub-tree");
340 i += subcnt;
341 sub->count = subcnt; /* to be used in the next loop */
342 *skip_count += subskip;
343 sub->used = 1;
346 discard_unused_subtrees(it);
349 * Then write out the tree object for this level.
351 strbuf_init(&buffer, 8192);
353 i = 0;
354 while (i < entries) {
355 const struct cache_entry *ce = cache[i];
356 struct cache_tree_sub *sub = NULL;
357 const char *path, *slash;
358 int pathlen, entlen;
359 const struct object_id *oid;
360 unsigned mode;
361 int expected_missing = 0;
362 int contains_ita = 0;
363 int ce_missing_ok;
365 path = ce->name;
366 pathlen = ce_namelen(ce);
367 if (pathlen <= baselen || memcmp(base, path, baselen))
368 break; /* at the end of this level */
370 slash = strchr(path + baselen, '/');
371 if (slash) {
372 entlen = slash - (path + baselen);
373 sub = find_subtree(it, path + baselen, entlen, 0);
374 if (!sub)
375 die("cache-tree.c: '%.*s' in '%s' not found",
376 entlen, path + baselen, path);
377 i += sub->count;
378 oid = &sub->cache_tree->oid;
379 mode = S_IFDIR;
380 contains_ita = sub->cache_tree->entry_count < 0;
381 if (contains_ita) {
382 to_invalidate = 1;
383 expected_missing = 1;
386 else {
387 oid = &ce->oid;
388 mode = ce->ce_mode;
389 entlen = pathlen - baselen;
390 i++;
393 ce_missing_ok = mode == S_IFGITLINK || missing_ok ||
394 !must_check_existence(ce);
395 if (is_null_oid(oid) ||
396 (!ce_missing_ok && !repo_has_object_file(the_repository, oid))) {
397 strbuf_release(&buffer);
398 if (expected_missing)
399 return -1;
400 return error("invalid object %06o %s for '%.*s'",
401 mode, oid_to_hex(oid), entlen+baselen, path);
405 * CE_REMOVE entries are removed before the index is
406 * written to disk. Skip them to remain consistent
407 * with the future on-disk index.
409 if (ce->ce_flags & CE_REMOVE) {
410 *skip_count = *skip_count + 1;
411 continue;
415 * CE_INTENT_TO_ADD entries exist in on-disk index but
416 * they are not part of generated trees. Invalidate up
417 * to root to force cache-tree users to read elsewhere.
419 if (!sub && ce_intent_to_add(ce)) {
420 to_invalidate = 1;
421 continue;
425 * "sub" can be an empty tree if all subentries are i-t-a.
427 if (contains_ita && is_empty_tree_oid(oid, the_repository->hash_algo))
428 continue;
430 strbuf_grow(&buffer, entlen + 100);
431 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
432 strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz);
434 #if DEBUG_CACHE_TREE
435 fprintf(stderr, "cache-tree update-one %o %.*s\n",
436 mode, entlen, path + baselen);
437 #endif
440 if (repair) {
441 struct object_id oid;
442 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
443 OBJ_TREE, &oid);
444 if (repo_has_object_file_with_flags(the_repository, &oid, OBJECT_INFO_SKIP_FETCH_OBJECT))
445 oidcpy(&it->oid, &oid);
446 else
447 to_invalidate = 1;
448 } else if (dryrun) {
449 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
450 OBJ_TREE, &it->oid);
451 } else if (write_object_file_flags(buffer.buf, buffer.len, OBJ_TREE,
452 &it->oid, NULL, flags & WRITE_TREE_SILENT
453 ? HASH_SILENT : 0)) {
454 strbuf_release(&buffer);
455 return -1;
458 strbuf_release(&buffer);
459 it->entry_count = to_invalidate ? -1 : i - *skip_count;
460 #if DEBUG_CACHE_TREE
461 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
462 it->entry_count, it->subtree_nr,
463 oid_to_hex(&it->oid));
464 #endif
465 return i;
468 int cache_tree_update(struct index_state *istate, int flags)
470 int skip, i;
472 i = verify_cache(istate, flags);
474 if (i)
475 return i;
477 if (!istate->cache_tree)
478 istate->cache_tree = cache_tree();
480 if (!(flags & WRITE_TREE_MISSING_OK) && repo_has_promisor_remote(the_repository))
481 prefetch_cache_entries(istate, must_check_existence);
483 trace_performance_enter();
484 trace2_region_enter("cache_tree", "update", the_repository);
485 begin_odb_transaction();
486 i = update_one(istate->cache_tree, istate->cache, istate->cache_nr,
487 "", 0, &skip, flags);
488 end_odb_transaction();
489 trace2_region_leave("cache_tree", "update", the_repository);
490 trace_performance_leave("cache_tree_update");
491 if (i < 0)
492 return i;
493 istate->cache_changed |= CACHE_TREE_CHANGED;
494 return 0;
497 static void write_one(struct strbuf *buffer, struct cache_tree *it,
498 const char *path, int pathlen)
500 int i;
502 /* One "cache-tree" entry consists of the following:
503 * path (NUL terminated)
504 * entry_count, subtree_nr ("%d %d\n")
505 * tree-sha1 (missing if invalid)
506 * subtree_nr "cache-tree" entries for subtrees.
508 strbuf_grow(buffer, pathlen + 100);
509 strbuf_add(buffer, path, pathlen);
510 strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
512 #if DEBUG_CACHE_TREE
513 if (0 <= it->entry_count)
514 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
515 pathlen, path, it->entry_count, it->subtree_nr,
516 oid_to_hex(&it->oid));
517 else
518 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
519 pathlen, path, it->subtree_nr);
520 #endif
522 if (0 <= it->entry_count) {
523 strbuf_add(buffer, it->oid.hash, the_hash_algo->rawsz);
525 for (i = 0; i < it->subtree_nr; i++) {
526 struct cache_tree_sub *down = it->down[i];
527 if (i) {
528 struct cache_tree_sub *prev = it->down[i-1];
529 if (subtree_name_cmp(down->name, down->namelen,
530 prev->name, prev->namelen) <= 0)
531 die("fatal - unsorted cache subtree");
533 write_one(buffer, down->cache_tree, down->name, down->namelen);
537 void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
539 trace2_region_enter("cache_tree", "write", the_repository);
540 write_one(sb, root, "", 0);
541 trace2_region_leave("cache_tree", "write", the_repository);
544 static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
546 const char *buf = *buffer;
547 unsigned long size = *size_p;
548 const char *cp;
549 char *ep;
550 struct cache_tree *it;
551 int i, subtree_nr;
552 const unsigned rawsz = the_hash_algo->rawsz;
554 it = NULL;
555 /* skip name, but make sure name exists */
556 while (size && *buf) {
557 size--;
558 buf++;
560 if (!size)
561 goto free_return;
562 buf++; size--;
563 it = cache_tree();
565 cp = buf;
566 it->entry_count = strtol(cp, &ep, 10);
567 if (cp == ep)
568 goto free_return;
569 cp = ep;
570 subtree_nr = strtol(cp, &ep, 10);
571 if (cp == ep)
572 goto free_return;
573 while (size && *buf && *buf != '\n') {
574 size--;
575 buf++;
577 if (!size)
578 goto free_return;
579 buf++; size--;
580 if (0 <= it->entry_count) {
581 if (size < rawsz)
582 goto free_return;
583 oidread(&it->oid, (const unsigned char *)buf,
584 the_repository->hash_algo);
585 buf += rawsz;
586 size -= rawsz;
589 #if DEBUG_CACHE_TREE
590 if (0 <= it->entry_count)
591 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
592 *buffer, it->entry_count, subtree_nr,
593 oid_to_hex(&it->oid));
594 else
595 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
596 *buffer, subtree_nr);
597 #endif
600 * Just a heuristic -- we do not add directories that often but
601 * we do not want to have to extend it immediately when we do,
602 * hence +2.
604 it->subtree_alloc = subtree_nr + 2;
605 CALLOC_ARRAY(it->down, it->subtree_alloc);
606 for (i = 0; i < subtree_nr; i++) {
607 /* read each subtree */
608 struct cache_tree *sub;
609 struct cache_tree_sub *subtree;
610 const char *name = buf;
612 sub = read_one(&buf, &size);
613 if (!sub)
614 goto free_return;
615 subtree = cache_tree_sub(it, name);
616 subtree->cache_tree = sub;
618 if (subtree_nr != it->subtree_nr)
619 die("cache-tree: internal error");
620 *buffer = buf;
621 *size_p = size;
622 return it;
624 free_return:
625 cache_tree_free(&it);
626 return NULL;
629 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
631 struct cache_tree *result;
633 if (buffer[0])
634 return NULL; /* not the whole tree */
636 trace2_region_enter("cache_tree", "read", the_repository);
637 result = read_one(&buffer, &size);
638 trace2_region_leave("cache_tree", "read", the_repository);
640 return result;
643 static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
645 if (!it)
646 return NULL;
647 while (*path) {
648 const char *slash;
649 struct cache_tree_sub *sub;
651 slash = strchrnul(path, '/');
653 * Between path and slash is the name of the subtree
654 * to look for.
656 sub = find_subtree(it, path, slash - path, 0);
657 if (!sub)
658 return NULL;
659 it = sub->cache_tree;
661 path = slash;
662 while (*path == '/')
663 path++;
665 return it;
668 static int write_index_as_tree_internal(struct object_id *oid,
669 struct index_state *index_state,
670 int cache_tree_valid,
671 int flags,
672 const char *prefix)
674 if (flags & WRITE_TREE_IGNORE_CACHE_TREE) {
675 cache_tree_free(&index_state->cache_tree);
676 cache_tree_valid = 0;
679 if (!cache_tree_valid && cache_tree_update(index_state, flags) < 0)
680 return WRITE_TREE_UNMERGED_INDEX;
682 if (prefix) {
683 struct cache_tree *subtree;
684 subtree = cache_tree_find(index_state->cache_tree, prefix);
685 if (!subtree)
686 return WRITE_TREE_PREFIX_ERROR;
687 oidcpy(oid, &subtree->oid);
689 else
690 oidcpy(oid, &index_state->cache_tree->oid);
692 return 0;
695 struct tree* write_in_core_index_as_tree(struct repository *repo) {
696 struct object_id o;
697 int was_valid, ret;
699 struct index_state *index_state = repo->index;
700 was_valid = index_state->cache_tree &&
701 cache_tree_fully_valid(index_state->cache_tree);
703 ret = write_index_as_tree_internal(&o, index_state, was_valid, 0, NULL);
704 if (ret == WRITE_TREE_UNMERGED_INDEX) {
705 int i;
706 bug("there are unmerged index entries:");
707 for (i = 0; i < index_state->cache_nr; i++) {
708 const struct cache_entry *ce = index_state->cache[i];
709 if (ce_stage(ce))
710 bug("%d %.*s", ce_stage(ce),
711 (int)ce_namelen(ce), ce->name);
713 BUG("unmerged index entries when writing in-core index");
716 return lookup_tree(repo, &index_state->cache_tree->oid);
720 int write_index_as_tree(struct object_id *oid, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
722 int entries, was_valid;
723 struct lock_file lock_file = LOCK_INIT;
724 int ret;
726 hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
728 entries = read_index_from(index_state, index_path,
729 repo_get_git_dir(the_repository));
730 if (entries < 0) {
731 ret = WRITE_TREE_UNREADABLE_INDEX;
732 goto out;
735 was_valid = !(flags & WRITE_TREE_IGNORE_CACHE_TREE) &&
736 index_state->cache_tree &&
737 cache_tree_fully_valid(index_state->cache_tree);
739 ret = write_index_as_tree_internal(oid, index_state, was_valid, flags,
740 prefix);
741 if (!ret && !was_valid) {
742 write_locked_index(index_state, &lock_file, COMMIT_LOCK);
743 /* Not being able to write is fine -- we are only interested
744 * in updating the cache-tree part, and if the next caller
745 * ends up using the old index with unupdated cache-tree part
746 * it misses the work we did here, but that is just a
747 * performance penalty and not a big deal.
751 out:
752 rollback_lock_file(&lock_file);
753 return ret;
756 static void prime_cache_tree_sparse_dir(struct cache_tree *it,
757 struct tree *tree)
760 oidcpy(&it->oid, &tree->object.oid);
761 it->entry_count = 1;
764 static void prime_cache_tree_rec(struct repository *r,
765 struct cache_tree *it,
766 struct tree *tree,
767 struct strbuf *tree_path)
769 struct tree_desc desc;
770 struct name_entry entry;
771 int cnt;
772 size_t base_path_len = tree_path->len;
774 oidcpy(&it->oid, &tree->object.oid);
776 init_tree_desc(&desc, &tree->object.oid, tree->buffer, tree->size);
777 cnt = 0;
778 while (tree_entry(&desc, &entry)) {
779 if (!S_ISDIR(entry.mode))
780 cnt++;
781 else {
782 struct cache_tree_sub *sub;
783 struct tree *subtree = lookup_tree(r, &entry.oid);
785 if (parse_tree(subtree) < 0)
786 exit(128);
787 sub = cache_tree_sub(it, entry.path);
788 sub->cache_tree = cache_tree();
791 * Recursively-constructed subtree path is only needed when working
792 * in a sparse index (where it's used to determine whether the
793 * subtree is a sparse directory in the index).
795 if (r->index->sparse_index) {
796 strbuf_setlen(tree_path, base_path_len);
797 strbuf_add(tree_path, entry.path, entry.pathlen);
798 strbuf_addch(tree_path, '/');
802 * If a sparse index is in use, the directory being processed may be
803 * sparse. To confirm that, we can check whether an entry with that
804 * exact name exists in the index. If it does, the created subtree
805 * should be sparse. Otherwise, cache tree expansion should continue
806 * as normal.
808 if (r->index->sparse_index &&
809 index_entry_exists(r->index, tree_path->buf, tree_path->len))
810 prime_cache_tree_sparse_dir(sub->cache_tree, subtree);
811 else
812 prime_cache_tree_rec(r, sub->cache_tree, subtree, tree_path);
813 cnt += sub->cache_tree->entry_count;
817 it->entry_count = cnt;
820 void prime_cache_tree(struct repository *r,
821 struct index_state *istate,
822 struct tree *tree)
824 struct strbuf tree_path = STRBUF_INIT;
826 trace2_region_enter("cache-tree", "prime_cache_tree", r);
827 cache_tree_free(&istate->cache_tree);
828 istate->cache_tree = cache_tree();
830 prime_cache_tree_rec(r, istate->cache_tree, tree, &tree_path);
831 strbuf_release(&tree_path);
832 istate->cache_changed |= CACHE_TREE_CHANGED;
833 trace2_region_leave("cache-tree", "prime_cache_tree", r);
837 * find the cache_tree that corresponds to the current level without
838 * exploding the full path into textual form. The root of the
839 * cache tree is given as "root", and our current level is "info".
840 * (1) When at root level, info->prev is NULL, so it is "root" itself.
841 * (2) Otherwise, find the cache_tree that corresponds to one level
842 * above us, and find ourselves in there.
844 static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
845 struct traverse_info *info)
847 struct cache_tree *our_parent;
849 if (!info->prev)
850 return root;
851 our_parent = find_cache_tree_from_traversal(root, info->prev);
852 return cache_tree_find(our_parent, info->name);
855 int cache_tree_matches_traversal(struct cache_tree *root,
856 struct name_entry *ent,
857 struct traverse_info *info)
859 struct cache_tree *it;
861 it = find_cache_tree_from_traversal(root, info);
862 it = cache_tree_find(it, ent->path);
863 if (it && it->entry_count > 0 && oideq(&ent->oid, &it->oid))
864 return it->entry_count;
865 return 0;
868 static void verify_one_sparse(struct index_state *istate,
869 struct strbuf *path,
870 int pos)
872 struct cache_entry *ce = istate->cache[pos];
874 if (!S_ISSPARSEDIR(ce->ce_mode))
875 BUG("directory '%s' is present in index, but not sparse",
876 path->buf);
880 * Returns:
881 * 0 - Verification completed.
882 * 1 - Restart verification - a call to ensure_full_index() freed the cache
883 * tree that is being verified and verification needs to be restarted from
884 * the new toplevel cache tree.
886 static int verify_one(struct repository *r,
887 struct index_state *istate,
888 struct cache_tree *it,
889 struct strbuf *path)
891 int i, pos, len = path->len;
892 struct strbuf tree_buf = STRBUF_INIT;
893 struct object_id new_oid;
895 for (i = 0; i < it->subtree_nr; i++) {
896 strbuf_addf(path, "%s/", it->down[i]->name);
897 if (verify_one(r, istate, it->down[i]->cache_tree, path))
898 return 1;
899 strbuf_setlen(path, len);
902 if (it->entry_count < 0 ||
903 /* no verification on tests (t7003) that replace trees */
904 lookup_replace_object(r, &it->oid) != &it->oid)
905 return 0;
907 if (path->len) {
909 * If the index is sparse and the cache tree is not
910 * index_name_pos() may trigger ensure_full_index() which will
911 * free the tree that is being verified.
913 int is_sparse = istate->sparse_index;
914 pos = index_name_pos(istate, path->buf, path->len);
915 if (is_sparse && !istate->sparse_index)
916 return 1;
918 if (pos >= 0) {
919 verify_one_sparse(istate, path, pos);
920 return 0;
923 pos = -pos - 1;
924 } else {
925 pos = 0;
928 i = 0;
929 while (i < it->entry_count) {
930 struct cache_entry *ce = istate->cache[pos + i];
931 const char *slash;
932 struct cache_tree_sub *sub = NULL;
933 const struct object_id *oid;
934 const char *name;
935 unsigned mode;
936 int entlen;
938 if (ce->ce_flags & (CE_STAGEMASK | CE_INTENT_TO_ADD | CE_REMOVE))
939 BUG("%s with flags 0x%x should not be in cache-tree",
940 ce->name, ce->ce_flags);
941 name = ce->name + path->len;
942 slash = strchr(name, '/');
943 if (slash) {
944 entlen = slash - name;
945 sub = find_subtree(it, ce->name + path->len, entlen, 0);
946 if (!sub || sub->cache_tree->entry_count < 0)
947 BUG("bad subtree '%.*s'", entlen, name);
948 oid = &sub->cache_tree->oid;
949 mode = S_IFDIR;
950 i += sub->cache_tree->entry_count;
951 } else {
952 oid = &ce->oid;
953 mode = ce->ce_mode;
954 entlen = ce_namelen(ce) - path->len;
955 i++;
957 strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0');
958 strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz);
960 hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, OBJ_TREE,
961 &new_oid);
962 if (!oideq(&new_oid, &it->oid))
963 BUG("cache-tree for path %.*s does not match. "
964 "Expected %s got %s", len, path->buf,
965 oid_to_hex(&new_oid), oid_to_hex(&it->oid));
966 strbuf_setlen(path, len);
967 strbuf_release(&tree_buf);
968 return 0;
971 void cache_tree_verify(struct repository *r, struct index_state *istate)
973 struct strbuf path = STRBUF_INIT;
975 if (!istate->cache_tree)
976 return;
977 if (verify_one(r, istate, istate->cache_tree, &path)) {
978 strbuf_reset(&path);
979 if (verify_one(r, istate, istate->cache_tree, &path))
980 BUG("ensure_full_index() called twice while verifying cache tree");
982 strbuf_release(&path);