cache-tree.c 17.3 KB
Newer Older
J
Junio C Hamano 已提交
1
#include "cache.h"
2
#include "lockfile.h"
J
Junio C Hamano 已提交
3
#include "tree.h"
4
#include "tree-walk.h"
J
Junio C Hamano 已提交
5 6
#include "cache-tree.h"

7
#ifndef DEBUG
J
Junio C Hamano 已提交
8
#define DEBUG 0
9
#endif
J
Junio C Hamano 已提交
10 11 12 13 14 15 16 17

struct cache_tree *cache_tree(void)
{
	struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
	it->entry_count = -1;
	return it;
}

18
void cache_tree_free(struct cache_tree **it_p)
J
Junio C Hamano 已提交
19 20
{
	int i;
21
	struct cache_tree *it = *it_p;
J
Junio C Hamano 已提交
22 23 24 25

	if (!it)
		return;
	for (i = 0; i < it->subtree_nr; i++)
26
		if (it->down[i]) {
27
			cache_tree_free(&it->down[i]->cache_tree);
28 29
			free(it->down[i]);
		}
J
Junio C Hamano 已提交
30 31
	free(it->down);
	free(it);
32
	*it_p = NULL;
J
Junio C Hamano 已提交
33 34
}

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
static int subtree_name_cmp(const char *one, int onelen,
			    const char *two, int twolen)
{
	if (onelen < twolen)
		return -1;
	if (twolen < onelen)
		return 1;
	return memcmp(one, two, onelen);
}

static int subtree_pos(struct cache_tree *it, const char *path, int pathlen)
{
	struct cache_tree_sub **down = it->down;
	int lo, hi;
	lo = 0;
	hi = it->subtree_nr;
	while (lo < hi) {
52
		int mi = lo + (hi - lo) / 2;
53 54 55 56 57 58 59 60 61 62 63 64 65
		struct cache_tree_sub *mdl = down[mi];
		int cmp = subtree_name_cmp(path, pathlen,
					   mdl->name, mdl->namelen);
		if (!cmp)
			return mi;
		if (cmp < 0)
			hi = mi;
		else
			lo = mi + 1;
	}
	return -lo-1;
}

J
Junio C Hamano 已提交
66 67 68 69 70 71
static struct cache_tree_sub *find_subtree(struct cache_tree *it,
					   const char *path,
					   int pathlen,
					   int create)
{
	struct cache_tree_sub *down;
72 73 74
	int pos = subtree_pos(it, path, pathlen);
	if (0 <= pos)
		return it->down[pos];
J
Junio C Hamano 已提交
75 76
	if (!create)
		return NULL;
77 78

	pos = -pos-1;
79
	ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
80 81
	it->subtree_nr++;

82
	FLEX_ALLOC_MEM(down, name, path, pathlen);
83
	down->cache_tree = NULL;
J
Junio C Hamano 已提交
84
	down->namelen = pathlen;
85 86

	if (pos < it->subtree_nr)
S
SZEDER Gábor 已提交
87 88
		MOVE_ARRAY(it->down + pos + 1, it->down + pos,
			   it->subtree_nr - pos - 1);
89
	it->down[pos] = down;
J
Junio C Hamano 已提交
90 91 92
	return down;
}

93 94 95 96 97 98
struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
{
	int pathlen = strlen(path);
	return find_subtree(it, path, pathlen, 1);
}

99
static int do_invalidate_path(struct cache_tree *it, const char *path)
J
Junio C Hamano 已提交
100 101 102 103 104 105 106 107 108 109 110 111
{
	/* a/b/c
	 * ==> invalidate self
	 * ==> find "a", have it invalidate "b/c"
	 * a
	 * ==> invalidate self
	 * ==> if "a" exists as a subtree, remove it.
	 */
	const char *slash;
	int namelen;
	struct cache_tree_sub *down;

112 113 114 115
#if DEBUG
	fprintf(stderr, "cache-tree invalidate <%s>\n", path);
#endif

J
Junio C Hamano 已提交
116
	if (!it)
117
		return 0;
118 119
	slash = strchrnul(path, '/');
	namelen = slash - path;
J
Junio C Hamano 已提交
120
	it->entry_count = -1;
121
	if (!*slash) {
122 123 124 125 126
		int pos;
		pos = subtree_pos(it, path, namelen);
		if (0 <= pos) {
			cache_tree_free(&it->down[pos]->cache_tree);
			free(it->down[pos]);
J
Junio C Hamano 已提交
127 128
			/* 0 1 2 3 4 5
			 *       ^     ^subtree_nr = 6
129
			 *       pos
J
Junio C Hamano 已提交
130
			 * move 4 and 5 up one place (2 entries)
131
			 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
J
Junio C Hamano 已提交
132
			 */
R
René Scharfe 已提交
133 134
			MOVE_ARRAY(it->down + pos, it->down + pos + 1,
				   it->subtree_nr - pos - 1);
J
Junio C Hamano 已提交
135 136
			it->subtree_nr--;
		}
137
		return 1;
J
Junio C Hamano 已提交
138 139 140
	}
	down = find_subtree(it, path, namelen, 0);
	if (down)
141 142 143 144 145 146 147 148
		do_invalidate_path(down->cache_tree, slash + 1);
	return 1;
}

