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

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

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

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

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

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
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) {
		int mi = (lo + hi) / 2;
		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 已提交
63 64 65 66 67 68
static struct cache_tree_sub *find_subtree(struct cache_tree *it,
					   const char *path,
					   int pathlen,
					   int create)
{
	struct cache_tree_sub *down;
69 70 71
	int pos = subtree_pos(it, path, pathlen);
	if (0 <= pos)
		return it->down[pos];
J
Junio C Hamano 已提交
72 73
	if (!create)
		return NULL;
74 75

	pos = -pos-1;
J
Junio C Hamano 已提交
76 77 78 79 80
	if (it->subtree_alloc <= it->subtree_nr) {
		it->subtree_alloc = alloc_nr(it->subtree_alloc);
		it->down = xrealloc(it->down, it->subtree_alloc *
				    sizeof(*it->down));
	}
81 82
	it->subtree_nr++;

J
Junio C Hamano 已提交
83
	down = xmalloc(sizeof(*down) + pathlen + 1);
84
	down->cache_tree = NULL;
J
Junio C Hamano 已提交
85 86
	down->namelen = pathlen;
	memcpy(down->name, path, pathlen);
87 88 89 90 91 92 93
	down->name[pathlen] = 0;

	if (pos < it->subtree_nr)
		memmove(it->down + pos + 1,
			it->down + pos,
			sizeof(down) * (it->subtree_nr - pos - 1));
	it->down[pos] = down;
J
Junio C Hamano 已提交
94 95 96
	return down;
}

97 98 99 100 101 102
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);
}

J
Junio C Hamano 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115
void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
{
	/* 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;

116 117 118 119
#if DEBUG
	fprintf(stderr, "cache-tree invalidate <%s>\n", path);
#endif

J
Junio C Hamano 已提交
120 121 122 123 124
	if (!it)
		return;
	slash = strchr(path, '/');
	it->entry_count = -1;
	if (!slash) {
125
		int pos;
J
Junio C Hamano 已提交
126
		namelen = strlen(path);
127 128 129 130
		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 已提交
131 132
			/* 0 1 2 3 4 5
			 *       ^     ^subtree_nr = 6
133
			 *       pos
J
Junio C Hamano 已提交
134
			 * move 4 and 5 up one place (2 entries)
135
			 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
J
Junio C Hamano 已提交
136
			 */
137
			memmove(it->down+pos, it->down+pos+1,
J
Junio C Hamano 已提交
138
				sizeof(struct cache_tree_sub *) *
139
				(it->subtree_nr - pos - 1));
J
Junio C Hamano 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
			it->subtree_nr--;
		}
		return;
	}
	namelen = slash - path;
	down = find_subtree(it, path, namelen, 0);
	if (down)
		cache_tree_invalidate_path(down->cache_tree, slash + 1);
}

