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