cache-tree: use ce_namelen() instead of strlen()
[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 struct cache_entry *this_ce = cache[i];
189                 const struct cache_entry *next_ce = cache[i + 1];
190                 const char *this_name = this_ce->name;
191                 const char *next_name = next_ce->name;
192                 int this_len = ce_namelen(this_ce);
193                 if (this_len < ce_namelen(next_ce) &&
194                     strncmp(this_name, next_name, this_len) == 0 &&
195                     next_name[this_len] == '/') {
196                         if (10 < ++funny) {
197                                 fprintf(stderr, "...\n");
198                                 break;
199                         }
200                         fprintf(stderr, "You have both %s and %s\n",
201                                 this_name, next_name);
202                 }
203         }
204         if (funny)
205                 return -1;
206         return 0;
207 }
208
209 static void discard_unused_subtrees(struct cache_tree *it)
210 {
211         struct cache_tree_sub **down = it->down;
212         int nr = it->subtree_nr;
213         int dst, src;
214         for (dst = src = 0; src < nr; src++) {
215                 struct cache_tree_sub *s = down[src];
216                 if (s->used)
217                         down[dst++] = s;
218                 else {
219                         cache_tree_free(&s->cache_tree);
220                         free(s);
221                         it->subtree_nr--;
222                 }
223         }
224 }
225
226 int cache_tree_fully_valid(struct cache_tree *it)
227 {
228         int i;
229         if (!it)
230                 return 0;
231         if (it->entry_count < 0 || !has_object_file(&it->oid))
232                 return 0;
233         for (i = 0; i < it->subtree_nr; i++) {
234                 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
235                         return 0;
236         }
237         return 1;
238 }
239
240 static int update_one(struct cache_tree *it,
241                       struct cache_entry **cache,
242                       int entries,
243                       const char *base,
244                       int baselen,
245                       int *skip_count,
246                       int flags)
247 {
248         struct strbuf buffer;
249         int missing_ok = flags & WRITE_TREE_MISSING_OK;
250         int dryrun = flags & WRITE_TREE_DRY_RUN;
251         int repair = flags & WRITE_TREE_REPAIR;
252         int to_invalidate = 0;
253         int i;
254
255         assert(!(dryrun && repair));
256
257         *skip_count = 0;
258
259         if (0 <= it->entry_count && has_object_file(&it->oid))
260                 return it->entry_count;
261
262         /*
263          * We first scan for subtrees and update them; we start by
264          * marking existing subtrees -- the ones that are unmarked
265          * should not be in the result.
266          */
267         for (i = 0; i < it->subtree_nr; i++)
268                 it->down[i]->used = 0;
269
270         /*
271          * Find the subtrees and update them.
272          */
273         i = 0;
274         while (i < entries) {
275                 const struct cache_entry *ce = cache[i];
276                 struct cache_tree_sub *sub;
277                 const char *path, *slash;
278                 int pathlen, sublen, subcnt, subskip;
279
280                 path = ce->name;
281                 pathlen = ce_namelen(ce);
282                 if (pathlen <= baselen || memcmp(base, path, baselen))
283                         break; /* at the end of this level */
284
285                 slash = strchr(path + baselen, '/');
286                 if (!slash) {
287                         i++;
288                         continue;
289                 }
290                 /*
291                  * a/bbb/c (base = a/, slash = /c)
292                  * ==>
293                  * path+baselen = bbb/c, sublen = 3
294                  */
295                 sublen = slash - (path + baselen);
296                 sub = find_subtree(it, path + baselen, sublen, 1);
297                 if (!sub->cache_tree)
298                         sub->cache_tree = cache_tree();
299                 subcnt = update_one(sub->cache_tree,
300                                     cache + i, entries - i,
301                                     path,
302                                     baselen + sublen + 1,
303                                     &subskip,
304                                     flags);
305                 if (subcnt < 0)
306                         return subcnt;
307                 if (!subcnt)
308                         die("index cache-tree records empty sub-tree");
309                 i += subcnt;
310                 sub->count = subcnt; /* to be used in the next loop */
311                 *skip_count += subskip;
312                 sub->used = 1;
313         }
314
315         discard_unused_subtrees(it);
316
317         /*
318          * Then write out the tree object for this level.
319          */
320         strbuf_init(&buffer, 8192);
321
322         i = 0;
323         while (i < entries) {
324                 const struct cache_entry *ce = cache[i];
325                 struct cache_tree_sub *sub = NULL;
326                 const char *path, *slash;
327                 int pathlen, entlen;
328                 const struct object_id *oid;
329                 unsigned mode;
330                 int expected_missing = 0;
331                 int contains_ita = 0;
332                 int ce_missing_ok;
333
334                 path = ce->name;
335                 pathlen = ce_namelen(ce);
336                 if (pathlen <= baselen || memcmp(base, path, baselen))
337                         break; /* at the end of this level */
338
339                 slash = strchr(path + baselen, '/');
340                 if (slash) {
341                         entlen = slash - (path + baselen);
342                         sub = find_subtree(it, path + baselen, entlen, 0);
343                         if (!sub)
344                                 die("cache-tree.c: '%.*s' in '%s' not found",
345                                     entlen, path + baselen, path);
346                         i += sub->count;
347                         oid = &sub->cache_tree->oid;
348                         mode = S_IFDIR;
349                         contains_ita = sub->cache_tree->entry_count < 0;
350                         if (contains_ita) {
351                                 to_invalidate = 1;
352                                 expected_missing = 1;
353                         }
354                 }
355                 else {
356                         oid = &ce->oid;
357                         mode = ce->ce_mode;
358                         entlen = pathlen - baselen;
359                         i++;
360                 }
361
362                 ce_missing_ok = mode == S_IFGITLINK || missing_ok ||
363                         (has_promisor_remote() &&
364                          ce_skip_worktree(ce));
365                 if (is_null_oid(oid) ||
366                     (!ce_missing_ok && !has_object_file(oid))) {
367                         strbuf_release(&buffer);
368                         if (expected_missing)
369                                 return -1;
370                         return error("invalid object %06o %s for '%.*s'",
371                                 mode, oid_to_hex(oid), entlen+baselen, path);
372                 }
373
374                 /*
375                  * CE_REMOVE entries are removed before the index is
376                  * written to disk. Skip them to remain consistent
377                  * with the future on-disk index.
378                  */
379                 if (ce->ce_flags & CE_REMOVE) {
380                         *skip_count = *skip_count + 1;
381                         continue;
382                 }
383
384                 /*
385                  * CE_INTENT_TO_ADD entries exist on on-disk index but
386                  * they are not part of generated trees. Invalidate up
387                  * to root to force cache-tree users to read elsewhere.
388                  */
389                 if (!sub && ce_intent_to_add(ce)) {
390                         to_invalidate = 1;
391                         continue;
392                 }
393
394                 /*
395                  * "sub" can be an empty tree if all subentries are i-t-a.
396                  */
397                 if (contains_ita && is_empty_tree_oid(oid))
398                         continue;
399
400                 strbuf_grow(&buffer, entlen + 100);
401                 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
402                 strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz);
403
404 #if DEBUG_CACHE_TREE
405                 fprintf(stderr, "cache-tree update-one %o %.*s\n",
406                         mode, entlen, path + baselen);
407 #endif
408         }
409
410         if (repair) {
411                 struct object_id oid;
412                 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
413                                  tree_type, &oid);
414                 if (has_object_file_with_flags(&oid, OBJECT_INFO_SKIP_FETCH_OBJECT))
415                         oidcpy(&it->oid, &oid);
416                 else
417                         to_invalidate = 1;
418         } else if (dryrun) {
419                 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
420                                  tree_type, &it->oid);
421         } else if (write_object_file(buffer.buf, buffer.len, tree_type,
422                                      &it->oid)) {
423                 strbuf_release(&buffer);
424                 return -1;
425         }
426
427         strbuf_release(&buffer);
428         it->entry_count = to_invalidate ? -1 : i - *skip_count;
429 #if DEBUG_CACHE_TREE
430         fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
431                 it->entry_count, it->subtree_nr,
432                 oid_to_hex(&it->oid));
433 #endif
434         return i;
435 }
436
437 int cache_tree_update(struct index_state *istate, int flags)
438 {
439         struct cache_tree *it = istate->cache_tree;
440         struct cache_entry **cache = istate->cache;
441         int entries = istate->cache_nr;
442         int skip, i = verify_cache(cache, entries, flags);
443
444         if (i)
445                 return i;
446         trace_performance_enter();
447         trace2_region_enter("cache_tree", "update", the_repository);
448         i = update_one(it, cache, entries, "", 0, &skip, flags);
449         trace2_region_leave("cache_tree", "update", the_repository);
450         trace_performance_leave("cache_tree_update");
451         if (i < 0)
452                 return i;
453         istate->cache_changed |= CACHE_TREE_CHANGED;
454         return 0;
455 }
456
457 static void write_one(struct strbuf *buffer, struct cache_tree *it,
458                       const char *path, int pathlen)
459 {
460         int i;
461
462         /* One "cache-tree" entry consists of the following:
463          * path (NUL terminated)
464          * entry_count, subtree_nr ("%d %d\n")
465          * tree-sha1 (missing if invalid)
466          * subtree_nr "cache-tree" entries for subtrees.
467          */
468         strbuf_grow(buffer, pathlen + 100);
469         strbuf_add(buffer, path, pathlen);
470         strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
471
472 #if DEBUG_CACHE_TREE
473         if (0 <= it->entry_count)
474                 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
475                         pathlen, path, it->entry_count, it->subtree_nr,
476                         oid_to_hex(&it->oid));
477         else
478                 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
479                         pathlen, path, it->subtree_nr);
480 #endif
481
482         if (0 <= it->entry_count) {
483                 strbuf_add(buffer, it->oid.hash, the_hash_algo->rawsz);
484         }
485         for (i = 0; i < it->subtree_nr; i++) {
486                 struct cache_tree_sub *down = it->down[i];
487                 if (i) {
488                         struct cache_tree_sub *prev = it->down[i-1];
489                         if (subtree_name_cmp(down->name, down->namelen,
490                                              prev->name, prev->namelen) <= 0)
491                                 die("fatal - unsorted cache subtree");
492                 }
493                 write_one(buffer, down->cache_tree, down->name, down->namelen);
494         }
495 }
496
497 void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
498 {
499         trace2_region_enter("cache_tree", "write", the_repository);
500         write_one(sb, root, "", 0);
501         trace2_region_leave("cache_tree", "write", the_repository);
502 }
503
504 static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
505 {
506         const char *buf = *buffer;
507         unsigned long size = *size_p;
508         const char *cp;
509         char *ep;
510         struct cache_tree *it;
511         int i, subtree_nr;
512         const unsigned rawsz = the_hash_algo->rawsz;
513
514         it = NULL;
515         /* skip name, but make sure name exists */
516         while (size && *buf) {
517                 size--;
518                 buf++;
519         }
520         if (!size)
521                 goto free_return;
522         buf++; size--;
523         it = cache_tree();
524
525         cp = buf;
526         it->entry_count = strtol(cp, &ep, 10);
527         if (cp == ep)
528                 goto free_return;
529         cp = ep;
530         subtree_nr = strtol(cp, &ep, 10);
531         if (cp == ep)
532                 goto free_return;
533         while (size && *buf && *buf != '\n') {
534                 size--;
535                 buf++;
536         }
537         if (!size)
538                 goto free_return;
539         buf++; size--;
540         if (0 <= it->entry_count) {
541                 if (size < rawsz)
542                         goto free_return;
543                 oidread(&it->oid, (const unsigned char *)buf);
544                 buf += rawsz;
545                 size -= rawsz;
546         }
547
548 #if DEBUG_CACHE_TREE
549         if (0 <= it->entry_count)
550                 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
551                         *buffer, it->entry_count, subtree_nr,
552                         oid_to_hex(&it->oid));
553         else
554                 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
555                         *buffer, subtree_nr);
556 #endif
557
558         /*
559          * Just a heuristic -- we do not add directories that often but
560          * we do not want to have to extend it immediately when we do,
561          * hence +2.
562          */
563         it->subtree_alloc = subtree_nr + 2;
564         it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
565         for (i = 0; i < subtree_nr; i++) {
566                 /* read each subtree */
567                 struct cache_tree *sub;
568                 struct cache_tree_sub *subtree;
569                 const char *name = buf;
570
571                 sub = read_one(&buf, &size);
572                 if (!sub)
573                         goto free_return;
574                 subtree = cache_tree_sub(it, name);
575                 subtree->cache_tree = sub;
576         }
577         if (subtree_nr != it->subtree_nr)
578                 die("cache-tree: internal error");
579         *buffer = buf;
580         *size_p = size;
581         return it;
582
583  free_return:
584         cache_tree_free(&it);
585         return NULL;
586 }
587
588 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
589 {
590         struct cache_tree *result;
591
592         if (buffer[0])
593                 return NULL; /* not the whole tree */
594
595         trace2_region_enter("cache_tree", "read", the_repository);
596         result = read_one(&buffer, &size);
597         trace2_region_leave("cache_tree", "read", the_repository);
598
599         return result;
600 }
601
602 static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
603 {
604         if (!it)
605                 return NULL;
606         while (*path) {
607                 const char *slash;
608                 struct cache_tree_sub *sub;
609
610                 slash = strchrnul(path, '/');
611                 /*
612                  * Between path and slash is the name of the subtree
613                  * to look for.
614                  */
615                 sub = find_subtree(it, path, slash - path, 0);
616                 if (!sub)
617                         return NULL;
618                 it = sub->cache_tree;
619
620                 path = slash;
621                 while (*path == '/')
622                         path++;
623         }
624         return it;
625 }
626
627 static int write_index_as_tree_internal(struct object_id *oid,
628                                         struct index_state *index_state,
629                                         int cache_tree_valid,
630                                         int flags,
631                                         const char *prefix)
632 {
633         if (flags & WRITE_TREE_IGNORE_CACHE_TREE) {
634                 cache_tree_free(&index_state->cache_tree);
635                 cache_tree_valid = 0;
636         }
637
638         if (!index_state->cache_tree)
639                 index_state->cache_tree = cache_tree();
640
641         if (!cache_tree_valid && cache_tree_update(index_state, flags) < 0)
642                 return WRITE_TREE_UNMERGED_INDEX;
643
644         if (prefix) {
645                 struct cache_tree *subtree;
646                 subtree = cache_tree_find(index_state->cache_tree, prefix);
647                 if (!subtree)
648                         return WRITE_TREE_PREFIX_ERROR;
649                 oidcpy(oid, &subtree->oid);
650         }
651         else
652                 oidcpy(oid, &index_state->cache_tree->oid);
653
654         return 0;
655 }
656
657 struct tree* write_in_core_index_as_tree(struct repository *repo) {
658         struct object_id o;
659         int was_valid, ret;
660
661         struct index_state *index_state = repo->index;
662         was_valid = index_state->cache_tree &&
663                     cache_tree_fully_valid(index_state->cache_tree);
664
665         ret = write_index_as_tree_internal(&o, index_state, was_valid, 0, NULL);
666         if (ret == WRITE_TREE_UNMERGED_INDEX) {
667                 int i;
668                 fprintf(stderr, "BUG: There are unmerged index entries:\n");
669                 for (i = 0; i < index_state->cache_nr; i++) {
670                         const struct cache_entry *ce = index_state->cache[i];
671                         if (ce_stage(ce))
672                                 fprintf(stderr, "BUG: %d %.*s\n", ce_stage(ce),
673                                         (int)ce_namelen(ce), ce->name);
674                 }
675                 BUG("unmerged index entries when writing inmemory index");
676         }
677
678         return lookup_tree(repo, &index_state->cache_tree->oid);
679 }
680
681
682 int write_index_as_tree(struct object_id *oid, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
683 {
684         int entries, was_valid;
685         struct lock_file lock_file = LOCK_INIT;
686         int ret;
687
688         hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
689
690         entries = read_index_from(index_state, index_path, get_git_dir());
691         if (entries < 0) {
692                 ret = WRITE_TREE_UNREADABLE_INDEX;
693                 goto out;
694         }
695
696         was_valid = !(flags & WRITE_TREE_IGNORE_CACHE_TREE) &&
697                     index_state->cache_tree &&
698                     cache_tree_fully_valid(index_state->cache_tree);
699
700         ret = write_index_as_tree_internal(oid, index_state, was_valid, flags,
701                                            prefix);
702         if (!ret && !was_valid) {
703                 write_locked_index(index_state, &lock_file, COMMIT_LOCK);
704                 /* Not being able to write is fine -- we are only interested
705                  * in updating the cache-tree part, and if the next caller
706                  * ends up using the old index with unupdated cache-tree part
707                  * it misses the work we did here, but that is just a
708                  * performance penalty and not a big deal.
709                  */
710         }
711
712 out:
713         rollback_lock_file(&lock_file);
714         return ret;
715 }
716
717 static void prime_cache_tree_rec(struct repository *r,
718                                  struct cache_tree *it,
719                                  struct tree *tree)
720 {
721         struct tree_desc desc;
722         struct name_entry entry;
723         int cnt;
724
725         oidcpy(&it->oid, &tree->object.oid);
726         init_tree_desc(&desc, tree->buffer, tree->size);
727         cnt = 0;
728         while (tree_entry(&desc, &entry)) {
729                 if (!S_ISDIR(entry.mode))
730                         cnt++;
731                 else {
732                         struct cache_tree_sub *sub;
733                         struct tree *subtree = lookup_tree(r, &entry.oid);
734                         if (!subtree->object.parsed)
735                                 parse_tree(subtree);
736                         sub = cache_tree_sub(it, entry.path);
737                         sub->cache_tree = cache_tree();
738                         prime_cache_tree_rec(r, sub->cache_tree, subtree);
739                         cnt += sub->cache_tree->entry_count;
740                 }
741         }
742         it->entry_count = cnt;
743 }
744
745 void prime_cache_tree(struct repository *r,
746                       struct index_state *istate,
747                       struct tree *tree)
748 {
749         trace2_region_enter("cache-tree", "prime_cache_tree", the_repository);
750         cache_tree_free(&istate->cache_tree);
751         istate->cache_tree = cache_tree();
752
753         prime_cache_tree_rec(r, istate->cache_tree, tree);
754         istate->cache_changed |= CACHE_TREE_CHANGED;
755         trace2_region_leave("cache-tree", "prime_cache_tree", the_repository);
756 }
757
758 /*
759  * find the cache_tree that corresponds to the current level without
760  * exploding the full path into textual form.  The root of the
761  * cache tree is given as "root", and our current level is "info".
762  * (1) When at root level, info->prev is NULL, so it is "root" itself.
763  * (2) Otherwise, find the cache_tree that corresponds to one level
764  *     above us, and find ourselves in there.
765  */
766 static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
767                                                          struct traverse_info *info)
768 {
769         struct cache_tree *our_parent;
770
771         if (!info->prev)
772                 return root;
773         our_parent = find_cache_tree_from_traversal(root, info->prev);
774         return cache_tree_find(our_parent, info->name);
775 }
776
777 int cache_tree_matches_traversal(struct cache_tree *root,
778                                  struct name_entry *ent,
779                                  struct traverse_info *info)
780 {
781         struct cache_tree *it;
782
783         it = find_cache_tree_from_traversal(root, info);
784         it = cache_tree_find(it, ent->path);
785         if (it && it->entry_count > 0 && oideq(&ent->oid, &it->oid))
786                 return it->entry_count;
787         return 0;
788 }
789
790 static void verify_one(struct repository *r,
791                        struct index_state *istate,
792                        struct cache_tree *it,
793                        struct strbuf *path)
794 {
795         int i, pos, len = path->len;
796         struct strbuf tree_buf = STRBUF_INIT;
797         struct object_id new_oid;
798
799         for (i = 0; i < it->subtree_nr; i++) {
800                 strbuf_addf(path, "%s/", it->down[i]->name);
801                 verify_one(r, istate, it->down[i]->cache_tree, path);
802                 strbuf_setlen(path, len);
803         }
804
805         if (it->entry_count < 0 ||
806             /* no verification on tests (t7003) that replace trees */
807             lookup_replace_object(r, &it->oid) != &it->oid)
808                 return;
809
810         if (path->len) {
811                 pos = index_name_pos(istate, path->buf, path->len);
812                 pos = -pos - 1;
813         } else {
814                 pos = 0;
815         }
816
817         i = 0;
818         while (i < it->entry_count) {
819                 struct cache_entry *ce = istate->cache[pos + i];
820                 const char *slash;
821                 struct cache_tree_sub *sub = NULL;
822                 const struct object_id *oid;
823                 const char *name;
824                 unsigned mode;
825                 int entlen;
826
827                 if (ce->ce_flags & (CE_STAGEMASK | CE_INTENT_TO_ADD | CE_REMOVE))
828                         BUG("%s with flags 0x%x should not be in cache-tree",
829                             ce->name, ce->ce_flags);
830                 name = ce->name + path->len;
831                 slash = strchr(name, '/');
832                 if (slash) {
833                         entlen = slash - name;
834                         sub = find_subtree(it, ce->name + path->len, entlen, 0);
835                         if (!sub || sub->cache_tree->entry_count < 0)
836                                 BUG("bad subtree '%.*s'", entlen, name);
837                         oid = &sub->cache_tree->oid;
838                         mode = S_IFDIR;
839                         i += sub->cache_tree->entry_count;
840                 } else {
841                         oid = &ce->oid;
842                         mode = ce->ce_mode;
843                         entlen = ce_namelen(ce) - path->len;
844                         i++;
845                 }
846                 strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0');
847                 strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz);
848         }
849         hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, tree_type,
850                          &new_oid);
851         if (!oideq(&new_oid, &it->oid))
852                 BUG("cache-tree for path %.*s does not match. "
853                     "Expected %s got %s", len, path->buf,
854                     oid_to_hex(&new_oid), oid_to_hex(&it->oid));
855         strbuf_setlen(path, len);
856         strbuf_release(&tree_buf);
857 }
858
859 void cache_tree_verify(struct repository *r, struct index_state *istate)
860 {
861         struct strbuf path = STRBUF_INIT;
862
863         if (!istate->cache_tree)
864                 return;
865         verify_one(r, istate, istate->cache_tree, &path);
866         strbuf_release(&path);
867 }