void cache_tree_invalidate_path(struct index_state *istate, const char *path)
{
	if (do_invalidate_path(istate->cache_tree, path))
		istate->cache_changed |= CACHE_TREE_CHANGED;
J
Junio C Hamano 已提交
149 150
}

151
static int verify_cache(struct cache_entry **cache,
152
			int entries, int flags)
J
Junio C Hamano 已提交
153 154
{
	int i, funny;
155
	int silent = flags & WRITE_TREE_SILENT;
J
Junio C Hamano 已提交
156 157 158 159

	/* Verify that the tree is merged */
	funny = 0;
	for (i = 0; i < entries; i++) {
160
		const struct cache_entry *ce = cache[i];
161
		if (ce_stage(ce)) {
162 163
			if (silent)
				return -1;
J
Junio C Hamano 已提交
164 165 166 167
			if (10 < ++funny) {
				fprintf(stderr, "...\n");
				break;
			}
168
			fprintf(stderr, "%s: unmerged (%s)\n",
169
				ce->name, oid_to_hex(&ce->oid));
J
Junio C Hamano 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
		}
	}
	if (funny)
		return -1;

	/* Also verify that the cache does not have path and path/file
	 * at the same time.  At this point we know the cache has only
	 * stage 0 entries.
	 */
	funny = 0;
	for (i = 0; i < entries - 1; i++) {
		/* path/file always comes after path because of the way
		 * the cache is sorted.  Also path can appear only once,
		 * which means conflicting one would immediately follow.
		 */
		const char *this_name = cache[i]->name;
		const char *next_name = cache[i+1]->name;
		int this_len = strlen(this_name);
		if (this_len < strlen(next_name) &&
		    strncmp(this_name, next_name, this_len) == 0 &&
		    next_name[this_len] == '/') {
			if (10 < ++funny) {
				fprintf(stderr, "...\n");
				break;
			}
			fprintf(stderr, "You have both %s and %s\n",
				this_name, next_name);
		}
	}
	if (funny)
		return -1;
	return 0;
}

static void discard_unused_subtrees(struct cache_tree *it)
{
	struct cache_tree_sub **down = it->down;
	int nr = it->subtree_nr;
	int dst, src;
	for (dst = src = 0; src < nr; src++) {
		struct cache_tree_sub *s = down[src];
		if (s->used)
			down[dst++] = s;
		else {
214
			cache_tree_free(&s->cache_tree);
J
Junio C Hamano 已提交
215 216 217 218 219 220
			free(s);
			it->subtree_nr--;
		}
	}
}

221 222 223 224 225
int cache_tree_fully_valid(struct cache_tree *it)
{
	int i;
	if (!it)
		return 0;
226
	if (it->entry_count < 0 || !has_sha1_file(it->oid.hash))
227 228 229 230 231 232 233 234
		return 0;
	for (i = 0; i < it->subtree_nr; i++) {
		if (!cache_tree_fully_valid(it->down[i]->cache_tree))
			return 0;
	}
	return 1;
}

J
Junio C Hamano 已提交
235
static int update_one(struct cache_tree *it,
236
		      struct cache_entry **cache,
J
Junio C Hamano 已提交
237 238 239
		      int entries,
		      const char *base,
		      int baselen,
240
		      int *skip_count,
241
		      int flags)
