pack-objects.c 33.1 KB
Newer Older
Michael Schubert committed
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
Michael Schubert committed
3 4 5 6 7 8 9
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include "pack-objects.h"

10
#include "zstream.h"
Michael Schubert committed
11 12 13 14 15 16
#include "delta.h"
#include "iterator.h"
#include "netops.h"
#include "pack.h"
#include "thread-utils.h"
#include "tree.h"
17
#include "util.h"
Michael Schubert committed
18 19 20 21 22 23 24 25 26 27 28

#include "git2/pack.h"
#include "git2/commit.h"
#include "git2/tag.h"
#include "git2/indexer.h"
#include "git2/config.h"

struct unpacked {
	git_pobject *object;
	void *data;
	struct git_delta_index *index;
Linquize committed
29
	int depth;
Michael Schubert committed
30 31
};

32 33 34 35 36
struct tree_walk_context {
	git_packbuilder *pb;
	git_buf buf;
};

37
struct pack_write_context {
38
	git_indexer *indexer;
39 40 41
	git_transfer_progress *stats;
};

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#ifdef GIT_THREADS

#define GIT_PACKBUILDER__MUTEX_OP(pb, mtx, op) do { \
		int result = git_mutex_##op(&(pb)->mtx); \
		assert(!result); \
		GIT_UNUSED(result); \
	} while (0)

#else

#define GIT_PACKBUILDER__MUTEX_OP(pb,mtx,op) GIT_UNUSED(pb)

#endif /* GIT_THREADS */

#define git_packbuilder__cache_lock(pb) GIT_PACKBUILDER__MUTEX_OP(pb, cache_mutex, lock)
#define git_packbuilder__cache_unlock(pb) GIT_PACKBUILDER__MUTEX_OP(pb, cache_mutex, unlock)
#define git_packbuilder__progress_lock(pb) GIT_PACKBUILDER__MUTEX_OP(pb, progress_mutex, lock)
#define git_packbuilder__progress_unlock(pb) GIT_PACKBUILDER__MUTEX_OP(pb, progress_mutex, unlock)

61 62 63
/* The minimal interval between progress updates (in seconds). */
#define MIN_PROGRESS_UPDATE_INTERVAL 0.5

64 65 66
/* Size of the buffer to feed to zlib */
#define COMPRESS_BUFLEN (1024 * 1024)

Michael Schubert committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
static unsigned name_hash(const char *name)
{
	unsigned c, hash = 0;

	if (!name)
		return 0;

	/*
	 * This effectively just creates a sortable number from the
	 * last sixteen non-whitespace characters. Last characters
	 * count "most", so things that end in ".c" sort together.
	 */
	while ((c = *name++) != 0) {
		if (git__isspace(c))
			continue;
		hash = (hash >> 2) + (c << 24);
	}
	return hash;
}

static int packbuilder_config(git_packbuilder *pb)
{
	git_config *config;
	int ret;
91
	int64_t val;
Michael Schubert committed
92

93
	if ((ret = git_repository_config_snapshot(&config, pb->repo)) < 0)
94
		return ret;
Michael Schubert committed
95

96 97 98 99 100
#define config_get(KEY,DST,DFLT) do { \
	ret = git_config_get_int64(&val, config, KEY); \
	if (!ret) (DST) = val; \
	else if (ret == GIT_ENOTFOUND) (DST) = (DFLT); \
	else if (ret < 0) return -1; } while (0)
Michael Schubert committed
101 102 103 104 105 106 107 108 109 110 111

	config_get("pack.deltaCacheSize", pb->max_delta_cache_size,
		   GIT_PACK_DELTA_CACHE_SIZE);
	config_get("pack.deltaCacheLimit", pb->cache_max_small_delta_size,
		   GIT_PACK_DELTA_CACHE_LIMIT);
	config_get("pack.deltaCacheSize", pb->big_file_threshold,
		   GIT_PACK_BIG_FILE_THRESHOLD);
	config_get("pack.windowMemory", pb->window_memory_limit, 0);

#undef config_get

112 113
	git_config_free(config);

Michael Schubert committed
114 115 116 117 118 119 120 121 122
	return 0;
}

int git_packbuilder_new(git_packbuilder **out, git_repository *repo)
{
	git_packbuilder *pb;

	*out = NULL;

123
	pb = git__calloc(1, sizeof(*pb));
Michael Schubert committed
124 125
	GITERR_CHECK_ALLOC(pb);

126
	pb->object_ix = git_oidmap_alloc();
127 128 129

	if (!pb->object_ix)
		goto on_error;
130

Michael Schubert committed
131 132 133
	pb->repo = repo;
	pb->nr_threads = 1; /* do not spawn any thread by default */

134
	if (git_hash_ctx_init(&pb->ctx) < 0 ||
135
		git_zstream_init(&pb->zstream) < 0 ||
136 137
		git_repository_odb(&pb->odb, repo) < 0 ||
		packbuilder_config(pb) < 0)
Michael Schubert committed
138 139
		goto on_error;

140 141 142 143 144
#ifdef GIT_THREADS

	if (git_mutex_init(&pb->cache_mutex) ||
		git_mutex_init(&pb->progress_mutex) ||
		git_cond_init(&pb->progress_cond))
Russell Belfer committed
145 146
	{
		giterr_set(GITERR_OS, "Failed to initialize packbuilder mutex");
Michael Schubert committed
147
		goto on_error;
Russell Belfer committed
148
	}
Michael Schubert committed
149

150 151
#endif

Michael Schubert committed
152 153 154 155
	*out = pb;
	return 0;

on_error:
156
	git_packbuilder_free(pb);
Michael Schubert committed
157 158 159
	return -1;
}

160
unsigned int git_packbuilder_set_threads(git_packbuilder *pb, unsigned int n)
Michael Schubert committed
161 162
{
	assert(pb);
163 164

#ifdef GIT_THREADS
Michael Schubert committed
165
	pb->nr_threads = n;
166 167 168 169 170
#else
	GIT_UNUSED(n);
	assert(1 == pb->nr_threads);
#endif

171
	return pb->nr_threads;
Michael Schubert committed
172 173
}

