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