]> git.ipfire.org Git - thirdparty/git.git/blame - cache-tree.c
trace.h: support nested performance tracing
[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 5#include "cache-tree.h"
cbd53a21 6#include "object-store.h"
74986462 7
4903161f 8#ifndef DEBUG
74986462 9#define DEBUG 0
4903161f 10#endif
74986462
JH
11
12struct cache_tree *cache_tree(void)
13{
14 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
15 it->entry_count = -1;
16 return it;
17}
18
bad68ec9 19void cache_tree_free(struct cache_tree **it_p)
74986462
JH
20{
21 int i;
bad68ec9 22 struct cache_tree *it = *it_p;
74986462
JH
23
24 if (!it)
25 return;
26 for (i = 0; i < it->subtree_nr; i++)
e92fa514 27 if (it->down[i]) {
61fa3097 28 cache_tree_free(&it->down[i]->cache_tree);
e92fa514
EN
29 free(it->down[i]);
30 }
74986462
JH
31 free(it->down);
32 free(it);
bad68ec9 33 *it_p = NULL;
74986462
JH
34}
35
61fa3097
JH
36static int subtree_name_cmp(const char *one, int onelen,
37 const char *two, int twolen)
38{
39 if (onelen < twolen)
40 return -1;
41 if (twolen < onelen)
42 return 1;
43 return memcmp(one, two, onelen);
44}
45
46static int subtree_pos(struct cache_tree *it, const char *path, int pathlen)
47{
48 struct cache_tree_sub **down = it->down;
49 int lo, hi;
50 lo = 0;
51 hi = it->subtree_nr;
52 while (lo < hi) {
19716b21 53 int mi = lo + (hi - lo) / 2;
61fa3097
JH
54 struct cache_tree_sub *mdl = down[mi];
55 int cmp = subtree_name_cmp(path, pathlen,
56 mdl->name, mdl->namelen);
57 if (!cmp)
58 return mi;
59 if (cmp < 0)
60 hi = mi;
61 else
62 lo = mi + 1;
63 }
64 return -lo-1;
65}
66
74986462
JH
67static struct cache_tree_sub *find_subtree(struct cache_tree *it,
68 const char *path,
69 int pathlen,
70 int create)
71{
74986462 72 struct cache_tree_sub *down;
61fa3097
JH
73 int pos = subtree_pos(it, path, pathlen);
74 if (0 <= pos)
75 return it->down[pos];
74986462
JH
76 if (!create)
77 return NULL;
61fa3097
JH
78
79 pos = -pos-1;
bcc7a032 80 ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
61fa3097
JH
81 it->subtree_nr++;
82
96ffc06f 83 FLEX_ALLOC_MEM(down, name, path, pathlen);
61fa3097 84 down->cache_tree = NULL;
74986462 85 down->namelen = pathlen;
61fa3097
JH
86
87 if (pos < it->subtree_nr)
f919ffeb
SG
88 MOVE_ARRAY(it->down + pos + 1, it->down + pos,
89 it->subtree_nr - pos - 1);
61fa3097 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 */
f331ab9d
RS
134 MOVE_ARRAY(it->down + pos, it->down + pos + 1,
135 it->subtree_nr - pos - 1);
74986462
JH
136 it->subtree_nr--;
137 }
a5400efe 138 return 1;
74986462 139 }
74986462
JH
140 down = find_subtree(it, path, namelen, 0);
141 if (down)
a5400efe
NTND
142 do_invalidate_path(down->cache_tree, slash + 1);
143 return 1;
144}
145
146void cache_tree_invalidate_path(struct index_state *istate, const char *path)
147{
148 if (do_invalidate_path(istate->cache_tree, path))
149 istate->cache_changed |= CACHE_TREE_CHANGED;
74986462
JH
150}
151
d0cfc3e8 152static int verify_cache(struct cache_entry **cache,
e859c69b 153 int entries, int flags)
74986462
JH
154{
155 int i, funny;
e859c69b 156 int silent = flags & WRITE_TREE_SILENT;
74986462
JH
157
158 /* Verify that the tree is merged */
159 funny = 0;
160 for (i = 0; i < entries; i++) {
9c5e6c80 161 const struct cache_entry *ce = cache[i];
3f6d56de 162 if (ce_stage(ce)) {
996277c5
TR
163 if (silent)
164 return -1;
74986462
JH
165 if (10 < ++funny) {
166 fprintf(stderr, "...\n");
167 break;
168 }
dbc3904e 169 fprintf(stderr, "%s: unmerged (%s)\n",
99d1a986 170 ce->name, oid_to_hex(&ce->oid));
74986462
JH
171 }
172 }
173 if (funny)
174 return -1;
175
176 /* Also verify that the cache does not have path and path/file
177 * at the same time. At this point we know the cache has only
178 * stage 0 entries.
179 */
180 funny = 0;
181 for (i = 0; i < entries - 1; i++) {
182 /* path/file always comes after path because of the way
183 * the cache is sorted. Also path can appear only once,
184 * which means conflicting one would immediately follow.
185 */
186 const char *this_name = cache[i]->name;
187 const char *next_name = cache[i+1]->name;
188 int this_len = strlen(this_name);
189 if (this_len < strlen(next_name) &&
190 strncmp(this_name, next_name, this_len) == 0 &&
191 next_name[this_len] == '/') {
192 if (10 < ++funny) {
193 fprintf(stderr, "...\n");
194 break;
195 }
196 fprintf(stderr, "You have both %s and %s\n",
197 this_name, next_name);
198 }
199 }
200 if (funny)
201 return -1;
202 return 0;
203}
204
205static void discard_unused_subtrees(struct cache_tree *it)
206{
207 struct cache_tree_sub **down = it->down;
208 int nr = it->subtree_nr;
209 int dst, src;
210 for (dst = src = 0; src < nr; src++) {
211 struct cache_tree_sub *s = down[src];
212 if (s->used)
213 down[dst++] = s;
214 else {
bad68ec9 215 cache_tree_free(&s->cache_tree);
74986462
JH
216 free(s);
217 it->subtree_nr--;
218 }
219 }
220}
221
bad68ec9
JH
222int cache_tree_fully_valid(struct cache_tree *it)
223{
224 int i;
225 if (!it)
226 return 0;
e0a92804 227 if (it->entry_count < 0 || !has_sha1_file(it->oid.hash))
bad68ec9
JH
228 return 0;
229 for (i = 0; i < it->subtree_nr; i++) {
230 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
231 return 0;
232 }
233 return 1;
234}
235
74986462 236static int update_one(struct cache_tree *it,
d0cfc3e8 237 struct cache_entry **cache,
74986462
JH
238 int entries,
239 const char *base,
240 int baselen,
3cf773e4 241 int *skip_count,
e859c69b 242 int flags)
74986462 243{
5242bcbb 244 struct strbuf buffer;
e859c69b
NTND
245 int missing_ok = flags & WRITE_TREE_MISSING_OK;
246 int dryrun = flags & WRITE_TREE_DRY_RUN;
aecf567c 247 int repair = flags & WRITE_TREE_REPAIR;
eec3e7e4 248 int to_invalidate = 0;
74986462
JH
249 int i;
250
aecf567c
DT
251 assert(!(dryrun && repair));
252
3cf773e4
NTND
253 *skip_count = 0;
254
e0a92804 255 if (0 <= it->entry_count && has_sha1_file(it->oid.hash))
74986462
JH
256 return it->entry_count;
257
258 /*
259 * We first scan for subtrees and update them; we start by
260 * marking existing subtrees -- the ones that are unmarked
261 * should not be in the result.
262 */
263 for (i = 0; i < it->subtree_nr; i++)
264 it->down[i]->used = 0;
265
266 /*
267 * Find the subtrees and update them.
268 */
386cc8b0
NTND
269 i = 0;
270 while (i < entries) {
9c5e6c80 271 const struct cache_entry *ce = cache[i];
74986462
JH
272 struct cache_tree_sub *sub;
273 const char *path, *slash;
3cf773e4 274 int pathlen, sublen, subcnt, subskip;
74986462
JH
275
276 path = ce->name;
277 pathlen = ce_namelen(ce);
278 if (pathlen <= baselen || memcmp(base, path, baselen))
279 break; /* at the end of this level */
280
281 slash = strchr(path + baselen, '/');
386cc8b0
NTND
282 if (!slash) {
283 i++;
74986462 284 continue;
386cc8b0 285 }
74986462
JH
286 /*
287 * a/bbb/c (base = a/, slash = /c)
288 * ==>
289 * path+baselen = bbb/c, sublen = 3
290 */
291 sublen = slash - (path + baselen);
292 sub = find_subtree(it, path + baselen, sublen, 1);
293 if (!sub->cache_tree)
294 sub->cache_tree = cache_tree();
295 subcnt = update_one(sub->cache_tree,
296 cache + i, entries - i,
297 path,
298 baselen + sublen + 1,
3cf773e4 299 &subskip,
e859c69b 300 flags);
3d12d0cf
JS
301 if (subcnt < 0)
302 return subcnt;
729dbbd9
JK
303 if (!subcnt)
304 die("index cache-tree records empty sub-tree");
386cc8b0 305 i += subcnt;
3cf773e4
NTND
306 sub->count = subcnt; /* to be used in the next loop */
307 *skip_count += subskip;
74986462
JH
308 sub->used = 1;
309 }
310
311 discard_unused_subtrees(it);
312
313 /*
314 * Then write out the tree object for this level.
315 */
f1696ee3 316 strbuf_init(&buffer, 8192);
74986462 317
386cc8b0
NTND
318 i = 0;
319 while (i < entries) {
9c5e6c80 320 const struct cache_entry *ce = cache[i];
c041d54a 321 struct cache_tree_sub *sub = NULL;
74986462
JH
322 const char *path, *slash;
323 int pathlen, entlen;
6dcb4625 324 const struct object_id *oid;
74986462 325 unsigned mode;
4ed115e9 326 int expected_missing = 0;
6d6a782f 327 int contains_ita = 0;
74986462
JH
328
329 path = ce->name;
330 pathlen = ce_namelen(ce);
331 if (pathlen <= baselen || memcmp(base, path, baselen))
332 break; /* at the end of this level */
333
334 slash = strchr(path + baselen, '/');
335 if (slash) {
336 entlen = slash - (path + baselen);
337 sub = find_subtree(it, path + baselen, entlen, 0);
338 if (!sub)
339 die("cache-tree.c: '%.*s' in '%s' not found",
340 entlen, path + baselen, path);
3cf773e4 341 i += sub->count;
6dcb4625 342 oid = &sub->cache_tree->oid;
74986462 343 mode = S_IFDIR;
6d6a782f
NTND
344 contains_ita = sub->cache_tree->entry_count < 0;
345 if (contains_ita) {
eec3e7e4 346 to_invalidate = 1;
4ed115e9
JH
347 expected_missing = 1;
348 }
74986462
JH
349 }
350 else {
6dcb4625 351 oid = &ce->oid;
7a51ed66 352 mode = ce->ce_mode;
74986462 353 entlen = pathlen - baselen;
386cc8b0 354 i++;
74986462 355 }
a96d3cc3 356
6dcb4625 357 if (is_null_oid(oid) ||
358 (mode != S_IFGITLINK && !missing_ok && !has_object_file(oid))) {
b6b56ace 359 strbuf_release(&buffer);
4ed115e9
JH
360 if (expected_missing)
361 return -1;
a3883734 362 return error("invalid object %06o %s for '%.*s'",
6dcb4625 363 mode, oid_to_hex(oid), entlen+baselen, path);
b6b56ace 364 }
74986462 365
3cf773e4
NTND
366 /*
367 * CE_REMOVE entries are removed before the index is
368 * written to disk. Skip them to remain consistent
369 * with the future on-disk index.
370 */
371 if (ce->ce_flags & CE_REMOVE) {
372 *skip_count = *skip_count + 1;
373 continue;
374 }
375
eec3e7e4
NTND
376 /*
377 * CE_INTENT_TO_ADD entries exist on on-disk index but
378 * they are not part of generated trees. Invalidate up
379 * to root to force cache-tree users to read elsewhere.
380 */
c041d54a 381 if (!sub && ce_intent_to_add(ce)) {
eec3e7e4 382 to_invalidate = 1;
3cf773e4 383 continue;
eec3e7e4 384 }
74986462 385
6d6a782f
NTND
386 /*
387 * "sub" can be an empty tree if all subentries are i-t-a.
388 */
a0554934 389 if (contains_ita && is_empty_tree_oid(oid))
6d6a782f
NTND
390 continue;
391
5242bcbb
PH
392 strbuf_grow(&buffer, entlen + 100);
393 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
6dcb4625 394 strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz);
74986462
JH
395
396#if DEBUG
00703e6d 397 fprintf(stderr, "cache-tree update-one %o %.*s\n",
74986462
JH
398 mode, entlen, path + baselen);
399#endif
400 }
401
aecf567c 402 if (repair) {
f070facc
PO
403 struct object_id oid;
404 hash_object_file(buffer.buf, buffer.len, tree_type, &oid);
6dcb4625 405 if (has_object_file(&oid))
f070facc 406 oidcpy(&it->oid, &oid);
aecf567c
DT
407 else
408 to_invalidate = 1;
a09c985e 409 } else if (dryrun) {
f070facc 410 hash_object_file(buffer.buf, buffer.len, tree_type, &it->oid);
a09c985e
PO
411 } else if (write_object_file(buffer.buf, buffer.len, tree_type,
412 &it->oid)) {
edae5f0c
JH
413 strbuf_release(&buffer);
414 return -1;
415 }
416
5242bcbb 417 strbuf_release(&buffer);
eec3e7e4 418 it->entry_count = to_invalidate ? -1 : i - *skip_count;
74986462 419#if DEBUG
00703e6d 420 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
74986462 421 it->entry_count, it->subtree_nr,
e0a92804 422 oid_to_hex(&it->oid));
74986462
JH
423#endif
424 return i;
425}
426
d0cfc3e8 427int cache_tree_update(struct index_state *istate, int flags)
74986462 428{
d0cfc3e8
NTND
429 struct cache_tree *it = istate->cache_tree;
430 struct cache_entry **cache = istate->cache;
431 int entries = istate->cache_nr;
432 int skip, i = verify_cache(cache, entries, flags);
433
74986462
JH
434 if (i)
435 return i;
3cf773e4 436 i = update_one(it, cache, entries, "", 0, &skip, flags);
74986462
JH
437 if (i < 0)
438 return i;
d0cfc3e8 439 istate->cache_changed |= CACHE_TREE_CHANGED;
74986462
JH
440 return 0;
441}
442
1dffb8fa
PH
443static void write_one(struct strbuf *buffer, struct cache_tree *it,
444 const char *path, int pathlen)
74986462
JH
445{
446 int i;
447
448 /* One "cache-tree" entry consists of the following:
449 * path (NUL terminated)
450 * entry_count, subtree_nr ("%d %d\n")
451 * tree-sha1 (missing if invalid)
452 * subtree_nr "cache-tree" entries for subtrees.
453 */
5242bcbb
PH
454 strbuf_grow(buffer, pathlen + 100);
455 strbuf_add(buffer, path, pathlen);
456 strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
74986462
JH
457
458#if DEBUG
459 if (0 <= it->entry_count)
460 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
461 pathlen, path, it->entry_count, it->subtree_nr,
e0a92804 462 oid_to_hex(&it->oid));
74986462
JH
463 else
464 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
465 pathlen, path, it->subtree_nr);
466#endif
467
468 if (0 <= it->entry_count) {
6dcb4625 469 strbuf_add(buffer, it->oid.hash, the_hash_algo->rawsz);
74986462
JH
470 }
471 for (i = 0; i < it->subtree_nr; i++) {
472 struct cache_tree_sub *down = it->down[i];
61fa3097
JH
473 if (i) {
474 struct cache_tree_sub *prev = it->down[i-1];
475 if (subtree_name_cmp(down->name, down->namelen,
476 prev->name, prev->namelen) <= 0)
477 die("fatal - unsorted cache subtree");
478 }
1dffb8fa 479 write_one(buffer, down->cache_tree, down->name, down->namelen);
74986462 480 }
74986462
JH
481}
482
1dffb8fa 483void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
74986462 484{
1dffb8fa 485 write_one(sb, root, "", 0);
74986462
JH
486}
487
488static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
489{
490 const char *buf = *buffer;
491 unsigned long size = *size_p;
0111ea38
JS
492 const char *cp;
493 char *ep;
74986462
JH
494 struct cache_tree *it;
495 int i, subtree_nr;
6dcb4625 496 const unsigned rawsz = the_hash_algo->rawsz;
74986462
JH
497
498 it = NULL;
499 /* skip name, but make sure name exists */
500 while (size && *buf) {
501 size--;
502 buf++;
503 }
504 if (!size)
505 goto free_return;
506 buf++; size--;
507 it = cache_tree();
0111ea38
JS
508
509 cp = buf;
510 it->entry_count = strtol(cp, &ep, 10);
511 if (cp == ep)
512 goto free_return;
513 cp = ep;
514 subtree_nr = strtol(cp, &ep, 10);
515 if (cp == ep)
74986462
JH
516 goto free_return;
517 while (size && *buf && *buf != '\n') {
518 size--;
519 buf++;
520 }
521 if (!size)
522 goto free_return;
523 buf++; size--;
524 if (0 <= it->entry_count) {
6dcb4625 525 if (size < rawsz)
74986462 526 goto free_return;
69d12425 527 oidread(&it->oid, (const unsigned char *)buf);
6dcb4625 528 buf += rawsz;
529 size -= rawsz;
74986462
JH
530 }
531
532#if DEBUG
533 if (0 <= it->entry_count)
534 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
535 *buffer, it->entry_count, subtree_nr,
e0a92804 536 oid_to_hex(&it->oid));
74986462
JH
537 else
538 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
539 *buffer, subtree_nr);
540#endif
541
542 /*
543 * Just a heuristic -- we do not add directories that often but
544 * we do not want to have to extend it immediately when we do,
545 * hence +2.
546 */
547 it->subtree_alloc = subtree_nr + 2;
548 it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
549 for (i = 0; i < subtree_nr; i++) {
550 /* read each subtree */
551 struct cache_tree *sub;
61fa3097 552 struct cache_tree_sub *subtree;
74986462 553 const char *name = buf;
7927a55d 554
74986462
JH
555 sub = read_one(&buf, &size);
556 if (!sub)
557 goto free_return;
7927a55d 558 subtree = cache_tree_sub(it, name);
61fa3097 559 subtree->cache_tree = sub;
74986462
JH
560 }
561 if (subtree_nr != it->subtree_nr)
562 die("cache-tree: internal error");
563 *buffer = buf;
564 *size_p = size;
565 return it;
566
567 free_return:
bad68ec9 568 cache_tree_free(&it);
74986462
JH
569 return NULL;
570}
571
bad68ec9 572struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
74986462 573{
bad68ec9 574 if (buffer[0])
74986462 575 return NULL; /* not the whole tree */
74986462
JH
576 return read_one(&buffer, &size);
577}
6bd20358 578
7ba04d9f 579static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
6bd20358 580{
b87fc964
JH
581 if (!it)
582 return NULL;
6bd20358
JH
583 while (*path) {
584 const char *slash;
585 struct cache_tree_sub *sub;
586
17e22ddc 587 slash = strchrnul(path, '/');
79192b87
MH
588 /*
589 * Between path and slash is the name of the subtree
590 * to look for.
6bd20358
JH
591 */
592 sub = find_subtree(it, path, slash - path, 0);
593 if (!sub)
594 return NULL;
595 it = sub->cache_tree;
3491047e 596
6bd20358 597 path = slash;
3491047e
MH
598 while (*path == '/')
599 path++;
6bd20358
JH
600 }
601 return it;
602}
45525bd0 603
fc5cb99f 604int write_index_as_tree(struct object_id *oid, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
45525bd0 605{
2954e5ec 606 int entries, was_valid;
bfffb48c 607 struct lock_file lock_file = LOCK_INIT;
c82c75b9 608 int ret = 0;
45525bd0 609
2954e5ec 610 hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
45525bd0 611
a125a223 612 entries = read_index_from(index_state, index_path, get_git_dir());
c82c75b9
JK
613 if (entries < 0) {
614 ret = WRITE_TREE_UNREADABLE_INDEX;
615 goto out;
616 }
d11b8d34 617 if (flags & WRITE_TREE_IGNORE_CACHE_TREE)
d23a5117 618 cache_tree_free(&index_state->cache_tree);
45525bd0 619
d23a5117
PT
620 if (!index_state->cache_tree)
621 index_state->cache_tree = cache_tree();
45525bd0 622
d23a5117 623 was_valid = cache_tree_fully_valid(index_state->cache_tree);
45525bd0 624 if (!was_valid) {
c82c75b9
JK
625 if (cache_tree_update(index_state, flags) < 0) {
626 ret = WRITE_TREE_UNMERGED_INDEX;
627 goto out;
628 }
2954e5ec 629 write_locked_index(index_state, &lock_file, COMMIT_LOCK);
45525bd0
JH
630 /* Not being able to write is fine -- we are only interested
631 * in updating the cache-tree part, and if the next caller
632 * ends up using the old index with unupdated cache-tree part
633 * it misses the work we did here, but that is just a
634 * performance penalty and not a big deal.
635 */
636 }
637
638 if (prefix) {
d23a5117
PT
639 struct cache_tree *subtree;
640 subtree = cache_tree_find(index_state->cache_tree, prefix);
c82c75b9
JK
641 if (!subtree) {
642 ret = WRITE_TREE_PREFIX_ERROR;
643 goto out;
644 }
fc5cb99f 645 oidcpy(oid, &subtree->oid);
45525bd0
JH
646 }
647 else
fc5cb99f 648 oidcpy(oid, &index_state->cache_tree->oid);
45525bd0 649
c82c75b9 650out:
2954e5ec 651 rollback_lock_file(&lock_file);
c82c75b9 652 return ret;
45525bd0 653}
b9d37a54 654
fc5cb99f 655int write_cache_as_tree(struct object_id *oid, int flags, const char *prefix)
d23a5117 656{
fc5cb99f 657 return write_index_as_tree(oid, &the_index, get_index_file(), flags, prefix);
d23a5117
PT
658}
659
b9d37a54
JH
660static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
661{
662 struct tree_desc desc;
663 struct name_entry entry;
664 int cnt;
665
e0a92804 666 oidcpy(&it->oid, &tree->object.oid);
b9d37a54
JH
667 init_tree_desc(&desc, tree->buffer, tree->size);
668 cnt = 0;
669 while (tree_entry(&desc, &entry)) {
670 if (!S_ISDIR(entry.mode))
671 cnt++;
672 else {
673 struct cache_tree_sub *sub;
f86bcc7b
SB
674 struct tree *subtree = lookup_tree(the_repository,
675 entry.oid);
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}