174 175 176 177 178 179 180 181 182 183 184 185 186 187
static void rehash(git_packbuilder *pb)
{
	git_pobject *po;
	khiter_t pos;
	unsigned int i;
	int ret;

	kh_clear(oid, pb->object_ix);
	for (i = 0, po = pb->object_list; i < pb->nr_objects; i++, po++) {
		pos = kh_put(oid, pb->object_ix, &po->id, &ret);
		kh_value(pb->object_ix, pos) = po;
	}
}

Michael Schubert committed
188 189 190 191
int git_packbuilder_insert(git_packbuilder *pb, const git_oid *oid,
			   const char *name)
{
	git_pobject *po;
192
	khiter_t pos;
193
	size_t newsize;
194
	int ret;
Michael Schubert committed
195 196 197

	assert(pb && oid);

198 199
	/* If the object already exists in the hash table, then we don't
	 * have any work to do */
200 201
	pos = kh_get(oid, pb->object_ix, oid);
	if (pos != kh_end(pb->object_ix))
Michael Schubert committed
202 203 204
		return 0;

	if (pb->nr_objects >= pb->nr_alloc) {
205 206
		GITERR_CHECK_ALLOC_ADD(&newsize, pb->nr_alloc, 1024);
		GITERR_CHECK_ALLOC_MULTIPLY(&newsize, newsize, 3 / 2);
207 208 209 210 211 212 213

		if (!git__is_uint32(newsize)) {
			giterr_set(GITERR_NOMEMORY, "Packfile too large to fit in memory.");
			return -1;
		}

		pb->nr_alloc = (uint32_t)newsize;
214

215 216
		pb->object_list = git__reallocarray(pb->object_list,
			pb->nr_alloc, sizeof(*po));
Michael Schubert committed
217
		GITERR_CHECK_ALLOC(pb->object_list);
218
		rehash(pb);
Michael Schubert committed
219 220
	}

221
	po = pb->object_list + pb->nr_objects;
Michael Schubert committed
222 223
	memset(po, 0x0, sizeof(*po));

224 225
	if ((ret = git_odb_read_header(&po->size, &po->type, pb->odb, oid)) < 0)
		return ret;
226 227

	pb->nr_objects++;
Michael Schubert committed
228
	git_oid_cpy(&po->id, oid);
229
	po->hash = name_hash(name);
Michael Schubert committed
230

231
	pos = kh_put(oid, pb->object_ix, &po->id, &ret);
232 233 234 235
	if (ret < 0) {
		giterr_set_oom();
		return ret;
	}
236 237
	assert(ret != 0);
	kh_value(pb->object_ix, pos) = po;
Michael Schubert committed
238

239 240
	pb->done = false;

241 242
	if (pb->progress_cb) {
		double current_time = git__timer();
243 244 245
		double elapsed = current_time - pb->last_progress_report_time;

		if (elapsed >= MIN_PROGRESS_UPDATE_INTERVAL) {
246
			pb->last_progress_report_time = current_time;
247

248
			ret = pb->progress_cb(
249
				GIT_PACKBUILDER_ADDING_OBJECTS,
250 251 252
				pb->nr_objects, 0, pb->progress_cb_payload);

			if (ret)
253
				return giterr_set_after_callback(ret);
254 255 256
		}
	}

Michael Schubert committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
	return 0;
}

static int get_delta(void **out, git_odb *odb, git_pobject *po)
{
	git_odb_object *src = NULL, *trg = NULL;
	unsigned long delta_size;
	void *delta_buf;

	*out = NULL;

	if (git_odb_read(&src, odb, &po->delta->id) < 0 ||
	    git_odb_read(&trg, odb, &po->id) < 0)
		goto on_error;

272 273 274 275
	delta_buf = git_delta(
		git_odb_object_data(src), (unsigned long)git_odb_object_size(src),
		git_odb_object_data(trg), (unsigned long)git_odb_object_size(trg),
		&delta_size, 0);
Michael Schubert committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

	if (!delta_buf || delta_size != po->delta_size) {
		giterr_set(GITERR_INVALID, "Delta size changed");
		goto on_error;
	}

	*out = delta_buf;

	git_odb_object_free(src);
	git_odb_object_free(trg);
	return 0;

on_error:
	git_odb_object_free(src);
	git_odb_object_free(trg);
	return -1;
}

294 295 296 297 298
static int write_object(
	git_packbuilder *pb,
	git_pobject *po,
	int (*write_cb)(void *buf, size_t size, void *cb_data),
	void *cb_data)