J
Junio C Hamano 已提交
242
{
P
Pierre Habouzit 已提交
243
	struct strbuf buffer;
244 245
	int missing_ok = flags & WRITE_TREE_MISSING_OK;
	int dryrun = flags & WRITE_TREE_DRY_RUN;
246
	int repair = flags & WRITE_TREE_REPAIR;
247
	int to_invalidate = 0;
J
Junio C Hamano 已提交
248 249
	int i;

250 251
	assert(!(dryrun && repair));

252 253
	*skip_count = 0;

254
	if (0 <= it->entry_count && has_sha1_file(it->oid.hash))
J
Junio C Hamano 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267
		return it->entry_count;

	/*
	 * We first scan for subtrees and update them; we start by
	 * marking existing subtrees -- the ones that are unmarked
	 * should not be in the result.
	 */
	for (i = 0; i < it->subtree_nr; i++)
		it->down[i]->used = 0;

	/*
	 * Find the subtrees and update them.
	 */
268 269
	i = 0;
	while (i < entries) {
270
		const struct cache_entry *ce = cache[i];
J
Junio C Hamano 已提交
271 272
		struct cache_tree_sub *sub;
		const char *path, *slash;
273
		int pathlen, sublen, subcnt, subskip;
J
Junio C Hamano 已提交
274 275 276 277 278 279 280

		path = ce->name;
		pathlen = ce_namelen(ce);
		if (pathlen <= baselen || memcmp(base, path, baselen))
			break; /* at the end of this level */

		slash = strchr(path + baselen, '/');
281 282
		if (!slash) {
			i++;
J
Junio C Hamano 已提交
283
			continue;
284
		}
J
Junio C Hamano 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297
		/*
		 * a/bbb/c (base = a/, slash = /c)
		 * ==>
		 * path+baselen = bbb/c, sublen = 3
		 */
		sublen = slash - (path + baselen);
		sub = find_subtree(it, path + baselen, sublen, 1);
		if (!sub->cache_tree)
			sub->cache_tree = cache_tree();
		subcnt = update_one(sub->cache_tree,
				    cache + i, entries - i,
				    path,
				    baselen + sublen + 1,
298
				    &subskip,
299
				    flags);
300 301
		if (subcnt < 0)
			return subcnt;
302 303
		if (!subcnt)
			die("index cache-tree records empty sub-tree");
304
		i += subcnt;
305 306
		sub->count = subcnt; /* to be used in the next loop */
		*skip_count += subskip;
J
Junio C Hamano 已提交
307 308 309 310 311 312 313 314
		sub->used = 1;
	}

	discard_unused_subtrees(it);

	/*
	 * Then write out the tree object for this level.
	 */
315
	strbuf_init(&buffer, 8192);
J
Junio C Hamano 已提交
316

317 318
	i = 0;
	while (i < entries) {
319
		const struct cache_entry *ce = cache[i];
320
		struct cache_tree_sub *sub = NULL;
J
Junio C Hamano 已提交
321 322 323 324
		const char *path, *slash;
		int pathlen, entlen;
		const unsigned char *sha1;
		unsigned mode;
325
		int expected_missing = 0;
326
		int contains_ita = 0;
J
Junio C Hamano 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339

		path = ce->name;
		pathlen = ce_namelen(ce);
		if (pathlen <= baselen || memcmp(base, path, baselen))
			break; /* at the end of this level */

		slash = strchr(path + baselen, '/');
		if (slash) {
			entlen = slash - (path + baselen);
			sub = find_subtree(it, path + baselen, entlen, 0);
			if (!sub)
				die("cache-tree.c: '%.*s' in '%s' not found",
				    entlen, path + baselen, path);
340
			i += sub->count;
341
			sha1 = sub->cache_tree->oid.hash;
J
Junio C Hamano 已提交
342
			mode = S_IFDIR;
343 344
			contains_ita = sub->cache_tree->entry_count < 0;
			if (contains_ita) {
345
				to_invalidate = 1;
346 347
				expected_missing = 1;
			}
J
Junio C Hamano 已提交
348 349
		}
		else {
350
			sha1 = ce->oid.hash;
351
			mode = ce->ce_mode;
J
Junio C Hamano 已提交
352
			entlen = pathlen - baselen;
353
			i++;
J
Junio C Hamano 已提交
354
		}
355 356 357

		if (is_null_sha1(sha1) ||
		    (mode != S_IFGITLINK && !missing_ok && !has_sha1_file(sha1))) {
358
			strbuf_release(&buffer);
359 360
			if (expected_missing)
				return -1;
361 362
			return error("invalid object %06o %s for '%.*s'",
				mode, sha1_to_hex(sha1), entlen+baselen, path);
363
		}
J
Junio C Hamano 已提交
364

365 366 367 368 369 370 371 372 373 374
		/*
		 * CE_REMOVE entries are removed before the index is
		 * written to disk. Skip them to remain consistent
		 * with the future on-disk index.
		 */
		if (ce->ce_flags & CE_REMOVE) {
			*skip_count = *skip_count + 1;
			continue;
		}

375 376 377 378 379
		/*
		 * CE_INTENT_TO_ADD entries exist on on-disk index but
		 * they are not part of generated trees. Invalidate up
		 * to root to force cache-tree users to read elsewhere.
		 */
380
		if (!sub && ce_intent_to_add(ce)) {
381
			to_invalidate = 1;
382
			continue;
383
		}
J
Junio C Hamano 已提交
384

385 386 387 388 389 390
		/*
		 * "sub" can be an empty tree if all subentries are i-t-a.
		 */
		if (contains_ita && !hashcmp(sha1, EMPTY_TREE_SHA1_BIN))
			continue;

P
Pierre Habouzit 已提交
391 392 393
		strbuf_grow(&buffer, entlen + 100);
		strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
		strbuf_add(&buffer, sha1, 20);
J
Junio C Hamano 已提交
394 395

#if DEBUG
396
		fprintf(stderr, "cache-tree update-one %o %.*s\n",
J
Junio C Hamano 已提交
397 398 399 400
			mode, entlen, path + baselen);
#endif
	}

401 402 403 404
	if (repair) {
		unsigned char sha1[20];
		hash_sha1_file(buffer.buf, buffer.len, tree_type, sha1);
		if (has_sha1_file(sha1))
405
			hashcpy(it->oid.hash, sha1);
406 407 408
		else
			to_invalidate = 1;
	} else if (dryrun)
409 410 411
		hash_sha1_file(buffer.buf, buffer.len, tree_type,
			       it->oid.hash);
	else if (write_sha1_file(buffer.buf, buffer.len, tree_type, it->oid.hash)) {
412 413 414 415
		strbuf_release(&buffer);
		return -1;
	}

P
Pierre Habouzit 已提交
416
	strbuf_release(&buffer);
417
	it->entry_count = to_invalidate ? -1 : i - *skip_count;
J
Junio C Hamano 已提交
418
#if DEBUG
419
	fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
J
Junio C Hamano 已提交
420
		it->entry_count, it->subtree_nr,
421
		oid_to_hex(&it->oid));
