5 #include "cache-tree.h"
11 struct cache_tree
*cache_tree(void)
13 struct cache_tree
*it
= xcalloc(1, sizeof(struct cache_tree
));
18 void cache_tree_free(struct cache_tree
**it_p
)
21 struct cache_tree
*it
= *it_p
;
25 for (i
= 0; i
< it
->subtree_nr
; i
++)
27 cache_tree_free(&it
->down
[i
]->cache_tree
);
35 static int subtree_name_cmp(const char *one
, int onelen
,
36 const char *two
, int twolen
)
42 return memcmp(one
, two
, onelen
);
45 static int subtree_pos(struct cache_tree
*it
, const char *path
, int pathlen
)
47 struct cache_tree_sub
**down
= it
->down
;
52 int mi
= lo
+ (hi
- lo
) / 2;
53 struct cache_tree_sub
*mdl
= down
[mi
];
54 int cmp
= subtree_name_cmp(path
, pathlen
,
55 mdl
->name
, mdl
->namelen
);
66 static struct cache_tree_sub
*find_subtree(struct cache_tree
*it
,
71 struct cache_tree_sub
*down
;
72 int pos
= subtree_pos(it
, path
, pathlen
);
79 ALLOC_GROW(it
->down
, it
->subtree_nr
+ 1, it
->subtree_alloc
);
82 FLEX_ALLOC_MEM(down
, name
, path
, pathlen
);
83 down
->cache_tree
= NULL
;
84 down
->namelen
= pathlen
;
86 if (pos
< it
->subtree_nr
)
87 MOVE_ARRAY(it
->down
+ pos
+ 1, it
->down
+ pos
,
88 it
->subtree_nr
- pos
- 1);
93 struct cache_tree_sub
*cache_tree_sub(struct cache_tree
*it
, const char *path
)
95 int pathlen
= strlen(path
);
96 return find_subtree(it
, path
, pathlen
, 1);
99 static int do_invalidate_path(struct cache_tree
*it
, const char *path
)
102 * ==> invalidate self
103 * ==> find "a", have it invalidate "b/c"
105 * ==> invalidate self
106 * ==> if "a" exists as a subtree, remove it.
110 struct cache_tree_sub
*down
;
113 fprintf(stderr
, "cache-tree invalidate <%s>\n", path
);
118 slash
= strchrnul(path
, '/');
119 namelen
= slash
- path
;
120 it
->entry_count
= -1;
123 pos
= subtree_pos(it
, path
, namelen
);
125 cache_tree_free(&it
->down
[pos
]->cache_tree
);
130 * move 4 and 5 up one place (2 entries)
131 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
133 MOVE_ARRAY(it
->down
+ pos
, it
->down
+ pos
+ 1,
134 it
->subtree_nr
- pos
- 1);
139 down
= find_subtree(it
, path
, namelen
, 0);
141 do_invalidate_path(down
->cache_tree
, slash
+ 1);
145 void cache_tree_invalidate_path(struct index_state
*istate
, const char *path
)
147 if (do_invalidate_path(istate
->cache_tree
, path
))
148 istate
->cache_changed
|= CACHE_TREE_CHANGED
;
151 static int verify_cache(struct cache_entry
**cache
,
152 int entries
, int flags
)
155 int silent
= flags
& WRITE_TREE_SILENT
;
157 /* Verify that the tree is merged */
159 for (i
= 0; i
< entries
; i
++) {
160 const struct cache_entry
*ce
= cache
[i
];
165 fprintf(stderr
, "...\n");
168 fprintf(stderr
, "%s: unmerged (%s)\n",
169 ce
->name
, oid_to_hex(&ce
->oid
));
175 /* Also verify that the cache does not have path and path/file
176 * at the same time. At this point we know the cache has only
180 for (i
= 0; i
< entries
- 1; i
++) {
181 /* path/file always comes after path because of the way
182 * the cache is sorted. Also path can appear only once,
183 * which means conflicting one would immediately follow.
185 const char *this_name
= cache
[i
]->name
;
186 const char *next_name
= cache
[i
+1]->name
;
187 int this_len
= strlen(this_name
);
188 if (this_len
< strlen(next_name
) &&
189 strncmp(this_name
, next_name
, this_len
) == 0 &&
190 next_name
[this_len
] == '/') {
192 fprintf(stderr
, "...\n");
195 fprintf(stderr
, "You have both %s and %s\n",
196 this_name
, next_name
);
204 static void discard_unused_subtrees(struct cache_tree
*it
)
206 struct cache_tree_sub
**down
= it
->down
;
207 int nr
= it
->subtree_nr
;
209 for (dst
= src
= 0; src
< nr
; src
++) {
210 struct cache_tree_sub
*s
= down
[src
];
214 cache_tree_free(&s
->cache_tree
);
221 int cache_tree_fully_valid(struct cache_tree
*it
)
226 if (it
->entry_count
< 0 || !has_sha1_file(it
->oid
.hash
))
228 for (i
= 0; i
< it
->subtree_nr
; i
++) {
229 if (!cache_tree_fully_valid(it
->down
[i
]->cache_tree
))
235 static int update_one(struct cache_tree
*it
,
236 struct cache_entry
**cache
,
243 struct strbuf buffer
;
244 int missing_ok
= flags
& WRITE_TREE_MISSING_OK
;
245 int dryrun
= flags
& WRITE_TREE_DRY_RUN
;
246 int repair
= flags
& WRITE_TREE_REPAIR
;
247 int to_invalidate
= 0;
250 assert(!(dryrun
&& repair
));
254 if (0 <= it
->entry_count
&& has_sha1_file(it
->oid
.hash
))
255 return it
->entry_count
;
258 * We first scan for subtrees and update them; we start by
259 * marking existing subtrees -- the ones that are unmarked
260 * should not be in the result.
262 for (i
= 0; i
< it
->subtree_nr
; i
++)
263 it
->down
[i
]->used
= 0;
266 * Find the subtrees and update them.
269 while (i
< entries
) {
270 const struct cache_entry
*ce
= cache
[i
];
271 struct cache_tree_sub
*sub
;
272 const char *path
, *slash
;
273 int pathlen
, sublen
, subcnt
, subskip
;
276 pathlen
= ce_namelen(ce
);
277 if (pathlen
<= baselen
|| memcmp(base
, path
, baselen
))
278 break; /* at the end of this level */
280 slash
= strchr(path
+ baselen
, '/');
286 * a/bbb/c (base = a/, slash = /c)
288 * path+baselen = bbb/c, sublen = 3
290 sublen
= slash
- (path
+ baselen
);
291 sub
= find_subtree(it
, path
+ baselen
, sublen
, 1);
292 if (!sub
->cache_tree
)
293 sub
->cache_tree
= cache_tree();
294 subcnt
= update_one(sub
->cache_tree
,
295 cache
+ i
, entries
- i
,
297 baselen
+ sublen
+ 1,
303 die("index cache-tree records empty sub-tree");
305 sub
->count
= subcnt
; /* to be used in the next loop */
306 *skip_count
+= subskip
;
310 discard_unused_subtrees(it
);
313 * Then write out the tree object for this level.
315 strbuf_init(&buffer
, 8192);
318 while (i
< entries
) {
319 const struct cache_entry
*ce
= cache
[i
];
320 struct cache_tree_sub
*sub
= NULL
;
321 const char *path
, *slash
;
323 const unsigned char *sha1
;
325 int expected_missing
= 0;
326 int contains_ita
= 0;
329 pathlen
= ce_namelen(ce
);
330 if (pathlen
<= baselen
|| memcmp(base
, path
, baselen
))
331 break; /* at the end of this level */
333 slash
= strchr(path
+ baselen
, '/');
335 entlen
= slash
- (path
+ baselen
);
336 sub
= find_subtree(it
, path
+ baselen
, entlen
, 0);
338 die("cache-tree.c: '%.*s' in '%s' not found",
339 entlen
, path
+ baselen
, path
);
341 sha1
= sub
->cache_tree
->oid
.hash
;
343 contains_ita
= sub
->cache_tree
->entry_count
< 0;
346 expected_missing
= 1;
352 entlen
= pathlen
- baselen
;
356 if (is_null_sha1(sha1
) ||
357 (mode
!= S_IFGITLINK
&& !missing_ok
&& !has_sha1_file(sha1
))) {
358 strbuf_release(&buffer
);
359 if (expected_missing
)
361 return error("invalid object %06o %s for '%.*s'",
362 mode
, sha1_to_hex(sha1
), entlen
+baselen
, path
);
366 * CE_REMOVE entries are removed before the index is
367 * written to disk. Skip them to remain consistent
368 * with the future on-disk index.
370 if (ce
->ce_flags
& CE_REMOVE
) {
371 *skip_count
= *skip_count
+ 1;
376 * CE_INTENT_TO_ADD entries exist on on-disk index but
377 * they are not part of generated trees. Invalidate up
378 * to root to force cache-tree users to read elsewhere.
380 if (!sub
&& ce_intent_to_add(ce
)) {
386 * "sub" can be an empty tree if all subentries are i-t-a.
388 if (contains_ita
&& !hashcmp(sha1
, EMPTY_TREE_SHA1_BIN
))
391 strbuf_grow(&buffer
, entlen
+ 100);
392 strbuf_addf(&buffer
, "%o %.*s%c", mode
, entlen
, path
+ baselen
, '\0');
393 strbuf_add(&buffer
, sha1
, 20);
396 fprintf(stderr
, "cache-tree update-one %o %.*s\n",
397 mode
, entlen
, path
+ baselen
);
402 unsigned char sha1
[20];
403 hash_sha1_file(buffer
.buf
, buffer
.len
, tree_type
, sha1
);
404 if (has_sha1_file(sha1
))
405 hashcpy(it
->oid
.hash
, sha1
);
409 hash_sha1_file(buffer
.buf
, buffer
.len
, tree_type
,
411 else if (write_sha1_file(buffer
.buf
, buffer
.len
, tree_type
, it
->oid
.hash
)) {
412 strbuf_release(&buffer
);
416 strbuf_release(&buffer
);
417 it
->entry_count
= to_invalidate
? -1 : i
- *skip_count
;
419 fprintf(stderr
, "cache-tree update-one (%d ent, %d subtree) %s\n",
420 it
->entry_count
, it
->subtree_nr
,
421 oid_to_hex(&it
->oid
));
426 int cache_tree_update(struct index_state
*istate
, int flags
)
428 struct cache_tree
*it
= istate
->cache_tree
;
429 struct cache_entry
**cache
= istate
->cache
;
430 int entries
= istate
->cache_nr
;
431 int skip
, i
= verify_cache(cache
, entries
, flags
);
435 i
= update_one(it
, cache
, entries
, "", 0, &skip
, flags
);
438 istate
->cache_changed
|= CACHE_TREE_CHANGED
;
442 static void write_one(struct strbuf
*buffer
, struct cache_tree
*it
,
443 const char *path
, int pathlen
)
447 /* One "cache-tree" entry consists of the following:
448 * path (NUL terminated)
449 * entry_count, subtree_nr ("%d %d\n")
450 * tree-sha1 (missing if invalid)
451 * subtree_nr "cache-tree" entries for subtrees.
453 strbuf_grow(buffer
, pathlen
+ 100);
454 strbuf_add(buffer
, path
, pathlen
);
455 strbuf_addf(buffer
, "%c%d %d\n", 0, it
->entry_count
, it
->subtree_nr
);
458 if (0 <= it
->entry_count
)
459 fprintf(stderr
, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
460 pathlen
, path
, it
->entry_count
, it
->subtree_nr
,
461 oid_to_hex(&it
->oid
));
463 fprintf(stderr
, "cache-tree <%.*s> (%d subtree) invalid\n",
464 pathlen
, path
, it
->subtree_nr
);
467 if (0 <= it
->entry_count
) {
468 strbuf_add(buffer
, it
->oid
.hash
, 20);
470 for (i
= 0; i
< it
->subtree_nr
; i
++) {
471 struct cache_tree_sub
*down
= it
->down
[i
];
473 struct cache_tree_sub
*prev
= it
->down
[i
-1];
474 if (subtree_name_cmp(down
->name
, down
->namelen
,
475 prev
->name
, prev
->namelen
) <= 0)
476 die("fatal - unsorted cache subtree");
478 write_one(buffer
, down
->cache_tree
, down
->name
, down
->namelen
);
482 void cache_tree_write(struct strbuf
*sb
, struct cache_tree
*root
)
484 write_one(sb
, root
, "", 0);
487 static struct cache_tree
*read_one(const char **buffer
, unsigned long *size_p
)
489 const char *buf
= *buffer
;
490 unsigned long size
= *size_p
;
493 struct cache_tree
*it
;
497 /* skip name, but make sure name exists */
498 while (size
&& *buf
) {
508 it
->entry_count
= strtol(cp
, &ep
, 10);
512 subtree_nr
= strtol(cp
, &ep
, 10);
515 while (size
&& *buf
&& *buf
!= '\n') {
522 if (0 <= it
->entry_count
) {
525 hashcpy(it
->oid
.hash
, (const unsigned char*)buf
);
531 if (0 <= it
->entry_count
)
532 fprintf(stderr
, "cache-tree <%s> (%d ent, %d subtree) %s\n",
533 *buffer
, it
->entry_count
, subtree_nr
,
534 oid_to_hex(&it
->oid
));
536 fprintf(stderr
, "cache-tree <%s> (%d subtrees) invalid\n",
537 *buffer
, subtree_nr
);
541 * Just a heuristic -- we do not add directories that often but
542 * we do not want to have to extend it immediately when we do,
545 it
->subtree_alloc
= subtree_nr
+ 2;
546 it
->down
= xcalloc(it
->subtree_alloc
, sizeof(struct cache_tree_sub
*));
547 for (i
= 0; i
< subtree_nr
; i
++) {
548 /* read each subtree */
549 struct cache_tree
*sub
;
550 struct cache_tree_sub
*subtree
;
551 const char *name
= buf
;
553 sub
= read_one(&buf
, &size
);
556 subtree
= cache_tree_sub(it
, name
);
557 subtree
->cache_tree
= sub
;
559 if (subtree_nr
!= it
->subtree_nr
)
560 die("cache-tree: internal error");
566 cache_tree_free(&it
);
570 struct cache_tree
*cache_tree_read(const char *buffer
, unsigned long size
)
573 return NULL
; /* not the whole tree */
574 return read_one(&buffer
, &size
);
577 static struct cache_tree
*cache_tree_find(struct cache_tree
*it
, const char *path
)
583 struct cache_tree_sub
*sub
;
585 slash
= strchrnul(path
, '/');
587 * Between path and slash is the name of the subtree
590 sub
= find_subtree(it
, path
, slash
- path
, 0);
593 it
= sub
->cache_tree
;
602 int write_index_as_tree(unsigned char *sha1
, struct index_state
*index_state
, const char *index_path
, int flags
, const char *prefix
)
604 int entries
, was_valid
;
605 struct lock_file lock_file
= LOCK_INIT
;
608 hold_lock_file_for_update(&lock_file
, index_path
, LOCK_DIE_ON_ERROR
);
610 entries
= read_index_from(index_state
, index_path
, get_git_dir());
612 ret
= WRITE_TREE_UNREADABLE_INDEX
;
615 if (flags
& WRITE_TREE_IGNORE_CACHE_TREE
)
616 cache_tree_free(&index_state
->cache_tree
);
618 if (!index_state
->cache_tree
)
619 index_state
->cache_tree
= cache_tree();
621 was_valid
= cache_tree_fully_valid(index_state
->cache_tree
);
623 if (cache_tree_update(index_state
, flags
) < 0) {
624 ret
= WRITE_TREE_UNMERGED_INDEX
;
627 write_locked_index(index_state
, &lock_file
, COMMIT_LOCK
);
628 /* Not being able to write is fine -- we are only interested
629 * in updating the cache-tree part, and if the next caller
630 * ends up using the old index with unupdated cache-tree part
631 * it misses the work we did here, but that is just a
632 * performance penalty and not a big deal.
637 struct cache_tree
*subtree
;
638 subtree
= cache_tree_find(index_state
->cache_tree
, prefix
);
640 ret
= WRITE_TREE_PREFIX_ERROR
;
643 hashcpy(sha1
, subtree
->oid
.hash
);
646 hashcpy(sha1
, index_state
->cache_tree
->oid
.hash
);
649 rollback_lock_file(&lock_file
);
653 int write_cache_as_tree(unsigned char *sha1
, int flags
, const char *prefix
)
655 return write_index_as_tree(sha1
, &the_index
, get_index_file(), flags
, prefix
);
658 static void prime_cache_tree_rec(struct cache_tree
*it
, struct tree
*tree
)
660 struct tree_desc desc
;
661 struct name_entry entry
;
664 oidcpy(&it
->oid
, &tree
->object
.oid
);
665 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
667 while (tree_entry(&desc
, &entry
)) {
668 if (!S_ISDIR(entry
.mode
))
671 struct cache_tree_sub
*sub
;
672 struct tree
*subtree
= lookup_tree(entry
.oid
);
673 if (!subtree
->object
.parsed
)
675 sub
= cache_tree_sub(it
, entry
.path
);
676 sub
->cache_tree
= cache_tree();
677 prime_cache_tree_rec(sub
->cache_tree
, subtree
);
678 cnt
+= sub
->cache_tree
->entry_count
;
681 it
->entry_count
= cnt
;
684 void prime_cache_tree(struct index_state
*istate
, struct tree
*tree
)
686 cache_tree_free(&istate
->cache_tree
);
687 istate
->cache_tree
= cache_tree();
688 prime_cache_tree_rec(istate
->cache_tree
, tree
);
689 istate
->cache_changed
|= CACHE_TREE_CHANGED
;
693 * find the cache_tree that corresponds to the current level without
694 * exploding the full path into textual form. The root of the
695 * cache tree is given as "root", and our current level is "info".
696 * (1) When at root level, info->prev is NULL, so it is "root" itself.
697 * (2) Otherwise, find the cache_tree that corresponds to one level
698 * above us, and find ourselves in there.
700 static struct cache_tree
*find_cache_tree_from_traversal(struct cache_tree
*root
,
701 struct traverse_info
*info
)
703 struct cache_tree
*our_parent
;
707 our_parent
= find_cache_tree_from_traversal(root
, info
->prev
);
708 return cache_tree_find(our_parent
, info
->name
.path
);
711 int cache_tree_matches_traversal(struct cache_tree
*root
,
712 struct name_entry
*ent
,
713 struct traverse_info
*info
)
715 struct cache_tree
*it
;
717 it
= find_cache_tree_from_traversal(root
, info
);
718 it
= cache_tree_find(it
, ent
->path
);
719 if (it
&& it
->entry_count
> 0 && !oidcmp(ent
->oid
, &it
->oid
))
720 return it
->entry_count
;
724 int update_main_cache_tree(int flags
)
726 if (!the_index
.cache_tree
)
727 the_index
.cache_tree
= cache_tree();
728 return cache_tree_update(&the_index
, flags
);