interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / cache-tree.c

1120 lines · 28427 bytes Raw Blame History
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
24struct 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
31void 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
48static 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
58int 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
79static 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
106struct 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
112static 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
158void 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 */
176static 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
212static 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
260static 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
277static 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
294int 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
300static 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
306static 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
526int 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
559static 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
600void 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
608static 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
639static 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
718struct 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
733static 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
758static 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
785struct 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
810int 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, fd;
813 struct lock_file lock_file = LOCK_INIT;
814 int ret;
815
816 /*
817 * The lock only protects the cache-tree write-back below; if somebody
818 * else holds it, compute the tree without updating the index.
819 */
820 fd = hold_lock_file_for_update(&lock_file, index_path, 0);
821
822 entries = read_index_from(index_state, index_path,
823 repo_get_git_dir(index_state->repo));
824 if (entries < 0) {
825 ret = WRITE_TREE_UNREADABLE_INDEX;
826 goto out;
827 }
828
829 was_valid = !(flags & WRITE_TREE_IGNORE_CACHE_TREE) &&
830 index_state->cache_tree &&
831 cache_tree_fully_valid(index_state);
832
833 ret = write_index_as_tree_internal(oid, index_state, was_valid, flags,
834 prefix);
835 if (!ret && !was_valid && fd >= 0) {
836 write_locked_index(index_state, &lock_file, COMMIT_LOCK);
837 /* Not being able to write is fine -- we are only interested
838 * in updating the cache-tree part, and if the next caller
839 * ends up using the old index with unupdated cache-tree part
840 * it misses the work we did here, but that is just a
841 * performance penalty and not a big deal.
842 */
843 }
844
845out:
846 rollback_lock_file(&lock_file);
847 return ret;
848}
849
850static void prime_cache_tree_sparse_dir(struct cache_tree *it,
851 struct tree *tree)
852{
853
854 oidcpy(&it->oid, &tree->object.oid);
855 it->entry_count = 1;
856}
857
858static void prime_cache_tree_rec(struct repository *r,
859 struct cache_tree *it,
860 struct tree *tree,
861 struct strbuf *tree_path)
862{
863 struct tree_desc desc;
864 struct name_entry entry;
865 int cnt;
866 size_t base_path_len = tree_path->len;
867
868 oidcpy(&it->oid, &tree->object.oid);
869
870 init_tree_desc(&desc, &tree->object.oid, tree->buffer, tree->size);
871 cnt = 0;
872 while (tree_entry(&desc, &entry)) {
873 if (!S_ISDIR(entry.mode))
874 cnt++;
875 else {
876 struct cache_tree_sub *sub;
877 struct tree *subtree = lookup_tree(r, &entry.oid);
878
879 if (repo_parse_tree(r, subtree) < 0)
880 exit(128);
881 sub = cache_tree_sub(it, entry.path);
882 sub->cache_tree = cache_tree();
883
884 /*
885 * Recursively-constructed subtree path is only needed when working
886 * in a sparse index (where it's used to determine whether the
887 * subtree is a sparse directory in the index).
888 */
889 if (r->index->sparse_index) {
890 strbuf_setlen(tree_path, base_path_len);
891 strbuf_add(tree_path, entry.path, entry.pathlen);
892 strbuf_addch(tree_path, '/');
893 }
894
895 /*
896 * If a sparse index is in use, the directory being processed may be
897 * sparse. To confirm that, we can check whether an entry with that
898 * exact name exists in the index. If it does, the created subtree
899 * should be sparse. Otherwise, cache tree expansion should continue
900 * as normal.
901 */
902 if (r->index->sparse_index &&
903 index_entry_exists(r->index, tree_path->buf, tree_path->len))
904 prime_cache_tree_sparse_dir(sub->cache_tree, subtree);
905 else
906 prime_cache_tree_rec(r, sub->cache_tree, subtree, tree_path);
907 cnt += sub->cache_tree->entry_count;
908 }
909 }
910
911 it->entry_count = cnt;
912}
913
914void prime_cache_tree(struct repository *r,
915 struct index_state *istate,
916 struct tree *tree)
917{
918 struct strbuf tree_path = STRBUF_INIT;
919
920 trace2_region_enter("cache-tree", "prime_cache_tree", r);
921 cache_tree_free(&istate->cache_tree);
922 istate->cache_tree = cache_tree();
923
924 prime_cache_tree_rec(r, istate->cache_tree, tree, &tree_path);
925 strbuf_release(&tree_path);
926 istate->cache_changed |= CACHE_TREE_CHANGED;
927 trace2_region_leave("cache-tree", "prime_cache_tree", r);
928}
929
930/*
931 * find the cache_tree that corresponds to the current level without
932 * exploding the full path into textual form. The root of the
933 * cache tree is given as "root", and our current level is "info".
934 * (1) When at root level, info->prev is NULL, so it is "root" itself.
935 * (2) Otherwise, find the cache_tree that corresponds to one level
936 * above us, and find ourselves in there.
937 */
938static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
939 struct traverse_info *info)
940{
941 struct cache_tree *our_parent;
942
943 if (!info->prev)
944 return root;
945 our_parent = find_cache_tree_from_traversal(root, info->prev);
946 return cache_tree_find(our_parent, info->name);
947}
948
949int cache_tree_matches_traversal(struct cache_tree *root,
950 struct name_entry *ent,
951 struct traverse_info *info)
952{
953 struct cache_tree *it;
954
955 it = find_cache_tree_from_traversal(root, info);
956 it = cache_tree_find(it, ent->path);
957 if (it && it->entry_count > 0 && oideq(&ent->oid, &it->oid))
958 return it->entry_count;
959 return 0;
960}
961
962static int verify_one_sparse(struct index_state *istate,
963 struct strbuf *path,
964 int pos)
965{
966 struct cache_entry *ce = istate->cache[pos];
967 if (!S_ISSPARSEDIR(ce->ce_mode))
968 return error(_("directory '%s' is present in index, but not sparse"),
969 path->buf);
970 return 0;
971}
972
973/*
974 * Returns:
975 * 0 - Verification completed.
976 * 1 - Restart verification - a call to ensure_full_index() freed the cache
977 * tree that is being verified and verification needs to be restarted from
978 * the new toplevel cache tree.
979 * -1 - Verification failed.
980 */
981static int verify_one(struct repository *r,
982 struct index_state *istate,
983 struct cache_tree *it,
984 struct strbuf *path)
985{
986 int i, pos, len = path->len;
987 struct strbuf tree_buf = STRBUF_INIT;
988 struct object_id new_oid;
989 int ret;
990
991 for (i = 0; i < it->subtree_nr; i++) {
992 strbuf_addf(path, "%s/", it->down[i]->name);
993 ret = verify_one(r, istate, it->down[i]->cache_tree, path);
994 if (ret)
995 goto out;
996
997 strbuf_setlen(path, len);
998 }
999
1000 if (it->entry_count < 0 ||
1001 /* no verification on tests (t7003) that replace trees */
1002 lookup_replace_object(r, &it->oid) != &it->oid) {
1003 ret = 0;
1004 goto out;
1005 }
1006
1007 if (path->len) {
1008 /*
1009 * If the index is sparse and the cache tree is not
1010 * index_name_pos() may trigger ensure_full_index() which will
1011 * free the tree that is being verified.
1012 */
1013 int is_sparse = istate->sparse_index;
1014 pos = index_name_pos(istate, path->buf, path->len);
1015 if (is_sparse && !istate->sparse_index) {
1016 ret = 1;
1017 goto out;
1018 }
1019
1020 if (pos >= 0) {
1021 ret = verify_one_sparse(istate, path, pos);
1022 goto out;
1023 }
1024
1025 pos = -pos - 1;
1026 } else {
1027 pos = 0;
1028 }
1029
1030 if (it->entry_count + pos > istate->cache_nr) {
1031 ret = error(_("corrupted cache-tree has entries not present in index"));
1032 goto out;
1033 }
1034
1035 i = 0;
1036 while (i < it->entry_count) {
1037 struct cache_entry *ce = istate->cache[pos + i];
1038 const char *slash;
1039 struct cache_tree_sub *sub = NULL;
1040 const struct object_id *oid;
1041 const char *name;
1042 unsigned mode;
1043 int entlen;
1044
1045 if (ce->ce_flags & (CE_STAGEMASK | CE_INTENT_TO_ADD | CE_REMOVE)) {
1046 ret = error(_("%s with flags 0x%x should not be in cache-tree"),
1047 ce->name, ce->ce_flags);
1048 goto out;
1049 }
1050
1051 name = ce->name + path->len;
1052 slash = strchr(name, '/');
1053 if (slash) {
1054 entlen = slash - name;
1055
1056 sub = find_subtree(it, ce->name + path->len, entlen, 0);
1057 if (!sub || sub->cache_tree->entry_count < 0) {
1058 ret = error(_("bad subtree '%.*s'"), entlen, name);
1059 goto out;
1060 }
1061
1062 oid = &sub->cache_tree->oid;
1063 mode = S_IFDIR;
1064 i += sub->cache_tree->entry_count;
1065 } else {
1066 oid = &ce->oid;
1067 mode = ce->ce_mode;
1068 entlen = ce_namelen(ce) - path->len;
1069 i++;
1070 }
1071 strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0');
1072 strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz);
1073 }
1074
1075 hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, OBJ_TREE,
1076 &new_oid);
1077
1078 if (!oideq(&new_oid, &it->oid)) {
1079 ret = error(_("cache-tree for path %.*s does not match. "
1080 "Expected %s got %s"), len, path->buf,
1081 oid_to_hex(&new_oid), oid_to_hex(&it->oid));
1082 goto out;
1083 }
1084
1085 ret = 0;
1086out:
1087 strbuf_setlen(path, len);
1088 strbuf_release(&tree_buf);
1089 return ret;
1090}
1091
1092int cache_tree_verify(struct repository *r, struct index_state *istate)
1093{
1094 struct strbuf path = STRBUF_INIT;
1095 int ret;
1096
1097 if (!istate->cache_tree) {
1098 ret = 0;
1099 goto out;
1100 }
1101
1102 ret = verify_one(r, istate, istate->cache_tree, &path);
1103 if (ret < 0)
1104 goto out;
1105 if (ret > 0) {
1106 strbuf_reset(&path);
1107
1108 ret = verify_one(r, istate, istate->cache_tree, &path);
1109 if (ret < 0)
1110 goto out;
1111 if (ret > 0)
1112 BUG("ensure_full_index() called twice while verifying cache tree");
1113 }
1114
1115 ret = 0;
1116
1117out:
1118 strbuf_release(&path);
1119 return ret;
1120}