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 if (it
->subtree_alloc
<= it
->subtree_nr
) {
79 it
->subtree_alloc
= alloc_nr(it
->subtree_alloc
);
80 it
->down
= xrealloc(it
->down
, it
->subtree_alloc
*
85 down
= xmalloc(sizeof(*down
) + pathlen
+ 1);
86 down
->cache_tree
= NULL
;
87 down
->namelen
= pathlen
;
88 memcpy(down
->name
, path
, pathlen
);
89 down
->name
[pathlen
] = 0;
91 if (pos
< it
->subtree_nr
)
92 memmove(it
->down
+ pos
+ 1,
94 sizeof(down
) * (it
->subtree_nr
- pos
- 1));
99 struct cache_tree_sub
*cache_tree_sub(struct cache_tree
*it
, const char *path
)
101 int pathlen
= strlen(path
);
102 return find_subtree(it
, path
, pathlen
, 1);
105 void cache_tree_invalidate_path(struct cache_tree
*it
, const char *path
)
108 * ==> invalidate self
109 * ==> find "a", have it invalidate "b/c"
111 * ==> invalidate self
112 * ==> if "a" exists as a subtree, remove it.
116 struct cache_tree_sub
*down
;
119 fprintf(stderr
, "cache-tree invalidate <%s>\n", path
);
124 slash
= strchr(path
, '/');
125 it
->entry_count
= -1;
128 namelen
= strlen(path
);
129 pos
= subtree_pos(it
, path
, namelen
);
131 cache_tree_free(&it
->down
[pos
]->cache_tree
);
136 * move 4 and 5 up one place (2 entries)
137 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
139 memmove(it
->down
+pos
, it
->down
+pos
+1,
140 sizeof(struct cache_tree_sub
*) *
141 (it
->subtree_nr
- pos
- 1));
146 namelen
= slash
- path
;
147 down
= find_subtree(it
, path
, namelen
, 0);
149 cache_tree_invalidate_path(down
->cache_tree
, slash
+ 1);
152 static int verify_cache(struct cache_entry
**cache
,
157 /* Verify that the tree is merged */
159 for (i
= 0; i
< entries
; i
++) {
160 struct cache_entry
*ce
= cache
[i
];
161 if (ce_stage(ce
) || (ce
->ce_flags
& CE_INTENT_TO_ADD
)) {
163 fprintf(stderr
, "...\n");
167 fprintf(stderr
, "%s: unmerged (%s)\n",
168 ce
->name
, sha1_to_hex(ce
->sha1
));
170 fprintf(stderr
, "%s: not added yet\n",
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
;
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.
262 for (i
= 0; i
< entries
; i
++) {
263 struct cache_entry
*ce
= cache
[i
];
264 struct cache_tree_sub
*sub
;
265 const char *path
, *slash
;
266 int pathlen
, sublen
, subcnt
;
269 pathlen
= ce_namelen(ce
);
270 if (pathlen
<= baselen
|| memcmp(base
, path
, baselen
))
271 break; /* at the end of this level */
273 slash
= strchr(path
+ baselen
, '/');
277 * a/bbb/c (base = a/, slash = /c)
279 * path+baselen = bbb/c, sublen = 3
281 sublen
= slash
- (path
+ baselen
);
282 sub
= find_subtree(it
, path
+ baselen
, sublen
, 1);
283 if (!sub
->cache_tree
)
284 sub
->cache_tree
= cache_tree();
285 subcnt
= update_one(sub
->cache_tree
,
286 cache
+ i
, entries
- i
,
288 baselen
+ sublen
+ 1,
297 discard_unused_subtrees(it
);
300 * Then write out the tree object for this level.
302 strbuf_init(&buffer
, 8192);
304 for (i
= 0; i
< entries
; i
++) {
305 struct cache_entry
*ce
= cache
[i
];
306 struct cache_tree_sub
*sub
;
307 const char *path
, *slash
;
309 const unsigned char *sha1
;
313 pathlen
= ce_namelen(ce
);
314 if (pathlen
<= baselen
|| memcmp(base
, path
, baselen
))
315 break; /* at the end of this level */
317 slash
= strchr(path
+ baselen
, '/');
319 entlen
= slash
- (path
+ baselen
);
320 sub
= find_subtree(it
, path
+ baselen
, entlen
, 0);
322 die("cache-tree.c: '%.*s' in '%s' not found",
323 entlen
, path
+ baselen
, path
);
324 i
+= sub
->cache_tree
->entry_count
- 1;
325 sha1
= sub
->cache_tree
->sha1
;
331 entlen
= pathlen
- baselen
;
333 if (mode
!= S_IFGITLINK
&& !missing_ok
&& !has_sha1_file(sha1
)) {
334 strbuf_release(&buffer
);
335 return error("invalid object %06o %s for '%.*s'",
336 mode
, sha1_to_hex(sha1
), entlen
+baselen
, path
);
339 if (ce
->ce_flags
& CE_REMOVE
)
340 continue; /* entry being removed */
342 strbuf_grow(&buffer
, entlen
+ 100);
343 strbuf_addf(&buffer
, "%o %.*s%c", mode
, entlen
, path
+ baselen
, '\0');
344 strbuf_add(&buffer
, sha1
, 20);
347 fprintf(stderr
, "cache-tree update-one %o %.*s\n",
348 mode
, entlen
, path
+ baselen
);
353 hash_sha1_file(buffer
.buf
, buffer
.len
, tree_type
, it
->sha1
);
354 else if (write_sha1_file(buffer
.buf
, buffer
.len
, tree_type
, it
->sha1
)) {
355 strbuf_release(&buffer
);
359 strbuf_release(&buffer
);
362 fprintf(stderr
, "cache-tree update-one (%d ent, %d subtree) %s\n",
363 it
->entry_count
, it
->subtree_nr
,
364 sha1_to_hex(it
->sha1
));
369 int cache_tree_update(struct cache_tree
*it
,
370 struct cache_entry
**cache
,
376 i
= verify_cache(cache
, entries
);
379 i
= update_one(it
, cache
, entries
, "", 0, missing_ok
, dryrun
);
385 static void write_one(struct strbuf
*buffer
, struct cache_tree
*it
,
386 const char *path
, int pathlen
)
390 /* One "cache-tree" entry consists of the following:
391 * path (NUL terminated)
392 * entry_count, subtree_nr ("%d %d\n")
393 * tree-sha1 (missing if invalid)
394 * subtree_nr "cache-tree" entries for subtrees.
396 strbuf_grow(buffer
, pathlen
+ 100);
397 strbuf_add(buffer
, path
, pathlen
);
398 strbuf_addf(buffer
, "%c%d %d\n", 0, it
->entry_count
, it
->subtree_nr
);
401 if (0 <= it
->entry_count
)
402 fprintf(stderr
, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
403 pathlen
, path
, it
->entry_count
, it
->subtree_nr
,
404 sha1_to_hex(it
->sha1
));
406 fprintf(stderr
, "cache-tree <%.*s> (%d subtree) invalid\n",
407 pathlen
, path
, it
->subtree_nr
);
410 if (0 <= it
->entry_count
) {
411 strbuf_add(buffer
, it
->sha1
, 20);
413 for (i
= 0; i
< it
->subtree_nr
; i
++) {
414 struct cache_tree_sub
*down
= it
->down
[i
];
416 struct cache_tree_sub
*prev
= it
->down
[i
-1];
417 if (subtree_name_cmp(down
->name
, down
->namelen
,
418 prev
->name
, prev
->namelen
) <= 0)
419 die("fatal - unsorted cache subtree");
421 write_one(buffer
, down
->cache_tree
, down
->name
, down
->namelen
);
425 void cache_tree_write(struct strbuf
*sb
, struct cache_tree
*root
)
427 write_one(sb
, root
, "", 0);
430 static struct cache_tree
*read_one(const char **buffer
, unsigned long *size_p
)
432 const char *buf
= *buffer
;
433 unsigned long size
= *size_p
;
436 struct cache_tree
*it
;
440 /* skip name, but make sure name exists */
441 while (size
&& *buf
) {
451 it
->entry_count
= strtol(cp
, &ep
, 10);
455 subtree_nr
= strtol(cp
, &ep
, 10);
458 while (size
&& *buf
&& *buf
!= '\n') {
465 if (0 <= it
->entry_count
) {
468 hashcpy(it
->sha1
, (const unsigned char*)buf
);
474 if (0 <= it
->entry_count
)
475 fprintf(stderr
, "cache-tree <%s> (%d ent, %d subtree) %s\n",
476 *buffer
, it
->entry_count
, subtree_nr
,
477 sha1_to_hex(it
->sha1
));
479 fprintf(stderr
, "cache-tree <%s> (%d subtrees) invalid\n",
480 *buffer
, subtree_nr
);
484 * Just a heuristic -- we do not add directories that often but
485 * we do not want to have to extend it immediately when we do,
488 it
->subtree_alloc
= subtree_nr
+ 2;
489 it
->down
= xcalloc(it
->subtree_alloc
, sizeof(struct cache_tree_sub
*));
490 for (i
= 0; i
< subtree_nr
; i
++) {
491 /* read each subtree */
492 struct cache_tree
*sub
;
493 struct cache_tree_sub
*subtree
;
494 const char *name
= buf
;
496 sub
= read_one(&buf
, &size
);
499 subtree
= cache_tree_sub(it
, name
);
500 subtree
->cache_tree
= sub
;
502 if (subtree_nr
!= it
->subtree_nr
)
503 die("cache-tree: internal error");
509 cache_tree_free(&it
);
513 struct cache_tree
*cache_tree_read(const char *buffer
, unsigned long size
)
516 return NULL
; /* not the whole tree */
517 return read_one(&buffer
, &size
);
520 static struct cache_tree
*cache_tree_find(struct cache_tree
*it
, const char *path
)
526 struct cache_tree_sub
*sub
;
528 slash
= strchr(path
, '/');
530 slash
= path
+ strlen(path
);
531 /* between path and slash is the name of the
532 * subtree to look for.
534 sub
= find_subtree(it
, path
, slash
- path
, 0);
537 it
= sub
->cache_tree
;
539 while (*slash
&& *slash
== '/')
541 if (!slash
|| !*slash
)
542 return it
; /* prefix ended with slashes */
548 int write_cache_as_tree(unsigned char *sha1
, int flags
, const char *prefix
)
550 int entries
, was_valid
, newfd
;
551 struct lock_file
*lock_file
;
554 * We can't free this memory, it becomes part of a linked list
557 lock_file
= xcalloc(1, sizeof(struct lock_file
));
559 newfd
= hold_locked_index(lock_file
, 1);
561 entries
= read_cache();
563 return WRITE_TREE_UNREADABLE_INDEX
;
564 if (flags
& WRITE_TREE_IGNORE_CACHE_TREE
)
565 cache_tree_free(&(active_cache_tree
));
567 if (!active_cache_tree
)
568 active_cache_tree
= cache_tree();
570 was_valid
= cache_tree_fully_valid(active_cache_tree
);
572 int missing_ok
= flags
& WRITE_TREE_MISSING_OK
;
574 if (cache_tree_update(active_cache_tree
,
575 active_cache
, active_nr
,
577 return WRITE_TREE_UNMERGED_INDEX
;
579 if (!write_cache(newfd
, active_cache
, active_nr
) &&
580 !commit_lock_file(lock_file
))
583 /* Not being able to write is fine -- we are only interested
584 * in updating the cache-tree part, and if the next caller
585 * ends up using the old index with unupdated cache-tree part
586 * it misses the work we did here, but that is just a
587 * performance penalty and not a big deal.
592 struct cache_tree
*subtree
=
593 cache_tree_find(active_cache_tree
, prefix
);
595 return WRITE_TREE_PREFIX_ERROR
;
596 hashcpy(sha1
, subtree
->sha1
);
599 hashcpy(sha1
, active_cache_tree
->sha1
);
602 rollback_lock_file(lock_file
);
607 static void prime_cache_tree_rec(struct cache_tree
*it
, struct tree
*tree
)
609 struct tree_desc desc
;
610 struct name_entry entry
;
613 hashcpy(it
->sha1
, tree
->object
.sha1
);
614 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
616 while (tree_entry(&desc
, &entry
)) {
617 if (!S_ISDIR(entry
.mode
))
620 struct cache_tree_sub
*sub
;
621 struct tree
*subtree
= lookup_tree(entry
.sha1
);
622 if (!subtree
->object
.parsed
)
624 sub
= cache_tree_sub(it
, entry
.path
);
625 sub
->cache_tree
= cache_tree();
626 prime_cache_tree_rec(sub
->cache_tree
, subtree
);
627 cnt
+= sub
->cache_tree
->entry_count
;
630 it
->entry_count
= cnt
;
633 void prime_cache_tree(struct cache_tree
**it
, struct tree
*tree
)
637 prime_cache_tree_rec(*it
, tree
);
641 * find the cache_tree that corresponds to the current level without
642 * exploding the full path into textual form. The root of the
643 * cache tree is given as "root", and our current level is "info".
644 * (1) When at root level, info->prev is NULL, so it is "root" itself.
645 * (2) Otherwise, find the cache_tree that corresponds to one level
646 * above us, and find ourselves in there.
648 static struct cache_tree
*find_cache_tree_from_traversal(struct cache_tree
*root
,
649 struct traverse_info
*info
)
651 struct cache_tree
*our_parent
;
655 our_parent
= find_cache_tree_from_traversal(root
, info
->prev
);
656 return cache_tree_find(our_parent
, info
->name
.path
);
659 int cache_tree_matches_traversal(struct cache_tree
*root
,
660 struct name_entry
*ent
,
661 struct traverse_info
*info
)
663 struct cache_tree
*it
;
665 it
= find_cache_tree_from_traversal(root
, info
);
666 it
= cache_tree_find(it
, ent
->path
);
667 if (it
&& it
->entry_count
> 0 && !hashcmp(ent
->sha1
, it
->sha1
))
668 return it
->entry_count
;