J
Junio C Hamano 已提交
422 423 424 425
#endif
	return i;
}

426
int cache_tree_update(struct index_state *istate, int flags)
J
Junio C Hamano 已提交
427
{
428 429 430 431 432
	struct cache_tree *it = istate->cache_tree;
	struct cache_entry **cache = istate->cache;
	int entries = istate->cache_nr;
	int skip, i = verify_cache(cache, entries, flags);

J
Junio C Hamano 已提交
433 434
	if (i)
		return i;
435
	i = update_one(it, cache, entries, "", 0, &skip, flags);
J
Junio C Hamano 已提交
436 437
	if (i < 0)
		return i;
438
	istate->cache_changed |= CACHE_TREE_CHANGED;
J
Junio C Hamano 已提交
439 440 441
	return 0;
}

442 443
static void write_one(struct strbuf *buffer, struct cache_tree *it,
                      const char *path, int pathlen)
J
Junio C Hamano 已提交
444 445 446 447 448 449 450 451 452
{
	int i;

	/* One "cache-tree" entry consists of the following:
	 * path (NUL terminated)
	 * entry_count, subtree_nr ("%d %d\n")
	 * tree-sha1 (missing if invalid)
	 * subtree_nr "cache-tree" entries for subtrees.
	 */
P
Pierre Habouzit 已提交
453 454 455
	strbuf_grow(buffer, pathlen + 100);
	strbuf_add(buffer, path, pathlen);
	strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
J
Junio C Hamano 已提交
456 457 458 459 460

#if DEBUG
	if (0 <= it->entry_count)
		fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
			pathlen, path, it->entry_count, it->subtree_nr,
461
			oid_to_hex(&it->oid));
J
Junio C Hamano 已提交
462 463 464 465 466 467
	else
		fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
			pathlen, path, it->subtree_nr);
