Raw
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 int cache_tree_fully_valid(struct cache_tree *it)
279 {
280 int i;
281 if (!it)
282 return 0;
283 if (it->entry_count < 0 ||
284 !odb_has_object(the_repository->objects, &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(it->down[i]->cache_tree))
289 return 0;
290 }
291 return 1;
292 }
293
294 static int must_check_existence(const struct cache_entry *ce)
295 {
296 return !(repo_has_promisor_remote(the_repository) && ce_skip_worktree(ce));
297 }
298
299 static int update_one(struct cache_tree *it,
300 struct cache_entry **cache,
301 int entries,
302 const char *base,
303 int baselen,
304 int *skip_count,
305 int flags)
306 {
307 struct strbuf buffer;
308 int missing_ok = flags & WRITE_TREE_MISSING_OK;
309 int dryrun = flags & WRITE_TREE_DRY_RUN;
310 int repair = flags & WRITE_TREE_REPAIR;
311 int to_invalidate = 0;
312 int i;
313
314 assert(!(dryrun && repair));
315
316 *skip_count = 0;
317
318 /*
319 * If the first entry of this region is a sparse directory
320 * entry corresponding exactly to 'base', then this cache_tree
321 * struct is a "leaf" in the data structure, pointing to the
322 * tree OID specified in the entry.
323 */
324 if (entries > 0) {
325 const struct cache_entry *ce = cache[0];
326
327 if (S_ISSPARSEDIR(ce->ce_mode) &&
328 ce->ce_namelen == baselen &&
329 !strncmp(ce->name, base, baselen)) {
330 it->entry_count = 1;
331 oidcpy(&it->oid, &ce->oid);
332 return 1;
333 }
334 }
335
336 if (0 <= it->entry_count &&
337 odb_has_object(the_repository->objects, &it->oid,
338 ODB_HAS_OBJECT_RECHECK_PACKED | ODB_HAS_OBJECT_FETCH_PROMISOR))
339 return it->entry_count;
340
341 /*
342 * We first scan for subtrees and update them; we start by
343 * marking existing subtrees -- the ones that are unmarked
344 * should not be in the result.
345 */
346 for (i = 0; i < it->subtree_nr; i++)
347 it->down[i]->used = 0;
348
349 /*
350 * Find the subtrees and update them.
351 */
352 i = 0;
353 while (i < entries) {
354 const struct cache_entry *ce = cache[i];
355 struct cache_tree_sub *sub;
356 const char *path, *slash;
357 int pathlen, sublen, subcnt, subskip;
358
359 path = ce->name;
360 pathlen = ce_namelen(ce);
361 if (pathlen <= baselen || memcmp(base, path, baselen))
362 break; /* at the end of this level */
363
364 slash = strchr(path + baselen, '/');
365 if (!slash) {
366 i++;
367 continue;
368 }
369 /*
370 * a/bbb/c (base = a/, slash = /c)
371 * ==>
372 * path+baselen = bbb/c, sublen = 3
373 */
374 sublen = slash - (path + baselen);
375 sub = find_subtree(it, path + baselen, sublen, 1);
376 if (!sub->cache_tree)
377 sub->cache_tree = cache_tree();
378 subcnt = update_one(sub->cache_tree,
379 cache + i, entries - i,
380 path,
381 baselen + sublen + 1,
382 &subskip,
383 flags);
384 if (subcnt < 0)
385 return subcnt;
386 if (!subcnt)
387 die("index cache-tree records empty sub-tree");
388 i += subcnt;
389 sub->count = subcnt; /* to be used in the next loop */
390 *skip_count += subskip;
391 sub->used = 1;
392 }
393
394 discard_unused_subtrees(it);
395
396 /*
397 * Then write out the tree object for this level.
398 */
399 strbuf_init(&buffer, 8192);
400
401 i = 0;
402 while (i < entries) {
403 const struct cache_entry *ce = cache[i];
404 struct cache_tree_sub *sub = NULL;
405 const char *path, *slash;
406 int pathlen, entlen;
407 const struct object_id *oid;
408 unsigned mode;
409 int expected_missing = 0;
410 int contains_ita = 0;
411 int ce_missing_ok;
412
413 path = ce->name;
414 pathlen = ce_namelen(ce);
415 if (pathlen <= baselen || memcmp(base, path, baselen))
416 break; /* at the end of this level */
417
418 slash = strchr(path + baselen, '/');
419 if (slash) {
420 entlen = slash - (path + baselen);
421 sub = find_subtree(it, path + baselen, entlen, 0);
422 if (!sub)
423 die("cache-tree.c: '%.*s' in '%s' not found",
424 entlen, path + baselen, path);
425 i += sub->count;
426 oid = &sub->cache_tree->oid;
427 mode = S_IFDIR;
428 contains_ita = sub->cache_tree->entry_count < 0;
429 if (contains_ita) {
430 to_invalidate = 1;
431 expected_missing = 1;
432 }
433 }
434 else {
435 oid = &ce->oid;
436 mode = ce->ce_mode;
437 entlen = pathlen - baselen;
438 i++;
439 }
440
441 ce_missing_ok = mode == S_IFGITLINK || missing_ok ||
442 !must_check_existence(ce);
443 if (is_null_oid(oid) ||
444 (!ce_missing_ok &&
445 !odb_has_object(the_repository->objects, oid,
446 ODB_HAS_OBJECT_RECHECK_PACKED | ODB_HAS_OBJECT_FETCH_PROMISOR))) {
447 strbuf_release(&buffer);
448 if (expected_missing)
449 return -1;
450 return error("invalid object %06o %s for '%.*s'",
451 mode, oid_to_hex(oid), entlen+baselen, path);
452 }
453
454 /*
455 * CE_REMOVE entries are removed before the index is
456 * written to disk. Skip them to remain consistent
457 * with the future on-disk index.
458 */
459 if (ce->ce_flags & CE_REMOVE) {
460 *skip_count = *skip_count + 1;
461 continue;
462 }
463
464 /*
465 * CE_INTENT_TO_ADD entries exist in on-disk index but
466 * they are not part of generated trees. Invalidate up
467 * to root to force cache-tree users to read elsewhere.
468 */
469 if (!sub && ce_intent_to_add(ce)) {
470 to_invalidate = 1;
471 continue;
472 }
473
474 /*
475 * "sub" can be an empty tree if all subentries are i-t-a.
476 */
477 if (contains_ita && is_empty_tree_oid(oid, the_repository->hash_algo))
478 continue;
479
480 strbuf_grow(&buffer, entlen + 100);
481 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
482 strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz);
483
484 #if DEBUG_CACHE_TREE
485 fprintf(stderr, "cache-tree update-one %o %.*s\n",
486 mode, entlen, path + baselen);
487 #endif
488 }
489
490 if (repair) {
491 struct object_id oid;
492 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
493 OBJ_TREE, &oid);
494 if (odb_has_object(the_repository->objects, &oid, ODB_HAS_OBJECT_RECHECK_PACKED))
495 oidcpy(&it->oid, &oid);
496 else
497 to_invalidate = 1;
498 } else if (dryrun) {
499 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
500 OBJ_TREE, &it->oid);
501 } else if (odb_write_object_ext(the_repository->objects, buffer.buf, buffer.len, OBJ_TREE,
502 &it->oid, NULL, flags & WRITE_TREE_SILENT ? ODB_WRITE_OBJECT_SILENT : 0)) {
503 strbuf_release(&buffer);
504 return -1;
505 }
506
507 strbuf_release(&buffer);
508 it->entry_count = to_invalidate ? -1 : i - *skip_count;
509 #if DEBUG_CACHE_TREE
510 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
511 it->entry_count, it->subtree_nr,
512 oid_to_hex(&it->oid));
513 #endif
514 return i;
515 }
516
517 int cache_tree_update(struct index_state *istate, int flags)
518 {
519 int inflight = !!the_repository->objects->transaction;
520 struct odb_transaction *transaction;
521 int skip, i;
522
523 i = verify_cache(istate, flags);
524
525 if (i)
526 return i;
527
528 if (!istate->cache_tree)
529 istate->cache_tree = cache_tree();
530
531 if (!(flags & WRITE_TREE_MISSING_OK) && repo_has_promisor_remote(the_repository))
532 prefetch_cache_entries(istate, must_check_existence);
533
534 trace_performance_enter();
535 trace2_region_enter("cache_tree", "update", istate->repo);
536 if (!inflight)
537 odb_transaction_begin_or_die(the_repository->objects, &transaction, 0);
538 i = update_one(istate->cache_tree, istate->cache, istate->cache_nr,
539 "", 0, &skip, flags);
540 if (!inflight)
541 odb_transaction_commit(transaction);
542 trace2_region_leave("cache_tree", "update", istate->repo);
543 trace_performance_leave("cache_tree_update");
544 if (i < 0)
545 return i;
546 istate->cache_changed |= CACHE_TREE_CHANGED;
547 return 0;
548 }
549
550 static void write_one(struct strbuf *buffer, struct cache_tree *it,
551 const char *path, int pathlen)
552 {
553 int i;
554
555 /* One "cache-tree" entry consists of the following:
556 * path (NUL terminated)
557 * entry_count, subtree_nr ("%d %d\n")
558 * tree-sha1 (missing if invalid)
559 * subtree_nr "cache-tree" entries for subtrees.
560 */
561 strbuf_grow(buffer, pathlen + 100);
562 strbuf_add(buffer, path, pathlen);
563 strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
564
565 #if DEBUG_CACHE_TREE
566 if (0 <= it->entry_count)
567 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
568 pathlen, path, it->entry_count, it->subtree_nr,
569 oid_to_hex(&it->oid));
570 else
571 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
572 pathlen, path, it->subtree_nr);
573 #endif
574
575 if (0 <= it->entry_count) {
576 strbuf_add(buffer, it->oid.hash, the_hash_algo->rawsz);
577 }
578 for (i = 0; i < it->subtree_nr; i++) {
579 struct cache_tree_sub *down = it->down[i];
580 if (i) {
581 struct cache_tree_sub *prev = it->down[i-1];
582 if (subtree_name_cmp(down->name, down->namelen,
583 prev->name, prev->namelen) <= 0)
584 die("fatal - unsorted cache subtree");
585 }
586 write_one(buffer, down->cache_tree, down->name, down->namelen);
587 }
588 }
589
590 void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
591 {
592 trace2_region_enter("cache_tree", "write", the_repository);
593 write_one(sb, root, "", 0);
594 trace2_region_leave("cache_tree", "write", the_repository);
595 }
596
597 static int parse_int(const char **ptr, unsigned long *len_p, int *out)
598 {
599 const char *s = *ptr;
600 unsigned long len = *len_p;
601 int ret = 0;
602 int sign = 1;
603
604 while (len && *s == '-') {
605 sign *= -1;
606 s++;
607 len--;
608 }
609
610 while (len) {
611 if (!isdigit(*s))
612 break;
613 ret *= 10;
614 ret += *s - '0';
615 s++;
616 len--;
617 }
618
619 if (s == *ptr)
620 return -1;
621
622 *ptr = s;
623 *len_p = len;
624 *out = sign * ret;
625 return 0;
626 }
627
628 static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
629 {
630 const char *buf = *buffer;
631 unsigned long size = *size_p;
632 struct cache_tree *it;
633 int i, subtree_nr;
634 const unsigned rawsz = the_hash_algo->rawsz;
635
636 it = NULL;
637 /* skip name, but make sure name exists */
638 while (size && *buf) {
639 size--;
640 buf++;
641 }
642 if (!size)
643 goto free_return;
644 buf++; size--;
645 it = cache_tree();
646
647 if (parse_int(&buf, &size, &it->entry_count) < 0)
648 goto free_return;
649 if (!size || *buf != ' ')
650 goto free_return;
651 buf++; size--;
652 if (parse_int(&buf, &size, &subtree_nr) < 0)
653 goto free_return;
654 if (!size || *buf != '\n')
655 goto free_return;
656 buf++; size--;
657 if (0 <= it->entry_count) {
658 if (size < rawsz)
659 goto free_return;
660 oidread(&it->oid, (const unsigned char *)buf,
661 the_repository->hash_algo);
662 buf += rawsz;
663 size -= rawsz;
664 }
665
666 #if DEBUG_CACHE_TREE
667 if (0 <= it->entry_count)
668 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
669 *buffer, it->entry_count, subtree_nr,
670 oid_to_hex(&it->oid));
671 else
672 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
673 *buffer, subtree_nr);
674 #endif
675
676 /*
677 * Just a heuristic -- we do not add directories that often but
678 * we do not want to have to extend it immediately when we do,
679 * hence +2.
680 */
681 it->subtree_alloc = subtree_nr + 2;
682 CALLOC_ARRAY(it->down, it->subtree_alloc);
683 for (i = 0; i < subtree_nr; i++) {
684 /* read each subtree */
685 struct cache_tree *sub;
686 struct cache_tree_sub *subtree;
687 const char *name = buf;
688
689 sub = read_one(&buf, &size);
690 if (!sub)
691 goto free_return;
692 subtree = cache_tree_sub(it, name);
693 subtree->cache_tree = sub;
694 }
695 if (subtree_nr != it->subtree_nr)
696 die("cache-tree: internal error");
697 *buffer = buf;
698 *size_p = size;
699 return it;
700
701 free_return:
702 cache_tree_free(&it);
703 return NULL;
704 }
705
706 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
707 {
708 struct cache_tree *result;
709
710 if (buffer[0])
711 return NULL; /* not the whole tree */
712
713 trace2_region_enter("cache_tree", "read", the_repository);
714 result = read_one(&buffer, &size);
715 trace2_region_leave("cache_tree", "read", the_repository);
716
717 return result;
718 }
719
720 static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
721 {
722 if (!it)
723 return NULL;
724 while (*path) {
725 const char *slash;
726 struct cache_tree_sub *sub;
727
728 slash = strchrnul(path, '/');
729 /*
730 * Between path and slash is the name of the subtree
731 * to look for.
732 */
733 sub = find_subtree(it, path, slash - path, 0);
734 if (!sub)
735 return NULL;
736 it = sub->cache_tree;
737
738 path = slash;
739 while (*path == '/')
740 path++;
741 }
742 return it;
743 }
744
745 static int write_index_as_tree_internal(struct object_id *oid,
746 struct index_state *index_state,
747 int cache_tree_valid,
748 int flags,
749 const char *prefix)
750 {
751 if (flags & WRITE_TREE_IGNORE_CACHE_TREE) {
752 cache_tree_free(&index_state->cache_tree);
753 cache_tree_valid = 0;
754 }
755
756 if (!cache_tree_valid && cache_tree_update(index_state, flags) < 0)
757 return WRITE_TREE_UNMERGED_INDEX;
758
759 if (prefix) {
760 struct cache_tree *subtree;
761 subtree = cache_tree_find(index_state->cache_tree, prefix);
762 if (!subtree)
763 return WRITE_TREE_PREFIX_ERROR;
764 oidcpy(oid, &subtree->oid);
765 }
766 else
767 oidcpy(oid, &index_state->cache_tree->oid);
768
769 return 0;
770 }
771
772 struct tree *write_in_core_index_as_tree(struct repository *repo,
773 struct index_state *index_state) {
774 struct object_id o;
775 int was_valid, ret;
776
777 was_valid = index_state->cache_tree &&
778 cache_tree_fully_valid(index_state->cache_tree);
779
780 ret = write_index_as_tree_internal(&o, index_state, was_valid, 0, NULL);
781 if (ret == WRITE_TREE_UNMERGED_INDEX) {
782 int i;
783 bug("there are unmerged index entries:");
784 for (i = 0; i < index_state->cache_nr; i++) {
785 const struct cache_entry *ce = index_state->cache[i];
786 if (ce_stage(ce))
787 bug("%d %.*s", ce_stage(ce),
788 (int)ce_namelen(ce), ce->name);
789 }
790 BUG("unmerged index entries when writing in-core index");
791 }
792
793 return lookup_tree(repo, &index_state->cache_tree->oid);
794 }
795
796
797 int write_index_as_tree(struct object_id *oid, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
798 {
799 int entries, was_valid;
800 struct lock_file lock_file = LOCK_INIT;
801 int ret;
802
803 hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
804
805 entries = read_index_from(index_state, index_path,
806 repo_get_git_dir(the_repository));
807 if (entries < 0) {
808 ret = WRITE_TREE_UNREADABLE_INDEX;
809 goto out;
810 }
811
812 was_valid = !(flags & WRITE_TREE_IGNORE_CACHE_TREE) &&
813 index_state->cache_tree &&
814 cache_tree_fully_valid(index_state->cache_tree);
815
816 ret = write_index_as_tree_internal(oid, index_state, was_valid, flags,
817 prefix);
818 if (!ret && !was_valid) {
819 write_locked_index(index_state, &lock_file, COMMIT_LOCK);
820 /* Not being able to write is fine -- we are only interested
821 * in updating the cache-tree part, and if the next caller
822 * ends up using the old index with unupdated cache-tree part
823 * it misses the work we did here, but that is just a
824 * performance penalty and not a big deal.
825 */
826 }
827
828 out:
829 rollback_lock_file(&lock_file);
830 return ret;
831 }
832
833 static void prime_cache_tree_sparse_dir(struct cache_tree *it,
834 struct tree *tree)
835 {
836
837 oidcpy(&it->oid, &tree->object.oid);
838 it->entry_count = 1;
839 }
840
841 static void prime_cache_tree_rec(struct repository *r,
842 struct cache_tree *it,
843 struct tree *tree,
844 struct strbuf *tree_path)
845 {
846 struct tree_desc desc;
847 struct name_entry entry;
848 int cnt;
849 size_t base_path_len = tree_path->len;
850
851 oidcpy(&it->oid, &tree->object.oid);
852
853 init_tree_desc(&desc, &tree->object.oid, tree->buffer, tree->size);
854 cnt = 0;
855 while (tree_entry(&desc, &entry)) {
856 if (!S_ISDIR(entry.mode))
857 cnt++;
858 else {
859 struct cache_tree_sub *sub;
860 struct tree *subtree = lookup_tree(r, &entry.oid);
861
862 if (repo_parse_tree(the_repository, subtree) < 0)
863 exit(128);
864 sub = cache_tree_sub(it, entry.path);
865 sub->cache_tree = cache_tree();
866
867 /*
868 * Recursively-constructed subtree path is only needed when working
869 * in a sparse index (where it's used to determine whether the
870 * subtree is a sparse directory in the index).
871 */
872 if (r->index->sparse_index) {
873 strbuf_setlen(tree_path, base_path_len);
874 strbuf_add(tree_path, entry.path, entry.pathlen);
875 strbuf_addch(tree_path, '/');
876 }
877
878 /*
879 * If a sparse index is in use, the directory being processed may be
880 * sparse. To confirm that, we can check whether an entry with that
881 * exact name exists in the index. If it does, the created subtree
882 * should be sparse. Otherwise, cache tree expansion should continue
883 * as normal.
884 */
885 if (r->index->sparse_index &&
886 index_entry_exists(r->index, tree_path->buf, tree_path->len))
887 prime_cache_tree_sparse_dir(sub->cache_tree, subtree);
888 else
889 prime_cache_tree_rec(r, sub->cache_tree, subtree, tree_path);
890 cnt += sub->cache_tree->entry_count;
891 }
892 }
893
894 it->entry_count = cnt;
895 }
896
897 void prime_cache_tree(struct repository *r,
898 struct index_state *istate,
899 struct tree *tree)
900 {
901 struct strbuf tree_path = STRBUF_INIT;
902
903 trace2_region_enter("cache-tree", "prime_cache_tree", r);
904 cache_tree_free(&istate->cache_tree);
905 istate->cache_tree = cache_tree();
906
907 prime_cache_tree_rec(r, istate->cache_tree, tree, &tree_path);
908 strbuf_release(&tree_path);
909 istate->cache_changed |= CACHE_TREE_CHANGED;
910 trace2_region_leave("cache-tree", "prime_cache_tree", r);
911 }
912
913 /*
914 * find the cache_tree that corresponds to the current level without
915 * exploding the full path into textual form. The root of the
916 * cache tree is given as "root", and our current level is "info".
917 * (1) When at root level, info->prev is NULL, so it is "root" itself.
918 * (2) Otherwise, find the cache_tree that corresponds to one level
919 * above us, and find ourselves in there.
920 */
921 static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
922 struct traverse_info *info)
923 {
924 struct cache_tree *our_parent;
925
926 if (!info->prev)
927 return root;
928 our_parent = find_cache_tree_from_traversal(root, info->prev);
929 return cache_tree_find(our_parent, info->name);
930 }
931
932 int cache_tree_matches_traversal(struct cache_tree *root,
933 struct name_entry *ent,
934 struct traverse_info *info)
935 {
936 struct cache_tree *it;
937
938 it = find_cache_tree_from_traversal(root, info);
939 it = cache_tree_find(it, ent->path);
940 if (it && it->entry_count > 0 && oideq(&ent->oid, &it->oid))
941 return it->entry_count;
942 return 0;
943 }
944
945 static int verify_one_sparse(struct index_state *istate,
946 struct strbuf *path,
947 int pos)
948 {
949 struct cache_entry *ce = istate->cache[pos];
950 if (!S_ISSPARSEDIR(ce->ce_mode))
951 return error(_("directory '%s' is present in index, but not sparse"),
952 path->buf);
953 return 0;
954 }
955
956 /*
957 * Returns:
958 * 0 - Verification completed.
959 * 1 - Restart verification - a call to ensure_full_index() freed the cache
960 * tree that is being verified and verification needs to be restarted from
961 * the new toplevel cache tree.
962 * -1 - Verification failed.
963 */
964 static int verify_one(struct repository *r,
965 struct index_state *istate,
966 struct cache_tree *it,
967 struct strbuf *path)
968 {
969 int i, pos, len = path->len;
970 struct strbuf tree_buf = STRBUF_INIT;
971 struct object_id new_oid;
972 int ret;
973
974 for (i = 0; i < it->subtree_nr; i++) {
975 strbuf_addf(path, "%s/", it->down[i]->name);
976 ret = verify_one(r, istate, it->down[i]->cache_tree, path);
977 if (ret)
978 goto out;
979
980 strbuf_setlen(path, len);
981 }
982
983 if (it->entry_count < 0 ||
984 /* no verification on tests (t7003) that replace trees */
985 lookup_replace_object(r, &it->oid) != &it->oid) {
986 ret = 0;
987 goto out;
988 }
989
990 if (path->len) {
991 /*
992 * If the index is sparse and the cache tree is not
993 * index_name_pos() may trigger ensure_full_index() which will
994 * free the tree that is being verified.
995 */
996 int is_sparse = istate->sparse_index;
997 pos = index_name_pos(istate, path->buf, path->len);
998 if (is_sparse && !istate->sparse_index) {
999 ret = 1;
1000 goto out;
1001 }
1002
1003 if (pos >= 0) {
1004 ret = verify_one_sparse(istate, path, pos);
1005 goto out;
1006 }
1007
1008 pos = -pos - 1;
1009 } else {
1010 pos = 0;
1011 }
1012
1013 if (it->entry_count + pos > istate->cache_nr) {
1014 ret = error(_("corrupted cache-tree has entries not present in index"));
1015 goto out;
1016 }
1017
1018 i = 0;
1019 while (i < it->entry_count) {
1020 struct cache_entry *ce = istate->cache[pos + i];
1021 const char *slash;
1022 struct cache_tree_sub *sub = NULL;
1023 const struct object_id *oid;
1024 const char *name;
1025 unsigned mode;
1026 int entlen;
1027
1028 if (ce->ce_flags & (CE_STAGEMASK | CE_INTENT_TO_ADD | CE_REMOVE)) {
1029 ret = error(_("%s with flags 0x%x should not be in cache-tree"),
1030 ce->name, ce->ce_flags);
1031 goto out;
1032 }
1033
1034 name = ce->name + path->len;
1035 slash = strchr(name, '/');
1036 if (slash) {
1037 entlen = slash - name;
1038
1039 sub = find_subtree(it, ce->name + path->len, entlen, 0);
1040 if (!sub || sub->cache_tree->entry_count < 0) {
1041 ret = error(_("bad subtree '%.*s'"), entlen, name);
1042 goto out;
1043 }
1044
1045 oid = &sub->cache_tree->oid;
1046 mode = S_IFDIR;
1047 i += sub->cache_tree->entry_count;
1048 } else {
1049 oid = &ce->oid;
1050 mode = ce->ce_mode;
1051 entlen = ce_namelen(ce) - path->len;
1052 i++;
1053 }
1054 strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0');
1055 strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz);
1056 }
1057
1058 hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, OBJ_TREE,
1059 &new_oid);
1060
1061 if (!oideq(&new_oid, &it->oid)) {
1062 ret = error(_("cache-tree for path %.*s does not match. "
1063 "Expected %s got %s"), len, path->buf,
1064 oid_to_hex(&new_oid), oid_to_hex(&it->oid));
1065 goto out;
1066 }
1067
1068 ret = 0;
1069 out:
1070 strbuf_setlen(path, len);
1071 strbuf_release(&tree_buf);
1072 return ret;
1073 }
1074
1075 int cache_tree_verify(struct repository *r, struct index_state *istate)
1076 {
1077 struct strbuf path = STRBUF_INIT;
1078 int ret;
1079
1080 if (!istate->cache_tree) {
1081 ret = 0;
1082 goto out;
1083 }
1084
1085 ret = verify_one(r, istate, istate->cache_tree, &path);
1086 if (ret < 0)
1087 goto out;
1088 if (ret > 0) {
1089 strbuf_reset(&path);
1090
1091 ret = verify_one(r, istate, istate->cache_tree, &path);
1092 if (ret < 0)
1093 goto out;
1094 if (ret > 0)
1095 BUG("ensure_full_index() called twice while verifying cache tree");
1096 }
1097
1098 ret = 0;
1099
1100 out:
1101 strbuf_release(&path);
1102 return ret;
1103 }