5 #include "cache-tree.h"
6 #include "object-store.h"
7 #include "replace-object.h"
8 #include "promisor-remote.h"
10 #ifndef DEBUG_CACHE_TREE
11 #define DEBUG_CACHE_TREE 0
14 struct cache_tree
*cache_tree(void)
16 struct cache_tree
*it
= xcalloc(1, sizeof(struct cache_tree
));
21 void cache_tree_free(struct cache_tree
**it_p
)
24 struct cache_tree
*it
= *it_p
;
28 for (i
= 0; i
< it
->subtree_nr
; i
++)
30 cache_tree_free(&it
->down
[i
]->cache_tree
);
38 static int subtree_name_cmp(const char *one
, int onelen
,
39 const char *two
, int twolen
)
45 return memcmp(one
, two
, onelen
);
48 static int subtree_pos(struct cache_tree
*it
, const char *path
, int pathlen
)
50 struct cache_tree_sub
**down
= it
->down
;
55 int mi
= lo
+ (hi
- lo
) / 2;
56 struct cache_tree_sub
*mdl
= down
[mi
];
57 int cmp
= subtree_name_cmp(path
, pathlen
,
58 mdl
->name
, mdl
->namelen
);
69 static struct cache_tree_sub
*find_subtree(struct cache_tree
*it
,
74 struct cache_tree_sub
*down
;
75 int pos
= subtree_pos(it
, path
, pathlen
);
82 ALLOC_GROW(it
->down
, it
->subtree_nr
+ 1, it
->subtree_alloc
);
85 FLEX_ALLOC_MEM(down
, name
, path
, pathlen
);
86 down
->cache_tree
= NULL
;
87 down
->namelen
= pathlen
;
89 if (pos
< it
->subtree_nr
)
90 MOVE_ARRAY(it
->down
+ pos
+ 1, it
->down
+ pos
,
91 it
->subtree_nr
- pos
- 1);
96 struct cache_tree_sub
*cache_tree_sub(struct cache_tree
*it
, const char *path
)
98 int pathlen
= strlen(path
);
99 return find_subtree(it
, path
, pathlen
, 1);
102 static int do_invalidate_path(struct cache_tree
*it
, const char *path
)
105 * ==> invalidate self
106 * ==> find "a", have it invalidate "b/c"
108 * ==> invalidate self
109 * ==> if "a" exists as a subtree, remove it.
113 struct cache_tree_sub
*down
;
116 fprintf(stderr
, "cache-tree invalidate <%s>\n", path
);
121 slash
= strchrnul(path
, '/');
122 namelen
= slash
- path
;
123 it
->entry_count
= -1;
126 pos
= subtree_pos(it
, path
, namelen
);
128 cache_tree_free(&it
->down
[pos
]->cache_tree
);
133 * move 4 and 5 up one place (2 entries)
134 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
136 MOVE_ARRAY(it
->down
+ pos
, it
->down
+ pos
+ 1,
137 it
->subtree_nr
- pos
- 1);
142 down
= find_subtree(it
, path
, namelen
, 0);
144 do_invalidate_path(down
->cache_tree
, slash
+ 1);
148 void cache_tree_invalidate_path(struct index_state
*istate
, const char *path
)
150 if (do_invalidate_path(istate
->cache_tree
, path
))
151 istate
->cache_changed
|= CACHE_TREE_CHANGED
;
154 static int verify_cache(struct cache_entry
**cache
,
155 int entries
, int flags
)
158 int silent
= flags
& WRITE_TREE_SILENT
;
160 /* Verify that the tree is merged */
162 for (i
= 0; i
< entries
; i
++) {
163 const struct cache_entry
*ce
= cache
[i
];
168 fprintf(stderr
, "...\n");
171 fprintf(stderr
, "%s: unmerged (%s)\n",
172 ce
->name
, oid_to_hex(&ce
->oid
));
178 /* Also verify that the cache does not have path and path/file
179 * at the same time. At this point we know the cache has only
183 for (i
= 0; i
< entries
- 1; i
++) {
184 /* path/file always comes after path because of the way
185 * the cache is sorted. Also path can appear only once,
186 * which means conflicting one would immediately follow.
188 const char *this_name
= cache
[i
]->name
;
189 const char *next_name
= cache
[i
+1]->name
;
190 int this_len
= strlen(this_name
);
191 if (this_len
< strlen(next_name
) &&
192 strncmp(this_name
, next_name
, this_len
) == 0 &&
193 next_name
[this_len
] == '/') {
195 fprintf(stderr
, "...\n");
198 fprintf(stderr
, "You have both %s and %s\n",
199 this_name
, next_name
);
207 static void discard_unused_subtrees(struct cache_tree
*it
)
209 struct cache_tree_sub
**down
= it
->down
;
210 int nr
= it
->subtree_nr
;
212 for (dst
= src
= 0; src
< nr
; src
++) {
213 struct cache_tree_sub
*s
= down
[src
];
217 cache_tree_free(&s
->cache_tree
);
224 int cache_tree_fully_valid(struct cache_tree
*it
)
229 if (it
->entry_count
< 0 || !has_object_file(&it
->oid
))
231 for (i
= 0; i
< it
->subtree_nr
; i
++) {
232 if (!cache_tree_fully_valid(it
->down
[i
]->cache_tree
))
238 static int update_one(struct cache_tree
*it
,
239 struct cache_entry
**cache
,
246 struct strbuf buffer
;
247 int missing_ok
= flags
& WRITE_TREE_MISSING_OK
;
248 int dryrun
= flags
& WRITE_TREE_DRY_RUN
;
249 int repair
= flags
& WRITE_TREE_REPAIR
;
250 int to_invalidate
= 0;
253 assert(!(dryrun
&& repair
));
257 if (0 <= it
->entry_count
&& has_object_file(&it
->oid
))
258 return it
->entry_count
;
261 * We first scan for subtrees and update them; we start by
262 * marking existing subtrees -- the ones that are unmarked
263 * should not be in the result.
265 for (i
= 0; i
< it
->subtree_nr
; i
++)
266 it
->down
[i
]->used
= 0;
269 * Find the subtrees and update them.
272 while (i
< entries
) {
273 const struct cache_entry
*ce
= cache
[i
];
274 struct cache_tree_sub
*sub
;
275 const char *path
, *slash
;
276 int pathlen
, sublen
, subcnt
, subskip
;
279 pathlen
= ce_namelen(ce
);
280 if (pathlen
<= baselen
|| memcmp(base
, path
, baselen
))
281 break; /* at the end of this level */
283 slash
= strchr(path
+ baselen
, '/');
289 * a/bbb/c (base = a/, slash = /c)
291 * path+baselen = bbb/c, sublen = 3
293 sublen
= slash
- (path
+ baselen
);
294 sub
= find_subtree(it
, path
+ baselen
, sublen
, 1);
295 if (!sub
->cache_tree
)
296 sub
->cache_tree
= cache_tree();
297 subcnt
= update_one(sub
->cache_tree
,
298 cache
+ i
, entries
- i
,
300 baselen
+ sublen
+ 1,
306 die("index cache-tree records empty sub-tree");
308 sub
->count
= subcnt
; /* to be used in the next loop */
309 *skip_count
+= subskip
;
313 discard_unused_subtrees(it
);
316 * Then write out the tree object for this level.
318 strbuf_init(&buffer
, 8192);
321 while (i
< entries
) {
322 const struct cache_entry
*ce
= cache
[i
];
323 struct cache_tree_sub
*sub
= NULL
;
324 const char *path
, *slash
;
326 const struct object_id
*oid
;
328 int expected_missing
= 0;
329 int contains_ita
= 0;
333 pathlen
= ce_namelen(ce
);
334 if (pathlen
<= baselen
|| memcmp(base
, path
, baselen
))
335 break; /* at the end of this level */
337 slash
= strchr(path
+ baselen
, '/');
339 entlen
= slash
- (path
+ baselen
);
340 sub
= find_subtree(it
, path
+ baselen
, entlen
, 0);
342 die("cache-tree.c: '%.*s' in '%s' not found",
343 entlen
, path
+ baselen
, path
);
345 oid
= &sub
->cache_tree
->oid
;
347 contains_ita
= sub
->cache_tree
->entry_count
< 0;
350 expected_missing
= 1;
356 entlen
= pathlen
- baselen
;
360 ce_missing_ok
= mode
== S_IFGITLINK
|| missing_ok
||
361 (has_promisor_remote() &&
362 ce_skip_worktree(ce
));
363 if (is_null_oid(oid
) ||
364 (!ce_missing_ok
&& !has_object_file(oid
))) {
365 strbuf_release(&buffer
);
366 if (expected_missing
)
368 return error("invalid object %06o %s for '%.*s'",
369 mode
, oid_to_hex(oid
), entlen
+baselen
, path
);
373 * CE_REMOVE entries are removed before the index is
374 * written to disk. Skip them to remain consistent
375 * with the future on-disk index.
377 if (ce
->ce_flags
& CE_REMOVE
) {
378 *skip_count
= *skip_count
+ 1;
383 * CE_INTENT_TO_ADD entries exist on on-disk index but
384 * they are not part of generated trees. Invalidate up
385 * to root to force cache-tree users to read elsewhere.
387 if (!sub
&& ce_intent_to_add(ce
)) {
393 * "sub" can be an empty tree if all subentries are i-t-a.
395 if (contains_ita
&& is_empty_tree_oid(oid
))
398 strbuf_grow(&buffer
, entlen
+ 100);
399 strbuf_addf(&buffer
, "%o %.*s%c", mode
, entlen
, path
+ baselen
, '\0');
400 strbuf_add(&buffer
, oid
->hash
, the_hash_algo
->rawsz
);
403 fprintf(stderr
, "cache-tree update-one %o %.*s\n",
404 mode
, entlen
, path
+ baselen
);
409 struct object_id oid
;
410 hash_object_file(the_hash_algo
, buffer
.buf
, buffer
.len
,
412 if (has_object_file_with_flags(&oid
, OBJECT_INFO_SKIP_FETCH_OBJECT
))
413 oidcpy(&it
->oid
, &oid
);
417 hash_object_file(the_hash_algo
, buffer
.buf
, buffer
.len
,
418 tree_type
, &it
->oid
);
419 } else if (write_object_file(buffer
.buf
, buffer
.len
, tree_type
,
421 strbuf_release(&buffer
);
425 strbuf_release(&buffer
);
426 it
->entry_count
= to_invalidate
? -1 : i
- *skip_count
;
428 fprintf(stderr
, "cache-tree update-one (%d ent, %d subtree) %s\n",
429 it
->entry_count
, it
->subtree_nr
,
430 oid_to_hex(&it
->oid
));
435 int cache_tree_update(struct index_state
*istate
, int flags
)
437 struct cache_tree
*it
= istate
->cache_tree
;
438 struct cache_entry
**cache
= istate
->cache
;
439 int entries
= istate
->cache_nr
;
440 int skip
, i
= verify_cache(cache
, entries
, flags
);
444 trace_performance_enter();
445 i
= update_one(it
, cache
, entries
, "", 0, &skip
, flags
);
446 trace_performance_leave("cache_tree_update");
449 istate
->cache_changed
|= CACHE_TREE_CHANGED
;
453 static void write_one(struct strbuf
*buffer
, struct cache_tree
*it
,
454 const char *path
, int pathlen
)
458 /* One "cache-tree" entry consists of the following:
459 * path (NUL terminated)
460 * entry_count, subtree_nr ("%d %d\n")
461 * tree-sha1 (missing if invalid)
462 * subtree_nr "cache-tree" entries for subtrees.
464 strbuf_grow(buffer
, pathlen
+ 100);
465 strbuf_add(buffer
, path
, pathlen
);
466 strbuf_addf(buffer
, "%c%d %d\n", 0, it
->entry_count
, it
->subtree_nr
);
469 if (0 <= it
->entry_count
)
470 fprintf(stderr
, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
471 pathlen
, path
, it
->entry_count
, it
->subtree_nr
,
472 oid_to_hex(&it
->oid
));
474 fprintf(stderr
, "cache-tree <%.*s> (%d subtree) invalid\n",
475 pathlen
, path
, it
->subtree_nr
);
478 if (0 <= it
->entry_count
) {
479 strbuf_add(buffer
, it
->oid
.hash
, the_hash_algo
->rawsz
);
481 for (i
= 0; i
< it
->subtree_nr
; i
++) {
482 struct cache_tree_sub
*down
= it
->down
[i
];
484 struct cache_tree_sub
*prev
= it
->down
[i
-1];
485 if (subtree_name_cmp(down
->name
, down
->namelen
,
486 prev
->name
, prev
->namelen
) <= 0)
487 die("fatal - unsorted cache subtree");
489 write_one(buffer
, down
->cache_tree
, down
->name
, down
->namelen
);
493 void cache_tree_write(struct strbuf
*sb
, struct cache_tree
*root
)
495 write_one(sb
, root
, "", 0);
498 static struct cache_tree
*read_one(const char **buffer
, unsigned long *size_p
)
500 const char *buf
= *buffer
;
501 unsigned long size
= *size_p
;
504 struct cache_tree
*it
;
506 const unsigned rawsz
= the_hash_algo
->rawsz
;
509 /* skip name, but make sure name exists */
510 while (size
&& *buf
) {
520 it
->entry_count
= strtol(cp
, &ep
, 10);
524 subtree_nr
= strtol(cp
, &ep
, 10);
527 while (size
&& *buf
&& *buf
!= '\n') {
534 if (0 <= it
->entry_count
) {
537 oidread(&it
->oid
, (const unsigned char *)buf
);
543 if (0 <= it
->entry_count
)
544 fprintf(stderr
, "cache-tree <%s> (%d ent, %d subtree) %s\n",
545 *buffer
, it
->entry_count
, subtree_nr
,
546 oid_to_hex(&it
->oid
));
548 fprintf(stderr
, "cache-tree <%s> (%d subtrees) invalid\n",
549 *buffer
, subtree_nr
);
553 * Just a heuristic -- we do not add directories that often but
554 * we do not want to have to extend it immediately when we do,
557 it
->subtree_alloc
= subtree_nr
+ 2;
558 it
->down
= xcalloc(it
->subtree_alloc
, sizeof(struct cache_tree_sub
*));
559 for (i
= 0; i
< subtree_nr
; i
++) {
560 /* read each subtree */
561 struct cache_tree
*sub
;
562 struct cache_tree_sub
*subtree
;
563 const char *name
= buf
;
565 sub
= read_one(&buf
, &size
);
568 subtree
= cache_tree_sub(it
, name
);
569 subtree
->cache_tree
= sub
;
571 if (subtree_nr
!= it
->subtree_nr
)
572 die("cache-tree: internal error");
578 cache_tree_free(&it
);
582 struct cache_tree
*cache_tree_read(const char *buffer
, unsigned long size
)
585 return NULL
; /* not the whole tree */
586 return read_one(&buffer
, &size
);
589 static struct cache_tree
*cache_tree_find(struct cache_tree
*it
, const char *path
)
595 struct cache_tree_sub
*sub
;
597 slash
= strchrnul(path
, '/');
599 * Between path and slash is the name of the subtree
602 sub
= find_subtree(it
, path
, slash
- path
, 0);
605 it
= sub
->cache_tree
;
614 static int write_index_as_tree_internal(struct object_id
*oid
,
615 struct index_state
*index_state
,
616 int cache_tree_valid
,
620 if (flags
& WRITE_TREE_IGNORE_CACHE_TREE
) {
621 cache_tree_free(&index_state
->cache_tree
);
622 cache_tree_valid
= 0;
625 if (!index_state
->cache_tree
)
626 index_state
->cache_tree
= cache_tree();
628 if (!cache_tree_valid
&& cache_tree_update(index_state
, flags
) < 0)
629 return WRITE_TREE_UNMERGED_INDEX
;
632 struct cache_tree
*subtree
;
633 subtree
= cache_tree_find(index_state
->cache_tree
, prefix
);
635 return WRITE_TREE_PREFIX_ERROR
;
636 oidcpy(oid
, &subtree
->oid
);
639 oidcpy(oid
, &index_state
->cache_tree
->oid
);
644 struct tree
* write_in_core_index_as_tree(struct repository
*repo
) {
648 struct index_state
*index_state
= repo
->index
;
649 was_valid
= index_state
->cache_tree
&&
650 cache_tree_fully_valid(index_state
->cache_tree
);
652 ret
= write_index_as_tree_internal(&o
, index_state
, was_valid
, 0, NULL
);
653 if (ret
== WRITE_TREE_UNMERGED_INDEX
) {
655 fprintf(stderr
, "BUG: There are unmerged index entries:\n");
656 for (i
= 0; i
< index_state
->cache_nr
; i
++) {
657 const struct cache_entry
*ce
= index_state
->cache
[i
];
659 fprintf(stderr
, "BUG: %d %.*s\n", ce_stage(ce
),
660 (int)ce_namelen(ce
), ce
->name
);
662 BUG("unmerged index entries when writing inmemory index");
665 return lookup_tree(repo
, &index_state
->cache_tree
->oid
);
669 int write_index_as_tree(struct object_id
*oid
, struct index_state
*index_state
, const char *index_path
, int flags
, const char *prefix
)
671 int entries
, was_valid
;
672 struct lock_file lock_file
= LOCK_INIT
;
675 hold_lock_file_for_update(&lock_file
, index_path
, LOCK_DIE_ON_ERROR
);
677 entries
= read_index_from(index_state
, index_path
, get_git_dir());
679 ret
= WRITE_TREE_UNREADABLE_INDEX
;
683 was_valid
= !(flags
& WRITE_TREE_IGNORE_CACHE_TREE
) &&
684 index_state
->cache_tree
&&
685 cache_tree_fully_valid(index_state
->cache_tree
);
687 ret
= write_index_as_tree_internal(oid
, index_state
, was_valid
, flags
,
689 if (!ret
&& !was_valid
) {
690 write_locked_index(index_state
, &lock_file
, COMMIT_LOCK
);
691 /* Not being able to write is fine -- we are only interested
692 * in updating the cache-tree part, and if the next caller
693 * ends up using the old index with unupdated cache-tree part
694 * it misses the work we did here, but that is just a
695 * performance penalty and not a big deal.
700 rollback_lock_file(&lock_file
);
704 static void prime_cache_tree_rec(struct repository
*r
,
705 struct cache_tree
*it
,
708 struct tree_desc desc
;
709 struct name_entry entry
;
712 oidcpy(&it
->oid
, &tree
->object
.oid
);
713 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
715 while (tree_entry(&desc
, &entry
)) {
716 if (!S_ISDIR(entry
.mode
))
719 struct cache_tree_sub
*sub
;
720 struct tree
*subtree
= lookup_tree(r
, &entry
.oid
);
721 if (!subtree
->object
.parsed
)
723 sub
= cache_tree_sub(it
, entry
.path
);
724 sub
->cache_tree
= cache_tree();
725 prime_cache_tree_rec(r
, sub
->cache_tree
, subtree
);
726 cnt
+= sub
->cache_tree
->entry_count
;
729 it
->entry_count
= cnt
;
732 void prime_cache_tree(struct repository
*r
,
733 struct index_state
*istate
,
736 cache_tree_free(&istate
->cache_tree
);
737 istate
->cache_tree
= cache_tree();
738 prime_cache_tree_rec(r
, istate
->cache_tree
, tree
);
739 istate
->cache_changed
|= CACHE_TREE_CHANGED
;
743 * find the cache_tree that corresponds to the current level without
744 * exploding the full path into textual form. The root of the
745 * cache tree is given as "root", and our current level is "info".
746 * (1) When at root level, info->prev is NULL, so it is "root" itself.
747 * (2) Otherwise, find the cache_tree that corresponds to one level
748 * above us, and find ourselves in there.
750 static struct cache_tree
*find_cache_tree_from_traversal(struct cache_tree
*root
,
751 struct traverse_info
*info
)
753 struct cache_tree
*our_parent
;
757 our_parent
= find_cache_tree_from_traversal(root
, info
->prev
);
758 return cache_tree_find(our_parent
, info
->name
);
761 int cache_tree_matches_traversal(struct cache_tree
*root
,
762 struct name_entry
*ent
,
763 struct traverse_info
*info
)
765 struct cache_tree
*it
;
767 it
= find_cache_tree_from_traversal(root
, info
);
768 it
= cache_tree_find(it
, ent
->path
);
769 if (it
&& it
->entry_count
> 0 && oideq(&ent
->oid
, &it
->oid
))
770 return it
->entry_count
;
774 static void verify_one(struct repository
*r
,
775 struct index_state
*istate
,
776 struct cache_tree
*it
,
779 int i
, pos
, len
= path
->len
;
780 struct strbuf tree_buf
= STRBUF_INIT
;
781 struct object_id new_oid
;
783 for (i
= 0; i
< it
->subtree_nr
; i
++) {
784 strbuf_addf(path
, "%s/", it
->down
[i
]->name
);
785 verify_one(r
, istate
, it
->down
[i
]->cache_tree
, path
);
786 strbuf_setlen(path
, len
);
789 if (it
->entry_count
< 0 ||
790 /* no verification on tests (t7003) that replace trees */
791 lookup_replace_object(r
, &it
->oid
) != &it
->oid
)
795 pos
= index_name_pos(istate
, path
->buf
, path
->len
);
802 while (i
< it
->entry_count
) {
803 struct cache_entry
*ce
= istate
->cache
[pos
+ i
];
805 struct cache_tree_sub
*sub
= NULL
;
806 const struct object_id
*oid
;
811 if (ce
->ce_flags
& (CE_STAGEMASK
| CE_INTENT_TO_ADD
| CE_REMOVE
))
812 BUG("%s with flags 0x%x should not be in cache-tree",
813 ce
->name
, ce
->ce_flags
);
814 name
= ce
->name
+ path
->len
;
815 slash
= strchr(name
, '/');
817 entlen
= slash
- name
;
818 sub
= find_subtree(it
, ce
->name
+ path
->len
, entlen
, 0);
819 if (!sub
|| sub
->cache_tree
->entry_count
< 0)
820 BUG("bad subtree '%.*s'", entlen
, name
);
821 oid
= &sub
->cache_tree
->oid
;
823 i
+= sub
->cache_tree
->entry_count
;
827 entlen
= ce_namelen(ce
) - path
->len
;
830 strbuf_addf(&tree_buf
, "%o %.*s%c", mode
, entlen
, name
, '\0');
831 strbuf_add(&tree_buf
, oid
->hash
, r
->hash_algo
->rawsz
);
833 hash_object_file(r
->hash_algo
, tree_buf
.buf
, tree_buf
.len
, tree_type
,
835 if (!oideq(&new_oid
, &it
->oid
))
836 BUG("cache-tree for path %.*s does not match. "
837 "Expected %s got %s", len
, path
->buf
,
838 oid_to_hex(&new_oid
), oid_to_hex(&it
->oid
));
839 strbuf_setlen(path
, len
);
840 strbuf_release(&tree_buf
);
843 void cache_tree_verify(struct repository
*r
, struct index_state
*istate
)
845 struct strbuf path
= STRBUF_INIT
;
847 if (!istate
->cache_tree
)
849 verify_one(r
, istate
, istate
->cache_tree
, &path
);
850 strbuf_release(&path
);