Michael Schubert committed
299 300 301
{
	git_odb_object *obj = NULL;
	git_otype type;
302
	unsigned char hdr[10], *zbuf = NULL;
303
	void *data = NULL;
304 305
	size_t hdr_len, zbuf_len = COMPRESS_BUFLEN, data_len;
	int error;
Michael Schubert committed
306

307 308 309 310 311
	/*
	 * If we have a delta base, let's use the delta to save space.
	 * Otherwise load the whole object. 'data' ends up pointing to
	 * whatever data we want to put into the packfile.
	 */
Michael Schubert committed
312 313
	if (po->delta) {
		if (po->delta_data)
314 315
			data = po->delta_data;
		else if ((error = get_delta(&data, pb->odb, po)) < 0)
316 317 318
				goto done;

		data_len = po->delta_size;
Michael Schubert committed
319 320
		type = GIT_OBJ_REF_DELTA;
	} else {
321 322
		if ((error = git_odb_read(&obj, pb->odb, &po->id)) < 0)
			goto done;
Michael Schubert committed
323 324

		data = (void *)git_odb_object_data(obj);
325
		data_len = git_odb_object_size(obj);
Michael Schubert committed
326 327 328 329
		type = git_odb_object_type(obj);
	}

	/* Write header */
330
	hdr_len = git_packfile__object_header(hdr, data_len, type);
Michael Schubert committed
331

332 333 334
	if ((error = write_cb(hdr, hdr_len, cb_data)) < 0 ||
		(error = git_hash_update(&pb->ctx, hdr, hdr_len)) < 0)
		goto done;
Michael Schubert committed
335 336

	if (type == GIT_OBJ_REF_DELTA) {
337 338 339
		if ((error = write_cb(po->delta->id.id, GIT_OID_RAWSZ, cb_data)) < 0 ||
			(error = git_hash_update(&pb->ctx, po->delta->id.id, GIT_OID_RAWSZ)) < 0)
			goto done;
Michael Schubert committed
340 341 342
	}

	/* Write data */
343 344 345 346 347 348 349 350 351 352
	if (po->z_delta_size) {
		data_len = po->z_delta_size;

		if ((error = write_cb(data, data_len, cb_data)) < 0 ||
			(error = git_hash_update(&pb->ctx, data, data_len)) < 0)
			goto done;
	} else {
		zbuf = git__malloc(zbuf_len);
		GITERR_CHECK_ALLOC(zbuf);

353
		git_zstream_reset(&pb->zstream);
354
		git_zstream_set_input(&pb->zstream, data, data_len);
355

356 357 358 359
		while (!git_zstream_done(&pb->zstream)) {
			if ((error = git_zstream_get_output(zbuf, &zbuf_len, &pb->zstream)) < 0 ||
				(error = write_cb(zbuf, zbuf_len, cb_data)) < 0 ||
				(error = git_hash_update(&pb->ctx, zbuf, zbuf_len)) < 0)
360 361
				goto done;

362
			zbuf_len = COMPRESS_BUFLEN; /* reuse buffer */
363
		}
Michael Schubert committed
364 365
	}

366 367 368 369 370 371 372 373 374
	/*
	 * If po->delta is true, data is a delta and it is our
	 * responsibility to free it (otherwise it's a git_object's
	 * data). We set po->delta_data to NULL in case we got the
	 * data from there instead of get_delta(). If we didn't,
	 * there's no harm.
	 */
	if (po->delta) {
		git__free(data);
375 376
		po->delta_data = NULL;
	}
Michael Schubert committed
377 378 379

	pb->nr_written++;

380 381
done:
	git__free(zbuf);
Michael Schubert committed
382
	git_odb_object_free(obj);
383
	return error;
Michael Schubert committed
384 385 386 387 388 389 390 391 392
}

enum write_one_status {
	WRITE_ONE_SKIP = -1, /* already written */
	WRITE_ONE_BREAK = 0, /* writing this will bust the limit; not written */
	WRITE_ONE_WRITTEN = 1, /* normal */
	WRITE_ONE_RECURSIVE = 2 /* already scheduled to be written */
};

393 394 395 396 397 398
static int write_one(
	enum write_one_status *status,
	git_packbuilder *pb,
	git_pobject *po,
	int (*write_cb)(void *buf, size_t size, void *cb_data),
	void *cb_data)
Michael Schubert committed
399
{
400 401
	int error;

Michael Schubert committed
402 403 404 405 406 407 408 409 410 411
	if (po->recursing) {
		*status = WRITE_ONE_RECURSIVE;
		return 0;
	} else if (po->written) {
		*status = WRITE_ONE_SKIP;
		return 0;
	}

	if (po->delta) {
		po->recursing = 1;
412 413 414 415 416 417

		if ((error = write_one(status, pb, po->delta, write_cb, cb_data)) < 0)
			return error;

		/* we cannot depend on this one */
		if (*status == WRITE_ONE_RECURSIVE)
Michael Schubert committed
418 419 420
			po->delta = NULL;
	}

421
	*status = WRITE_ONE_WRITTEN;
Michael Schubert committed
422 423
	po->written = 1;
	po->recursing = 0;
424 425

	return write_object(pb, po, write_cb, cb_data);
Michael Schubert committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
}

GIT_INLINE(void) add_to_write_order(git_pobject **wo, unsigned int *endp,
				    git_pobject *po)
{
	if (po->filled)
		return;
	wo[(*endp)++] = po;
	po->filled = 1;
}

static void add_descendants_to_write_order(git_pobject **wo, unsigned int *endp,
					   git_pobject *po)
{
	int add_to_order = 1;
	while (po) {
		if (add_to_order) {
			git_pobject *s;
			/* add this node... */
			add_to_write_order(wo, endp, po);
			/* all its siblings... */
			for (s = po->delta_sibling; s; s = s->delta_sibling) {
				add_to_write_order(wo, endp, s);
			}
		}
		/* drop down a level to add left subtree nodes if possible */
		if (po->delta_child) {
			add_to_order = 1;
			po = po->delta_child;
		} else {
			add_to_order = 0;
			/* our sibling might have some children, it is next */
			if (po->delta_sibling) {
				po = po->delta_sibling;
				continue;
			}
			/* go back to our parent node */
			po = po->delta;
			while (po && !po->delta_sibling) {
				/* we're on the right side of a subtree, keep
				 * going up until we can go right again */
				po = po->delta;
			}
			if (!po) {
				/* done- we hit our original root node */
				return;
			}
			/* pass it off to sibling at this level */
			po = po->delta_sibling;
		}
	};
}

static void add_family_to_write_order(git_pobject **wo, unsigned int *endp,
				      git_pobject *po)
{
	git_pobject *root;

	for (root = po; root->delta; root = root->delta)
		; /* nothing */
	add_descendants_to_write_order(wo, endp, root);
}

static int cb_tag_foreach(const char *name, git_oid *oid, void *data)
{
	git_packbuilder *pb = data;
492 493
	git_pobject *po;
	khiter_t pos;
Michael Schubert committed
494 495 496

	GIT_UNUSED(name);

497 498 499 500 501 502 503
	pos = kh_get(oid, pb->object_ix, oid);
	if (pos == kh_end(pb->object_ix))
		return 0;

	po = kh_value(pb->object_ix, pos);
	po->tagged = 1;

Michael Schubert committed
504
	/* TODO: peel objects */
505

Michael Schubert committed
506 507 508 509 510 511
	return 0;
}

