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

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

18 19
extern bool git_disable_pack_keep_file_checks;

Michael Schubert committed
20 21
void test_pack_packbuilder__initialize(void)
{
Vicent Marti committed
22
	_repo = cl_git_sandbox_init("testrepo.git");
23
	cl_git_pass(p_chdir("testrepo.git"));
Michael Schubert committed
24 25 26
	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));
27
	_commits_is_initialized = 1;
28
	memset(&_stats, 0, sizeof(_stats));
29
	p_fsync__cnt = 0;
Michael Schubert committed
30 31 32 33 34 35 36
}

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

37
	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_FSYNC_GITDIR, 0));
38
	cl_git_pass(git_libgit2_opts(GIT_OPT_DISABLE_PACK_KEEP_FILE_CHECKS, false));
39

40 41 42 43 44 45
	if (_commits_is_initialized) {
		_commits_is_initialized = 0;
		git_vector_foreach(&_commits, i, o) {
			git__free(o);
		}
		git_vector_free(&_commits);
Michael Schubert committed
46
	}
47

Michael Schubert committed
48
	git_packbuilder_free(_packbuilder);
49 50
	_packbuilder = NULL;

Michael Schubert committed
51
	git_revwalk_free(_revwalker);
52 53
	_revwalker = NULL;

54
	git_indexer_free(_indexer);
55
	_indexer = NULL;
56

57
	cl_git_pass(p_chdir(".."));
Vicent Marti committed
58
	cl_git_sandbox_cleanup();
59
	_repo = NULL;
Michael Schubert committed
60 61
}

62
static void seed_packbuilder(void)
Michael Schubert committed
63 64 65 66 67 68 69 70
{
	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) {
71
		o = git__malloc(sizeof(git_oid));
Michael Schubert committed
72 73 74 75 76 77 78 79 80 81 82
		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;
83
		cl_git_pass(git_object_lookup(&obj, _repo, o, GIT_OBJECT_COMMIT));
Michael Schubert committed
84
		cl_git_pass(git_packbuilder_insert_tree(_packbuilder,
Vicent Marti committed
85
					git_commit_tree_id((git_commit *)obj)));
Michael Schubert committed
86 87
		git_object_free(obj);
	}
88 89
}

90 91
static int feed_indexer(void *ptr, size_t len, void *payload)
{
92
	git_indexer_progress *stats = (git_indexer_progress *)payload;
93

94
	return git_indexer_append(_indexer, ptr, len, stats);
95 96
}

97 98
void test_pack_packbuilder__create_pack(void)
{
99
	git_indexer_progress stats;
100
	git_str buf = GIT_STR_INIT, path = GIT_STR_INIT;
101
	git_hash_ctx ctx;
102 103
	unsigned char hash[GIT_HASH_SHA1_SIZE];
	char hex[(GIT_HASH_SHA1_SIZE * 2) + 1];
Michael Schubert committed
104

105
	seed_packbuilder();
Michael Schubert committed
106

107
	cl_git_pass(git_indexer_new(&_indexer, ".", 0, NULL, NULL));
108
	cl_git_pass(git_packbuilder_foreach(_packbuilder, feed_indexer, &stats));
109
	cl_git_pass(git_indexer_commit(_indexer, &stats));
110

111
	git_str_printf(&path, "pack-%s.pack", git_indexer_name(_indexer));
112 113 114 115 116 117 118

	/*
	 * 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).
	 *
119
	 * $ cd tests/resources/testrepo.git
120 121
	 * $ git rev-list --objects HEAD | \
	 * 	git pack-objects -q --no-reuse-delta --threads=1 pack
122
	 * $ sha1sum pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.pack
123 124 125 126
	 * 5d410bdf97cf896f9007681b92868471d636954b
	 *
	 */

127
	cl_git_pass(git_futils_readbuffer(&buf, git_str_cstr(&path)));
128

129
	cl_git_pass(git_hash_ctx_init(&ctx, GIT_HASH_ALGORITHM_SHA1));
130
	cl_git_pass(git_hash_update(&ctx, buf.ptr, buf.size));
131
	cl_git_pass(git_hash_final(hash, &ctx));
132
	git_hash_ctx_cleanup(&ctx);
133

134 135
	git_str_dispose(&path);
	git_str_dispose(&buf);
136

137
	git_hash_fmt(hex, hash, GIT_HASH_SHA1_SIZE);
138
	cl_assert_equal_s(hex, "5d410bdf97cf896f9007681b92868471d636954b");
Michael Schubert committed
139
}
140

141
void test_pack_packbuilder__get_name(void)
142 143 144
{
	seed_packbuilder();

145
	cl_git_pass(git_packbuilder_write(_packbuilder, ".", 0, NULL, NULL));
146
	cl_assert_equal_s("7f5fa362c664d68ba7221259be1cbd187434b2f0", git_packbuilder_name(_packbuilder));
147 148
}

