at commit b4dedbc0

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