at commit b758bc5b

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