149 150 151 152 153
void test_pack_packbuilder__write_default_path(void)
{
	seed_packbuilder();

	cl_git_pass(git_packbuilder_write(_packbuilder, NULL, 0, NULL, NULL));
154 155
	cl_assert(git_fs_path_exists("objects/pack/pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.idx"));
	cl_assert(git_fs_path_exists("objects/pack/pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.pack"));
156 157
}

158 159 160 161 162 163 164
static void test_write_pack_permission(mode_t given, mode_t expected)
{
	struct stat statbuf;
	mode_t mask, os_mask;

	seed_packbuilder();

165
	cl_git_pass(git_packbuilder_write(_packbuilder, ".", given, NULL, NULL));
166 167 168 169 170 171 172 173 174 175 176 177 178

	/* Windows does not return group/user bits from stat,
	* files are never executable.
	*/
#ifdef GIT_WIN32
	os_mask = 0600;
#else
	os_mask = 0777;
#endif

	mask = p_umask(0);
	p_umask(mask);

179
	cl_git_pass(p_stat("pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.idx", &statbuf));
180 181
	cl_assert_equal_i(statbuf.st_mode & os_mask, (expected & ~mask) & os_mask);

182
	cl_git_pass(p_stat("pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.pack", &statbuf));
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
	cl_assert_equal_i(statbuf.st_mode & os_mask, (expected & ~mask) & os_mask);
}

void test_pack_packbuilder__permissions_standard(void)
{
	test_write_pack_permission(0, GIT_PACK_FILE_MODE);
}

void test_pack_packbuilder__permissions_readonly(void)
{
	test_write_pack_permission(0444, 0444);
}

void test_pack_packbuilder__permissions_readwrite(void)
{
	test_write_pack_permission(0666, 0666);
}

201 202 203
void test_pack_packbuilder__does_not_fsync_by_default(void)
{
	seed_packbuilder();
204
	cl_git_pass(git_packbuilder_write(_packbuilder, ".", 0666, NULL, NULL));
205 206 207
	cl_assert_equal_sz(0, p_fsync__cnt);
}

208 209 210
/* We fsync the packfile and index.  On non-Windows, we also fsync
 * the parent directories.
 */
211
#ifdef GIT_WIN32
212
static int expected_fsyncs = 2;
213
#else
214
static int expected_fsyncs = 4;
215 216
#endif

217 218
void test_pack_packbuilder__fsync_global_setting(void)
{
219
	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_FSYNC_GITDIR, 1));
220 221
	p_fsync__cnt = 0;
	seed_packbuilder();
222
	cl_git_pass(git_packbuilder_write(_packbuilder, ".", 0666, NULL, NULL));
223 224 225 226 227 228 229 230
	cl_assert_equal_sz(expected_fsyncs, p_fsync__cnt);
}

void test_pack_packbuilder__fsync_repo_setting(void)
{
	cl_repo_set_bool(_repo, "core.fsyncObjectFiles", true);
	p_fsync__cnt = 0;
	seed_packbuilder();
231
	cl_git_pass(git_packbuilder_write(_packbuilder, ".", 0666, NULL, NULL));
232
	cl_assert_equal_sz(expected_fsyncs, p_fsync__cnt);
233 234
}

235 236
static int foreach_cb(void *buf, size_t len, void *payload)
{
237
	git_indexer *idx = (git_indexer *) payload;
238
	cl_git_pass(git_indexer_append(idx, buf, len, &_stats));
239 240 241 242 243
	return 0;
}

void test_pack_packbuilder__foreach(void)
{
244
	git_indexer *idx;
245 246

	seed_packbuilder();
247
	cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL));
248
	cl_git_pass(git_packbuilder_foreach(_packbuilder, foreach_cb, idx));
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
	cl_git_pass(git_indexer_commit(idx, &_stats));
	git_indexer_free(idx);
}

static int foreach_cancel_cb(void *buf, size_t len, void *payload)
{
	git_indexer *idx = (git_indexer *)payload;
	cl_git_pass(git_indexer_append(idx, buf, len, &_stats));
	return (_stats.total_objects > 2) ? -1111 : 0;
}

void test_pack_packbuilder__foreach_with_cancel(void)
{
	git_indexer *idx;

	seed_packbuilder();
265
	cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL));
266 267
	cl_git_fail_with(
		git_packbuilder_foreach(_packbuilder, foreach_cancel_cb, idx), -1111);
268
	git_indexer_free(idx);
269
}
270 271 272 273 274 275 276

void test_pack_packbuilder__keep_file_check(void)
{
	assert(!git_disable_pack_keep_file_checks);
	cl_git_pass(git_libgit2_opts(GIT_OPT_DISABLE_PACK_KEEP_FILE_CHECKS, true));
	assert(git_disable_pack_keep_file_checks);
}