packbuilder.c 2.55 KB
Newer Older
Michael Schubert committed
1 2 3 4 5 6 7 8 9
#include "clar_libgit2.h"
#include "iterator.h"
#include "vector.h"

static git_repository *_repo;
static git_revwalk *_revwalker;
static git_packbuilder *_packbuilder;
static git_indexer *_indexer;
static git_vector _commits;
10
static int _commits_is_initialized;
Michael Schubert committed
11 12 13 14 15 16 17

void test_pack_packbuilder__initialize(void)
{
	cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
	cl_git_pass(git_revwalk_new(&_revwalker, _repo));
	cl_git_pass(git_packbuilder_new(&_packbuilder, _repo));
	cl_git_pass(git_vector_init(&_commits, 0, NULL));
18
	_commits_is_initialized = 1;
Michael Schubert committed
19 20 21 22 23 24 25
}

void test_pack_packbuilder__cleanup(void)
{
	git_oid *o;
	unsigned int i;

26 27 28 29 30 31
	if (_commits_is_initialized) {
		_commits_is_initialized = 0;
		git_vector_foreach(&_commits, i, o) {
			git__free(o);
		}
		git_vector_free(&_commits);
Michael Schubert committed
32
	}
33

Michael Schubert committed
34
	git_packbuilder_free(_packbuilder);
35 36
	_packbuilder = NULL;

Michael Schubert committed
37
	git_revwalk_free(_revwalker);
38 39
	_revwalker = NULL;

Michael Schubert committed
40
	git_indexer_free(_indexer);
41
	_indexer = NULL;
42

Michael Schubert committed
43
	git_repository_free(_repo);
44
	_repo = NULL;
Michael Schubert committed
45 46
}

47
static void seed_packbuilder(void)
Michael Schubert committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
{
	git_oid oid, *o;
	unsigned int i;

	git_revwalk_sorting(_revwalker, GIT_SORT_TIME);
	cl_git_pass(git_revwalk_push_ref(_revwalker, "HEAD"));

	while (git_revwalk_next(&oid, _revwalker) == 0) {
		o = git__malloc(GIT_OID_RAWSZ);
		cl_assert(o != NULL);
		git_oid_cpy(o, &oid);
		cl_git_pass(git_vector_insert(&_commits, o));
	}

	git_vector_foreach(&_commits, i, o) {
		cl_git_pass(git_packbuilder_insert(_packbuilder, o, NULL));
	}

	git_vector_foreach(&_commits, i, o) {
		git_object *obj;
		cl_git_pass(git_object_lookup(&obj, _repo, o, GIT_OBJ_COMMIT));
		cl_git_pass(git_packbuilder_insert_tree(_packbuilder,
Vicent Marti committed
70
					git_commit_tree_id((git_commit *)obj)));
Michael Schubert committed
71 72
		git_object_free(obj);
	}
73 74 75 76 77
}

void test_pack_packbuilder__create_pack(void)
{
	git_transfer_progress stats;
Michael Schubert committed
78

79
	seed_packbuilder();
Michael Schubert committed
80 81 82 83 84 85
	cl_git_pass(git_packbuilder_write(_packbuilder, "testpack.pack"));

	cl_git_pass(git_indexer_new(&_indexer, "testpack.pack"));
	cl_git_pass(git_indexer_run(_indexer, &stats));
	cl_git_pass(git_indexer_write(_indexer));
}
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

static git_transfer_progress stats;
static int foreach_cb(void *buf, size_t len, void *payload)
{
	git_indexer_stream *idx = (git_indexer_stream *) payload;

	cl_git_pass(git_indexer_stream_add(idx, buf, len, &stats));

	return 0;
}

void test_pack_packbuilder__foreach(void)
{
	git_indexer_stream *idx;

	seed_packbuilder();
	cl_git_pass(git_indexer_stream_new(&idx, ".", NULL, NULL));
	cl_git_pass(git_packbuilder_foreach(_packbuilder, foreach_cb, idx));
	cl_git_pass(git_indexer_stream_finalize(idx, &stats));
	git_indexer_stream_free(idx);
}