packbuilder.c 3.82 KB
Newer Older
Michael Schubert committed
1
#include "clar_libgit2.h"
2 3
#include "fileops.h"
#include "hash.h"
Michael Schubert committed
4 5
#include "iterator.h"
#include "vector.h"
Ben Straub committed
6
#include "posix.h"
Michael Schubert committed
7 8 9 10

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

void test_pack_packbuilder__initialize(void)
{
Vicent Marti committed
17
	_repo = cl_git_sandbox_init("testrepo.git");
Michael Schubert committed
18 19 20
	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));
21
	_commits_is_initialized = 1;
Michael Schubert committed
22 23 24 25 26 27 28
}

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

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

Michael Schubert committed
37
	git_packbuilder_free(_packbuilder);
38 39
	_packbuilder = NULL;

Michael Schubert committed
40
	git_revwalk_free(_revwalker);
41 42
	_revwalker = NULL;

43
	git_indexer_stream_free(_indexer);
44
	_indexer = NULL;
45

Vicent Marti committed
46
	cl_git_sandbox_cleanup();
47
	_repo = NULL;
Michael Schubert committed
48 49
}

50
static void seed_packbuilder(void)
Michael Schubert committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
{
	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
73
					git_commit_tree_id((git_commit *)obj)));
Michael Schubert committed
74 75
		git_object_free(obj);
	}
76 77
}

78 79 80 81 82 83 84
static int feed_indexer(void *ptr, size_t len, void *payload)
{
	git_transfer_progress *stats = (git_transfer_progress *)payload;

	return git_indexer_stream_add(_indexer, ptr, len, stats);
}

85 86 87
void test_pack_packbuilder__create_pack(void)
{
	git_transfer_progress stats;
88
	git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT;
89 90 91
	git_hash_ctx ctx;
	git_oid hash;
	char hex[41]; hex[40] = '\0';
Michael Schubert committed
92

93
	seed_packbuilder();
Michael Schubert committed
94

95 96 97 98 99 100
	cl_git_pass(git_indexer_stream_new(&_indexer, ".", NULL, NULL));
	cl_git_pass(git_packbuilder_foreach(_packbuilder, feed_indexer, &stats));
	cl_git_pass(git_indexer_stream_finalize(_indexer, &stats));

	git_oid_fmt(hex, git_indexer_stream_hash(_indexer));
	git_buf_printf(&path, "pack-%s.pack", hex);
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

	/*
	 * By default, packfiles are created with only one thread.
	 * Therefore we can predict the object ordering and make sure
	 * we create exactly the same pack as git.git does when *not*
	 * reusing existing deltas (as libgit2).
	 *
	 * $ cd tests-clar/resources/testrepo.git
	 * $ git rev-list --objects HEAD | \
	 * 	git pack-objects -q --no-reuse-delta --threads=1 pack
	 * $ sha1sum git-80e61eb315239ef3c53033e37fee43b744d57122.pack
	 * 5d410bdf97cf896f9007681b92868471d636954b
	 *
	 */

116
	cl_git_pass(git_futils_readbuffer(&buf, git_buf_cstr(&path)));
117

118
	cl_git_pass(git_hash_ctx_init(&ctx));
119 120
	cl_git_pass(git_hash_update(&ctx, buf.ptr, buf.size));
	cl_git_pass(git_hash_final(&hash, &ctx));
121
	git_hash_ctx_cleanup(&ctx);
122

123
	git_buf_free(&path);
124 125 126 127 128
	git_buf_free(&buf);

	git_oid_fmt(hex, &hash);

	cl_assert_equal_s(hex, "5d410bdf97cf896f9007681b92868471d636954b");
Michael Schubert committed
129
}
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

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);
}