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