static git_pobject **compute_write_order(git_packbuilder *pb)
{
	unsigned int i, wo_end, last_untagged;
512
	git_pobject **wo;
Michael Schubert committed
513

514
	if ((wo = git__mallocarray(pb->nr_objects, sizeof(*wo))) == NULL)
515
		return NULL;
Michael Schubert committed
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541

	for (i = 0; i < pb->nr_objects; i++) {
		git_pobject *po = pb->object_list + i;
		po->tagged = 0;
		po->filled = 0;
		po->delta_child = NULL;
		po->delta_sibling = NULL;
	}

	/*
	 * Fully connect delta_child/delta_sibling network.
	 * Make sure delta_sibling is sorted in the original
	 * recency order.
	 */
	for (i = pb->nr_objects; i > 0;) {
		git_pobject *po = &pb->object_list[--i];
		if (!po->delta)
			continue;
		/* Mark me as the first child */
		po->delta_sibling = po->delta->delta_child;
		po->delta->delta_child = po;
	}

	/*
	 * Mark objects that are at the tip of tags.
	 */
542 543
	if (git_tag_foreach(pb->repo, &cb_tag_foreach, pb) < 0) {
		git__free(wo);
Michael Schubert committed
544
		return NULL;
545
	}
Michael Schubert committed
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 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 599 600 601 602 603 604 605 606

	/*
	 * Give the objects in the original recency order until
	 * we see a tagged tip.
	 */
	for (i = wo_end = 0; i < pb->nr_objects; i++) {
		git_pobject *po = pb->object_list + i;
		if (po->tagged)
			break;
		add_to_write_order(wo, &wo_end, po);
	}
	last_untagged = i;

	/*
	 * Then fill all the tagged tips.
	 */
	for (; i < pb->nr_objects; i++) {
		git_pobject *po = pb->object_list + i;
		if (po->tagged)
			add_to_write_order(wo, &wo_end, po);
	}

	/*
	 * And then all remaining commits and tags.
	 */
	for (i = last_untagged; i < pb->nr_objects; i++) {
		git_pobject *po = pb->object_list + i;
		if (po->type != GIT_OBJ_COMMIT &&
		    po->type != GIT_OBJ_TAG)
			continue;
		add_to_write_order(wo, &wo_end, po);
	}

	/*
	 * And then all the trees.
	 */
	for (i = last_untagged; i < pb->nr_objects; i++) {
		git_pobject *po = pb->object_list + i;
		if (po->type != GIT_OBJ_TREE)
			continue;
		add_to_write_order(wo, &wo_end, po);
	}

	/*
	 * Finally all the rest in really tight order
	 */
	for (i = last_untagged; i < pb->nr_objects; i++) {
		git_pobject *po = pb->object_list + i;
		if (!po->filled)
			add_family_to_write_order(wo, &wo_end, po);
	}

	if (wo_end != pb->nr_objects) {
		giterr_set(GITERR_INVALID, "invalid write order");
		return NULL;
	}

	return wo;
}

static int write_pack(git_packbuilder *pb,
607 608
	int (*write_cb)(void *buf, size_t size, void *cb_data),
	void *cb_data)
Michael Schubert committed
609 610 611 612 613
{
	git_pobject **write_order;
	git_pobject *po;
	enum write_one_status status;
	struct git_pack_header ph;
614
	git_oid entry_oid;
Michael Schubert committed
615
	unsigned int i = 0;
616
	int error = 0;
Michael Schubert committed
617 618

	write_order = compute_write_order(pb);
619 620 621 622
	if (write_order == NULL) {
		error = -1;
		goto done;
	}
Michael Schubert committed
623 624 625 626 627 628

	/* Write pack header */
	ph.hdr_signature = htonl(PACK_SIGNATURE);
	ph.hdr_version = htonl(PACK_VERSION);
	ph.hdr_entries = htonl(pb->nr_objects);

629 630
	if ((error = write_cb(&ph, sizeof(ph), cb_data)) < 0 ||
		(error = git_hash_update(&pb->ctx, &ph, sizeof(ph))) < 0)
631
		goto done;
Michael Schubert committed
632 633 634 635 636 637

	pb->nr_remaining = pb->nr_objects;
	do {
		pb->nr_written = 0;
		for ( ; i < pb->nr_objects; ++i) {
			po = write_order[i];
638 639

			if ((error = write_one(&status, pb, po, write_cb, cb_data)) < 0)
640
				goto done;
Michael Schubert committed
641 642 643 644 645
		}

		pb->nr_remaining -= pb->nr_written;
	} while (pb->nr_remaining && i < pb->nr_objects);

646
	if ((error = git_hash_final(&entry_oid, &pb->ctx)) < 0)
647
		goto done;
Michael Schubert committed
648

649
	error = write_cb(entry_oid.id, GIT_OID_RAWSZ, cb_data);
Michael Schubert committed
650

651
done:
652 653 654 655 656 657 658 659 660
	/* if callback cancelled writing, we must still free delta_data */
	for ( ; i < pb->nr_objects; ++i) {
		po = write_order[i];
		if (po->delta_data) {
			git__free(po->delta_data);
			po->delta_data = NULL;
		}
	}

Michael Schubert committed
661
	git__free(write_order);
662
	return error;
Michael Schubert committed
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 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
}

static int write_pack_buf(void *buf, size_t size, void *data)
{
	git_buf *b = (git_buf *)data;
	return git_buf_put(b, buf, size);
}

static int type_size_sort(const void *_a, const void *_b)
{
	const git_pobject *a = (git_pobject *)_a;
	const git_pobject *b = (git_pobject *)_b;

	if (a->type > b->type)
		return -1;
	if (a->type < b->type)
		return 1;
	if (a->hash > b->hash)
		return -1;
	if (a->hash < b->hash)
		return 1;
	/*
	 * TODO
	 *
	if (a->preferred_base > b->preferred_base)
		return -1;
	if (a->preferred_base < b->preferred_base)
		return 1;
	*/
	if (a->size > b->size)
		return -1;
	if (a->size < b->size)
		return 1;
	return a < b ? -1 : (a > b); /* newest first */
}

