Declare that the next one will be named 2.20
[git/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"
4592e608 7#include "replace-object.h"
74986462 8
4903161f 9#ifndef DEBUG
74986462 10#define DEBUG 0
4903161f 11#endif
74986462
JH
12
13struct cache_tree *cache_tree(void)
14{
15 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
16 it->entry_count = -1;
17 return it;
18}
19
bad68ec9 20void cache_tree_free(struct cache_tree **it_p)
74986462
JH
21{
22 int i;
bad68ec9 23 struct cache_tree *it = *it_p;
74986462
JH
24
25 if (!it)
26 return;
27 for (i = 0; i < it->subtree_nr; i++)
e92fa514 28 if (it->down[i]) {
61fa3097 29 cache_tree_free(&it->down[i]->cache_tree);
e92fa514
EN
30 free(it->down[i]);
31 }
74986462
JH
32 free(it->down);
33 free(it);
bad68ec9 34 *it_p = NULL;
74986462
JH
35}
36
61fa3097
JH
37static int subtree_name_cmp(const char *one, int onelen,
38 const char *two, int twolen)
39{
40 if (onelen < twolen)
41 return -1;
42 if (twolen < onelen)
43 return 1;
44 return memcmp(one, two, onelen);
45}
46
47static int subtree_pos(struct cache_tree *it, const char *path, int pathlen)
48{
49 struct cache_tree_sub **down = it->down;
50 int lo, hi;
51 lo = 0;
52 hi = it->subtree_nr;
53 while (lo < hi) {
19716b21 54 int mi = lo + (hi - lo) / 2;
61fa3097
JH
55 struct cache_tree_sub *mdl = down[mi];
56 int cmp = subtree_name_cmp(path, pathlen,
57 mdl->name, mdl->namelen);
58 if (!cmp)
59 return mi;
60 if (cmp < 0)
61 hi = mi;
62 else
63 lo = mi + 1;
64 }
65 return -lo-1;
66}
67
74986462
JH
68static struct cache_tree_sub *find_subtree(struct cache_tree *it,
69 const char *path,
70 int pathlen,
71 int create)
72{
74986462 73 struct cache_tree_sub *down;
61fa3097
JH
74 int pos = subtree_pos(it, path, pathlen);
75 if (0 <= pos)
76 return it->down[pos];
74986462
JH
77 if (!create)
78 return NULL;
61fa3097
JH
79
80 pos = -pos-1;
bcc7a032 81 ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
61fa3097
JH
82 it->subtree_nr++;
83
96ffc06f 84 FLEX_ALLOC_MEM(down, name, path, pathlen);
61fa3097 85 down->cache_tree = NULL;
74986462 86 down->namelen = pathlen;
61fa3097
JH
87
88 if (pos < it->subtree_nr)
f919ffeb
SG
89 MOVE_ARRAY(it->down + pos + 1, it->down + pos,
90 it->subtree_nr - pos - 1);
61fa3097 91 it->down[pos] = down;
74986462
JH
92 return down;
93}
94
7927a55d
JH
95struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
96{
97 int pathlen = strlen(path);
98 return find_subtree(it, path, pathlen, 1);
99}
100
a5400efe 101static int do_invalidate_path(struct cache_tree *it, const char *path)
74986462
JH
102{
103 /* a/b/c
104 * ==> invalidate self
105 * ==> find "a", have it invalidate "b/c"
106 * a
107 * ==> invalidate self
108 * ==> if "a" exists as a subtree, remove it.
109 */
110 const char *slash;
111 int namelen;
112 struct cache_tree_sub *down;
113
00703e6d
JH
114#if DEBUG
115 fprintf(stderr, "cache-tree invalidate <%s>\n", path);
116#endif
117
74986462 118 if (!it)
a5400efe 119 return 0;
2c5495f7
RM
120 slash = strchrnul(path, '/');
121 namelen = slash - path;
74986462 122 it->entry_count = -1;
2c5495f7 123 if (!*slash) {
61fa3097 124 int pos;
61fa3097
JH
125 pos = subtree_pos(it, path, namelen);
126 if (0 <= pos) {
127 cache_tree_free(&it->down[pos]->cache_tree);
128 free(it->down[pos]);
74986462
JH
129 /* 0 1 2 3 4 5
130 * ^ ^subtree_nr = 6
61fa3097 131 * pos
74986462 132 * move 4 and 5 up one place (2 entries)
61fa3097 133 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
74986462 134 */
f331ab9d
RS
135 MOVE_ARRAY(it->down + pos, it->down + pos + 1,
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;
6dcb4625 325 const struct object_id *oid;
74986462 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;
6dcb4625 343 oid = &sub->cache_tree->oid;
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 {
6dcb4625 352 oid = &ce->oid;
7a51ed66 353 mode = ce->ce_mode;
74986462 354 entlen = pathlen - baselen;
386cc8b0 355 i++;
74986462 356 }
a96d3cc3 357
6dcb4625 358 if (is_null_oid(oid) ||
359 (mode != S_IFGITLINK && !missing_ok && !has_object_file(oid))) {
b6b56ace 360 strbuf_release(&buffer);
4ed115e9
JH
361 if (expected_missing)
362 return -1;
a3883734 363 return error("invalid object %06o %s for '%.*s'",
6dcb4625 364 mode, oid_to_hex(oid), entlen+baselen, path);
b6b56ace 365 }
74986462 366
3cf773e4
NTND
367 /*
368 * CE_REMOVE entries are removed before the index is
369 * written to disk. Skip them to remain consistent
370 * with the future on-disk index.
371 */
372 if (ce->ce_flags & CE_REMOVE) {
373 *skip_count = *skip_count + 1;
374 continue;
375 }
376
eec3e7e4
NTND
377 /*
378 * CE_INTENT_TO_ADD entries exist on on-disk index but
379 * they are not part of generated trees. Invalidate up
380 * to root to force cache-tree users to read elsewhere.
381 */
c041d54a 382 if (!sub && ce_intent_to_add(ce)) {
eec3e7e4 383 to_invalidate = 1;
3cf773e4 384 continue;
eec3e7e4 385 }
74986462 386
6d6a782f
NTND
387 /*
388 * "sub" can be an empty tree if all subentries are i-t-a.
389 */
a0554934 390 if (contains_ita && is_empty_tree_oid(oid))
6d6a782f
NTND
391 continue;
392
5242bcbb
PH
393 strbuf_grow(&buffer, entlen + 100);
394 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
6dcb4625 395 strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz);
74986462
JH
396
397#if DEBUG
00703e6d 398 fprintf(stderr, "cache-tree update-one %o %.*s\n",
74986462
JH
399 mode, entlen, path + baselen);
400#endif
401 }
402
aecf567c 403 if (repair) {
f070facc
PO
404 struct object_id oid;
405 hash_object_file(buffer.buf, buffer.len, tree_type, &oid);
6dcb4625 406 if (has_object_file(&oid))
f070facc 407 oidcpy(&it->oid, &oid);
aecf567c
DT
408 else
409 to_invalidate = 1;
a09c985e 410 } else if (dryrun) {
f070facc 411 hash_object_file(buffer.buf, buffer.len, tree_type, &it->oid);
a09c985e
PO
412 } else if (write_object_file(buffer.buf, buffer.len, tree_type,
413 &it->oid)) {
edae5f0c
JH
414 strbuf_release(&buffer);
415 return -1;
416 }
417
5242bcbb 418 strbuf_release(&buffer);
eec3e7e4 419 it->entry_count = to_invalidate ? -1 : i - *skip_count;
74986462 420#if DEBUG
00703e6d 421 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
74986462 422 it->entry_count, it->subtree_nr,
e0a92804 423 oid_to_hex(&it->oid));
74986462
JH
424#endif
425 return i;
426}
427
d0cfc3e8 428int cache_tree_update(struct index_state *istate, int flags)
74986462 429{
d0cfc3e8
NTND
430 struct cache_tree *it = istate->cache_tree;
431 struct cache_entry **cache = istate->cache;
432 int entries = istate->cache_nr;
433 int skip, i = verify_cache(cache, entries, flags);
434
74986462
JH
435 if (i)
436 return i;
0d1ed596 437 trace_performance_enter();
3cf773e4 438 i = update_one(it, cache, entries, "", 0, &skip, flags);
0d1ed596 439 trace_performance_leave("cache_tree_update");
74986462
JH
440 if (i < 0)
441 return i;
d0cfc3e8 442 istate->cache_changed |= CACHE_TREE_CHANGED;
74986462
JH
443 return 0;
444}
445
1dffb8fa
PH
446static void write_one(struct strbuf *buffer, struct cache_tree *it,
447 const char *path, int pathlen)
74986462
JH
448{
449 int i;
450
451 /* One "cache-tree" entry consists of the following:
452 * path (NUL terminated)
453 * entry_count, subtree_nr ("%d %d\n")
454 * tree-sha1 (missing if invalid)
455 * subtree_nr "cache-tree" entries for subtrees.
456 */
5242bcbb
PH
457 strbuf_grow(buffer, pathlen + 100);
458 strbuf_add(buffer, path, pathlen);
459 strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
74986462
JH
460
461#if DEBUG
462 if (0 <= it->entry_count)
463 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
464 pathlen, path, it->entry_count, it->subtree_nr,
e0a92804 465 oid_to_hex(&it->oid));
74986462
JH
466 else
467 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
468 pathlen, path, it->subtree_nr);
469#endif
470
471 if (0 <= it->entry_count) {
6dcb4625 472 strbuf_add(buffer, it->oid.hash, the_hash_algo->rawsz);
74986462
JH
473 }
474 for (i = 0; i < it->subtree_nr; i++) {
475 struct cache_tree_sub *down = it->down[i];
61fa3097
JH
476 if (i) {
477 struct cache_tree_sub *prev = it->down[i-1];
478 if (subtree_name_cmp(down->name, down->namelen,
479 prev->name, prev->namelen) <= 0)
480 die("fatal - unsorted cache subtree");
481 }
1dffb8fa 482 write_one(buffer, down->cache_tree, down->name, down->namelen);
74986462 483 }
74986462
JH
484}
485
1dffb8fa 486void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
74986462 487{
1dffb8fa 488 write_one(sb, root, "", 0);
74986462
JH
489}
490
491static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
492{
493 const char *buf = *buffer;
494 unsigned long size = *size_p;
0111ea38
JS
495 const char *cp;
496 char *ep;
74986462
JH
497 struct cache_tree *it;
498 int i, subtree_nr;
6dcb4625 499 const unsigned rawsz = the_hash_algo->rawsz;
74986462
JH
500
501 it = NULL;
502 /* skip name, but make sure name exists */
503 while (size && *buf) {
504 size--;
505 buf++;
506 }
507 if (!size)
508 goto free_return;
509 buf++; size--;
510 it = cache_tree();
0111ea38
JS
511
512 cp = buf;
513 it->entry_count = strtol(cp, &ep, 10);
514 if (cp == ep)
515 goto free_return;
516 cp = ep;
517 subtree_nr = strtol(cp, &ep, 10);
518 if (cp == ep)
74986462
JH
519 goto free_return;
520 while (size && *buf && *buf != '\n') {
521 size--;
522 buf++;
523 }
524 if (!size)
525 goto free_return;
526 buf++; size--;
527 if (0 <= it->entry_count) {
6dcb4625 528 if (size < rawsz)
74986462 529 goto free_return;
69d12425 530 oidread(&it->oid, (const unsigned char *)buf);
6dcb4625 531 buf += rawsz;
532 size -= rawsz;
74986462
JH
533 }
534
535#if DEBUG
536 if (0 <= it->entry_count)
537 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
538 *buffer, it->entry_count, subtree_nr,
e0a92804 539 oid_to_hex(&it->oid));
74986462
JH
540 else
541 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
542 *buffer, subtree_nr);
543#endif
544
545 /*
546 * Just a heuristic -- we do not add directories that often but
547 * we do not want to have to extend it immediately when we do,
548 * hence +2.
549 */
550 it->subtree_alloc = subtree_nr + 2;
551 it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
552 for (i = 0; i < subtree_nr; i++) {
553 /* read each subtree */
554 struct cache_tree *sub;
61fa3097 555 struct cache_tree_sub *subtree;
74986462 556 const char *name = buf;
7927a55d 557
74986462
JH
558 sub = read_one(&buf, &size);
559 if (!sub)
560 goto free_return;
7927a55d 561 subtree = cache_tree_sub(it, name);
61fa3097 562 subtree->cache_tree = sub;
74986462
JH
563 }
564 if (subtree_nr != it->subtree_nr)
565 die("cache-tree: internal error");
566 *buffer = buf;
567 *size_p = size;
568 return it;
569
570 free_return:
bad68ec9 571 cache_tree_free(&it);
74986462
JH
572 return NULL;
573}
574
bad68ec9 575struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
74986462 576{
bad68ec9 577 if (buffer[0])
74986462 578 return NULL; /* not the whole tree */
74986462
JH
579 return read_one(&buffer, &size);
580}
6bd20358 581
7ba04d9f 582static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
6bd20358 583{
b87fc964
JH
584 if (!it)
585 return NULL;
6bd20358
JH
586 while (*path) {
587 const char *slash;
588 struct cache_tree_sub *sub;
589
17e22ddc 590 slash = strchrnul(path, '/');
79192b87
MH
591 /*
592 * Between path and slash is the name of the subtree
593 * to look for.
6bd20358
JH
594 */
595 sub = find_subtree(it, path, slash - path, 0);
596 if (!sub)
597 return NULL;
598 it = sub->cache_tree;
3491047e 599
6bd20358 600 path = slash;
3491047e
MH
601 while (*path == '/')
602 path++;
6bd20358
JH
603 }
604 return it;
605}
45525bd0 606
fc5cb99f 607int write_index_as_tree(struct object_id *oid, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
45525bd0 608{
2954e5ec 609 int entries, was_valid;
bfffb48c 610 struct lock_file lock_file = LOCK_INIT;
c82c75b9 611 int ret = 0;
45525bd0 612
2954e5ec 613 hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
45525bd0 614
a125a223 615 entries = read_index_from(index_state, index_path, get_git_dir());
c82c75b9
JK
616 if (entries < 0) {
617 ret = WRITE_TREE_UNREADABLE_INDEX;
618 goto out;
619 }
d11b8d34 620 if (flags & WRITE_TREE_IGNORE_CACHE_TREE)
d23a5117 621 cache_tree_free(&index_state->cache_tree);
45525bd0 622
d23a5117
PT
623 if (!index_state->cache_tree)
624 index_state->cache_tree = cache_tree();
45525bd0 625
d23a5117 626 was_valid = cache_tree_fully_valid(index_state->cache_tree);
45525bd0 627 if (!was_valid) {
c82c75b9
JK
628 if (cache_tree_update(index_state, flags) < 0) {
629 ret = WRITE_TREE_UNMERGED_INDEX;
630 goto out;
631 }
2954e5ec 632 write_locked_index(index_state, &lock_file, COMMIT_LOCK);
45525bd0
JH
633 /* Not being able to write is fine -- we are only interested
634 * in updating the cache-tree part, and if the next caller
635 * ends up using the old index with unupdated cache-tree part
636 * it misses the work we did here, but that is just a
637 * performance penalty and not a big deal.
638 */
639 }
640
641 if (prefix) {
d23a5117
PT
642 struct cache_tree *subtree;
643 subtree = cache_tree_find(index_state->cache_tree, prefix);
c82c75b9
JK
644 if (!subtree) {
645 ret = WRITE_TREE_PREFIX_ERROR;
646 goto out;
647 }
fc5cb99f 648 oidcpy(oid, &subtree->oid);
45525bd0
JH
649 }
650 else
fc5cb99f 651 oidcpy(oid, &index_state->cache_tree->oid);
45525bd0 652
c82c75b9 653out:
2954e5ec 654 rollback_lock_file(&lock_file);
c82c75b9 655 return ret;
45525bd0 656}
b9d37a54
JH
657
658static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
659{
660 struct tree_desc desc;
661 struct name_entry entry;
662 int cnt;
663
e0a92804 664 oidcpy(&it->oid, &tree->object.oid);
b9d37a54
JH
665 init_tree_desc(&desc, tree->buffer, tree->size);
666 cnt = 0;
667 while (tree_entry(&desc, &entry)) {
668 if (!S_ISDIR(entry.mode))
669 cnt++;
670 else {
671 struct cache_tree_sub *sub;
f86bcc7b
SB
672 struct tree *subtree = lookup_tree(the_repository,
673 entry.oid);
b9d37a54
JH
674 if (!subtree->object.parsed)
675 parse_tree(subtree);
676 sub = cache_tree_sub(it, entry.path);
677 sub->cache_tree = cache_tree();
678 prime_cache_tree_rec(sub->cache_tree, subtree);
679 cnt += sub->cache_tree->entry_count;
680 }
681 }
682 it->entry_count = cnt;
683}
684
e6c286e8 685void prime_cache_tree(struct index_state *istate, struct tree *tree)
b9d37a54 686{
e6c286e8
NTND
687 cache_tree_free(&istate->cache_tree);
688 istate->cache_tree = cache_tree();
689 prime_cache_tree_rec(istate->cache_tree, tree);
690 istate->cache_changed |= CACHE_TREE_CHANGED;
b9d37a54 691}
b65982b6
JH
692
693/*
694 * find the cache_tree that corresponds to the current level without
695 * exploding the full path into textual form. The root of the
696 * cache tree is given as "root", and our current level is "info".
697 * (1) When at root level, info->prev is NULL, so it is "root" itself.
698 * (2) Otherwise, find the cache_tree that corresponds to one level
699 * above us, and find ourselves in there.
700 */
701static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
702 struct traverse_info *info)
703{
704 struct cache_tree *our_parent;
705
706 if (!info->prev)
707 return root;
708 our_parent = find_cache_tree_from_traversal(root, info->prev);
709 return cache_tree_find(our_parent, info->name.path);
710}
711
712int cache_tree_matches_traversal(struct cache_tree *root,
713 struct name_entry *ent,
714 struct traverse_info *info)
715{
716 struct cache_tree *it;
717
718 it = find_cache_tree_from_traversal(root, info);
719 it = cache_tree_find(it, ent->path);
4a7e27e9 720 if (it && it->entry_count > 0 && oideq(ent->oid, &it->oid))
b65982b6
JH
721 return it->entry_count;
722 return 0;
723}
996277c5 724
4592e608
NTND
725static void verify_one(struct index_state *istate,
726 struct cache_tree *it,
727 struct strbuf *path)
728{
729 int i, pos, len = path->len;
730 struct strbuf tree_buf = STRBUF_INIT;
731 struct object_id new_oid;
732
733 for (i = 0; i < it->subtree_nr; i++) {
734 strbuf_addf(path, "%s/", it->down[i]->name);
735 verify_one(istate, it->down[i]->cache_tree, path);
736 strbuf_setlen(path, len);
737 }
738
739 if (it->entry_count < 0 ||
740 /* no verification on tests (t7003) that replace trees */
741 lookup_replace_object(the_repository, &it->oid) != &it->oid)
742 return;
743
744 if (path->len) {
745 pos = index_name_pos(istate, path->buf, path->len);
746 pos = -pos - 1;
747 } else {
748 pos = 0;
749 }
750
751 i = 0;
752 while (i < it->entry_count) {
753 struct cache_entry *ce = istate->cache[pos + i];
754 const char *slash;
755 struct cache_tree_sub *sub = NULL;
756 const struct object_id *oid;
757 const char *name;
758 unsigned mode;
759 int entlen;
760
761 if (ce->ce_flags & (CE_STAGEMASK | CE_INTENT_TO_ADD | CE_REMOVE))
762 BUG("%s with flags 0x%x should not be in cache-tree",
763 ce->name, ce->ce_flags);
764 name = ce->name + path->len;
765 slash = strchr(name, '/');
766 if (slash) {
767 entlen = slash - name;
768 sub = find_subtree(it, ce->name + path->len, entlen, 0);
769 if (!sub || sub->cache_tree->entry_count < 0)
770 BUG("bad subtree '%.*s'", entlen, name);
771 oid = &sub->cache_tree->oid;
772 mode = S_IFDIR;
773 i += sub->cache_tree->entry_count;
774 } else {
775 oid = &ce->oid;
776 mode = ce->ce_mode;
777 entlen = ce_namelen(ce) - path->len;
778 i++;
779 }
780 strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0');
781 strbuf_add(&tree_buf, oid->hash, the_hash_algo->rawsz);
782 }
783 hash_object_file(tree_buf.buf, tree_buf.len, tree_type, &new_oid);
784 if (oidcmp(&new_oid, &it->oid))
785 BUG("cache-tree for path %.*s does not match. "
786 "Expected %s got %s", len, path->buf,
787 oid_to_hex(&new_oid), oid_to_hex(&it->oid));
788 strbuf_setlen(path, len);
789 strbuf_release(&tree_buf);
790}
791
792void cache_tree_verify(struct index_state *istate)
793{
794 struct strbuf path = STRBUF_INIT;
795
796 if (!istate->cache_tree)
797 return;
798 verify_one(istate, istate->cache_tree, &path);
799 strbuf_release(&path);
800}