3 #include "cache-tree.h"
9 struct cache_tree *cache_tree(void)
11 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
16 void cache_tree_free(struct cache_tree **it_p)
19 struct cache_tree *it = *it_p;
23 for (i = 0; i < it->subtree_nr; i++)
25 cache_tree_free(&it->down[i]->cache_tree);
31 static int subtree_name_cmp(const char *one, int onelen,
32 const char *two, int twolen)
38 return memcmp(one, two, onelen);
41 static int subtree_pos(struct cache_tree *it, const char *path, int pathlen)
43 struct cache_tree_sub **down = it->down;
48 int mi = (lo + hi) / 2;
49 struct cache_tree_sub *mdl = down[mi];
50 int cmp = subtree_name_cmp(path, pathlen,
51 mdl->name, mdl->namelen);
62 static struct cache_tree_sub *find_subtree(struct cache_tree *it,
67 struct cache_tree_sub *down;
68 int pos = subtree_pos(it, path, pathlen);
75 if (it->subtree_alloc <= it->subtree_nr) {
76 it->subtree_alloc = alloc_nr(it->subtree_alloc);
77 it->down = xrealloc(it->down, it->subtree_alloc *
82 down = xmalloc(sizeof(*down) + pathlen + 1);
83 down->cache_tree = NULL;
84 down->namelen = pathlen;
85 memcpy(down->name, path, pathlen);
86 down->name[pathlen] = 0;
88 if (pos < it->subtree_nr)
89 memmove(it->down + pos + 1,
91 sizeof(down) * (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 void cache_tree_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 = strchr(path, '/');
122 it->entry_count = -1;
125 namelen = strlen(path);
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 memmove(it->down+pos, it->down+pos+1,
137 sizeof(struct cache_tree_sub *) *
138 (it->subtree_nr - pos - 1));
143 namelen = slash - path;
144 down = find_subtree(it, path, namelen, 0);
146 cache_tree_invalidate_path(down->cache_tree, slash + 1);
149 static int verify_cache(struct cache_entry **cache,
154 /* Verify that the tree is merged */
156 for (i = 0; i < entries; i++) {
157 struct cache_entry *ce = cache[i];
158 if (ce_stage(ce) || (ce->ce_flags & CE_INTENT_TO_ADD)) {
160 fprintf(stderr, "...\n");
164 fprintf(stderr, "%s: unmerged (%s)\n",
165 ce->name, sha1_to_hex(ce->sha1));
167 fprintf(stderr, "%s: not added yet\n",
174 /* Also verify that the cache does not have path and path/file
175 * at the same time. At this point we know the cache has only
179 for (i = 0; i < entries - 1; i++) {
180 /* path/file always comes after path because of the way
181 * the cache is sorted. Also path can appear only once,
182 * which means conflicting one would immediately follow.
184 const char *this_name = cache[i]->name;
185 const char *next_name = cache[i+1]->name;
186 int this_len = strlen(this_name);
187 if (this_len < strlen(next_name) &&
188 strncmp(this_name, next_name, this_len) == 0 &&
189 next_name[this_len] == '/') {
191 fprintf(stderr, "...\n");
194 fprintf(stderr, "You have both %s and %s\n",
195 this_name, next_name);
203 static void discard_unused_subtrees(struct cache_tree *it)
205 struct cache_tree_sub **down = it->down;
206 int nr = it->subtree_nr;
208 for (dst = src = 0; src < nr; src++) {
209 struct cache_tree_sub *s = down[src];
213 cache_tree_free(&s->cache_tree);
220 int cache_tree_fully_valid(struct cache_tree *it)
225 if (it->entry_count < 0 || !has_sha1_file(it->sha1))
227 for (i = 0; i < it->subtree_nr; i++) {
228 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
234 static int update_one(struct cache_tree *it,
235 struct cache_entry **cache,
242 struct strbuf buffer;
245 if (0 <= it->entry_count && has_sha1_file(it->sha1))
246 return it->entry_count;
249 * We first scan for subtrees and update them; we start by
250 * marking existing subtrees -- the ones that are unmarked
251 * should not be in the result.
253 for (i = 0; i < it->subtree_nr; i++)
254 it->down[i]->used = 0;
257 * Find the subtrees and update them.
259 for (i = 0; i < entries; i++) {
260 struct cache_entry *ce = cache[i];
261 struct cache_tree_sub *sub;
262 const char *path, *slash;
263 int pathlen, sublen, subcnt;
266 pathlen = ce_namelen(ce);
267 if (pathlen <= baselen || memcmp(base, path, baselen))
268 break; /* at the end of this level */
270 slash = strchr(path + baselen, '/');
274 * a/bbb/c (base = a/, slash = /c)
276 * path+baselen = bbb/c, sublen = 3
278 sublen = slash - (path + baselen);
279 sub = find_subtree(it, path + baselen, sublen, 1);
280 if (!sub->cache_tree)
281 sub->cache_tree = cache_tree();
282 subcnt = update_one(sub->cache_tree,
283 cache + i, entries - i,
285 baselen + sublen + 1,
294 discard_unused_subtrees(it);
297 * Then write out the tree object for this level.
299 strbuf_init(&buffer, 8192);
301 for (i = 0; i < entries; i++) {
302 struct cache_entry *ce = cache[i];
303 struct cache_tree_sub *sub;
304 const char *path, *slash;
306 const unsigned char *sha1;
310 pathlen = ce_namelen(ce);
311 if (pathlen <= baselen || memcmp(base, path, baselen))
312 break; /* at the end of this level */
314 slash = strchr(path + baselen, '/');
316 entlen = slash - (path + baselen);
317 sub = find_subtree(it, path + baselen, entlen, 0);
319 die("cache-tree.c: '%.*s' in '%s' not found",
320 entlen, path + baselen, path);
321 i += sub->cache_tree->entry_count - 1;
322 sha1 = sub->cache_tree->sha1;
328 entlen = pathlen - baselen;
330 if (mode != S_IFGITLINK && !missing_ok && !has_sha1_file(sha1))
331 return error("invalid object %s", sha1_to_hex(sha1));
333 if (ce->ce_flags & CE_REMOVE)
334 continue; /* entry being removed */
336 strbuf_grow(&buffer, entlen + 100);
337 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
338 strbuf_add(&buffer, sha1, 20);
341 fprintf(stderr, "cache-tree update-one %o %.*s\n",
342 mode, entlen, path + baselen);
347 hash_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1);
348 else if (write_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1)) {
349 strbuf_release(&buffer);
353 strbuf_release(&buffer);
356 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
357 it->entry_count, it->subtree_nr,
358 sha1_to_hex(it->sha1));
363 int cache_tree_update(struct cache_tree *it,
364 struct cache_entry **cache,
370 i = verify_cache(cache, entries);
373 i = update_one(it, cache, entries, "", 0, missing_ok, dryrun);
379 static void write_one(struct strbuf *buffer, struct cache_tree *it,
380 const char *path, int pathlen)
384 /* One "cache-tree" entry consists of the following:
385 * path (NUL terminated)
386 * entry_count, subtree_nr ("%d %d\n")
387 * tree-sha1 (missing if invalid)
388 * subtree_nr "cache-tree" entries for subtrees.
390 strbuf_grow(buffer, pathlen + 100);
391 strbuf_add(buffer, path, pathlen);
392 strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
395 if (0 <= it->entry_count)
396 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
397 pathlen, path, it->entry_count, it->subtree_nr,
398 sha1_to_hex(it->sha1));
400 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
401 pathlen, path, it->subtree_nr);
404 if (0 <= it->entry_count) {
405 strbuf_add(buffer, it->sha1, 20);
407 for (i = 0; i < it->subtree_nr; i++) {
408 struct cache_tree_sub *down = it->down[i];
410 struct cache_tree_sub *prev = it->down[i-1];
411 if (subtree_name_cmp(down->name, down->namelen,
412 prev->name, prev->namelen) <= 0)
413 die("fatal - unsorted cache subtree");
415 write_one(buffer, down->cache_tree, down->name, down->namelen);
419 void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
421 write_one(sb, root, "", 0);
424 static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
426 const char *buf = *buffer;
427 unsigned long size = *size_p;
430 struct cache_tree *it;
434 /* skip name, but make sure name exists */
435 while (size && *buf) {
445 it->entry_count = strtol(cp, &ep, 10);
449 subtree_nr = strtol(cp, &ep, 10);
452 while (size && *buf && *buf != '\n') {
459 if (0 <= it->entry_count) {
462 hashcpy(it->sha1, (const unsigned char*)buf);
468 if (0 <= it->entry_count)
469 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
470 *buffer, it->entry_count, subtree_nr,
471 sha1_to_hex(it->sha1));
473 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
474 *buffer, subtree_nr);
478 * Just a heuristic -- we do not add directories that often but
479 * we do not want to have to extend it immediately when we do,
482 it->subtree_alloc = subtree_nr + 2;
483 it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
484 for (i = 0; i < subtree_nr; i++) {
485 /* read each subtree */
486 struct cache_tree *sub;
487 struct cache_tree_sub *subtree;
488 const char *name = buf;
490 sub = read_one(&buf, &size);
493 subtree = cache_tree_sub(it, name);
494 subtree->cache_tree = sub;
496 if (subtree_nr != it->subtree_nr)
497 die("cache-tree: internal error");
503 cache_tree_free(&it);
507 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
510 return NULL; /* not the whole tree */
511 return read_one(&buffer, &size);
514 static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
518 struct cache_tree_sub *sub;
520 slash = strchr(path, '/');
522 slash = path + strlen(path);
523 /* between path and slash is the name of the
524 * subtree to look for.
526 sub = find_subtree(it, path, slash - path, 0);
529 it = sub->cache_tree;
531 while (*slash && *slash == '/')
533 if (!slash || !*slash)
534 return it; /* prefix ended with slashes */
540 int write_cache_as_tree(unsigned char *sha1, int missing_ok, const char *prefix)
542 int entries, was_valid, newfd;
545 * We can't free this memory, it becomes part of a linked list
548 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
550 newfd = hold_locked_index(lock_file, 1);
552 entries = read_cache();
554 return WRITE_TREE_UNREADABLE_INDEX;
556 if (!active_cache_tree)
557 active_cache_tree = cache_tree();
559 was_valid = cache_tree_fully_valid(active_cache_tree);
562 if (cache_tree_update(active_cache_tree,
563 active_cache, active_nr,
565 return WRITE_TREE_UNMERGED_INDEX;
567 if (!write_cache(newfd, active_cache, active_nr) &&
568 !commit_lock_file(lock_file))
571 /* Not being able to write is fine -- we are only interested
572 * in updating the cache-tree part, and if the next caller
573 * ends up using the old index with unupdated cache-tree part
574 * it misses the work we did here, but that is just a
575 * performance penalty and not a big deal.
580 struct cache_tree *subtree =
581 cache_tree_find(active_cache_tree, prefix);
583 return WRITE_TREE_PREFIX_ERROR;
584 hashcpy(sha1, subtree->sha1);
587 hashcpy(sha1, active_cache_tree->sha1);
590 rollback_lock_file(lock_file);