static int delta_cacheable(git_packbuilder *pb, unsigned long src_size,
			   unsigned long trg_size, unsigned long delta_size)
{
	if (pb->max_delta_cache_size &&
		pb->delta_cache_size + delta_size > pb->max_delta_cache_size)
		return 0;

	if (delta_size < pb->cache_max_small_delta_size)
		return 1;

	/* cache delta, if objects are large enough compared to delta size */
	if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10))
		return 1;

	return 0;
}

static int try_delta(git_packbuilder *pb, struct unpacked *trg,
Linquize committed
717
		     struct unpacked *src, int max_depth,
Michael Schubert committed
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
		     unsigned long *mem_usage, int *ret)
{
	git_pobject *trg_object = trg->object;
	git_pobject *src_object = src->object;
	git_odb_object *obj;
	unsigned long trg_size, src_size, delta_size,
		      sizediff, max_size, sz;
	unsigned int ref_depth;
	void *delta_buf;

	/* Don't bother doing diffs between different types */
	if (trg_object->type != src_object->type) {
		*ret = -1;
		return 0;
	}

	*ret = 0;

	/* TODO: support reuse-delta */

	/* Let's not bust the allowed depth. */
	if (src->depth >= max_depth)
		return 0;

	/* Now some size filtering heuristics. */
743
	trg_size = (unsigned long)trg_object->size;
Michael Schubert committed
744 745 746 747 748 749 750 751 752 753 754 755 756
	if (!trg_object->delta) {
		max_size = trg_size/2 - 20;
		ref_depth = 1;
	} else {
		max_size = trg_object->delta_size;
		ref_depth = trg->depth;
	}

	max_size = (uint64_t)max_size * (max_depth - src->depth) /
					(max_depth - ref_depth + 1);
	if (max_size == 0)
		return 0;

757
	src_size = (unsigned long)src_object->size;
Michael Schubert committed
758 759 760 761 762 763 764 765 766 767 768
	sizediff = src_size < trg_size ? trg_size - src_size : 0;
	if (sizediff >= max_size)
		return 0;
	if (trg_size < src_size / 32)
		return 0;

	/* Load data if not already done */
	if (!trg->data) {
		if (git_odb_read(&obj, pb->odb, &trg_object->id) < 0)
			return -1;

769
		sz = (unsigned long)git_odb_object_size(obj);
Michael Schubert committed
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
		trg->data = git__malloc(sz);
		GITERR_CHECK_ALLOC(trg->data);
		memcpy(trg->data, git_odb_object_data(obj), sz);

		git_odb_object_free(obj);

		if (sz != trg_size) {
			giterr_set(GITERR_INVALID,
				   "Inconsistent target object length");
			return -1;
		}

		*mem_usage += sz;
	}
	if (!src->data) {
785 786 787 788
		size_t obj_sz;

		if (git_odb_read(&obj, pb->odb, &src_object->id) < 0 ||
			!git__is_ulong(obj_sz = git_odb_object_size(obj)))
Michael Schubert committed
789 790
			return -1;

791
		sz = (unsigned long)obj_sz;
Michael Schubert committed
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
		src->data = git__malloc(sz);
		GITERR_CHECK_ALLOC(src->data);
		memcpy(src->data, git_odb_object_data(obj), sz);

		git_odb_object_free(obj);

		if (sz != src_size) {
			giterr_set(GITERR_INVALID,
				   "Inconsistent source object length");
			return -1;
		}

		*mem_usage += sz;
	}
	if (!src->index) {
		src->index = git_delta_create_index(src->data, src_size);
		if (!src->index)
			return 0; /* suboptimal pack - out of memory */

		*mem_usage += git_delta_sizeof_index(src->index);
	}

	delta_buf = git_delta_create(src->index, trg->data, trg_size,
				     &delta_size, max_size);
	if (!delta_buf)
		return 0;

	if (trg_object->delta) {
		/* Prefer only shallower same-sized deltas. */
		if (delta_size == trg_object->delta_size &&
		    src->depth + 1 >= trg->depth) {
			git__free(delta_buf);
			return 0;
		}
	}

828
	git_packbuilder__cache_lock(pb);
Michael Schubert committed
829 830 831 832 833 834
	if (trg_object->delta_data) {
		git__free(trg_object->delta_data);
		pb->delta_cache_size -= trg_object->delta_size;
		trg_object->delta_data = NULL;
	}
	if (delta_cacheable(pb, src_size, trg_size, delta_size)) {
835 836
		bool overflow = git__add_uint64_overflow(
			&pb->delta_cache_size, pb->delta_cache_size, delta_size);
837

838
		git_packbuilder__cache_unlock(pb);
Michael Schubert committed
839

840 841 842
		if (overflow ||
			!(trg_object->delta_data = git__realloc(delta_buf, delta_size)))
			return -1;
Michael Schubert committed
843 844
	} else {
		/* create delta when writing the pack */
845
		git_packbuilder__cache_unlock(pb);
Michael Schubert committed
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
		git__free(delta_buf);
	}

	trg_object->delta = src_object;
	trg_object->delta_size = delta_size;
	trg->depth = src->depth + 1;

	*ret = 1;
	return 0;
}

static unsigned int check_delta_limit(git_pobject *me, unsigned int n)
{
	git_pobject *child = me->delta_child;
	unsigned int m = n;

	while (child) {
		unsigned int c = check_delta_limit(child, n + 1);
		if (m < c)
			m = c;
		child = child->delta_sibling;
	}
	return m;
}

static unsigned long free_unpacked(struct unpacked *n)
{
	unsigned long freed_mem = git_delta_sizeof_index(n->index);
	git_delta_free_index(n->index);
	n->index = NULL;
	if (n->data) {
877
		freed_mem += (unsigned long)n->object->size;
Michael Schubert committed
878 879 880 881 882 883 884 885 886 887
		git__free(n->data);
		n->data = NULL;
	}
	n->object = NULL;
	n->depth = 0;
	return freed_mem;
}

static int find_deltas(git_packbuilder *pb, git_pobject **list,
		       unsigned int *list_size, unsigned int window,
888
		       int depth)
