]> git.ipfire.org Git - thirdparty/git.git/blame - cache-tree.c
builtin/reflog: convert tree_is_complete to take struct object_id
[thirdparty/git.git] / cache-tree.c
CommitLineData
74986462 1#include "cache.h"
697cc8ef 2#include "lockfile.h"
74986462 3#include "tree.h"
b9d37a54 4#include "tree-walk.h"
74986462
JH
5#include "cache-tree.h"
6
4903161f 7#ifndef DEBUG
74986462 8#define DEBUG 0
4903161f 9#endif
74986462
JH
10
11struct cache_tree *cache_tree(void)
12{
13 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
14 it->entry_count = -1;
15 return it;
16}
17
bad68ec9 18void cache_tree_free(struct cache_tree **it_p)
74986462
JH
19{
20 int i;
bad68ec9 21 struct cache_tree *it = *it_p;
74986462
JH
22
23 if (!it)
24 return;
25 for (i = 0; i < it->subtree_nr; i++)
e92fa514 26 if (it->down[i]) {
61fa3097 27 cache_tree_free(&it->down[i]->cache_tree);
e92fa514
EN
28 free(it->down[i]);
29 }
74986462
JH
30 free(it->down);
31 free(it);
bad68ec9 32 *it_p = NULL;
74986462
JH
33}
34
61fa3097
JH
35static int subtree_name_cmp(const char *one, int onelen,
36 const char *two, int twolen)
37{
38 if (onelen < twolen)
39 return -1;
40 if (twolen < onelen)
41 return 1;
42 return memcmp(one, two, onelen);
43}
44
45static int subtree_pos(struct cache_tree *it, const char *path, int pathlen)
46{
47 struct cache_tree_sub **down = it->down;
48 int lo, hi;
49 lo = 0;
50 hi = it->subtree_nr;
51 while (lo < hi) {
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);
56 if (!cmp)
57 return mi;
58 if (cmp < 0)
59 hi = mi;
60 else
61 lo = mi + 1;
62 }
63 return -lo-1;
64}
65
74986462
JH
66static struct cache_tree_sub *find_subtree(struct cache_tree *it,
67 const char *path,
68 int pathlen,
69 int create)
70{
74986462 71 struct cache_tree_sub *down;
61fa3097
JH
72 int pos = subtree_pos(it, path, pathlen);
73 if (0 <= pos)
74 return it->down[pos];
74986462
JH
75 if (!create)
76 return NULL;
61fa3097
JH
77
78 pos = -pos-1;
bcc7a032 79 ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
61fa3097
JH
80 it->subtree_nr++;
81
96ffc06f 82 FLEX_ALLOC_MEM(down, name, path, pathlen);
61fa3097 83 down->cache_tree = NULL;
74986462 84 down->namelen = pathlen;
61fa3097
JH
85
86 if (pos < it->subtree_nr)
87 memmove(it->down + pos + 1,
88 it->down + pos,
89 sizeof(down) * (it->subtree_nr - pos - 1));
90 it->down[pos] = down;
74986462
JH
91 return down;
92}
93
7927a55d
JH
94struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
95{
96 int pathlen = strlen(path);
97 return find_subtree(it, path, pathlen, 1);
98}
99
a5400efe 100static int do_invalidate_path(struct cache_tree *it, const char *path)
74986462
JH
101{
102 /* a/b/c
103 * ==> invalidate self
104 * ==> find "a", have it invalidate "b/c"
105 * a
106 * ==> invalidate self
107 * ==> if "a" exists as a subtree, remove it.
108 */
109 const char *slash;
110 int namelen;
111 struct cache_tree_sub *down;
112
00703e6d
JH
113#if DEBUG
114 fprintf(stderr, "cache-tree invalidate <%s>\n", path);
115#endif
116
74986462 117 if (!it)
a5400efe 118 return 0;
2c5495f7
RM
119 slash = strchrnul(path, '/');
120 namelen = slash - path;
74986462 121 it->entry_count = -1;
2c5495f7 122 if (!*slash) {
61fa3097 123 int pos;
61fa3097
JH
124 pos = subtree_pos(it, path, namelen);
125 if (0 <= pos) {
126 cache_tree_free(&it->down[pos]->cache_tree);
127 free(it->down[pos]);
74986462
JH
128 /* 0 1 2 3 4 5
129 * ^ ^subtree_nr = 6
61fa3097 130 * pos
74986462 131 * move 4 and 5 up one place (2 entries)
61fa3097 132 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
74986462 133 */
61fa3097 134 memmove(it->down+pos, it->down+pos+1,
74986462 135 sizeof(struct cache_tree_sub *) *
61fa3097 136 (it->subtree_nr - pos - 1));
74986462
JH
137 it->subtree_nr--;
138 }
a5400efe 139 return 1;
74986462 140 }
74986462
JH
141 down = find_subtree(it, path, namelen, 0);
142 if (down)
a5400efe
NTND
143 do_invalidate_path(down->cache_tree, slash + 1);
144 return 1;
145}
146
147void cache_tree_invalidate_path(struct index_state *istate, const char *path)
148{
149 if (do_invalidate_path(istate->cache_tree, path))
150 istate->cache_changed |= CACHE_TREE_CHANGED;
74986462
JH
151}
152
d0cfc3e8 153static int verify_cache(struct cache_entry **cache,
e859c69b 154 int entries, int flags)
74986462
JH
155{
156 int i, funny;
e859c69b 157 int silent = flags & WRITE_TREE_SILENT;
74986462
JH
158
159 /* Verify that the tree is merged */
160 funny = 0;
161 for (i = 0; i < entries; i++) {
9c5e6c80 162 const struct cache_entry *ce = cache[i];
3f6d56de 163 if (ce_stage(ce)) {
996277c5
TR
164 if (silent)
165 return -1;
74986462
JH
166 if (10 < ++funny) {
167 fprintf(stderr, "...\n");
168 break;
169 }
dbc3904e 170 fprintf(stderr, "%s: unmerged (%s)\n",
99d1a986 171 ce->name, oid_to_hex(&ce->oid));
74986462
JH
172 }
173 }
174 if (funny)
175 return -1;
176
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
179 * stage 0 entries.
180 */
181 funny = 0;
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.
186 */
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] == '/') {
193 if (10 < ++funny) {
194 fprintf(stderr, "...\n");
195 break;
196 }
197 fprintf(stderr, "You have both %s and %s\n",
198 this_name, next_name);
199 }
200 }
201 if (funny)
202 return -1;
203 return 0;
204}
205
206static void discard_unused_subtrees(struct cache_tree *it)
207{
208 struct cache_tree_sub **down = it->down;
209 int nr = it->subtree_nr;
210 int dst, src;
211 for (dst = src = 0; src < nr; src++) {
212 struct cache_tree_sub *s = down[src];
213 if (s->used)
214 down[dst++] = s;
215 else {
bad68ec9 216 cache_tree_free(&s->cache_tree);
74986462
JH
217 free(s);
218 it->subtree_nr--;
219 }
220 }
221}
222
bad68ec9
JH
223int cache_tree_fully_valid(struct cache_tree *it)
224{
225 int i;
226 if (!it)
227 return 0;
e0a92804 228 if (it->entry_count < 0 || !has_sha1_file(it->oid.hash))
bad68ec9
JH
229 return 0;
230 for (i = 0; i < it->subtree_nr; i++) {
231 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
232 return 0;
233 }
234 return 1;
235}
236
74986462 237static int update_one(struct cache_tree *it,
d0cfc3e8 238 struct cache_entry **cache,
74986462
JH
239 int entries,
240 const char *base,
241 int baselen,
3cf773e4 242 int *skip_count,
e859c69b 243 int flags)
74986462 244{
5242bcbb 245 struct strbuf buffer;
e859c69b
NTND
246 int missing_ok = flags & WRITE_TREE_MISSING_OK;
247 int dryrun = flags & WRITE_TREE_DRY_RUN;
aecf567c 248 int repair = flags & WRITE_TREE_REPAIR;
eec3e7e4 249 int to_invalidate = 0;
74986462
JH
250 int i;
251
aecf567c
DT
252 assert(!(dryrun && repair));
253
3cf773e4
NTND
254 *skip_count = 0;
255
e0a92804 256 if (0 <= it->entry_count && has_sha1_file(it->oid.hash))
74986462
JH
257 return it->entry_count;
258
259 /*
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.
263 */
264 for (i = 0; i < it->subtree_nr; i++)
265 it->down[i]->used = 0;
266
267 /*
268 * Find the subtrees and update them.
269 */
386cc8b0
NTND
270 i = 0;
271 while (i < entries) {
9c5e6c80 272 const struct cache_entry *ce = cache[i];
74986462
JH
273 struct cache_tree_sub *sub;
274 const char *path, *slash;
3cf773e4 275 int pathlen, sublen, subcnt, subskip;
74986462
JH
276
277 path = ce->name;
278 pathlen = ce_namelen(ce);
279 if (pathlen <= baselen || memcmp(base, path, baselen))
280 break; /* at the end of this level */
281
282 slash = strchr(path + baselen, '/');
386cc8b0
NTND
283 if (!slash) {
284 i++;
74986462 285 continue;
386cc8b0 286 }
74986462
JH
287 /*
288 * a/bbb/c (base = a/, slash = /c)
289 * ==>
290 * path+baselen = bbb/c, sublen = 3
291 */
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,
298 path,
299 baselen + sublen + 1,
3cf773e4 300 &subskip,
e859c69b 301 flags);
3d12d0cf
JS
302 if (subcnt < 0)
303 return subcnt;
729dbbd9
JK
304 if (!subcnt)
305 die("index cache-tree records empty sub-tree");
386cc8b0 306 i += subcnt;
3cf773e4
NTND
307 sub->count = subcnt; /* to be used in the next loop */
308 *skip_count += subskip;
74986462
JH
309 sub->used = 1;
310 }
311
312 discard_unused_subtrees(it);
313
314 /*
315 * Then write out the tree object for this level.
316 */
f1696ee3 317 strbuf_init(&buffer, 8192);
74986462 318
386cc8b0
NTND
319 i = 0;
320 while (i < entries) {
9c5e6c80 321 const struct cache_entry *ce = cache[i];
c041d54a 322 struct cache_tree_sub *sub = NULL;
74986462
JH
323 const char *path, *slash;
324 int pathlen, entlen;
325 const unsigned char *sha1;
326 unsigned mode;
4ed115e9 327 int expected_missing = 0;
6d6a782f 328 int contains_ita = 0;
74986462
JH
329
330 path = ce->name;
331 pathlen = ce_namelen(ce);
332 if (pathlen <= baselen || memcmp(base, path, baselen))
333 break; /* at the end of this level */
334
335 slash = strchr(path + baselen, '/');
336 if (slash) {
337 entlen = slash - (path + baselen);
338 sub = find_subtree(it, path + baselen, entlen, 0);
339 if (!sub)
340 die("cache-tree.c: '%.*s' in '%s' not found",
341 entlen, path + baselen, path);
3cf773e4 342 i += sub->count;
e0a92804 343 sha1 = sub->cache_tree->oid.hash;
74986462 344 mode = S_IFDIR;
6d6a782f
NTND
345 contains_ita = sub->cache_tree->entry_count < 0;
346 if (contains_ita) {
eec3e7e4 347 to_invalidate = 1;
4ed115e9
JH
348 expected_missing = 1;
349 }
74986462
JH
350 }
351 else {
99d1a986 352 sha1 = ce->oid.hash;
7a51ed66 353 mode = ce->ce_mode;
74986462 354 entlen = pathlen - baselen;
386cc8b0 355 i++;
74986462 356 }
b6b56ace
JN
357 if (mode != S_IFGITLINK && !missing_ok && !has_sha1_file(sha1)) {
358 strbuf_release(&buffer);
4ed115e9
JH
359 if (expected_missing)
360 return -1;
a3883734
LT
361 return error("invalid object %06o %s for '%.*s'",
362 mode, sha1_to_hex(sha1), entlen+baselen, path);
b6b56ace 363 }
74986462 364
3cf773e4
NTND
365 /*
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.
369 */
370 if (ce->ce_flags & CE_REMOVE) {
371 *skip_count = *skip_count + 1;
372 continue;
373 }
374
eec3e7e4
NTND
375 /*
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.
379 */
c041d54a 380 if (!sub && ce_intent_to_add(ce)) {
eec3e7e4 381 to_invalidate = 1;
3cf773e4 382 continue;
eec3e7e4 383 }
74986462 384
6d6a782f
NTND
385 /*
386 * "sub" can be an empty tree if all subentries are i-t-a.
387 */
388 if (contains_ita && !hashcmp(sha1, EMPTY_TREE_SHA1_BIN))
389 continue;
390
5242bcbb
PH
391 strbuf_grow(&buffer, entlen + 100);
392 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
393 strbuf_add(&buffer, sha1, 20);
74986462
JH
394
395#if DEBUG
00703e6d 396 fprintf(stderr, "cache-tree update-one %o %.*s\n",
74986462
JH
397 mode, entlen, path + baselen);
398#endif
399 }
400
aecf567c
DT
401 if (repair) {
402 unsigned char sha1[20];
403 hash_sha1_file(buffer.buf, buffer.len, tree_type, sha1);
404 if (has_sha1_file(sha1))
e0a92804 405 hashcpy(it->oid.hash, sha1);
aecf567c
DT
406 else
407 to_invalidate = 1;
408 } else if (dryrun)
e0a92804 409 hash_sha1_file(buffer.buf, buffer.len, tree_type,
410 it->oid.hash);
411 else if (write_sha1_file(buffer.buf, buffer.len, tree_type, it->oid.hash)) {
edae5f0c
JH
412 strbuf_release(&buffer);
413 return -1;
414 }
415
5242bcbb 416 strbuf_release(&buffer);
eec3e7e4 417 it->entry_count = to_invalidate ? -1 : i - *skip_count;
74986462 418#if DEBUG
00703e6d 419 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
74986462 420 it->entry_count, it->subtree_nr,
e0a92804 421 oid_to_hex(&it->oid));
74986462
JH
422#endif
423 return i;
424}
425
d0cfc3e8 426int cache_tree_update(struct index_state *istate, int flags)
74986462 427{
d0cfc3e8
NTND
428 struct cache_tree *it = istate->cache_tree;
429 struct cache_entry **cache = istate->cache;
430 int entries = istate->cache_nr;
431 int skip, i = verify_cache(cache, entries, flags);
432
74986462
JH
433 if (i)
434 return i;
3cf773e4 435 i = update_one(it, cache, entries, "", 0, &skip, flags);
74986462
JH
436 if (i < 0)
437 return i;
d0cfc3e8 438 istate->cache_changed |= CACHE_TREE_CHANGED;
74986462
JH
439 return 0;
440}
441
1dffb8fa
PH
442static void write_one(struct strbuf *buffer, struct cache_tree *it,
443 const char *path, int pathlen)
74986462
JH
444{
445 int i;
446
447 /* One "cache-tree" entry consists of the following:
448 * path (NUL terminated)
449 * entry_count, subtree_nr ("%d %d\n")
450 * tree-sha1 (missing if invalid)
451 * subtree_nr "cache-tree" entries for subtrees.
452 */
5242bcbb
PH
453 strbuf_grow(buffer, pathlen + 100);
454 strbuf_add(buffer, path, pathlen);
455 strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
74986462
JH
456
457#if DEBUG
458 if (0 <= it->entry_count)
459 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
460 pathlen, path, it->entry_count, it->subtree_nr,
e0a92804 461 oid_to_hex(&it->oid));
74986462
JH
462 else
463 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
464 pathlen, path, it->subtree_nr);
465#endif
466
467 if (0 <= it->entry_count) {
e0a92804 468 strbuf_add(buffer, it->oid.hash, 20);
74986462
JH
469 }
470 for (i = 0; i < it->subtree_nr; i++) {
471 struct cache_tree_sub *down = it->down[i];
61fa3097
JH
472 if (i) {
473 struct cache_tree_sub *prev = it->down[i-1];
474 if (subtree_name_cmp(down->name, down->namelen,
475 prev->name, prev->namelen) <= 0)
476 die("fatal - unsorted cache subtree");
477 }
1dffb8fa 478 write_one(buffer, down->cache_tree, down->name, down->namelen);
74986462 479 }
74986462
JH
480}
481
1dffb8fa 482void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
74986462 483{
1dffb8fa 484 write_one(sb, root, "", 0);
74986462
JH
485}
486
487static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
488{
489 const char *buf = *buffer;
490 unsigned long size = *size_p;
0111ea38
JS
491 const char *cp;
492 char *ep;
74986462
JH
493 struct cache_tree *it;
494 int i, subtree_nr;
495
496 it = NULL;
497 /* skip name, but make sure name exists */
498 while (size && *buf) {
499 size--;
500 buf++;
501 }
502 if (!size)
503 goto free_return;
504 buf++; size--;
505 it = cache_tree();
0111ea38
JS
506
507 cp = buf;
508 it->entry_count = strtol(cp, &ep, 10);
509 if (cp == ep)
510 goto free_return;
511 cp = ep;
512 subtree_nr = strtol(cp, &ep, 10);
513 if (cp == ep)
74986462
JH
514 goto free_return;
515 while (size && *buf && *buf != '\n') {
516 size--;
517 buf++;
518 }
519 if (!size)
520 goto free_return;
521 buf++; size--;
522 if (0 <= it->entry_count) {
523 if (size < 20)
524 goto free_return;
e0a92804 525 hashcpy(it->oid.hash, (const unsigned char*)buf);
74986462
JH
526 buf += 20;
527 size -= 20;
528 }
529
530#if DEBUG
531 if (0 <= it->entry_count)
532 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
533 *buffer, it->entry_count, subtree_nr,
e0a92804 534 oid_to_hex(&it->oid));
74986462
JH
535 else
536 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
537 *buffer, subtree_nr);
538#endif
539
540 /*
541 * Just a heuristic -- we do not add directories that often but
542 * we do not want to have to extend it immediately when we do,
543 * hence +2.
544 */
545 it->subtree_alloc = subtree_nr + 2;
546 it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
547 for (i = 0; i < subtree_nr; i++) {
548 /* read each subtree */
549 struct cache_tree *sub;
61fa3097 550 struct cache_tree_sub *subtree;
74986462 551 const char *name = buf;
7927a55d 552
74986462
JH
553 sub = read_one(&buf, &size);
554 if (!sub)
555 goto free_return;
7927a55d 556 subtree = cache_tree_sub(it, name);
61fa3097 557 subtree->cache_tree = sub;
74986462
JH
558 }
559 if (subtree_nr != it->subtree_nr)
560 die("cache-tree: internal error");
561 *buffer = buf;
562 *size_p = size;
563 return it;
564
565 free_return:
bad68ec9 566 cache_tree_free(&it);
74986462
JH
567 return NULL;
568}
569
bad68ec9 570struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
74986462 571{
bad68ec9 572 if (buffer[0])
74986462 573 return NULL; /* not the whole tree */
74986462
JH
574 return read_one(&buffer, &size);
575}
6bd20358 576
7ba04d9f 577static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
6bd20358 578{
b87fc964
JH
579 if (!it)
580 return NULL;
6bd20358
JH
581 while (*path) {
582 const char *slash;
583 struct cache_tree_sub *sub;
584
17e22ddc 585 slash = strchrnul(path, '/');
79192b87
MH
586 /*
587 * Between path and slash is the name of the subtree
588 * to look for.
6bd20358
JH
589 */
590 sub = find_subtree(it, path, slash - path, 0);
591 if (!sub)
592 return NULL;
593 it = sub->cache_tree;
3491047e 594
6bd20358 595 path = slash;
3491047e
MH
596 while (*path == '/')
597 path++;
6bd20358
JH
598 }
599 return it;
600}
45525bd0 601
d23a5117 602int write_index_as_tree(unsigned char *sha1, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
45525bd0
JH
603{
604 int entries, was_valid, newfd;
d11b8d34 605 struct lock_file *lock_file;
45525bd0
JH
606
607 /*
608 * We can't free this memory, it becomes part of a linked list
609 * parsed atexit()
610 */
d11b8d34 611 lock_file = xcalloc(1, sizeof(struct lock_file));
45525bd0 612
d23a5117 613 newfd = hold_lock_file_for_update(lock_file, index_path, LOCK_DIE_ON_ERROR);
45525bd0 614
d23a5117 615 entries = read_index_from(index_state, index_path);
45525bd0
JH
616 if (entries < 0)
617 return WRITE_TREE_UNREADABLE_INDEX;
d11b8d34 618 if (flags & WRITE_TREE_IGNORE_CACHE_TREE)
d23a5117 619 cache_tree_free(&index_state->cache_tree);
45525bd0 620
d23a5117
PT
621 if (!index_state->cache_tree)
622 index_state->cache_tree = cache_tree();
45525bd0 623
d23a5117 624 was_valid = cache_tree_fully_valid(index_state->cache_tree);
45525bd0 625 if (!was_valid) {
d23a5117 626 if (cache_tree_update(index_state, flags) < 0)
45525bd0
JH
627 return WRITE_TREE_UNMERGED_INDEX;
628 if (0 <= newfd) {
d23a5117 629 if (!write_locked_index(index_state, lock_file, COMMIT_LOCK))
45525bd0
JH
630 newfd = -1;
631 }
632 /* Not being able to write is fine -- we are only interested
633 * in updating the cache-tree part, and if the next caller
634 * ends up using the old index with unupdated cache-tree part
635 * it misses the work we did here, but that is just a
636 * performance penalty and not a big deal.
637 */
638 }
639
640 if (prefix) {
d23a5117
PT
641 struct cache_tree *subtree;
642 subtree = cache_tree_find(index_state->cache_tree, prefix);
45525bd0
JH
643 if (!subtree)
644 return WRITE_TREE_PREFIX_ERROR;
e0a92804 645 hashcpy(sha1, subtree->oid.hash);
45525bd0
JH
646 }
647 else
e0a92804 648 hashcpy(sha1, index_state->cache_tree->oid.hash);
45525bd0
JH
649
650 if (0 <= newfd)
651 rollback_lock_file(lock_file);
652
653 return 0;
654}
b9d37a54 655
d23a5117
PT
656int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
657{
658 return write_index_as_tree(sha1, &the_index, get_index_file(), flags, prefix);
659}
660
b9d37a54
JH
661static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
662{
663 struct tree_desc desc;
664 struct name_entry entry;
665 int cnt;
666
e0a92804 667 oidcpy(&it->oid, &tree->object.oid);
b9d37a54
JH
668 init_tree_desc(&desc, tree->buffer, tree->size);
669 cnt = 0;
670 while (tree_entry(&desc, &entry)) {
671 if (!S_ISDIR(entry.mode))
672 cnt++;
673 else {
674 struct cache_tree_sub *sub;
7d924c91 675 struct tree *subtree = lookup_tree(entry.oid->hash);
b9d37a54
JH
676 if (!subtree->object.parsed)
677 parse_tree(subtree);
678 sub = cache_tree_sub(it, entry.path);
679 sub->cache_tree = cache_tree();
680 prime_cache_tree_rec(sub->cache_tree, subtree);
681 cnt += sub->cache_tree->entry_count;
682 }
683 }
684 it->entry_count = cnt;
685}
686
e6c286e8 687void prime_cache_tree(struct index_state *istate, struct tree *tree)
b9d37a54 688{
e6c286e8
NTND
689 cache_tree_free(&istate->cache_tree);
690 istate->cache_tree = cache_tree();
691 prime_cache_tree_rec(istate->cache_tree, tree);
692 istate->cache_changed |= CACHE_TREE_CHANGED;
b9d37a54 693}
b65982b6
JH
694
695/*
696 * find the cache_tree that corresponds to the current level without
697 * exploding the full path into textual form. The root of the
698 * cache tree is given as "root", and our current level is "info".
699 * (1) When at root level, info->prev is NULL, so it is "root" itself.
700 * (2) Otherwise, find the cache_tree that corresponds to one level
701 * above us, and find ourselves in there.
702 */
703static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
704 struct traverse_info *info)
705{
706 struct cache_tree *our_parent;
707
708 if (!info->prev)
709 return root;
710 our_parent = find_cache_tree_from_traversal(root, info->prev);
711 return cache_tree_find(our_parent, info->name.path);
712}
713
714int cache_tree_matches_traversal(struct cache_tree *root,
715 struct name_entry *ent,
716 struct traverse_info *info)
717{
718 struct cache_tree *it;
719
720 it = find_cache_tree_from_traversal(root, info);
721 it = cache_tree_find(it, ent->path);
e0a92804 722 if (it && it->entry_count > 0 && !oidcmp(ent->oid, &it->oid))
b65982b6
JH
723 return it->entry_count;
724 return 0;
725}
996277c5 726
e859c69b 727int update_main_cache_tree(int flags)
996277c5
TR
728{
729 if (!the_index.cache_tree)
730 the_index.cache_tree = cache_tree();
d0cfc3e8 731 return cache_tree_update(&the_index, flags);
996277c5 732}