#endif

	if (0 <= it->entry_count) {
468
		strbuf_add(buffer, it->oid.hash, 20);
J
Junio C Hamano 已提交
469 470 471
	}
	for (i = 0; i < it->subtree_nr; i++) {
		struct cache_tree_sub *down = it->down[i];
472 473 474 475 476 477
		if (i) {
			struct cache_tree_sub *prev = it->down[i-1];
			if (subtree_name_cmp(down->name, down->namelen,
					     prev->name, prev->namelen) <= 0)
				die("fatal - unsorted cache subtree");
		}
478
		write_one(buffer, down->cache_tree, down->name, down->namelen);
J
Junio C Hamano 已提交
479 480 481
	}
}

482
void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
J
Junio C Hamano 已提交
483
{
484
	write_one(sb, root, "", 0);
J
Junio C Hamano 已提交
485 486 487 488 489 490
}

static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
{
	const char *buf = *buffer;
	unsigned long size = *size_p;
491 492
	const char *cp;
	char *ep;
J
Junio C Hamano 已提交
493 494 495 496 497 498 499 500 501 502 503 504 505
	struct cache_tree *it;
	int i, subtree_nr;

	it = NULL;
	/* skip name, but make sure name exists */
	while (size && *buf) {
		size--;
		buf++;
	}
	if (!size)
		goto free_return;
	buf++; size--;
	it = cache_tree();
506 507 508 509 510 511 512 513

	cp = buf;
	it->entry_count = strtol(cp, &ep, 10);
	if (cp == ep)
		goto free_return;
	cp = ep;
	subtree_nr = strtol(cp, &ep, 10);
	if (cp == ep)
J
Junio C Hamano 已提交
514 515 516 517 518 519 520 521 522 523 524
		goto free_return;
	while (size && *buf && *buf != '\n') {
		size--;
		buf++;
	}
	if (!size)
		goto free_return;
	buf++; size--;
	if (0 <= it->entry_count) {
		if (size < 20)
			goto free_return;
525
		hashcpy(it->oid.hash, (const unsigned char*)buf);
J
Junio C Hamano 已提交
526 527 528 529 530 531 532 533
		buf += 20;
		size -= 20;
	}

#if DEBUG
	if (0 <= it->entry_count)
		fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
			*buffer, it->entry_count, subtree_nr,
534
			oid_to_hex(&it->oid));
J
Junio C Hamano 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
	else
		fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
			*buffer, subtree_nr);
#endif

	/*
	 * Just a heuristic -- we do not add directories that often but
	 * we do not want to have to extend it immediately when we do,
	 * hence +2.
	 */
	it->subtree_alloc = subtree_nr + 2;
	it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
	for (i = 0; i < subtree_nr; i++) {
		/* read each subtree */
		struct cache_tree *sub;
550
		struct cache_tree_sub *subtree;
J
Junio C Hamano 已提交
551
		const char *name = buf;
552

J
Junio C Hamano 已提交
553 554 555
		sub = read_one(&buf, &size);
		if (!sub)
			goto free_return;
556
		subtree = cache_tree_sub(it, name);
557
		subtree->cache_tree = sub;
J
Junio C Hamano 已提交
558 559 560 561 562 563 564 565
	}
	if (subtree_nr != it->subtree_nr)
		die("cache-tree: internal error");
	*buffer = buf;
	*size_p = size;
	return it;

 free_return:
566
	cache_tree_free(&it);
J
Junio C Hamano 已提交
567 568 569
	return NULL;
}

570
struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
J
Junio C Hamano 已提交
571
{
572
	if (buffer[0])
J
Junio C Hamano 已提交
573 574 575
		return NULL; /* not the whole tree */
	return read_one(&buffer, &size);
}
J
Junio C Hamano 已提交
576

577
static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
J
Junio C Hamano 已提交
578
{
579 580
	if (!it)
		return NULL;
J
Junio C Hamano 已提交
581 582 583 584
	while (*path) {
		const char *slash;
		struct cache_tree_sub *sub;

585
		slash = strchrnul(path, '/');
586 587 588
		/*
		 * Between path and slash is the name of the subtree
		 * to look for.
J
Junio C Hamano 已提交
589 590 591 592 593
		 */
		sub = find_subtree(it, path, slash - path, 0);
		if (!sub)
			return NULL;
		it = sub->cache_tree;
594

J
Junio C Hamano 已提交
595
		path = slash;
596 597
		while (*path == '/')
			path++;
J
Junio C Hamano 已提交
598 599 600
	}
	return it;
}
601

