4 #include "cache-tree.h"
10 struct cache_tree *cache_tree(void)
12 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
17 void cache_tree_free(struct cache_tree **it_p)
20 struct cache_tree *it = *it_p;
24 for (i = 0; i < it->subtree_nr; i++)
26 cache_tree_free(&it->down[i]->cache_tree);
34 static int subtree_name_cmp(const char *one, int onelen,
35 const char *two, int twolen)
41 return memcmp(one, two, onelen);
44 static int subtree_pos(struct cache_tree *it, const char *path, int pathlen)
46 struct cache_tree_sub **down = it->down;
51 int mi = (lo + hi) / 2;
52 struct cache_tree_sub *mdl = down[mi];
53 int cmp = subtree_name_cmp(path, pathlen,
54 mdl->name, mdl->namelen);
65 static struct cache_tree_sub *find_subtree(struct cache_tree *it,
70 struct cache_tree_sub *down;
71 int pos = subtree_pos(it, path, pathlen);
78 ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
81 down = xmalloc(sizeof(*down) + pathlen + 1);
82 down->cache_tree = NULL;
83 down->namelen = pathlen;
84 memcpy(down->name, path, pathlen);
85 down->name[pathlen] = 0;
87 if (pos < it->subtree_nr)
88 memmove(it->down + pos + 1,
90 sizeof(down) * (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 void cache_tree_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 = strchr(path, '/');
121 it->entry_count = -1;
124 namelen = strlen(path);
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 memmove(it->down+pos, it->down+pos+1,
136 sizeof(struct cache_tree_sub *) *
137 (it->subtree_nr - pos - 1));
142 namelen = slash - path;
143 down = find_subtree(it, path, namelen, 0);
145 cache_tree_invalidate_path(down->cache_tree, slash + 1);
148 static int verify_cache(const struct cache_entry * const *cache,
149 int entries, int flags)
152 int silent = flags & WRITE_TREE_SILENT;
154 /* Verify that the tree is merged */
156 for (i = 0; i < entries; i++) {
157 const struct cache_entry *ce = cache[i];
162 fprintf(stderr, "...\n");
165 fprintf(stderr, "%s: unmerged (%s)\n",
166 ce->name, sha1_to_hex(ce->sha1));
172 /* Also verify that the cache does not have path and path/file
173 * at the same time. At this point we know the cache has only
177 for (i = 0; i < entries - 1; i++) {
178 /* path/file always comes after path because of the way
179 * the cache is sorted. Also path can appear only once,
180 * which means conflicting one would immediately follow.
182 const char *this_name = cache[i]->name;
183 const char *next_name = cache[i+1]->name;
184 int this_len = strlen(this_name);
185 if (this_len < strlen(next_name) &&
186 strncmp(this_name, next_name, this_len) == 0 &&
187 next_name[this_len] == '/') {
189 fprintf(stderr, "...\n");
192 fprintf(stderr, "You have both %s and %s\n",
193 this_name, next_name);
201 static void discard_unused_subtrees(struct cache_tree *it)
203 struct cache_tree_sub **down = it->down;
204 int nr = it->subtree_nr;
206 for (dst = src = 0; src < nr; src++) {
207 struct cache_tree_sub *s = down[src];
211 cache_tree_free(&s->cache_tree);
218 int cache_tree_fully_valid(struct cache_tree *it)
223 if (it->entry_count < 0 || !has_sha1_file(it->sha1))
225 for (i = 0; i < it->subtree_nr; i++) {
226 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
232 static int update_one(struct cache_tree *it,
233 const struct cache_entry * const *cache,
240 struct strbuf buffer;
241 int missing_ok = flags & WRITE_TREE_MISSING_OK;
242 int dryrun = flags & WRITE_TREE_DRY_RUN;
243 int to_invalidate = 0;
248 if (0 <= it->entry_count && has_sha1_file(it->sha1))
249 return it->entry_count;
252 * We first scan for subtrees and update them; we start by
253 * marking existing subtrees -- the ones that are unmarked
254 * should not be in the result.
256 for (i = 0; i < it->subtree_nr; i++)
257 it->down[i]->used = 0;
260 * Find the subtrees and update them.
263 while (i < entries) {
264 const struct cache_entry *ce = cache[i];
265 struct cache_tree_sub *sub;
266 const char *path, *slash;
267 int pathlen, sublen, subcnt, subskip;
270 pathlen = ce_namelen(ce);
271 if (pathlen <= baselen || memcmp(base, path, baselen))
272 break; /* at the end of this level */
274 slash = strchr(path + baselen, '/');
280 * a/bbb/c (base = a/, slash = /c)
282 * path+baselen = bbb/c, sublen = 3
284 sublen = slash - (path + baselen);
285 sub = find_subtree(it, path + baselen, sublen, 1);
286 if (!sub->cache_tree)
287 sub->cache_tree = cache_tree();
288 subcnt = update_one(sub->cache_tree,
289 cache + i, entries - i,
291 baselen + sublen + 1,
297 sub->count = subcnt; /* to be used in the next loop */
298 *skip_count += subskip;
302 discard_unused_subtrees(it);
305 * Then write out the tree object for this level.
307 strbuf_init(&buffer, 8192);
310 while (i < entries) {
311 const struct cache_entry *ce = cache[i];
312 struct cache_tree_sub *sub;
313 const char *path, *slash;
315 const unsigned char *sha1;
319 pathlen = ce_namelen(ce);
320 if (pathlen <= baselen || memcmp(base, path, baselen))
321 break; /* at the end of this level */
323 slash = strchr(path + baselen, '/');
325 entlen = slash - (path + baselen);
326 sub = find_subtree(it, path + baselen, entlen, 0);
328 die("cache-tree.c: '%.*s' in '%s' not found",
329 entlen, path + baselen, path);
331 sha1 = sub->cache_tree->sha1;
333 if (sub->cache_tree->entry_count < 0)
339 entlen = pathlen - baselen;
342 if (mode != S_IFGITLINK && !missing_ok && !has_sha1_file(sha1)) {
343 strbuf_release(&buffer);
344 return error("invalid object %06o %s for '%.*s'",
345 mode, sha1_to_hex(sha1), entlen+baselen, path);
349 * CE_REMOVE entries are removed before the index is
350 * written to disk. Skip them to remain consistent
351 * with the future on-disk index.
353 if (ce->ce_flags & CE_REMOVE) {
354 *skip_count = *skip_count + 1;
359 * CE_INTENT_TO_ADD entries exist on on-disk index but
360 * they are not part of generated trees. Invalidate up
361 * to root to force cache-tree users to read elsewhere.
363 if (ce->ce_flags & CE_INTENT_TO_ADD) {
368 strbuf_grow(&buffer, entlen + 100);
369 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
370 strbuf_add(&buffer, sha1, 20);
373 fprintf(stderr, "cache-tree update-one %o %.*s\n",
374 mode, entlen, path + baselen);
379 hash_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1);
380 else if (write_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1)) {
381 strbuf_release(&buffer);
385 strbuf_release(&buffer);
386 it->entry_count = to_invalidate ? -1 : i - *skip_count;
388 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
389 it->entry_count, it->subtree_nr,
390 sha1_to_hex(it->sha1));
395 int cache_tree_update(struct cache_tree *it,
396 const struct cache_entry * const *cache,
401 i = verify_cache(cache, entries, flags);
404 i = update_one(it, cache, entries, "", 0, &skip, flags);
410 static void write_one(struct strbuf *buffer, struct cache_tree *it,
411 const char *path, int pathlen)
415 /* One "cache-tree" entry consists of the following:
416 * path (NUL terminated)
417 * entry_count, subtree_nr ("%d %d\n")
418 * tree-sha1 (missing if invalid)
419 * subtree_nr "cache-tree" entries for subtrees.
421 strbuf_grow(buffer, pathlen + 100);
422 strbuf_add(buffer, path, pathlen);
423 strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
426 if (0 <= it->entry_count)
427 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
428 pathlen, path, it->entry_count, it->subtree_nr,
429 sha1_to_hex(it->sha1));
431 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
432 pathlen, path, it->subtree_nr);
435 if (0 <= it->entry_count) {
436 strbuf_add(buffer, it->sha1, 20);
438 for (i = 0; i < it->subtree_nr; i++) {
439 struct cache_tree_sub *down = it->down[i];
441 struct cache_tree_sub *prev = it->down[i-1];
442 if (subtree_name_cmp(down->name, down->namelen,
443 prev->name, prev->namelen) <= 0)
444 die("fatal - unsorted cache subtree");
446 write_one(buffer, down->cache_tree, down->name, down->namelen);
450 void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
452 write_one(sb, root, "", 0);
455 static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
457 const char *buf = *buffer;
458 unsigned long size = *size_p;
461 struct cache_tree *it;
465 /* skip name, but make sure name exists */
466 while (size && *buf) {
476 it->entry_count = strtol(cp, &ep, 10);
480 subtree_nr = strtol(cp, &ep, 10);
483 while (size && *buf && *buf != '\n') {
490 if (0 <= it->entry_count) {
493 hashcpy(it->sha1, (const unsigned char*)buf);
499 if (0 <= it->entry_count)
500 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
501 *buffer, it->entry_count, subtree_nr,
502 sha1_to_hex(it->sha1));
504 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
505 *buffer, subtree_nr);
509 * Just a heuristic -- we do not add directories that often but
510 * we do not want to have to extend it immediately when we do,
513 it->subtree_alloc = subtree_nr + 2;
514 it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
515 for (i = 0; i < subtree_nr; i++) {
516 /* read each subtree */
517 struct cache_tree *sub;
518 struct cache_tree_sub *subtree;
519 const char *name = buf;
521 sub = read_one(&buf, &size);
524 subtree = cache_tree_sub(it, name);
525 subtree->cache_tree = sub;
527 if (subtree_nr != it->subtree_nr)
528 die("cache-tree: internal error");
534 cache_tree_free(&it);
538 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
541 return NULL; /* not the whole tree */
542 return read_one(&buffer, &size);
545 static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
551 struct cache_tree_sub *sub;
553 slash = strchr(path, '/');
555 slash = path + strlen(path);
556 /* between path and slash is the name of the
557 * subtree to look for.
559 sub = find_subtree(it, path, slash - path, 0);
562 it = sub->cache_tree;
564 while (*slash && *slash == '/')
566 if (!slash || !*slash)
567 return it; /* prefix ended with slashes */
573 int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
575 int entries, was_valid, newfd;
576 struct lock_file *lock_file;
579 * We can't free this memory, it becomes part of a linked list
582 lock_file = xcalloc(1, sizeof(struct lock_file));
584 newfd = hold_locked_index(lock_file, 1);
586 entries = read_cache();
588 return WRITE_TREE_UNREADABLE_INDEX;
589 if (flags & WRITE_TREE_IGNORE_CACHE_TREE)
590 cache_tree_free(&(active_cache_tree));
592 if (!active_cache_tree)
593 active_cache_tree = cache_tree();
595 was_valid = cache_tree_fully_valid(active_cache_tree);
597 if (cache_tree_update(active_cache_tree,
598 (const struct cache_entry * const *)active_cache,
599 active_nr, flags) < 0)
600 return WRITE_TREE_UNMERGED_INDEX;
602 if (!write_cache(newfd, active_cache, active_nr) &&
603 !commit_lock_file(lock_file))
606 /* Not being able to write is fine -- we are only interested
607 * in updating the cache-tree part, and if the next caller
608 * ends up using the old index with unupdated cache-tree part
609 * it misses the work we did here, but that is just a
610 * performance penalty and not a big deal.
615 struct cache_tree *subtree =
616 cache_tree_find(active_cache_tree, prefix);
618 return WRITE_TREE_PREFIX_ERROR;
619 hashcpy(sha1, subtree->sha1);
622 hashcpy(sha1, active_cache_tree->sha1);
625 rollback_lock_file(lock_file);
630 static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
632 struct tree_desc desc;
633 struct name_entry entry;
636 hashcpy(it->sha1, tree->object.sha1);
637 init_tree_desc(&desc, tree->buffer, tree->size);
639 while (tree_entry(&desc, &entry)) {
640 if (!S_ISDIR(entry.mode))
643 struct cache_tree_sub *sub;
644 struct tree *subtree = lookup_tree(entry.sha1);
645 if (!subtree->object.parsed)
647 sub = cache_tree_sub(it, entry.path);
648 sub->cache_tree = cache_tree();
649 prime_cache_tree_rec(sub->cache_tree, subtree);
650 cnt += sub->cache_tree->entry_count;
653 it->entry_count = cnt;
656 void prime_cache_tree(struct cache_tree **it, struct tree *tree)
660 prime_cache_tree_rec(*it, tree);
664 * find the cache_tree that corresponds to the current level without
665 * exploding the full path into textual form. The root of the
666 * cache tree is given as "root", and our current level is "info".
667 * (1) When at root level, info->prev is NULL, so it is "root" itself.
668 * (2) Otherwise, find the cache_tree that corresponds to one level
669 * above us, and find ourselves in there.
671 static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
672 struct traverse_info *info)
674 struct cache_tree *our_parent;
678 our_parent = find_cache_tree_from_traversal(root, info->prev);
679 return cache_tree_find(our_parent, info->name.path);
682 int cache_tree_matches_traversal(struct cache_tree *root,
683 struct name_entry *ent,
684 struct traverse_info *info)
686 struct cache_tree *it;
688 it = find_cache_tree_from_traversal(root, info);
689 it = cache_tree_find(it, ent->path);
690 if (it && it->entry_count > 0 && !hashcmp(ent->sha1, it->sha1))
691 return it->entry_count;
695 int update_main_cache_tree(int flags)
697 if (!the_index.cache_tree)
698 the_index.cache_tree = cache_tree();
699 return cache_tree_update(the_index.cache_tree,
700 (const struct cache_entry * const *)the_index.cache,
701 the_index.cache_nr, flags);