static int verify_cache(struct cache_entry **cache,
			int entries)
{
	int i, funny;

	/* Verify that the tree is merged */
	funny = 0;
	for (i = 0; i < entries; i++) {
		struct cache_entry *ce = cache[i];
159
		if (ce_stage(ce) || (ce->ce_flags & CE_INTENT_TO_ADD)) {
J
Junio C Hamano 已提交
160 161 162 163
			if (10 < ++funny) {
				fprintf(stderr, "...\n");
				break;
			}
164 165 166 167 168 169
			if (ce_stage(ce))
				fprintf(stderr, "%s: unmerged (%s)\n",
					ce->name, sha1_to_hex(ce->sha1));
			else
				fprintf(stderr, "%s: not added yet\n",
					ce->name);
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 226 227 228 229 230 231 232 233 234
int cache_tree_fully_valid(struct cache_tree *it)
{
	int i;
	if (!it)
		return 0;
	if (it->entry_count < 0 || !has_sha1_file(it->sha1))
		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 236 237 238 239
static int update_one(struct cache_tree *it,
		      struct cache_entry **cache,
		      int entries,
		      const char *base,
		      int baselen,
240 241
		      int missing_ok,
		      int dryrun)
J
Junio C Hamano 已提交
242
{
P
Pierre Habouzit 已提交
243
	struct strbuf buffer;
J
Junio C Hamano 已提交
244 245
	int i;

246
	if (0 <= it->entry_count && has_sha1_file(it->sha1))
J
Junio C Hamano 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
		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.
	 */
	for (i = 0; i < entries; i++) {
		struct cache_entry *ce = cache[i];
		struct cache_tree_sub *sub;
		const char *path, *slash;
		int pathlen, sublen, subcnt;

		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)
			continue;
		/*
		 * 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,
287 288
				    missing_ok,
				    dryrun);
289 290
		if (subcnt < 0)
			return subcnt;
J
Junio C Hamano 已提交
291 292 293 294 295 296 297 298 299
		i += subcnt - 1;
		sub->used = 1;
	}

	discard_unused_subtrees(it);

	/*
	 * Then write out the tree object for this level.
	 */
300
	strbuf_init(&buffer, 8192);
J
Junio C Hamano 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327

	for (i = 0; i < entries; i++) {
		struct cache_entry *ce = cache[i];
		struct cache_tree_sub *sub;
		const char *path, *slash;
		int pathlen, entlen;
		const unsigned char *sha1;
		unsigned mode;

		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);
			i += sub->cache_tree->entry_count - 1;
			sha1 = sub->cache_tree->sha1;
			mode = S_IFDIR;
		}
		else {
			sha1 = ce->sha1;
328
			mode = ce->ce_mode;
J
Junio C Hamano 已提交
329 330
			entlen = pathlen - baselen;
		}
M
Martin Waitz 已提交
331
		if (mode != S_IFGITLINK && !missing_ok && !has_sha1_file(sha1))
J
Junio C Hamano 已提交
332 333
			return error("invalid object %s", sha1_to_hex(sha1));

334
		if (ce->ce_flags & CE_REMOVE)
J
Junio C Hamano 已提交
335 336
			continue; /* entry being removed */

P
Pierre Habouzit 已提交
337 338 339
		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 已提交
340 341

#if DEBUG
342
		fprintf(stderr, "cache-tree update-one %o %.*s\n",
J
Junio C Hamano 已提交
343 344 345 346
			mode, entlen, path + baselen);
#endif
	}

R
Rene Scharfe 已提交
347
	if (dryrun)
P
Pierre Habouzit 已提交
348
		hash_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1);
349 350 351 352 353
	else if (write_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1)) {
		strbuf_release(&buffer);
		return -1;
	}

P
Pierre Habouzit 已提交
354
	strbuf_release(&buffer);
J
Junio C Hamano 已提交
355 356
	it->entry_count = i;
#if DEBUG
357
	fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
J
Junio C Hamano 已提交
358 359 360 361 362 363 364 365 366
		it->entry_count, it->subtree_nr,
		sha1_to_hex(it->sha1));
#endif
	return i;
}

int cache_tree_update(struct cache_tree *it,
		      struct cache_entry **cache,
		      int entries,
367 368
		      int missing_ok,
		      int dryrun)
J
Junio C Hamano 已提交
369 370 371 372 373
{
	int i;
	i = verify_cache(cache, entries);
	if (i)
		return i;
374
	i = update_one(it, cache, entries, "", 0, missing_ok, dryrun);
J
Junio C Hamano 已提交
375 376 377 378 379
	if (i < 0)
		return i;
	return 0;
}

380 381
static void write_one(struct strbuf *buffer, struct cache_tree *it,
                      const char *path, int pathlen)
J
Junio C Hamano 已提交
382 383 384 385 386 387 388 389 390
{
	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 已提交
391 392 393
	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 已提交
394 395 396 397 398 399 400 401 402 403 404 405

#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,
			sha1_to_hex(it->sha1));
	else
		fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
			pathlen, path, it->subtree_nr);
#endif

	if (0 <= it->entry_count) {
P
Pierre Habouzit 已提交
406
		strbuf_add(buffer, it->sha1, 20);
J
Junio C Hamano 已提交
407 408 409
	}
	for (i = 0; i < it->subtree_nr; i++) {
		struct cache_tree_sub *down = it->down[i];
410 411 412 413 414 415
		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");
		}
416
		write_one(buffer, down->cache_tree, down->name, down->namelen);
J
Junio C Hamano 已提交
417 418 419
	}
}

420
void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
J
Junio C Hamano 已提交
421
{
422
	write_one(sb, root, "", 0);
J
Junio C Hamano 已提交
423 424 425 426 427 428
}