602
int write_index_as_tree(unsigned char *sha1, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
603
{
M
Martin Ågren 已提交
604
	int entries, was_valid;
605
	struct lock_file lock_file = LOCK_INIT;
606
	int ret = 0;
607

M
Martin Ågren 已提交
608
	hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
609

610
	entries = read_index_from(index_state, index_path, get_git_dir());
611 612 613 614
	if (entries < 0) {
		ret = WRITE_TREE_UNREADABLE_INDEX;
		goto out;
	}
J
Junio C Hamano 已提交
615
	if (flags & WRITE_TREE_IGNORE_CACHE_TREE)
616
		cache_tree_free(&index_state->cache_tree);
617

618 619
	if (!index_state->cache_tree)
		index_state->cache_tree = cache_tree();
620

621
	was_valid = cache_tree_fully_valid(index_state->cache_tree);
622
	if (!was_valid) {
623 624 625 626
		if (cache_tree_update(index_state, flags) < 0) {
			ret = WRITE_TREE_UNMERGED_INDEX;
			goto out;
		}
M
Martin Ågren 已提交
627
		write_locked_index(index_state, &lock_file, COMMIT_LOCK);
628 629 630 631 632 633 634 635 636
		/* Not being able to write is fine -- we are only interested
		 * in updating the cache-tree part, and if the next caller
		 * ends up using the old index with unupdated cache-tree part
		 * it misses the work we did here, but that is just a
		 * performance penalty and not a big deal.
		 */
	}

	if (prefix) {
637 638
		struct cache_tree *subtree;
		subtree = cache_tree_find(index_state->cache_tree, prefix);
639 640 641 642
		if (!subtree) {
			ret = WRITE_TREE_PREFIX_ERROR;
			goto out;
		}
643
		hashcpy(sha1, subtree->oid.hash);
644 645
	}
	else
646
		hashcpy(sha1, index_state->cache_tree->oid.hash);
647

648
out:
M
Martin Ågren 已提交
649
	rollback_lock_file(&lock_file);
650
	return ret;
651
}
652

653 654 655 656 657
int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
{
	return write_index_as_tree(sha1, &the_index, get_index_file(), flags, prefix);
}

658 659 660 661 662 663
static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
{
	struct tree_desc desc;
	struct name_entry entry;
	int cnt;

664
	oidcpy(&it->oid, &tree->object.oid);
665 666 667 668 669 670 671
	init_tree_desc(&desc, tree->buffer, tree->size);
	cnt = 0;
	while (tree_entry(&desc, &entry)) {
		if (!S_ISDIR(entry.mode))
			cnt++;
		else {
			struct cache_tree_sub *sub;
672
			struct tree *subtree = lookup_tree(entry.oid);
673 674 675 676 677 678 679 680 681 682 683
			if (!subtree->object.parsed)
				parse_tree(subtree);
			sub = cache_tree_sub(it, entry.path);
			sub->cache_tree = cache_tree();
			prime_cache_tree_rec(sub->cache_tree, subtree);
			cnt += sub->cache_tree->entry_count;
		}
	}
	it->entry_count = cnt;
}

684
void prime_cache_tree(struct index_state *istate, struct tree *tree)
685
{
686 687 688 689
	cache_tree_free(&istate->cache_tree);
	istate->cache_tree = cache_tree();
	prime_cache_tree_rec(istate->cache_tree, tree);
	istate->cache_changed |= CACHE_TREE_CHANGED;
690
}
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718

/*
 * find the cache_tree that corresponds to the current level without
 * exploding the full path into textual form.  The root of the
 * cache tree is given as "root", and our current level is "info".
 * (1) When at root level, info->prev is NULL, so it is "root" itself.
 * (2) Otherwise, find the cache_tree that corresponds to one level
 *     above us, and find ourselves in there.
 */
static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
							 struct traverse_info *info)
{
	struct cache_tree *our_parent;

	if (!info->prev)
		return root;
	our_parent = find_cache_tree_from_traversal(root, info->prev);
	return cache_tree_find(our_parent, info->name.path);
}

int cache_tree_matches_traversal(struct cache_tree *root,
				 struct name_entry *ent,
				 struct traverse_info *info)
{
	struct cache_tree *it;

	it = find_cache_tree_from_traversal(root, info);
	it = cache_tree_find(it, ent->path);
719
	if (it && it->entry_count > 0 && !oidcmp(ent->oid, &it->oid))
720 721 722
		return it->entry_count;
	return 0;
}
723

724
int update_main_cache_tree(int flags)
725 726 727
{
	if (!the_index.cache_tree)
		the_index.cache_tree = cache_tree();
728
	return cache_tree_update(&the_index, flags);
729
}