5 #include "cache-tree.h"
6 #include "object-store.h"
7 #include "replace-object.h"
9 #ifndef DEBUG_CACHE_TREE
10 #define DEBUG_CACHE_TREE 0
13 struct cache_tree
*cache_tree(void)
15 struct cache_tree
*it
= xcalloc(1, sizeof(struct cache_tree
));
20 void cache_tree_free(struct cache_tree
**it_p
)
23 struct cache_tree
*it
= *it_p
;
27 for (i
= 0; i
< it
->subtree_nr
; i
++)
29 cache_tree_free(&it
->down
[i
]->cache_tree
);
37 static int subtree_name_cmp(const char *one
, int onelen
,
38 const char *two
, int twolen
)
44 return memcmp(one
, two
, onelen
);
47 static int subtree_pos(struct cache_tree
*it
, const char *path
, int pathlen
)
49 struct cache_tree_sub
**down
= it
->down
;
54 int mi
= lo
+ (hi
- lo
) / 2;
55 struct cache_tree_sub
*mdl
= down
[mi
];
56 int cmp
= subtree_name_cmp(path
, pathlen
,
57 mdl
->name
, mdl
->namelen
);
68 static struct cache_tree_sub
*find_subtree(struct cache_tree
*it
,
73 struct cache_tree_sub
*down
;
74 int pos
= subtree_pos(it
, path
, pathlen
);
81 ALLOC_GROW(it
->down
, it
->subtree_nr
+ 1, it
->subtree_alloc
);
84 FLEX_ALLOC_MEM(down
, name
, path
, pathlen
);
85 down
->cache_tree
= NULL
;
86 down
->namelen
= pathlen
;
88 if (pos
< it
->subtree_nr
)
89 MOVE_ARRAY(it
->down
+ pos
+ 1, it
->down
+ pos
,
90 it
->subtree_nr
- pos
- 1);
95 struct cache_tree_sub
*cache_tree_sub(struct cache_tree
*it
, const char *path
)
97 int pathlen
= strlen(path
);
98 return find_subtree(it
, path
, pathlen
, 1);
101 static int do_invalidate_path(struct cache_tree
*it
, const char *path
)
104 * ==> invalidate self
105 * ==> find "a", have it invalidate "b/c"
107 * ==> invalidate self
108 * ==> if "a" exists as a subtree, remove it.
112 struct cache_tree_sub
*down
;
115 fprintf(stderr
, "cache-tree invalidate <%s>\n", path
);
120 slash
= strchrnul(path
, '/');
121 namelen
= slash
- path
;
122 it
->entry_count
= -1;
125 pos
= subtree_pos(it
, path
, namelen
);
127 cache_tree_free(&it
->down
[pos
]->cache_tree
);
132 * move 4 and 5 up one place (2 entries)
133 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
135 MOVE_ARRAY(it
->down
+ pos
, it
->down
+ pos
+ 1,
136 it
->subtree_nr
- pos
- 1);
141 down
= find_subtree(it
, path
, namelen
, 0);
143 do_invalidate_path(down
->cache_tree
, slash
+ 1);
147 void cache_tree_invalidate_path(struct index_state
*istate
, const char *path
)
149 if (do_invalidate_path(istate
->cache_tree
, path
))
150 istate
->cache_changed
|= CACHE_TREE_CHANGED
;
153 static int verify_cache(struct cache_entry
**cache
,
154 int entries
, int flags
)
157 int silent
= flags
& WRITE_TREE_SILENT
;
159 /* Verify that the tree is merged */
161 for (i
= 0; i
< entries
; i
++) {
162 const struct cache_entry
*ce
= cache
[i
];
167 fprintf(stderr
, "...\n");
170 fprintf(stderr
, "%s: unmerged (%s)\n",
171 ce
->name
, oid_to_hex(&ce
->oid
));
177 /* Also verify that the cache does not have path and path/file
178 * at the same time. At this point we know the cache has only
182 for (i
= 0; i
< entries
- 1; i
++) {
183 /* path/file always comes after path because of the way
184 * the cache is sorted. Also path can appear only once,
185 * which means conflicting one would immediately follow.
187 const char *this_name
= cache
[i
]->name
;
188 const char *next_name
= cache
[i
+1]->name
;
189 int this_len
= strlen(this_name
);
190 if (this_len
< strlen(next_name
) &&
191 strncmp(this_name
, next_name
, this_len
) == 0 &&
192 next_name
[this_len
] == '/') {
194 fprintf(stderr
, "...\n");
197 fprintf(stderr
, "You have both %s and %s\n",
198 this_name
, next_name
);
206 static void discard_unused_subtrees(struct cache_tree
*it
)
208 struct cache_tree_sub
**down
= it
->down
;
209 int nr
= it
->subtree_nr
;
211 for (dst
= src
= 0; src
< nr
; src
++) {
212 struct cache_tree_sub
*s
= down
[src
];
216 cache_tree_free(&s
->cache_tree
);
223 int cache_tree_fully_valid(struct cache_tree
*it
)
228 if (it
->entry_count
< 0 || !has_object_file(&it
->oid
))
230 for (i
= 0; i
< it
->subtree_nr
; i
++) {
231 if (!cache_tree_fully_valid(it
->down
[i
]->cache_tree
))
237 static int update_one(struct cache_tree
*it
,
238 struct cache_entry
**cache
,
245 struct strbuf buffer
;
246 int missing_ok
= flags
& WRITE_TREE_MISSING_OK
;
247 int dryrun
= flags
& WRITE_TREE_DRY_RUN
;
248 int repair
= flags
& WRITE_TREE_REPAIR
;
249 int to_invalidate
= 0;
252 assert(!(dryrun
&& repair
));
256 if (0 <= it
->entry_count
&& has_object_file(&it
->oid
))
257 return it
->entry_count
;
260 * We first scan for subtrees and update them; we start by
261 * marking existing subtrees -- the ones that are unmarked
262 * should not be in the result.
264 for (i
= 0; i
< it
->subtree_nr
; i
++)
265 it
->down
[i
]->used
= 0;
268 * Find the subtrees and update them.
271 while (i
< entries
) {
272 const struct cache_entry
*ce
= cache
[i
];
273 struct cache_tree_sub
*sub
;
274 const char *path
, *slash
;
275 int pathlen
, sublen
, subcnt
, subskip
;
278 pathlen
= ce_namelen(ce
);
279 if (pathlen
<= baselen
|| memcmp(base
, path
, baselen
))
280 break; /* at the end of this level */
282 slash
= strchr(path
+ baselen
, '/');
288 * a/bbb/c (base = a/, slash = /c)
290 * path+baselen = bbb/c, sublen = 3
292 sublen
= slash
- (path
+ baselen
);
293 sub
= find_subtree(it
, path
+ baselen
, sublen
, 1);
294 if (!sub
->cache_tree
)
295 sub
->cache_tree
= cache_tree();
296 subcnt
= update_one(sub
->cache_tree
,
297 cache
+ i
, entries
- i
,
299 baselen
+ sublen
+ 1,
305 die("index cache-tree records empty sub-tree");
307 sub
->count
= subcnt
; /* to be used in the next loop */
308 *skip_count
+= subskip
;
312 discard_unused_subtrees(it
);
315 * Then write out the tree object for this level.
317 strbuf_init(&buffer
, 8192);
320 while (i
< entries
) {
321 const struct cache_entry
*ce
= cache
[i
];
322 struct cache_tree_sub
*sub
= NULL
;
323 const char *path
, *slash
;
325 const struct object_id
*oid
;
327 int expected_missing
= 0;
328 int contains_ita
= 0;
332 pathlen
= ce_namelen(ce
);
333 if (pathlen
<= baselen
|| memcmp(base
, path
, baselen
))
334 break; /* at the end of this level */
336 slash
= strchr(path
+ baselen
, '/');
338 entlen
= slash
- (path
+ baselen
);
339 sub
= find_subtree(it
, path
+ baselen
, entlen
, 0);
341 die("cache-tree.c: '%.*s' in '%s' not found",
342 entlen
, path
+ baselen
, path
);
344 oid
= &sub
->cache_tree
->oid
;
346 contains_ita
= sub
->cache_tree
->entry_count
< 0;
349 expected_missing
= 1;
355 entlen
= pathlen
- baselen
;
359 ce_missing_ok
= mode
== S_IFGITLINK
|| missing_ok
||
360 (repository_format_partial_clone
&&
361 ce_skip_worktree(ce
));
362 if (is_null_oid(oid
) ||
363 (!ce_missing_ok
&& !has_object_file(oid
))) {
364 strbuf_release(&buffer
);
365 if (expected_missing
)
367 return error("invalid object %06o %s for '%.*s'",
368 mode
, oid_to_hex(oid
), entlen
+baselen
, path
);
372 * CE_REMOVE entries are removed before the index is
373 * written to disk. Skip them to remain consistent
374 * with the future on-disk index.
376 if (ce
->ce_flags
& CE_REMOVE
) {
377 *skip_count
= *skip_count
+ 1;
382 * CE_INTENT_TO_ADD entries exist on on-disk index but
383 * they are not part of generated trees. Invalidate up
384 * to root to force cache-tree users to read elsewhere.
386 if (!sub
&& ce_intent_to_add(ce
)) {
392 * "sub" can be an empty tree if all subentries are i-t-a.
394 if (contains_ita
&& is_empty_tree_oid(oid
))
397 strbuf_grow(&buffer
, entlen
+ 100);
398 strbuf_addf(&buffer
, "%o %.*s%c", mode
, entlen
, path
+ baselen
, '\0');
399 strbuf_add(&buffer
, oid
->hash
, the_hash_algo
->rawsz
);
402 fprintf(stderr
, "cache-tree update-one %o %.*s\n",
403 mode
, entlen
, path
+ baselen
);
408 struct object_id oid
;
409 hash_object_file(buffer
.buf
, buffer
.len
, tree_type
, &oid
);
410 if (has_object_file(&oid
))
411 oidcpy(&it
->oid
, &oid
);
415 hash_object_file(buffer
.buf
, buffer
.len
, tree_type
, &it
->oid
);
416 } else if (write_object_file(buffer
.buf
, buffer
.len
, tree_type
,
418 strbuf_release(&buffer
);
422 strbuf_release(&buffer
);
423 it
->entry_count
= to_invalidate
? -1 : i
- *skip_count
;
425 fprintf(stderr
, "cache-tree update-one (%d ent, %d subtree) %s\n",
426 it
->entry_count
, it
->subtree_nr
,
427 oid_to_hex(&it
->oid
));
432 int cache_tree_update(struct index_state
*istate
, int flags
)
434 struct cache_tree
*it
= istate
->cache_tree
;
435 struct cache_entry
**cache
= istate
->cache
;
436 int entries
= istate
->cache_nr
;
437 int skip
, i
= verify_cache(cache
, entries
, flags
);
441 trace_performance_enter();
442 i
= update_one(it
, cache
, entries
, "", 0, &skip
, flags
);
443 trace_performance_leave("cache_tree_update");
446 istate
->cache_changed
|= CACHE_TREE_CHANGED
;
450 static void write_one(struct strbuf
*buffer
, struct cache_tree
*it
,
451 const char *path
, int pathlen
)
455 /* One "cache-tree" entry consists of the following:
456 * path (NUL terminated)
457 * entry_count, subtree_nr ("%d %d\n")
458 * tree-sha1 (missing if invalid)
459 * subtree_nr "cache-tree" entries for subtrees.
461 strbuf_grow(buffer
, pathlen
+ 100);
462 strbuf_add(buffer
, path
, pathlen
);
463 strbuf_addf(buffer
, "%c%d %d\n", 0, it
->entry_count
, it
->subtree_nr
);
466 if (0 <= it
->entry_count
)
467 fprintf(stderr
, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
468 pathlen
, path
, it
->entry_count
, it
->subtree_nr
,
469 oid_to_hex(&it
->oid
));
471 fprintf(stderr
, "cache-tree <%.*s> (%d subtree) invalid\n",
472 pathlen
, path
, it
->subtree_nr
);
475 if (0 <= it
->entry_count
) {
476 strbuf_add(buffer
, it
->oid
.hash
, the_hash_algo
->rawsz
);
478 for (i
= 0; i
< it
->subtree_nr
; i
++) {
479 struct cache_tree_sub
*down
= it
->down
[i
];
481 struct cache_tree_sub
*prev
= it
->down
[i
-1];
482 if (subtree_name_cmp(down
->name
, down
->namelen
,
483 prev
->name
, prev
->namelen
) <= 0)
484 die("fatal - unsorted cache subtree");
486 write_one(buffer
, down
->cache_tree
, down
->name
, down
->namelen
);
490 void cache_tree_write(struct strbuf
*sb
, struct cache_tree
*root
)
492 write_one(sb
, root
, "", 0);
495 static struct cache_tree
*read_one(const char **buffer
, unsigned long *size_p
)
497 const char *buf
= *buffer
;
498 unsigned long size
= *size_p
;
501 struct cache_tree
*it
;
503 const unsigned rawsz
= the_hash_algo
->rawsz
;
506 /* skip name, but make sure name exists */
507 while (size
&& *buf
) {
517 it
->entry_count
= strtol(cp
, &ep
, 10);
521 subtree_nr
= strtol(cp
, &ep
, 10);
524 while (size
&& *buf
&& *buf
!= '\n') {
531 if (0 <= it
->entry_count
) {
534 oidread(&it
->oid
, (const unsigned char *)buf
);
540 if (0 <= it
->entry_count
)
541 fprintf(stderr
, "cache-tree <%s> (%d ent, %d subtree) %s\n",
542 *buffer
, it
->entry_count
, subtree_nr
,
543 oid_to_hex(&it
->oid
));
545 fprintf(stderr
, "cache-tree <%s> (%d subtrees) invalid\n",
546 *buffer
, subtree_nr
);
550 * Just a heuristic -- we do not add directories that often but
551 * we do not want to have to extend it immediately when we do,
554 it
->subtree_alloc
= subtree_nr
+ 2;
555 it
->down
= xcalloc(it
->subtree_alloc
, sizeof(struct cache_tree_sub
*));
556 for (i
= 0; i
< subtree_nr
; i
++) {
557 /* read each subtree */
558 struct cache_tree
*sub
;
559 struct cache_tree_sub
*subtree
;
560 const char *name
= buf
;
562 sub
= read_one(&buf
, &size
);
565 subtree
= cache_tree_sub(it
, name
);
566 subtree
->cache_tree
= sub
;
568 if (subtree_nr
!= it
->subtree_nr
)
569 die("cache-tree: internal error");
575 cache_tree_free(&it
);
579 struct cache_tree
*cache_tree_read(const char *buffer
, unsigned long size
)
582 return NULL
; /* not the whole tree */
583 return read_one(&buffer
, &size
);
586 static struct cache_tree
*cache_tree_find(struct cache_tree
*it
, const char *path
)
592 struct cache_tree_sub
*sub
;
594 slash
= strchrnul(path
, '/');
596 * Between path and slash is the name of the subtree
599 sub
= find_subtree(it
, path
, slash
- path
, 0);
602 it
= sub
->cache_tree
;
611 int write_index_as_tree(struct object_id
*oid
, struct index_state
*index_state
, const char *index_path
, int flags
, const char *prefix
)
613 int entries
, was_valid
;
614 struct lock_file lock_file
= LOCK_INIT
;
617 hold_lock_file_for_update(&lock_file
, index_path
, LOCK_DIE_ON_ERROR
);
619 entries
= read_index_from(index_state
, index_path
, get_git_dir());
621 ret
= WRITE_TREE_UNREADABLE_INDEX
;
624 if (flags
& WRITE_TREE_IGNORE_CACHE_TREE
)
625 cache_tree_free(&index_state
->cache_tree
);
627 if (!index_state
->cache_tree
)
628 index_state
->cache_tree
= cache_tree();
630 was_valid
= cache_tree_fully_valid(index_state
->cache_tree
);
632 if (cache_tree_update(index_state
, flags
) < 0) {
633 ret
= WRITE_TREE_UNMERGED_INDEX
;
636 write_locked_index(index_state
, &lock_file
, COMMIT_LOCK
);
637 /* Not being able to write is fine -- we are only interested
638 * in updating the cache-tree part, and if the next caller
639 * ends up using the old index with unupdated cache-tree part
640 * it misses the work we did here, but that is just a
641 * performance penalty and not a big deal.
646 struct cache_tree
*subtree
;
647 subtree
= cache_tree_find(index_state
->cache_tree
, prefix
);
649 ret
= WRITE_TREE_PREFIX_ERROR
;
652 oidcpy(oid
, &subtree
->oid
);
655 oidcpy(oid
, &index_state
->cache_tree
->oid
);
658 rollback_lock_file(&lock_file
);
662 static void prime_cache_tree_rec(struct repository
*r
,
663 struct cache_tree
*it
,
666 struct tree_desc desc
;
667 struct name_entry entry
;
670 oidcpy(&it
->oid
, &tree
->object
.oid
);
671 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
673 while (tree_entry(&desc
, &entry
)) {
674 if (!S_ISDIR(entry
.mode
))
677 struct cache_tree_sub
*sub
;
678 struct tree
*subtree
= lookup_tree(r
, &entry
.oid
);
679 if (!subtree
->object
.parsed
)
681 sub
= cache_tree_sub(it
, entry
.path
);
682 sub
->cache_tree
= cache_tree();
683 prime_cache_tree_rec(r
, sub
->cache_tree
, subtree
);
684 cnt
+= sub
->cache_tree
->entry_count
;
687 it
->entry_count
= cnt
;
690 void prime_cache_tree(struct repository
*r
,
691 struct index_state
*istate
,
694 cache_tree_free(&istate
->cache_tree
);
695 istate
->cache_tree
= cache_tree();
696 prime_cache_tree_rec(r
, istate
->cache_tree
, tree
);
697 istate
->cache_changed
|= CACHE_TREE_CHANGED
;
701 * find the cache_tree that corresponds to the current level without
702 * exploding the full path into textual form. The root of the
703 * cache tree is given as "root", and our current level is "info".
704 * (1) When at root level, info->prev is NULL, so it is "root" itself.
705 * (2) Otherwise, find the cache_tree that corresponds to one level
706 * above us, and find ourselves in there.
708 static struct cache_tree
*find_cache_tree_from_traversal(struct cache_tree
*root
,
709 struct traverse_info
*info
)
711 struct cache_tree
*our_parent
;
715 our_parent
= find_cache_tree_from_traversal(root
, info
->prev
);
716 return cache_tree_find(our_parent
, info
->name
.path
);
719 int cache_tree_matches_traversal(struct cache_tree
*root
,
720 struct name_entry
*ent
,
721 struct traverse_info
*info
)
723 struct cache_tree
*it
;
725 it
= find_cache_tree_from_traversal(root
, info
);
726 it
= cache_tree_find(it
, ent
->path
);
727 if (it
&& it
->entry_count
> 0 && oideq(&ent
->oid
, &it
->oid
))
728 return it
->entry_count
;
732 static void verify_one(struct repository
*r
,
733 struct index_state
*istate
,
734 struct cache_tree
*it
,
737 int i
, pos
, len
= path
->len
;
738 struct strbuf tree_buf
= STRBUF_INIT
;
739 struct object_id new_oid
;
741 for (i
= 0; i
< it
->subtree_nr
; i
++) {
742 strbuf_addf(path
, "%s/", it
->down
[i
]->name
);
743 verify_one(r
, istate
, it
->down
[i
]->cache_tree
, path
);
744 strbuf_setlen(path
, len
);
747 if (it
->entry_count
< 0 ||
748 /* no verification on tests (t7003) that replace trees */
749 lookup_replace_object(r
, &it
->oid
) != &it
->oid
)
753 pos
= index_name_pos(istate
, path
->buf
, path
->len
);
760 while (i
< it
->entry_count
) {
761 struct cache_entry
*ce
= istate
->cache
[pos
+ i
];
763 struct cache_tree_sub
*sub
= NULL
;
764 const struct object_id
*oid
;
769 if (ce
->ce_flags
& (CE_STAGEMASK
| CE_INTENT_TO_ADD
| CE_REMOVE
))
770 BUG("%s with flags 0x%x should not be in cache-tree",
771 ce
->name
, ce
->ce_flags
);
772 name
= ce
->name
+ path
->len
;
773 slash
= strchr(name
, '/');
775 entlen
= slash
- name
;
776 sub
= find_subtree(it
, ce
->name
+ path
->len
, entlen
, 0);
777 if (!sub
|| sub
->cache_tree
->entry_count
< 0)
778 BUG("bad subtree '%.*s'", entlen
, name
);
779 oid
= &sub
->cache_tree
->oid
;
781 i
+= sub
->cache_tree
->entry_count
;
785 entlen
= ce_namelen(ce
) - path
->len
;
788 strbuf_addf(&tree_buf
, "%o %.*s%c", mode
, entlen
, name
, '\0');
789 strbuf_add(&tree_buf
, oid
->hash
, the_hash_algo
->rawsz
);
791 hash_object_file(tree_buf
.buf
, tree_buf
.len
, tree_type
, &new_oid
);
792 if (!oideq(&new_oid
, &it
->oid
))
793 BUG("cache-tree for path %.*s does not match. "
794 "Expected %s got %s", len
, path
->buf
,
795 oid_to_hex(&new_oid
), oid_to_hex(&it
->oid
));
796 strbuf_setlen(path
, len
);
797 strbuf_release(&tree_buf
);
800 void cache_tree_verify(struct repository
*r
, struct index_state
*istate
)
802 struct strbuf path
= STRBUF_INIT
;
804 if (!istate
->cache_tree
)
806 verify_one(r
, istate
, istate
->cache_tree
, &path
);
807 strbuf_release(&path
);