1 #include "git-compat-util.h"
6 #include "cache-tree.h"
7 #include "bulk-checkin.h"
8 #include "object-store.h"
9 #include "replace-object.h"
10 #include "promisor-remote.h"
11 #include "sparse-index.h"
13 #ifndef DEBUG_CACHE_TREE
14 #define DEBUG_CACHE_TREE 0
17 struct cache_tree
*cache_tree(void)
19 struct cache_tree
*it
= xcalloc(1, sizeof(struct cache_tree
));
24 void cache_tree_free(struct cache_tree
**it_p
)
27 struct cache_tree
*it
= *it_p
;
31 for (i
= 0; i
< it
->subtree_nr
; i
++)
33 cache_tree_free(&it
->down
[i
]->cache_tree
);
41 static int subtree_name_cmp(const char *one
, int onelen
,
42 const char *two
, int twolen
)
48 return memcmp(one
, two
, onelen
);
51 int cache_tree_subtree_pos(struct cache_tree
*it
, const char *path
, int pathlen
)
53 struct cache_tree_sub
**down
= it
->down
;
58 int mi
= lo
+ (hi
- lo
) / 2;
59 struct cache_tree_sub
*mdl
= down
[mi
];
60 int cmp
= subtree_name_cmp(path
, pathlen
,
61 mdl
->name
, mdl
->namelen
);
72 static struct cache_tree_sub
*find_subtree(struct cache_tree
*it
,
77 struct cache_tree_sub
*down
;
78 int pos
= cache_tree_subtree_pos(it
, path
, pathlen
);
85 ALLOC_GROW(it
->down
, it
->subtree_nr
+ 1, it
->subtree_alloc
);
88 FLEX_ALLOC_MEM(down
, name
, path
, pathlen
);
89 down
->cache_tree
= NULL
;
90 down
->namelen
= pathlen
;
92 if (pos
< it
->subtree_nr
)
93 MOVE_ARRAY(it
->down
+ pos
+ 1, it
->down
+ pos
,
94 it
->subtree_nr
- pos
- 1);
99 struct cache_tree_sub
*cache_tree_sub(struct cache_tree
*it
, const char *path
)
101 int pathlen
= strlen(path
);
102 return find_subtree(it
, path
, pathlen
, 1);
105 static int do_invalidate_path(struct cache_tree
*it
, const char *path
)
108 * ==> invalidate self
109 * ==> find "a", have it invalidate "b/c"
111 * ==> invalidate self
112 * ==> if "a" exists as a subtree, remove it.
116 struct cache_tree_sub
*down
;
119 fprintf(stderr
, "cache-tree invalidate <%s>\n", path
);
124 slash
= strchrnul(path
, '/');
125 namelen
= slash
- path
;
126 it
->entry_count
= -1;
129 pos
= cache_tree_subtree_pos(it
, path
, namelen
);
131 cache_tree_free(&it
->down
[pos
]->cache_tree
);
136 * move 4 and 5 up one place (2 entries)
137 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
139 MOVE_ARRAY(it
->down
+ pos
, it
->down
+ pos
+ 1,
140 it
->subtree_nr
- pos
- 1);
145 down
= find_subtree(it
, path
, namelen
, 0);
147 do_invalidate_path(down
->cache_tree
, slash
+ 1);
151 void cache_tree_invalidate_path(struct index_state
*istate
, const char *path
)
153 if (do_invalidate_path(istate
->cache_tree
, path
))
154 istate
->cache_changed
|= CACHE_TREE_CHANGED
;
157 static int verify_cache(struct index_state
*istate
, int flags
)
160 int silent
= flags
& WRITE_TREE_SILENT
;
162 /* Verify that the tree is merged */
164 for (i
= 0; i
< istate
->cache_nr
; i
++) {
165 const struct cache_entry
*ce
= istate
->cache
[i
];
170 fprintf(stderr
, "...\n");
173 fprintf(stderr
, "%s: unmerged (%s)\n",
174 ce
->name
, oid_to_hex(&ce
->oid
));
180 /* Also verify that the cache does not have path and path/file
181 * at the same time. At this point we know the cache has only
185 for (i
= 0; i
+ 1 < istate
->cache_nr
; i
++) {
186 /* path/file always comes after path because of the way
187 * the cache is sorted. Also path can appear only once,
188 * which means conflicting one would immediately follow.
190 const struct cache_entry
*this_ce
= istate
->cache
[i
];
191 const struct cache_entry
*next_ce
= istate
->cache
[i
+ 1];
192 const char *this_name
= this_ce
->name
;
193 const char *next_name
= next_ce
->name
;
194 int this_len
= ce_namelen(this_ce
);
195 if (this_len
< ce_namelen(next_ce
) &&
196 next_name
[this_len
] == '/' &&
197 strncmp(this_name
, next_name
, this_len
) == 0) {
199 fprintf(stderr
, "...\n");
202 fprintf(stderr
, "You have both %s and %s\n",
203 this_name
, next_name
);
211 static void discard_unused_subtrees(struct cache_tree
*it
)
213 struct cache_tree_sub
**down
= it
->down
;
214 int nr
= it
->subtree_nr
;
216 for (dst
= src
= 0; src
< nr
; src
++) {
217 struct cache_tree_sub
*s
= down
[src
];
221 cache_tree_free(&s
->cache_tree
);
228 int cache_tree_fully_valid(struct cache_tree
*it
)
233 if (it
->entry_count
< 0 || !has_object_file(&it
->oid
))
235 for (i
= 0; i
< it
->subtree_nr
; i
++) {
236 if (!cache_tree_fully_valid(it
->down
[i
]->cache_tree
))
242 static int must_check_existence(const struct cache_entry
*ce
)
244 return !(has_promisor_remote() && ce_skip_worktree(ce
));
247 static int update_one(struct cache_tree
*it
,
248 struct cache_entry
**cache
,
255 struct strbuf buffer
;
256 int missing_ok
= flags
& WRITE_TREE_MISSING_OK
;
257 int dryrun
= flags
& WRITE_TREE_DRY_RUN
;
258 int repair
= flags
& WRITE_TREE_REPAIR
;
259 int to_invalidate
= 0;
262 assert(!(dryrun
&& repair
));
267 * If the first entry of this region is a sparse directory
268 * entry corresponding exactly to 'base', then this cache_tree
269 * struct is a "leaf" in the data structure, pointing to the
270 * tree OID specified in the entry.
273 const struct cache_entry
*ce
= cache
[0];
275 if (S_ISSPARSEDIR(ce
->ce_mode
) &&
276 ce
->ce_namelen
== baselen
&&
277 !strncmp(ce
->name
, base
, baselen
)) {
279 oidcpy(&it
->oid
, &ce
->oid
);
284 if (0 <= it
->entry_count
&& has_object_file(&it
->oid
))
285 return it
->entry_count
;
288 * We first scan for subtrees and update them; we start by
289 * marking existing subtrees -- the ones that are unmarked
290 * should not be in the result.
292 for (i
= 0; i
< it
->subtree_nr
; i
++)
293 it
->down
[i
]->used
= 0;
296 * Find the subtrees and update them.
299 while (i
< entries
) {
300 const struct cache_entry
*ce
= cache
[i
];
301 struct cache_tree_sub
*sub
;
302 const char *path
, *slash
;
303 int pathlen
, sublen
, subcnt
, subskip
;
306 pathlen
= ce_namelen(ce
);
307 if (pathlen
<= baselen
|| memcmp(base
, path
, baselen
))
308 break; /* at the end of this level */
310 slash
= strchr(path
+ baselen
, '/');
316 * a/bbb/c (base = a/, slash = /c)
318 * path+baselen = bbb/c, sublen = 3
320 sublen
= slash
- (path
+ baselen
);
321 sub
= find_subtree(it
, path
+ baselen
, sublen
, 1);
322 if (!sub
->cache_tree
)
323 sub
->cache_tree
= cache_tree();
324 subcnt
= update_one(sub
->cache_tree
,
325 cache
+ i
, entries
- i
,
327 baselen
+ sublen
+ 1,
333 die("index cache-tree records empty sub-tree");
335 sub
->count
= subcnt
; /* to be used in the next loop */
336 *skip_count
+= subskip
;
340 discard_unused_subtrees(it
);
343 * Then write out the tree object for this level.
345 strbuf_init(&buffer
, 8192);
348 while (i
< entries
) {
349 const struct cache_entry
*ce
= cache
[i
];
350 struct cache_tree_sub
*sub
= NULL
;
351 const char *path
, *slash
;
353 const struct object_id
*oid
;
355 int expected_missing
= 0;
356 int contains_ita
= 0;
360 pathlen
= ce_namelen(ce
);
361 if (pathlen
<= baselen
|| memcmp(base
, path
, baselen
))
362 break; /* at the end of this level */
364 slash
= strchr(path
+ baselen
, '/');
366 entlen
= slash
- (path
+ baselen
);
367 sub
= find_subtree(it
, path
+ baselen
, entlen
, 0);
369 die("cache-tree.c: '%.*s' in '%s' not found",
370 entlen
, path
+ baselen
, path
);
372 oid
= &sub
->cache_tree
->oid
;
374 contains_ita
= sub
->cache_tree
->entry_count
< 0;
377 expected_missing
= 1;
383 entlen
= pathlen
- baselen
;
387 ce_missing_ok
= mode
== S_IFGITLINK
|| missing_ok
||
388 !must_check_existence(ce
);
389 if (is_null_oid(oid
) ||
390 (!ce_missing_ok
&& !has_object_file(oid
))) {
391 strbuf_release(&buffer
);
392 if (expected_missing
)
394 return error("invalid object %06o %s for '%.*s'",
395 mode
, oid_to_hex(oid
), entlen
+baselen
, path
);
399 * CE_REMOVE entries are removed before the index is
400 * written to disk. Skip them to remain consistent
401 * with the future on-disk index.
403 if (ce
->ce_flags
& CE_REMOVE
) {
404 *skip_count
= *skip_count
+ 1;
409 * CE_INTENT_TO_ADD entries exist in on-disk index but
410 * they are not part of generated trees. Invalidate up
411 * to root to force cache-tree users to read elsewhere.
413 if (!sub
&& ce_intent_to_add(ce
)) {
419 * "sub" can be an empty tree if all subentries are i-t-a.
421 if (contains_ita
&& is_empty_tree_oid(oid
))
424 strbuf_grow(&buffer
, entlen
+ 100);
425 strbuf_addf(&buffer
, "%o %.*s%c", mode
, entlen
, path
+ baselen
, '\0');
426 strbuf_add(&buffer
, oid
->hash
, the_hash_algo
->rawsz
);
429 fprintf(stderr
, "cache-tree update-one %o %.*s\n",
430 mode
, entlen
, path
+ baselen
);
435 struct object_id oid
;
436 hash_object_file(the_hash_algo
, buffer
.buf
, buffer
.len
,
438 if (has_object_file_with_flags(&oid
, OBJECT_INFO_SKIP_FETCH_OBJECT
))
439 oidcpy(&it
->oid
, &oid
);
443 hash_object_file(the_hash_algo
, buffer
.buf
, buffer
.len
,
445 } else if (write_object_file_flags(buffer
.buf
, buffer
.len
, OBJ_TREE
,
446 &it
->oid
, flags
& WRITE_TREE_SILENT
447 ? HASH_SILENT
: 0)) {
448 strbuf_release(&buffer
);
452 strbuf_release(&buffer
);
453 it
->entry_count
= to_invalidate
? -1 : i
- *skip_count
;
455 fprintf(stderr
, "cache-tree update-one (%d ent, %d subtree) %s\n",
456 it
->entry_count
, it
->subtree_nr
,
457 oid_to_hex(&it
->oid
));
462 int cache_tree_update(struct index_state
*istate
, int flags
)
466 i
= verify_cache(istate
, flags
);
471 if (!istate
->cache_tree
)
472 istate
->cache_tree
= cache_tree();
474 if (!(flags
& WRITE_TREE_MISSING_OK
) && has_promisor_remote())
475 prefetch_cache_entries(istate
, must_check_existence
);
477 trace_performance_enter();
478 trace2_region_enter("cache_tree", "update", the_repository
);
479 begin_odb_transaction();
480 i
= update_one(istate
->cache_tree
, istate
->cache
, istate
->cache_nr
,
481 "", 0, &skip
, flags
);
482 end_odb_transaction();
483 trace2_region_leave("cache_tree", "update", the_repository
);
484 trace_performance_leave("cache_tree_update");
487 istate
->cache_changed
|= CACHE_TREE_CHANGED
;
491 static void write_one(struct strbuf
*buffer
, struct cache_tree
*it
,
492 const char *path
, int pathlen
)
496 /* One "cache-tree" entry consists of the following:
497 * path (NUL terminated)
498 * entry_count, subtree_nr ("%d %d\n")
499 * tree-sha1 (missing if invalid)
500 * subtree_nr "cache-tree" entries for subtrees.
502 strbuf_grow(buffer
, pathlen
+ 100);
503 strbuf_add(buffer
, path
, pathlen
);
504 strbuf_addf(buffer
, "%c%d %d\n", 0, it
->entry_count
, it
->subtree_nr
);
507 if (0 <= it
->entry_count
)
508 fprintf(stderr
, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
509 pathlen
, path
, it
->entry_count
, it
->subtree_nr
,
510 oid_to_hex(&it
->oid
));
512 fprintf(stderr
, "cache-tree <%.*s> (%d subtree) invalid\n",
513 pathlen
, path
, it
->subtree_nr
);
516 if (0 <= it
->entry_count
) {
517 strbuf_add(buffer
, it
->oid
.hash
, the_hash_algo
->rawsz
);
519 for (i
= 0; i
< it
->subtree_nr
; i
++) {
520 struct cache_tree_sub
*down
= it
->down
[i
];
522 struct cache_tree_sub
*prev
= it
->down
[i
-1];
523 if (subtree_name_cmp(down
->name
, down
->namelen
,
524 prev
->name
, prev
->namelen
) <= 0)
525 die("fatal - unsorted cache subtree");
527 write_one(buffer
, down
->cache_tree
, down
->name
, down
->namelen
);
531 void cache_tree_write(struct strbuf
*sb
, struct cache_tree
*root
)
533 trace2_region_enter("cache_tree", "write", the_repository
);
534 write_one(sb
, root
, "", 0);
535 trace2_region_leave("cache_tree", "write", the_repository
);
538 static struct cache_tree
*read_one(const char **buffer
, unsigned long *size_p
)
540 const char *buf
= *buffer
;
541 unsigned long size
= *size_p
;
544 struct cache_tree
*it
;
546 const unsigned rawsz
= the_hash_algo
->rawsz
;
549 /* skip name, but make sure name exists */
550 while (size
&& *buf
) {
560 it
->entry_count
= strtol(cp
, &ep
, 10);
564 subtree_nr
= strtol(cp
, &ep
, 10);
567 while (size
&& *buf
&& *buf
!= '\n') {
574 if (0 <= it
->entry_count
) {
577 oidread(&it
->oid
, (const unsigned char *)buf
);
583 if (0 <= it
->entry_count
)
584 fprintf(stderr
, "cache-tree <%s> (%d ent, %d subtree) %s\n",
585 *buffer
, it
->entry_count
, subtree_nr
,
586 oid_to_hex(&it
->oid
));
588 fprintf(stderr
, "cache-tree <%s> (%d subtrees) invalid\n",
589 *buffer
, subtree_nr
);
593 * Just a heuristic -- we do not add directories that often but
594 * we do not want to have to extend it immediately when we do,
597 it
->subtree_alloc
= subtree_nr
+ 2;
598 CALLOC_ARRAY(it
->down
, it
->subtree_alloc
);
599 for (i
= 0; i
< subtree_nr
; i
++) {
600 /* read each subtree */
601 struct cache_tree
*sub
;
602 struct cache_tree_sub
*subtree
;
603 const char *name
= buf
;
605 sub
= read_one(&buf
, &size
);
608 subtree
= cache_tree_sub(it
, name
);
609 subtree
->cache_tree
= sub
;
611 if (subtree_nr
!= it
->subtree_nr
)
612 die("cache-tree: internal error");
618 cache_tree_free(&it
);
622 struct cache_tree
*cache_tree_read(const char *buffer
, unsigned long size
)
624 struct cache_tree
*result
;
627 return NULL
; /* not the whole tree */
629 trace2_region_enter("cache_tree", "read", the_repository
);
630 result
= read_one(&buffer
, &size
);
631 trace2_region_leave("cache_tree", "read", the_repository
);
636 static struct cache_tree
*cache_tree_find(struct cache_tree
*it
, const char *path
)
642 struct cache_tree_sub
*sub
;
644 slash
= strchrnul(path
, '/');
646 * Between path and slash is the name of the subtree
649 sub
= find_subtree(it
, path
, slash
- path
, 0);
652 it
= sub
->cache_tree
;
661 static int write_index_as_tree_internal(struct object_id
*oid
,
662 struct index_state
*index_state
,
663 int cache_tree_valid
,
667 if (flags
& WRITE_TREE_IGNORE_CACHE_TREE
) {
668 cache_tree_free(&index_state
->cache_tree
);
669 cache_tree_valid
= 0;
672 if (!cache_tree_valid
&& cache_tree_update(index_state
, flags
) < 0)
673 return WRITE_TREE_UNMERGED_INDEX
;
676 struct cache_tree
*subtree
;
677 subtree
= cache_tree_find(index_state
->cache_tree
, prefix
);
679 return WRITE_TREE_PREFIX_ERROR
;
680 oidcpy(oid
, &subtree
->oid
);
683 oidcpy(oid
, &index_state
->cache_tree
->oid
);
688 struct tree
* write_in_core_index_as_tree(struct repository
*repo
) {
692 struct index_state
*index_state
= repo
->index
;
693 was_valid
= index_state
->cache_tree
&&
694 cache_tree_fully_valid(index_state
->cache_tree
);
696 ret
= write_index_as_tree_internal(&o
, index_state
, was_valid
, 0, NULL
);
697 if (ret
== WRITE_TREE_UNMERGED_INDEX
) {
699 bug("there are unmerged index entries:");
700 for (i
= 0; i
< index_state
->cache_nr
; i
++) {
701 const struct cache_entry
*ce
= index_state
->cache
[i
];
703 bug("%d %.*s", ce_stage(ce
),
704 (int)ce_namelen(ce
), ce
->name
);
706 BUG("unmerged index entries when writing in-core index");
709 return lookup_tree(repo
, &index_state
->cache_tree
->oid
);
713 int write_index_as_tree(struct object_id
*oid
, struct index_state
*index_state
, const char *index_path
, int flags
, const char *prefix
)
715 int entries
, was_valid
;
716 struct lock_file lock_file
= LOCK_INIT
;
719 hold_lock_file_for_update(&lock_file
, index_path
, LOCK_DIE_ON_ERROR
);
721 entries
= read_index_from(index_state
, index_path
, get_git_dir());
723 ret
= WRITE_TREE_UNREADABLE_INDEX
;
727 was_valid
= !(flags
& WRITE_TREE_IGNORE_CACHE_TREE
) &&
728 index_state
->cache_tree
&&
729 cache_tree_fully_valid(index_state
->cache_tree
);
731 ret
= write_index_as_tree_internal(oid
, index_state
, was_valid
, flags
,
733 if (!ret
&& !was_valid
) {
734 write_locked_index(index_state
, &lock_file
, COMMIT_LOCK
);
735 /* Not being able to write is fine -- we are only interested
736 * in updating the cache-tree part, and if the next caller
737 * ends up using the old index with unupdated cache-tree part
738 * it misses the work we did here, but that is just a
739 * performance penalty and not a big deal.
744 rollback_lock_file(&lock_file
);
748 static void prime_cache_tree_sparse_dir(struct cache_tree
*it
,
752 oidcpy(&it
->oid
, &tree
->object
.oid
);
756 static void prime_cache_tree_rec(struct repository
*r
,
757 struct cache_tree
*it
,
759 struct strbuf
*tree_path
)
761 struct tree_desc desc
;
762 struct name_entry entry
;
764 size_t base_path_len
= tree_path
->len
;
766 oidcpy(&it
->oid
, &tree
->object
.oid
);
768 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
770 while (tree_entry(&desc
, &entry
)) {
771 if (!S_ISDIR(entry
.mode
))
774 struct cache_tree_sub
*sub
;
775 struct tree
*subtree
= lookup_tree(r
, &entry
.oid
);
777 if (!subtree
->object
.parsed
)
779 sub
= cache_tree_sub(it
, entry
.path
);
780 sub
->cache_tree
= cache_tree();
783 * Recursively-constructed subtree path is only needed when working
784 * in a sparse index (where it's used to determine whether the
785 * subtree is a sparse directory in the index).
787 if (r
->index
->sparse_index
) {
788 strbuf_setlen(tree_path
, base_path_len
);
789 strbuf_add(tree_path
, entry
.path
, entry
.pathlen
);
790 strbuf_addch(tree_path
, '/');
794 * If a sparse index is in use, the directory being processed may be
795 * sparse. To confirm that, we can check whether an entry with that
796 * exact name exists in the index. If it does, the created subtree
797 * should be sparse. Otherwise, cache tree expansion should continue
800 if (r
->index
->sparse_index
&&
801 index_entry_exists(r
->index
, tree_path
->buf
, tree_path
->len
))
802 prime_cache_tree_sparse_dir(sub
->cache_tree
, subtree
);
804 prime_cache_tree_rec(r
, sub
->cache_tree
, subtree
, tree_path
);
805 cnt
+= sub
->cache_tree
->entry_count
;
809 it
->entry_count
= cnt
;
812 void prime_cache_tree(struct repository
*r
,
813 struct index_state
*istate
,
816 struct strbuf tree_path
= STRBUF_INIT
;
818 trace2_region_enter("cache-tree", "prime_cache_tree", the_repository
);
819 cache_tree_free(&istate
->cache_tree
);
820 istate
->cache_tree
= cache_tree();
822 prime_cache_tree_rec(r
, istate
->cache_tree
, tree
, &tree_path
);
823 strbuf_release(&tree_path
);
824 istate
->cache_changed
|= CACHE_TREE_CHANGED
;
825 trace2_region_leave("cache-tree", "prime_cache_tree", the_repository
);
829 * find the cache_tree that corresponds to the current level without
830 * exploding the full path into textual form. The root of the
831 * cache tree is given as "root", and our current level is "info".
832 * (1) When at root level, info->prev is NULL, so it is "root" itself.
833 * (2) Otherwise, find the cache_tree that corresponds to one level
834 * above us, and find ourselves in there.
836 static struct cache_tree
*find_cache_tree_from_traversal(struct cache_tree
*root
,
837 struct traverse_info
*info
)
839 struct cache_tree
*our_parent
;
843 our_parent
= find_cache_tree_from_traversal(root
, info
->prev
);
844 return cache_tree_find(our_parent
, info
->name
);
847 int cache_tree_matches_traversal(struct cache_tree
*root
,
848 struct name_entry
*ent
,
849 struct traverse_info
*info
)
851 struct cache_tree
*it
;
853 it
= find_cache_tree_from_traversal(root
, info
);
854 it
= cache_tree_find(it
, ent
->path
);
855 if (it
&& it
->entry_count
> 0 && oideq(&ent
->oid
, &it
->oid
))
856 return it
->entry_count
;
860 static void verify_one_sparse(struct index_state
*istate
,
864 struct cache_entry
*ce
= istate
->cache
[pos
];
866 if (!S_ISSPARSEDIR(ce
->ce_mode
))
867 BUG("directory '%s' is present in index, but not sparse",
873 * 0 - Verification completed.
874 * 1 - Restart verification - a call to ensure_full_index() freed the cache
875 * tree that is being verified and verification needs to be restarted from
876 * the new toplevel cache tree.
878 static int verify_one(struct repository
*r
,
879 struct index_state
*istate
,
880 struct cache_tree
*it
,
883 int i
, pos
, len
= path
->len
;
884 struct strbuf tree_buf
= STRBUF_INIT
;
885 struct object_id new_oid
;
887 for (i
= 0; i
< it
->subtree_nr
; i
++) {
888 strbuf_addf(path
, "%s/", it
->down
[i
]->name
);
889 if (verify_one(r
, istate
, it
->down
[i
]->cache_tree
, path
))
891 strbuf_setlen(path
, len
);
894 if (it
->entry_count
< 0 ||
895 /* no verification on tests (t7003) that replace trees */
896 lookup_replace_object(r
, &it
->oid
) != &it
->oid
)
901 * If the index is sparse and the cache tree is not
902 * index_name_pos() may trigger ensure_full_index() which will
903 * free the tree that is being verified.
905 int is_sparse
= istate
->sparse_index
;
906 pos
= index_name_pos(istate
, path
->buf
, path
->len
);
907 if (is_sparse
&& !istate
->sparse_index
)
911 verify_one_sparse(istate
, path
, pos
);
921 while (i
< it
->entry_count
) {
922 struct cache_entry
*ce
= istate
->cache
[pos
+ i
];
924 struct cache_tree_sub
*sub
= NULL
;
925 const struct object_id
*oid
;
930 if (ce
->ce_flags
& (CE_STAGEMASK
| CE_INTENT_TO_ADD
| CE_REMOVE
))
931 BUG("%s with flags 0x%x should not be in cache-tree",
932 ce
->name
, ce
->ce_flags
);
933 name
= ce
->name
+ path
->len
;
934 slash
= strchr(name
, '/');
936 entlen
= slash
- name
;
937 sub
= find_subtree(it
, ce
->name
+ path
->len
, entlen
, 0);
938 if (!sub
|| sub
->cache_tree
->entry_count
< 0)
939 BUG("bad subtree '%.*s'", entlen
, name
);
940 oid
= &sub
->cache_tree
->oid
;
942 i
+= sub
->cache_tree
->entry_count
;
946 entlen
= ce_namelen(ce
) - path
->len
;
949 strbuf_addf(&tree_buf
, "%o %.*s%c", mode
, entlen
, name
, '\0');
950 strbuf_add(&tree_buf
, oid
->hash
, r
->hash_algo
->rawsz
);
952 hash_object_file(r
->hash_algo
, tree_buf
.buf
, tree_buf
.len
, OBJ_TREE
,
954 if (!oideq(&new_oid
, &it
->oid
))
955 BUG("cache-tree for path %.*s does not match. "
956 "Expected %s got %s", len
, path
->buf
,
957 oid_to_hex(&new_oid
), oid_to_hex(&it
->oid
));
958 strbuf_setlen(path
, len
);
959 strbuf_release(&tree_buf
);
963 void cache_tree_verify(struct repository
*r
, struct index_state
*istate
)
965 struct strbuf path
= STRBUF_INIT
;
967 if (!istate
->cache_tree
)
969 if (verify_one(r
, istate
, istate
->cache_tree
, &path
)) {
971 if (verify_one(r
, istate
, istate
->cache_tree
, &path
))
972 BUG("ensure_full_index() called twice while verifying cache tree");
974 strbuf_release(&path
);