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
) / 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 memmove(it
->down
+ pos
+ 1,
89 sizeof(down
) * (it
->subtree_nr
- pos
- 1));
94 struct cache_tree_sub
*cache_tree_sub(struct cache_tree
*it
, const char *path
)
96 int pathlen
= strlen(path
);
97 return find_subtree(it
, path
, pathlen
, 1);
100 static int do_invalidate_path(struct cache_tree
*it
, const char *path
)
103 * ==> invalidate self
104 * ==> find "a", have it invalidate "b/c"
106 * ==> invalidate self
107 * ==> if "a" exists as a subtree, remove it.
111 struct cache_tree_sub
*down
;
114 fprintf(stderr
, "cache-tree invalidate <%s>\n", path
);
119 slash
= strchrnul(path
, '/');
120 namelen
= slash
- path
;
121 it
->entry_count
= -1;
124 pos
= subtree_pos(it
, path
, namelen
);
126 cache_tree_free(&it
->down
[pos
]->cache_tree
);
131 * move 4 and 5 up one place (2 entries)
132 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
134 memmove(it
->down
+pos
, it
->down
+pos
+1,
135 sizeof(struct cache_tree_sub
*) *
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
, sha1_to_hex(ce
->sha1
));
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_sha1_file(it
->sha1
))
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_sha1_file(it
->sha1
))
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
;
323 const char *path
, *slash
;
325 const unsigned char *sha1
;
327 int expected_missing
= 0;
330 pathlen
= ce_namelen(ce
);
331 if (pathlen
<= baselen
|| memcmp(base
, path
, baselen
))
332 break; /* at the end of this level */
334 slash
= strchr(path
+ baselen
, '/');
336 entlen
= slash
- (path
+ baselen
);
337 sub
= find_subtree(it
, path
+ baselen
, entlen
, 0);
339 die("cache-tree.c: '%.*s' in '%s' not found",
340 entlen
, path
+ baselen
, path
);
342 sha1
= sub
->cache_tree
->sha1
;
344 if (sub
->cache_tree
->entry_count
< 0) {
346 expected_missing
= 1;
352 entlen
= pathlen
- baselen
;
355 if (mode
!= S_IFGITLINK
&& !missing_ok
&& !has_sha1_file(sha1
)) {
356 strbuf_release(&buffer
);
357 if (expected_missing
)
359 return error("invalid object %06o %s for '%.*s'",
360 mode
, sha1_to_hex(sha1
), entlen
+baselen
, path
);
364 * CE_REMOVE entries are removed before the index is
365 * written to disk. Skip them to remain consistent
366 * with the future on-disk index.
368 if (ce
->ce_flags
& CE_REMOVE
) {
369 *skip_count
= *skip_count
+ 1;
374 * CE_INTENT_TO_ADD entries exist on on-disk index but
375 * they are not part of generated trees. Invalidate up
376 * to root to force cache-tree users to read elsewhere.
378 if (ce_intent_to_add(ce
)) {
383 strbuf_grow(&buffer
, entlen
+ 100);
384 strbuf_addf(&buffer
, "%o %.*s%c", mode
, entlen
, path
+ baselen
, '\0');
385 strbuf_add(&buffer
, sha1
, 20);
388 fprintf(stderr
, "cache-tree update-one %o %.*s\n",
389 mode
, entlen
, path
+ baselen
);
394 unsigned char sha1
[20];
395 hash_sha1_file(buffer
.buf
, buffer
.len
, tree_type
, sha1
);
396 if (has_sha1_file(sha1
))
397 hashcpy(it
->sha1
, sha1
);
401 hash_sha1_file(buffer
.buf
, buffer
.len
, tree_type
, it
->sha1
);
402 else if (write_sha1_file(buffer
.buf
, buffer
.len
, tree_type
, it
->sha1
)) {
403 strbuf_release(&buffer
);
407 strbuf_release(&buffer
);
408 it
->entry_count
= to_invalidate
? -1 : i
- *skip_count
;
410 fprintf(stderr
, "cache-tree update-one (%d ent, %d subtree) %s\n",
411 it
->entry_count
, it
->subtree_nr
,
412 sha1_to_hex(it
->sha1
));
417 int cache_tree_update(struct index_state
*istate
, int flags
)
419 struct cache_tree
*it
= istate
->cache_tree
;
420 struct cache_entry
**cache
= istate
->cache
;
421 int entries
= istate
->cache_nr
;
422 int skip
, i
= verify_cache(cache
, entries
, flags
);
426 i
= update_one(it
, cache
, entries
, "", 0, &skip
, flags
);
429 istate
->cache_changed
|= CACHE_TREE_CHANGED
;
433 static void write_one(struct strbuf
*buffer
, struct cache_tree
*it
,
434 const char *path
, int pathlen
)
438 /* One "cache-tree" entry consists of the following:
439 * path (NUL terminated)
440 * entry_count, subtree_nr ("%d %d\n")
441 * tree-sha1 (missing if invalid)
442 * subtree_nr "cache-tree" entries for subtrees.
444 strbuf_grow(buffer
, pathlen
+ 100);
445 strbuf_add(buffer
, path
, pathlen
);
446 strbuf_addf(buffer
, "%c%d %d\n", 0, it
->entry_count
, it
->subtree_nr
);
449 if (0 <= it
->entry_count
)
450 fprintf(stderr
, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
451 pathlen
, path
, it
->entry_count
, it
->subtree_nr
,
452 sha1_to_hex(it
->sha1
));
454 fprintf(stderr
, "cache-tree <%.*s> (%d subtree) invalid\n",
455 pathlen
, path
, it
->subtree_nr
);
458 if (0 <= it
->entry_count
) {
459 strbuf_add(buffer
, it
->sha1
, 20);
461 for (i
= 0; i
< it
->subtree_nr
; i
++) {
462 struct cache_tree_sub
*down
= it
->down
[i
];
464 struct cache_tree_sub
*prev
= it
->down
[i
-1];
465 if (subtree_name_cmp(down
->name
, down
->namelen
,
466 prev
->name
, prev
->namelen
) <= 0)
467 die("fatal - unsorted cache subtree");
469 write_one(buffer
, down
->cache_tree
, down
->name
, down
->namelen
);
473 void cache_tree_write(struct strbuf
*sb
, struct cache_tree
*root
)
475 write_one(sb
, root
, "", 0);
478 static struct cache_tree
*read_one(const char **buffer
, unsigned long *size_p
)
480 const char *buf
= *buffer
;
481 unsigned long size
= *size_p
;
484 struct cache_tree
*it
;
488 /* skip name, but make sure name exists */
489 while (size
&& *buf
) {
499 it
->entry_count
= strtol(cp
, &ep
, 10);
503 subtree_nr
= strtol(cp
, &ep
, 10);
506 while (size
&& *buf
&& *buf
!= '\n') {
513 if (0 <= it
->entry_count
) {
516 hashcpy(it
->sha1
, (const unsigned char*)buf
);
522 if (0 <= it
->entry_count
)
523 fprintf(stderr
, "cache-tree <%s> (%d ent, %d subtree) %s\n",
524 *buffer
, it
->entry_count
, subtree_nr
,
525 sha1_to_hex(it
->sha1
));
527 fprintf(stderr
, "cache-tree <%s> (%d subtrees) invalid\n",
528 *buffer
, subtree_nr
);
532 * Just a heuristic -- we do not add directories that often but
533 * we do not want to have to extend it immediately when we do,
536 it
->subtree_alloc
= subtree_nr
+ 2;
537 it
->down
= xcalloc(it
->subtree_alloc
, sizeof(struct cache_tree_sub
*));
538 for (i
= 0; i
< subtree_nr
; i
++) {
539 /* read each subtree */
540 struct cache_tree
*sub
;
541 struct cache_tree_sub
*subtree
;
542 const char *name
= buf
;
544 sub
= read_one(&buf
, &size
);
547 subtree
= cache_tree_sub(it
, name
);
548 subtree
->cache_tree
= sub
;
550 if (subtree_nr
!= it
->subtree_nr
)
551 die("cache-tree: internal error");
557 cache_tree_free(&it
);
561 struct cache_tree
*cache_tree_read(const char *buffer
, unsigned long size
)
564 return NULL
; /* not the whole tree */
565 return read_one(&buffer
, &size
);
568 static struct cache_tree
*cache_tree_find(struct cache_tree
*it
, const char *path
)
574 struct cache_tree_sub
*sub
;
576 slash
= strchrnul(path
, '/');
578 * Between path and slash is the name of the subtree
581 sub
= find_subtree(it
, path
, slash
- path
, 0);
584 it
= sub
->cache_tree
;
593 int write_index_as_tree(unsigned char *sha1
, struct index_state
*index_state
, const char *index_path
, int flags
, const char *prefix
)
595 int entries
, was_valid
, newfd
;
596 struct lock_file
*lock_file
;
599 * We can't free this memory, it becomes part of a linked list
602 lock_file
= xcalloc(1, sizeof(struct lock_file
));
604 newfd
= hold_lock_file_for_update(lock_file
, index_path
, LOCK_DIE_ON_ERROR
);
606 entries
= read_index_from(index_state
, index_path
);
608 return WRITE_TREE_UNREADABLE_INDEX
;
609 if (flags
& WRITE_TREE_IGNORE_CACHE_TREE
)
610 cache_tree_free(&index_state
->cache_tree
);
612 if (!index_state
->cache_tree
)
613 index_state
->cache_tree
= cache_tree();
615 was_valid
= cache_tree_fully_valid(index_state
->cache_tree
);
617 if (cache_tree_update(index_state
, flags
) < 0)
618 return WRITE_TREE_UNMERGED_INDEX
;
620 if (!write_locked_index(index_state
, lock_file
, COMMIT_LOCK
))
623 /* Not being able to write is fine -- we are only interested
624 * in updating the cache-tree part, and if the next caller
625 * ends up using the old index with unupdated cache-tree part
626 * it misses the work we did here, but that is just a
627 * performance penalty and not a big deal.
632 struct cache_tree
*subtree
;
633 subtree
= cache_tree_find(index_state
->cache_tree
, prefix
);
635 return WRITE_TREE_PREFIX_ERROR
;
636 hashcpy(sha1
, subtree
->sha1
);
639 hashcpy(sha1
, index_state
->cache_tree
->sha1
);
642 rollback_lock_file(lock_file
);
647 int write_cache_as_tree(unsigned char *sha1
, int flags
, const char *prefix
)
649 return write_index_as_tree(sha1
, &the_index
, get_index_file(), flags
, prefix
);
652 static void prime_cache_tree_rec(struct cache_tree
*it
, struct tree
*tree
)
654 struct tree_desc desc
;
655 struct name_entry entry
;
658 hashcpy(it
->sha1
, tree
->object
.oid
.hash
);
659 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
661 while (tree_entry(&desc
, &entry
)) {
662 if (!S_ISDIR(entry
.mode
))
665 struct cache_tree_sub
*sub
;
666 struct tree
*subtree
= lookup_tree(entry
.sha1
);
667 if (!subtree
->object
.parsed
)
669 sub
= cache_tree_sub(it
, entry
.path
);
670 sub
->cache_tree
= cache_tree();
671 prime_cache_tree_rec(sub
->cache_tree
, subtree
);
672 cnt
+= sub
->cache_tree
->entry_count
;
675 it
->entry_count
= cnt
;
678 void prime_cache_tree(struct index_state
*istate
, struct tree
*tree
)
680 cache_tree_free(&istate
->cache_tree
);
681 istate
->cache_tree
= cache_tree();
682 prime_cache_tree_rec(istate
->cache_tree
, tree
);
683 istate
->cache_changed
|= CACHE_TREE_CHANGED
;
687 * find the cache_tree that corresponds to the current level without
688 * exploding the full path into textual form. The root of the
689 * cache tree is given as "root", and our current level is "info".
690 * (1) When at root level, info->prev is NULL, so it is "root" itself.
691 * (2) Otherwise, find the cache_tree that corresponds to one level
692 * above us, and find ourselves in there.
694 static struct cache_tree
*find_cache_tree_from_traversal(struct cache_tree
*root
,
695 struct traverse_info
*info
)
697 struct cache_tree
*our_parent
;
701 our_parent
= find_cache_tree_from_traversal(root
, info
->prev
);
702 return cache_tree_find(our_parent
, info
->name
.path
);
705 int cache_tree_matches_traversal(struct cache_tree
*root
,
706 struct name_entry
*ent
,
707 struct traverse_info
*info
)
709 struct cache_tree
*it
;
711 it
= find_cache_tree_from_traversal(root
, info
);
712 it
= cache_tree_find(it
, ent
->path
);
713 if (it
&& it
->entry_count
> 0 && !hashcmp(ent
->sha1
, it
->sha1
))
714 return it
->entry_count
;
718 int update_main_cache_tree(int flags
)
720 if (!the_index
.cache_tree
)
721 the_index
.cache_tree
= cache_tree();
722 return cache_tree_update(&the_index
, flags
);