Michael Schubert committed
889 890 891 892 893 894 895 896 897 898 899 900 901 902
{
	git_pobject *po;
	git_buf zbuf = GIT_BUF_INIT;
	struct unpacked *array;
	uint32_t idx = 0, count = 0;
	unsigned long mem_usage = 0;
	unsigned int i;
	int error = -1;

	array = git__calloc(window, sizeof(struct unpacked));
	GITERR_CHECK_ALLOC(array);

	for (;;) {
		struct unpacked *n = array + idx;
903
		int max_depth, j, best_base = -1;
Michael Schubert committed
904

905
		git_packbuilder__progress_lock(pb);
Michael Schubert committed
906
		if (!*list_size) {
907
			git_packbuilder__progress_unlock(pb);
Michael Schubert committed
908 909 910 911 912
			break;
		}

		po = *list++;
		(*list_size)--;
913
		git_packbuilder__progress_unlock(pb);
Michael Schubert committed
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973

		mem_usage -= free_unpacked(n);
		n->object = po;

		while (pb->window_memory_limit &&
		       mem_usage > pb->window_memory_limit &&
		       count > 1) {
			uint32_t tail = (idx + window - count) % window;
			mem_usage -= free_unpacked(array + tail);
			count--;
		}

		/*
		 * If the current object is at pack edge, take the depth the
		 * objects that depend on the current object into account
		 * otherwise they would become too deep.
		 */
		max_depth = depth;
		if (po->delta_child) {
			max_depth -= check_delta_limit(po, 0);
			if (max_depth <= 0)
				goto next;
		}

		j = window;
		while (--j > 0) {
			int ret;
			uint32_t other_idx = idx + j;
			struct unpacked *m;

			if (other_idx >= window)
				other_idx -= window;

			m = array + other_idx;
			if (!m->object)
				break;

			if (try_delta(pb, n, m, max_depth, &mem_usage, &ret) < 0)
				goto on_error;
			if (ret < 0)
				break;
			else if (ret > 0)
				best_base = other_idx;
		}

		/*
		 * If we decided to cache the delta data, then it is best
		 * to compress it right away.  First because we have to do
		 * it anyway, and doing it here while we're threaded will
		 * save a lot of time in the non threaded write phase,
		 * as well as allow for caching more deltas within
		 * the same cache size limit.
		 * ...
		 * But only if not writing to stdout, since in that case
		 * the network is most likely throttling writes anyway,
		 * and therefore it is best to go to the write phase ASAP
		 * instead, as we can afford spending more time compressing
		 * between writes at that moment.
		 */
		if (po->delta_data) {
974
			if (git_zstream_deflatebuf(&zbuf, po->delta_data, po->delta_size) < 0)
Michael Schubert committed
975 976 977 978 979 980 981
				goto on_error;

			git__free(po->delta_data);
			po->delta_data = git__malloc(zbuf.size);
			GITERR_CHECK_ALLOC(po->delta_data);

			memcpy(po->delta_data, zbuf.ptr, zbuf.size);
982
			po->z_delta_size = (unsigned long)zbuf.size;
Michael Schubert committed
983 984
			git_buf_clear(&zbuf);

985
			git_packbuilder__cache_lock(pb);
Michael Schubert committed
986 987
			pb->delta_cache_size -= po->delta_size;
			pb->delta_cache_size += po->z_delta_size;
988
			git_packbuilder__cache_unlock(pb);
Michael Schubert committed
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
		}

		/*
		 * If we made n a delta, and if n is already at max
		 * depth, leaving it in the window is pointless.  we
		 * should evict it first.
		 */
		if (po->delta && max_depth <= n->depth)
			continue;

		/*
		 * Move the best delta base up in the window, after the
		 * currently deltified object, to keep it longer.  It will
		 * be the first base object to be attempted next.
		 */
		if (po->delta) {
			struct unpacked swap = array[best_base];
			int dist = (window + idx - best_base) % window;
			int dst = best_base;
			while (dist--) {
				int src = (dst + 1) % window;
				array[dst] = array[src];
				dst = src;
			}
			array[dst] = swap;
		}

		next:
		idx++;
		if (count + 1 < window)
			count++;
		if (idx >= window)
			idx = 0;
	}
	error = 0;

on_error:
	for (i = 0; i < window; ++i) {
		git__free(array[i].index);
		git__free(array[i].data);
	}
	git__free(array);
	git_buf_free(&zbuf);

	return error;
}

#ifdef GIT_THREADS

struct thread_params {
	git_thread thread;
	git_packbuilder *pb;

	git_pobject **list;

	git_cond cond;
	git_mutex mutex;

	unsigned int list_size;
	unsigned int remaining;

	int window;
	int depth;
	int working;
	int data_ready;
};

static void *threaded_find_deltas(void *arg)
{
	struct thread_params *me = arg;

	while (me->remaining) {
		if (find_deltas(me->pb, me->list, &me->remaining,
				me->window, me->depth) < 0) {
			; /* TODO */
		}

1066
		git_packbuilder__progress_lock(me->pb);
Michael Schubert committed
1067
		me->working = 0;
1068 1069
		git_cond_signal(&me->pb->progress_cond);
		git_packbuilder__progress_unlock(me->pb);
Michael Schubert committed
1070

1071 1072 1073 1074 1075 1076 1077 1078
		if (git_mutex_lock(&me->mutex)) {
			giterr_set(GITERR_THREAD, "unable to lock packfile condition mutex");
			return NULL;
		}

		while (!me->data_ready)
			git_cond_wait(&me->cond, &me->mutex);

Michael Schubert committed
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
		/*
		 * We must not set ->data_ready before we wait on the
		 * condition because the main thread may have set it to 1
		 * before we get here. In order to be sure that new
		 * work is available if we see 1 in ->data_ready, it
		 * was initialized to 0 before this thread was spawned
		 * and we reset it to 0 right away.
		 */
		me->data_ready = 0;
		git_mutex_unlock(&me->mutex);
	}
	/* leave ->working 1 so that this doesn't get more work assigned */
	return NULL;
}

static int ll_find_deltas(git_packbuilder *pb, git_pobject **list,
			  unsigned int list_size, unsigned int window,
1096
			  int depth)