static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
{
	const char *buf = *buffer;
	unsigned long size = *size_p;
429 430
	const char *cp;
	char *ep;
J
Junio C Hamano 已提交
431 432 433 434 435 436 437 438 439 440 441 442 443
	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();
444 445 446 447 448 449 450 451

	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 已提交
452 453 454 455 456 457 458 459 460 461 462
		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;
J
Junio C Hamano 已提交
463
		hashcpy(it->sha1, (const unsigned char*)buf);
J
Junio C Hamano 已提交
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
		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,
			sha1_to_hex(it->sha1));
	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;
488
		struct cache_tree_sub *subtree;
J
Junio C Hamano 已提交
489
		const char *name = buf;
490

J
Junio C Hamano 已提交
491 492 493
		sub = read_one(&buf, &size);
		if (!sub)
			goto free_return;
494
		subtree = cache_tree_sub(it, name);
495
		subtree->cache_tree = sub;
J
Junio C Hamano 已提交
496 497 498 499 500 501 502 503
	}
	if (subtree_nr != it->subtree_nr)
		die("cache-tree: internal error");
	*buffer = buf;
	*size_p = size;
	return it;

 free_return:
504
	cache_tree_free(&it);
J
Junio C Hamano 已提交
505 506 507
	return NULL;
}

508
struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
J
Junio C Hamano 已提交
509
{
510
	if (buffer[0])
J
Junio C Hamano 已提交
511 512 513
		return NULL; /* not the whole tree */
	return read_one(&buffer, &size);
}
J
Junio C Hamano 已提交
514

515
static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
J
Junio C Hamano 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
{
	while (*path) {
		const char *slash;
		struct cache_tree_sub *sub;

		slash = strchr(path, '/');
		if (!slash)
			slash = path + strlen(path);
		/* between path and slash is the name of the
		 * subtree to look for.
		 */
		sub = find_subtree(it, path, slash - path, 0);
		if (!sub)
			return NULL;
		it = sub->cache_tree;
		if (slash)
			while (*slash && *slash == '/')
				slash++;
		if (!slash || !*slash)
			return it; /* prefix ended with slashes */
		path = slash;
	}
	return it;
}
540

J
Junio C Hamano 已提交
541
int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
542 543
{
	int entries, was_valid, newfd;
J
Junio C Hamano 已提交
544
	struct lock_file *lock_file;
545 546 547 548 549

	/*
	 * We can't free this memory, it becomes part of a linked list
	 * parsed atexit()
	 */
J
Junio C Hamano 已提交
550
	lock_file = xcalloc(1, sizeof(struct lock_file));
551 552 553 554 555 556

	newfd = hold_locked_index(lock_file, 1);

	entries = read_cache();
	if (entries < 0)
		return WRITE_TREE_UNREADABLE_INDEX;
J
Junio C Hamano 已提交
557 558
	if (flags & WRITE_TREE_IGNORE_CACHE_TREE)
		cache_tree_free(&(active_cache_tree));
559 560 561 562 563 564

	if (!active_cache_tree)
		active_cache_tree = cache_tree();

	was_valid = cache_tree_fully_valid(active_cache_tree);
	if (!was_valid) {
J
Junio C Hamano 已提交
565 566
		int missing_ok = flags & WRITE_TREE_MISSING_OK;

567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
		if (cache_tree_update(active_cache_tree,
				      active_cache, active_nr,
				      missing_ok, 0) < 0)
			return WRITE_TREE_UNMERGED_INDEX;
		if (0 <= newfd) {
			if (!write_cache(newfd, active_cache, active_nr) &&
			    !commit_lock_file(lock_file))
				newfd = -1;
		}
		/* 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) {
		struct cache_tree *subtree =
			cache_tree_find(active_cache_tree, prefix);
		if (!subtree)
			return WRITE_TREE_PREFIX_ERROR;
		hashcpy(sha1, subtree->sha1);
	}
	else
		hashcpy(sha1, active_cache_tree->sha1);

	if (0 <= newfd)
		rollback_lock_file(lock_file);

	return 0;
}
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631

static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
{
	struct tree_desc desc;
	struct name_entry entry;
	int cnt;

	hashcpy(it->sha1, tree->object.sha1);
	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;
			struct tree *subtree = lookup_tree(entry.sha1);
			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;
}

void prime_cache_tree(struct cache_tree **it, struct tree *tree)
{
	cache_tree_free(it);
	*it = cache_tree();
	prime_cache_tree_rec(*it, tree);
}