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 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 static int do_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
= strchrnul(path
, '/');
122 namelen
= slash
- path
;
123 it
->entry_count
= -1;
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 down
= find_subtree(it
, path
, namelen
, 0);
145 do_invalidate_path(down
->cache_tree
, slash
+ 1);
149 void cache_tree_invalidate_path(struct index_state
*istate
, const char *path
)
151 if (do_invalidate_path(istate
->cache_tree
, path
))
152 istate
->cache_changed
|= CACHE_TREE_CHANGED
;
155 static int verify_cache(struct cache_entry
**cache
,
156 int entries
, int flags
)
159 int silent
= flags
& WRITE_TREE_SILENT
;
161 /* Verify that the tree is merged */
163 for (i
= 0; i
< entries
; i
++) {
164 const struct cache_entry
*ce
= cache
[i
];
169 fprintf(stderr
, "...\n");
172 fprintf(stderr
, "%s: unmerged (%s)\n",
173 ce
->name
, sha1_to_hex(ce
->sha1
));
179 /* Also verify that the cache does not have path and path/file
180 * at the same time. At this point we know the cache has only
184 for (i
= 0; i
< entries
- 1; i
++) {
185 /* path/file always comes after path because of the way
186 * the cache is sorted. Also path can appear only once,
187 * which means conflicting one would immediately follow.
189 const char *this_name
= cache
[i
]->name
;
190 const char *next_name
= cache
[i
+1]->name
;
191 int this_len
= strlen(this_name
);
192 if (this_len
< strlen(next_name
) &&
193 strncmp(this_name
, next_name
, this_len
) == 0 &&
194 next_name
[this_len
] == '/') {
196 fprintf(stderr
, "...\n");
199 fprintf(stderr
, "You have both %s and %s\n",
200 this_name
, next_name
);
208 static void discard_unused_subtrees(struct cache_tree
*it
)
210 struct cache_tree_sub
**down
= it
->down
;
211 int nr
= it
->subtree_nr
;
213 for (dst
= src
= 0; src
< nr
; src
++) {
214 struct cache_tree_sub
*s
= down
[src
];
218 cache_tree_free(&s
->cache_tree
);
225 int cache_tree_fully_valid(struct cache_tree
*it
)
230 if (it
->entry_count
< 0 || !has_sha1_file(it
->sha1
))
232 for (i
= 0; i
< it
->subtree_nr
; i
++) {
233 if (!cache_tree_fully_valid(it
->down
[i
]->cache_tree
))
239 static int update_one(struct cache_tree
*it
,
240 struct cache_entry
**cache
,
247 struct strbuf buffer
;
248 int missing_ok
= flags
& WRITE_TREE_MISSING_OK
;
249 int dryrun
= flags
& WRITE_TREE_DRY_RUN
;
250 int repair
= flags
& WRITE_TREE_REPAIR
;
251 int to_invalidate
= 0;
254 assert(!(dryrun
&& repair
));
258 if (0 <= it
->entry_count
&& has_sha1_file(it
->sha1
))
259 return it
->entry_count
;
262 * We first scan for subtrees and update them; we start by
263 * marking existing subtrees -- the ones that are unmarked
264 * should not be in the result.
266 for (i
= 0; i
< it
->subtree_nr
; i
++)
267 it
->down
[i
]->used
= 0;
270 * Find the subtrees and update them.
273 while (i
< entries
) {
274 const struct cache_entry
*ce
= cache
[i
];
275 struct cache_tree_sub
*sub
;
276 const char *path
, *slash
;
277 int pathlen
, sublen
, subcnt
, subskip
;
280 pathlen
= ce_namelen(ce
);
281 if (pathlen
<= baselen
|| memcmp(base
, path
, baselen
))
282 break; /* at the end of this level */
284 slash
= strchr(path
+ baselen
, '/');
290 * a/bbb/c (base = a/, slash = /c)
292 * path+baselen = bbb/c, sublen = 3
294 sublen
= slash
- (path
+ baselen
);
295 sub
= find_subtree(it
, path
+ baselen
, sublen
, 1);
296 if (!sub
->cache_tree
)
297 sub
->cache_tree
= cache_tree();
298 subcnt
= update_one(sub
->cache_tree
,
299 cache
+ i
, entries
- i
,
301 baselen
+ sublen
+ 1,
307 die("index cache-tree records empty sub-tree");
309 sub
->count
= subcnt
; /* to be used in the next loop */
310 *skip_count
+= subskip
;
314 discard_unused_subtrees(it
);
317 * Then write out the tree object for this level.
319 strbuf_init(&buffer
, 8192);
322 while (i
< entries
) {
323 const struct cache_entry
*ce
= cache
[i
];
324 struct cache_tree_sub
*sub
;
325 const char *path
, *slash
;
327 const unsigned char *sha1
;
329 int expected_missing
= 0;
332 pathlen
= ce_namelen(ce
);
333 if (pathlen
<= baselen
|| memcmp(base
, path
, baselen
))
334 break; /* at the end of this level */
336 slash
= strchr(path
+ baselen
, '/');
338 entlen
= slash
- (path
+ baselen
);
339 sub
= find_subtree(it
, path
+ baselen
, entlen
, 0);
341 die("cache-tree.c: '%.*s' in '%s' not found",
342 entlen
, path
+ baselen
, path
);
344 sha1
= sub
->cache_tree
->sha1
;
346 if (sub
->cache_tree
->entry_count
< 0) {
348 expected_missing
= 1;
354 entlen
= pathlen
- baselen
;
357 if (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 (ce_intent_to_add(ce
)) {
385 strbuf_grow(&buffer
, entlen
+ 100);
386 strbuf_addf(&buffer
, "%o %.*s%c", mode
, entlen
, path
+ baselen
, '\0');
387 strbuf_add(&buffer
, sha1
, 20);
390 fprintf(stderr
, "cache-tree update-one %o %.*s\n",
391 mode
, entlen
, path
+ baselen
);
396 unsigned char sha1
[20];
397 hash_sha1_file(buffer
.buf
, buffer
.len
, tree_type
, sha1
);
398 if (has_sha1_file(sha1
))
399 hashcpy(it
->sha1
, sha1
);
403 hash_sha1_file(buffer
.buf
, buffer
.len
, tree_type
, it
->sha1
);
404 else if (write_sha1_file(buffer
.buf
, buffer
.len
, tree_type
, it
->sha1
)) {
405 strbuf_release(&buffer
);
409 strbuf_release(&buffer
);
410 it
->entry_count
= to_invalidate
? -1 : i
- *skip_count
;
412 fprintf(stderr
, "cache-tree update-one (%d ent, %d subtree) %s\n",
413 it
->entry_count
, it
->subtree_nr
,
414 sha1_to_hex(it
->sha1
));
419 int cache_tree_update(struct index_state
*istate
, int flags
)
421 struct cache_tree
*it
= istate
->cache_tree
;
422 struct cache_entry
**cache
= istate
->cache
;
423 int entries
= istate
->cache_nr
;
424 int skip
, i
= verify_cache(cache
, entries
, flags
);
428 i
= update_one(it
, cache
, entries
, "", 0, &skip
, flags
);
431 istate
->cache_changed
|= CACHE_TREE_CHANGED
;
435 static void write_one(struct strbuf
*buffer
, struct cache_tree
*it
,
436 const char *path
, int pathlen
)
440 /* One "cache-tree" entry consists of the following:
441 * path (NUL terminated)
442 * entry_count, subtree_nr ("%d %d\n")
443 * tree-sha1 (missing if invalid)
444 * subtree_nr "cache-tree" entries for subtrees.
446 strbuf_grow(buffer
, pathlen
+ 100);
447 strbuf_add(buffer
, path
, pathlen
);
448 strbuf_addf(buffer
, "%c%d %d\n", 0, it
->entry_count
, it
->subtree_nr
);
451 if (0 <= it
->entry_count
)
452 fprintf(stderr
, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
453 pathlen
, path
, it
->entry_count
, it
->subtree_nr
,
454 sha1_to_hex(it
->sha1
));
456 fprintf(stderr
, "cache-tree <%.*s> (%d subtree) invalid\n",
457 pathlen
, path
, it
->subtree_nr
);
460 if (0 <= it
->entry_count
) {
461 strbuf_add(buffer
, it
->sha1
, 20);
463 for (i
= 0; i
< it
->subtree_nr
; i
++) {
464 struct cache_tree_sub
*down
= it
->down
[i
];
466 struct cache_tree_sub
*prev
= it
->down
[i
-1];
467 if (subtree_name_cmp(down
->name
, down
->namelen
,
468 prev
->name
, prev
->namelen
) <= 0)
469 die("fatal - unsorted cache subtree");
471 write_one(buffer
, down
->cache_tree
, down
->name
, down
->namelen
);
475 void cache_tree_write(struct strbuf
*sb
, struct cache_tree
*root
)
477 write_one(sb
, root
, "", 0);
480 static struct cache_tree
*read_one(const char **buffer
, unsigned long *size_p
)
482 const char *buf
= *buffer
;
483 unsigned long size
= *size_p
;
486 struct cache_tree
*it
;
490 /* skip name, but make sure name exists */
491 while (size
&& *buf
) {
501 it
->entry_count
= strtol(cp
, &ep
, 10);
505 subtree_nr
= strtol(cp
, &ep
, 10);
508 while (size
&& *buf
&& *buf
!= '\n') {
515 if (0 <= it
->entry_count
) {
518 hashcpy(it
->sha1
, (const unsigned char*)buf
);
524 if (0 <= it
->entry_count
)
525 fprintf(stderr
, "cache-tree <%s> (%d ent, %d subtree) %s\n",
526 *buffer
, it
->entry_count
, subtree_nr
,
527 sha1_to_hex(it
->sha1
));
529 fprintf(stderr
, "cache-tree <%s> (%d subtrees) invalid\n",
530 *buffer
, subtree_nr
);
534 * Just a heuristic -- we do not add directories that often but
535 * we do not want to have to extend it immediately when we do,
538 it
->subtree_alloc
= subtree_nr
+ 2;
539 it
->down
= xcalloc(it
->subtree_alloc
, sizeof(struct cache_tree_sub
*));
540 for (i
= 0; i
< subtree_nr
; i
++) {
541 /* read each subtree */
542 struct cache_tree
*sub
;
543 struct cache_tree_sub
*subtree
;
544 const char *name
= buf
;
546 sub
= read_one(&buf
, &size
);
549 subtree
= cache_tree_sub(it
, name
);
550 subtree
->cache_tree
= sub
;
552 if (subtree_nr
!= it
->subtree_nr
)
553 die("cache-tree: internal error");
559 cache_tree_free(&it
);
563 struct cache_tree
*cache_tree_read(const char *buffer
, unsigned long size
)
566 return NULL
; /* not the whole tree */
567 return read_one(&buffer
, &size
);
570 static struct cache_tree
*cache_tree_find(struct cache_tree
*it
, const char *path
)
576 struct cache_tree_sub
*sub
;
578 slash
= strchrnul(path
, '/');
580 * Between path and slash is the name of the subtree
583 sub
= find_subtree(it
, path
, slash
- path
, 0);
586 it
= sub
->cache_tree
;
595 int write_index_as_tree(unsigned char *sha1
, struct index_state
*index_state
, const char *index_path
, int flags
, const char *prefix
)
597 int entries
, was_valid
, newfd
;
598 struct lock_file
*lock_file
;
601 * We can't free this memory, it becomes part of a linked list
604 lock_file
= xcalloc(1, sizeof(struct lock_file
));
606 newfd
= hold_lock_file_for_update(lock_file
, index_path
, LOCK_DIE_ON_ERROR
);
608 entries
= read_index_from(index_state
, index_path
);
610 return WRITE_TREE_UNREADABLE_INDEX
;
611 if (flags
& WRITE_TREE_IGNORE_CACHE_TREE
)
612 cache_tree_free(&index_state
->cache_tree
);
614 if (!index_state
->cache_tree
)
615 index_state
->cache_tree
= cache_tree();
617 was_valid
= cache_tree_fully_valid(index_state
->cache_tree
);
619 if (cache_tree_update(index_state
, flags
) < 0)
620 return WRITE_TREE_UNMERGED_INDEX
;
622 if (!write_locked_index(index_state
, lock_file
, COMMIT_LOCK
))
625 /* Not being able to write is fine -- we are only interested
626 * in updating the cache-tree part, and if the next caller
627 * ends up using the old index with unupdated cache-tree part
628 * it misses the work we did here, but that is just a
629 * performance penalty and not a big deal.
634 struct cache_tree
*subtree
;
635 subtree
= cache_tree_find(index_state
->cache_tree
, prefix
);
637 return WRITE_TREE_PREFIX_ERROR
;
638 hashcpy(sha1
, subtree
->sha1
);
641 hashcpy(sha1
, index_state
->cache_tree
->sha1
);
644 rollback_lock_file(lock_file
);
649 int write_cache_as_tree(unsigned char *sha1
, int flags
, const char *prefix
)
651 return write_index_as_tree(sha1
, &the_index
, get_index_file(), flags
, prefix
);
654 static void prime_cache_tree_rec(struct cache_tree
*it
, struct tree
*tree
)
656 struct tree_desc desc
;
657 struct name_entry entry
;
660 hashcpy(it
->sha1
, tree
->object
.oid
.hash
);
661 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
663 while (tree_entry(&desc
, &entry
)) {
664 if (!S_ISDIR(entry
.mode
))
667 struct cache_tree_sub
*sub
;
668 struct tree
*subtree
= lookup_tree(entry
.sha1
);
669 if (!subtree
->object
.parsed
)
671 sub
= cache_tree_sub(it
, entry
.path
);
672 sub
->cache_tree
= cache_tree();
673 prime_cache_tree_rec(sub
->cache_tree
, subtree
);
674 cnt
+= sub
->cache_tree
->entry_count
;
677 it
->entry_count
= cnt
;
680 void prime_cache_tree(struct index_state
*istate
, struct tree
*tree
)
682 cache_tree_free(&istate
->cache_tree
);
683 istate
->cache_tree
= cache_tree();
684 prime_cache_tree_rec(istate
->cache_tree
, tree
);
685 istate
->cache_changed
|= CACHE_TREE_CHANGED
;
689 * find the cache_tree that corresponds to the current level without
690 * exploding the full path into textual form. The root of the
691 * cache tree is given as "root", and our current level is "info".
692 * (1) When at root level, info->prev is NULL, so it is "root" itself.
693 * (2) Otherwise, find the cache_tree that corresponds to one level
694 * above us, and find ourselves in there.
696 static struct cache_tree
*find_cache_tree_from_traversal(struct cache_tree
*root
,
697 struct traverse_info
*info
)
699 struct cache_tree
*our_parent
;
703 our_parent
= find_cache_tree_from_traversal(root
, info
->prev
);
704 return cache_tree_find(our_parent
, info
->name
.path
);
707 int cache_tree_matches_traversal(struct cache_tree
*root
,
708 struct name_entry
*ent
,
709 struct traverse_info
*info
)
711 struct cache_tree
*it
;
713 it
= find_cache_tree_from_traversal(root
, info
);
714 it
= cache_tree_find(it
, ent
->path
);
715 if (it
&& it
->entry_count
> 0 && !hashcmp(ent
->sha1
, it
->sha1
))
716 return it
->entry_count
;
720 int update_main_cache_tree(int flags
)
722 if (!the_index
.cache_tree
)
723 the_index
.cache_tree
= cache_tree();
724 return cache_tree_update(&the_index
, flags
);