Michael Schubert committed
1097 1098 1099 1100 1101 1102
{
	struct thread_params *p;
	int i, ret, active_threads = 0;

	if (!pb->nr_threads)
		pb->nr_threads = git_online_cpus();
1103

Michael Schubert committed
1104 1105 1106 1107 1108
	if (pb->nr_threads <= 1) {
		find_deltas(pb, list, &list_size, window, depth);
		return 0;
	}

1109
	p = git__mallocarray(pb->nr_threads, sizeof(*p));
Michael Schubert committed
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
	GITERR_CHECK_ALLOC(p);

	/* Partition the work among the threads */
	for (i = 0; i < pb->nr_threads; ++i) {
		unsigned sub_size = list_size / (pb->nr_threads - i);

		/* don't use too small segments or no deltas will be found */
		if (sub_size < 2*window && i+1 < pb->nr_threads)
			sub_size = 0;

		p[i].pb = pb;
		p[i].window = window;
		p[i].depth = depth;
		p[i].working = 1;
		p[i].data_ready = 0;

		/* try to split chunks on "path" boundaries */
		while (sub_size && sub_size < list_size &&
		       list[sub_size]->hash &&
		       list[sub_size]->hash == list[sub_size-1]->hash)
			sub_size++;

		p[i].list = list;
		p[i].list_size = sub_size;
		p[i].remaining = sub_size;

		list += sub_size;
		list_size -= sub_size;
	}

	/* Start work threads */
	for (i = 0; i < pb->nr_threads; ++i) {
		if (!p[i].list_size)
			continue;

		git_mutex_init(&p[i].mutex);
		git_cond_init(&p[i].cond);

		ret = git_thread_create(&p[i].thread, NULL,
					threaded_find_deltas, &p[i]);
		if (ret) {
			giterr_set(GITERR_THREAD, "unable to create thread");
			return -1;
		}
		active_threads++;
	}

	/*
	 * Now let's wait for work completion.  Each time a thread is done
	 * with its work, we steal half of the remaining work from the
	 * thread with the largest number of unprocessed objects and give
	 * it to that newly idle thread.  This ensure good load balancing
	 * until the remaining object list segments are simply too short
	 * to be worth splitting anymore.
	 */
	while (active_threads) {
		struct thread_params *target = NULL;
		struct thread_params *victim = NULL;
		unsigned sub_size = 0;

1170 1171 1172 1173 1174
		/* Start by locating a thread that has transitioned its
		 * 'working' flag from 1 -> 0. This indicates that it is
		 * ready to receive more work using our work-stealing
		 * algorithm. */
		git_packbuilder__progress_lock(pb);
Michael Schubert committed
1175 1176 1177 1178 1179 1180
		for (;;) {
			for (i = 0; !target && i < pb->nr_threads; i++)
				if (!p[i].working)
					target = &p[i];
			if (target)
				break;
1181
			git_cond_wait(&pb->progress_cond, &pb->progress_mutex);
Michael Schubert committed
1182 1183
		}

1184 1185 1186
		/* At this point we hold the progress lock and have located
		 * a thread to receive more work. We still need to locate a
		 * thread from which to steal work (the victim). */
Michael Schubert committed
1187 1188 1189 1190
		for (i = 0; i < pb->nr_threads; i++)
			if (p[i].remaining > 2*window &&
			    (!victim || victim->remaining < p[i].remaining))
				victim = &p[i];
1191

Michael Schubert committed
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
		if (victim) {
			sub_size = victim->remaining / 2;
			list = victim->list + victim->list_size - sub_size;
			while (sub_size && list[0]->hash &&
			       list[0]->hash == list[-1]->hash) {
				list++;
				sub_size--;
			}
			if (!sub_size) {
				/*
				 * It is possible for some "paths" to have
				 * so many objects that no hash boundary
				 * might be found.  Let's just steal the
				 * exact half in that case.
				 */
				sub_size = victim->remaining / 2;
				list -= sub_size;
			}
			target->list = list;
			victim->list_size -= sub_size;
			victim->remaining -= sub_size;
		}
		target->list_size = sub_size;
		target->remaining = sub_size;
		target->working = 1;
1217
		git_packbuilder__progress_unlock(pb);
Michael Schubert committed
1218

1219 1220 1221 1222 1223 1224
		if (git_mutex_lock(&target->mutex)) {
			giterr_set(GITERR_THREAD, "unable to lock packfile condition mutex");
			git__free(p);
			return -1;
		}

Michael Schubert committed
1225 1226 1227 1228 1229
		target->data_ready = 1;
		git_cond_signal(&target->cond);
		git_mutex_unlock(&target->mutex);

		if (!sub_size) {
1230
			git_thread_join(&target->thread, NULL);
Michael Schubert committed
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
			git_cond_free(&target->cond);
			git_mutex_free(&target->mutex);
			active_threads--;
		}
	}

	git__free(p);
	return 0;
}

#else
#define ll_find_deltas(pb, l, ls, w, d) find_deltas(pb, l, &ls, w, d)
#endif

static int prepare_pack(git_packbuilder *pb)
{
	git_pobject **delta_list;
	unsigned int i, n = 0;

	if (pb->nr_objects == 0 || pb->done)
		return 0; /* nothing to do */

1253 1254 1255 1256 1257 1258 1259
	/*
	 * Although we do not report progress during deltafication, we
	 * at least report that we are in the deltafication stage
	 */
	if (pb->progress_cb)
			pb->progress_cb(GIT_PACKBUILDER_DELTAFICATION, 0, pb->nr_objects, pb->progress_cb_payload);

1260
	delta_list = git__mallocarray(pb->nr_objects, sizeof(*delta_list));
Michael Schubert committed
1261 1262 1263 1264 1265
	GITERR_CHECK_ALLOC(delta_list);

	for (i = 0; i < pb->nr_objects; ++i) {
		git_pobject *po = pb->object_list + i;

1266 1267
		/* Make sure the item is within our size limits */
		if (po->size < 50 || po->size > pb->big_file_threshold)
Michael Schubert committed
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
			continue;

		delta_list[n++] = po;
	}

	if (n > 1) {
		git__tsort((void **)delta_list, n, type_size_sort);
		if (ll_find_deltas(pb, delta_list, n,
				   GIT_PACK_WINDOW + 1,
				   GIT_PACK_DEPTH) < 0) {
			git__free(delta_list);
			return -1;
		}
	}

	pb->done = true;
	git__free(delta_list);
	return 0;
}

#define PREPARE_PACK if (prepare_pack(pb) < 0) { return -1; }

1290 1291 1292 1293 1294 1295
int git_packbuilder_foreach(git_packbuilder *pb, int (*cb)(void *buf, size_t size, void *payload), void *payload)
{
	PREPARE_PACK;
	return write_pack(pb, cb, payload);
}

Michael Schubert committed
1296 1297 1298
int git_packbuilder_write_buf(git_buf *buf, git_packbuilder *pb)
{
	PREPARE_PACK;
1299
	git_buf_sanitize(buf);
Michael Schubert committed
1300 1301 1302
	return write_pack(pb, &write_pack_buf, buf);
}

1303
static int write_cb(void *buf, size_t len, void *payload)
Michael Schubert committed
1304
{
1305
	struct pack_write_context *ctx = payload;
1306
	return git_indexer_append(ctx->indexer, buf, len, ctx->stats);
1307 1308 1309 1310 1311
}

int git_packbuilder_write(
	git_packbuilder *pb,
	const char *path,
1312
	unsigned int mode,
1313
	git_transfer_progress_cb progress_cb,
1314 1315
	void *progress_cb_payload)
{
1316
	git_indexer *indexer;
1317 1318 1319
	git_transfer_progress stats;
	struct pack_write_context ctx;

Michael Schubert committed
1320
	PREPARE_PACK;
1321

1322
	if (git_indexer_new(
1323
		&indexer, path, mode, pb->odb, progress_cb, progress_cb_payload) < 0)
1324 1325 1326 1327 1328 1329
		return -1;

	ctx.indexer = indexer;
	ctx.stats = &stats;

	if (git_packbuilder_foreach(pb, write_cb, &ctx) < 0 ||
1330 1331
		git_indexer_commit(indexer, &stats) < 0) {
		git_indexer_free(indexer);
1332 1333 1334
		return -1;
	}

1335 1336
	git_oid_cpy(&pb->pack_oid, git_indexer_hash(indexer));

1337
	git_indexer_free(indexer);
1338
	return 0;
Michael Schubert committed
1339 1340 1341 1342
}

#undef PREPARE_PACK

1343 1344 1345 1346 1347
const git_oid *git_packbuilder_hash(git_packbuilder *pb)
{
	return &pb->pack_oid;
}

1348 1349
static int cb_tree_walk(
	const char *root, const git_tree_entry *entry, void *payload)
Michael Schubert committed
1350
{
1351
	int error;
1352
	struct tree_walk_context *ctx = payload;
Michael Schubert committed
1353

1354
	/* A commit inside a tree represents a submodule commit and should be skipped. */
1355
	if (git_tree_entry_type(entry) == GIT_OBJ_COMMIT)
1356 1357
		return 0;

1358 1359 1360 1361
	if (!(error = git_buf_sets(&ctx->buf, root)) &&
		!(error = git_buf_puts(&ctx->buf, git_tree_entry_name(entry))))
		error = git_packbuilder_insert(
			ctx->pb, git_tree_entry_id(entry), git_buf_cstr(&ctx->buf));
Michael Schubert committed
1362

1363
	return error;
Michael Schubert committed
1364 1365
}

1366 1367
int git_packbuilder_insert_commit(git_packbuilder *pb, const git_oid *oid)
{
Xavier L committed
1368
	git_commit *commit;
1369

Xavier L committed
1370 1371 1372
	if (git_commit_lookup(&commit, pb->repo, oid) < 0 ||
		git_packbuilder_insert(pb, oid, NULL) < 0)
		return -1;
1373

Xavier L committed
1374 1375
	if (git_packbuilder_insert_tree(pb, git_commit_tree_id(commit)) < 0)
		return -1;
1376

Xavier L committed
1377 1378
	git_commit_free(commit);
	return 0;
1379 1380
}

Michael Schubert committed
1381 1382
int git_packbuilder_insert_tree(git_packbuilder *pb, const git_oid *oid)
{
1383 1384
	int error;
	git_tree *tree = NULL;
1385
	struct tree_walk_context context = { pb, GIT_BUF_INIT };
Michael Schubert committed
1386

1387 1388 1389
	if (!(error = git_tree_lookup(&tree, pb->repo, oid)) &&
	    !(error = git_packbuilder_insert(pb, oid, NULL)))
		error = git_tree_walk(tree, GIT_TREEWALK_PRE, cb_tree_walk, &context);
Michael Schubert committed
1390 1391

	git_tree_free(tree);
1392
	git_buf_free(&context.buf);
1393
	return error;
Michael Schubert committed
1394 1395
}

1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
uint32_t git_packbuilder_object_count(git_packbuilder *pb)
{
	return pb->nr_objects;
}

uint32_t git_packbuilder_written(git_packbuilder *pb)
{
	return pb->nr_written;
}

1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
int git_packbuilder_set_callbacks(git_packbuilder *pb, git_packbuilder_progress progress_cb, void *progress_cb_payload)
{
	if (!pb)
		return -1;

	pb->progress_cb = progress_cb;
	pb->progress_cb_payload = progress_cb_payload;

	return 0;
}

Michael Schubert committed
1417 1418 1419 1420 1421
void git_packbuilder_free(git_packbuilder *pb)
{
	if (pb == NULL)
		return;

1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
#ifdef GIT_THREADS

	git_mutex_free(&pb->cache_mutex);
	git_mutex_free(&pb->progress_mutex);
	git_cond_free(&pb->progress_cond);

#endif

	if (pb->odb)
		git_odb_free(pb->odb);

	if (pb->object_ix)
		git_oidmap_free(pb->object_ix);

	if (pb->object_list)
		git__free(pb->object_list);

1439
	git_hash_ctx_cleanup(&pb->ctx);
1440
	git_zstream_free(&pb->zstream);
1441

Michael Schubert committed
1442 1443
	git__free(pb);
}