Blame tests/pack/packbuilder.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
#include "fileops.h"
Packit Service 20376f
#include "pack.h"
Packit Service 20376f
#include "hash.h"
Packit Service 20376f
#include "iterator.h"
Packit Service 20376f
#include "vector.h"
Packit Service 20376f
#include "posix.h"
Packit Service 20376f
Packit Service 20376f
static git_repository *_repo;
Packit Service 20376f
static git_revwalk *_revwalker;
Packit Service 20376f
static git_packbuilder *_packbuilder;
Packit Service 20376f
static git_indexer *_indexer;
Packit Service 20376f
static git_vector _commits;
Packit Service 20376f
static int _commits_is_initialized;
Packit Service 20376f
static git_transfer_progress _stats;
Packit Service 20376f
Packit Service 20376f
void test_pack_packbuilder__initialize(void)
Packit Service 20376f
{
Packit Service 20376f
	_repo = cl_git_sandbox_init("testrepo.git");
Packit Service 20376f
	cl_git_pass(p_chdir("testrepo.git"));
Packit Service 20376f
	cl_git_pass(git_revwalk_new(&_revwalker, _repo));
Packit Service 20376f
	cl_git_pass(git_packbuilder_new(&_packbuilder, _repo));
Packit Service 20376f
	cl_git_pass(git_vector_init(&_commits, 0, NULL));
Packit Service 20376f
	_commits_is_initialized = 1;
Packit Service 20376f
	memset(&_stats, 0, sizeof(_stats));
Packit Service 20376f
	p_fsync__cnt = 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_pack_packbuilder__cleanup(void)
Packit Service 20376f
{
Packit Service 20376f
	git_oid *o;
Packit Service 20376f
	unsigned int i;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_FSYNC_GITDIR, 0));
Packit Service 20376f
Packit Service 20376f
	if (_commits_is_initialized) {
Packit Service 20376f
		_commits_is_initialized = 0;
Packit Service 20376f
		git_vector_foreach(&_commits, i, o) {
Packit Service 20376f
			git__free(o);
Packit Service 20376f
		}
Packit Service 20376f
		git_vector_free(&_commits);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_packbuilder_free(_packbuilder);
Packit Service 20376f
	_packbuilder = NULL;
Packit Service 20376f
Packit Service 20376f
	git_revwalk_free(_revwalker);
Packit Service 20376f
	_revwalker = NULL;
Packit Service 20376f
Packit Service 20376f
	git_indexer_free(_indexer);
Packit Service 20376f
	_indexer = NULL;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(p_chdir(".."));
Packit Service 20376f
	cl_git_sandbox_cleanup();
Packit Service 20376f
	_repo = NULL;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void seed_packbuilder(void)
Packit Service 20376f
{
Packit Service 20376f
	git_oid oid, *o;
Packit Service 20376f
	unsigned int i;
Packit Service 20376f
Packit Service 20376f
	git_revwalk_sorting(_revwalker, GIT_SORT_TIME);
Packit Service 20376f
	cl_git_pass(git_revwalk_push_ref(_revwalker, "HEAD"));
Packit Service 20376f
Packit Service 20376f
	while (git_revwalk_next(&oid, _revwalker) == 0) {
Packit Service 20376f
		o = git__malloc(GIT_OID_RAWSZ);
Packit Service 20376f
		cl_assert(o != NULL);
Packit Service 20376f
		git_oid_cpy(o, &oid;;
Packit Service 20376f
		cl_git_pass(git_vector_insert(&_commits, o));
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(&_commits, i, o) {
Packit Service 20376f
		cl_git_pass(git_packbuilder_insert(_packbuilder, o, NULL));
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(&_commits, i, o) {
Packit Service 20376f
		git_object *obj;
Packit Service 20376f
		cl_git_pass(git_object_lookup(&obj, _repo, o, GIT_OBJ_COMMIT));
Packit Service 20376f
		cl_git_pass(git_packbuilder_insert_tree(_packbuilder,
Packit Service 20376f
					git_commit_tree_id((git_commit *)obj)));
Packit Service 20376f
		git_object_free(obj);
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int feed_indexer(void *ptr, size_t len, void *payload)
Packit Service 20376f
{
Packit Service 20376f
	git_transfer_progress *stats = (git_transfer_progress *)payload;
Packit Service 20376f
Packit Service 20376f
	return git_indexer_append(_indexer, ptr, len, stats);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_pack_packbuilder__create_pack(void)
Packit Service 20376f
{
Packit Service 20376f
	git_transfer_progress stats;
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT;
Packit Service 20376f
	git_hash_ctx ctx;
Packit Service 20376f
	git_oid hash;
Packit Service 20376f
	char hex[GIT_OID_HEXSZ+1]; hex[GIT_OID_HEXSZ] = '\0';
Packit Service 20376f
Packit Service 20376f
	seed_packbuilder();
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_indexer_new(&_indexer, ".", 0, NULL, NULL, NULL));
Packit Service 20376f
	cl_git_pass(git_packbuilder_foreach(_packbuilder, feed_indexer, &stats));
Packit Service 20376f
	cl_git_pass(git_indexer_commit(_indexer, &stats));
Packit Service 20376f
Packit Service 20376f
	git_oid_fmt(hex, git_indexer_hash(_indexer));
Packit Service 20376f
	git_buf_printf(&path, "pack-%s.pack", hex);
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * By default, packfiles are created with only one thread.
Packit Service 20376f
	 * Therefore we can predict the object ordering and make sure
Packit Service 20376f
	 * we create exactly the same pack as git.git does when *not*
Packit Service 20376f
	 * reusing existing deltas (as libgit2).
Packit Service 20376f
	 *
Packit Service 20376f
	 * $ cd tests/resources/testrepo.git
Packit Service 20376f
	 * $ git rev-list --objects HEAD | \
Packit Service 20376f
	 * 	git pack-objects -q --no-reuse-delta --threads=1 pack
Packit Service 20376f
	 * $ sha1sum pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.pack
Packit Service 20376f
	 * 5d410bdf97cf896f9007681b92868471d636954b
Packit Service 20376f
	 *
Packit Service 20376f
	 */
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_futils_readbuffer(&buf, git_buf_cstr(&path)));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_hash_ctx_init(&ctx));
Packit Service 20376f
	cl_git_pass(git_hash_update(&ctx, buf.ptr, buf.size));
Packit Service 20376f
	cl_git_pass(git_hash_final(&hash, &ctx));
Packit Service 20376f
	git_hash_ctx_cleanup(&ctx;;
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
Packit Service 20376f
	git_oid_fmt(hex, &hash);
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_s(hex, "5d410bdf97cf896f9007681b92868471d636954b");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_pack_packbuilder__get_hash(void)
Packit Service 20376f
{
Packit Service 20376f
	char hex[GIT_OID_HEXSZ+1]; hex[GIT_OID_HEXSZ] = '\0';
Packit Service 20376f
Packit Service 20376f
	seed_packbuilder();
Packit Service 20376f
Packit Service 20376f
	git_packbuilder_write(_packbuilder, ".", 0, NULL, NULL);
Packit Service 20376f
	git_oid_fmt(hex, git_packbuilder_hash(_packbuilder));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_s(hex, "7f5fa362c664d68ba7221259be1cbd187434b2f0");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void test_write_pack_permission(mode_t given, mode_t expected)
Packit Service 20376f
{
Packit Service 20376f
	struct stat statbuf;
Packit Service 20376f
	mode_t mask, os_mask;
Packit Service 20376f
Packit Service 20376f
	seed_packbuilder();
Packit Service 20376f
Packit Service 20376f
	git_packbuilder_write(_packbuilder, ".", given, NULL, NULL);
Packit Service 20376f
Packit Service 20376f
	/* Windows does not return group/user bits from stat,
Packit Service 20376f
	* files are never executable.
Packit Service 20376f
	*/
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	os_mask = 0600;
Packit Service 20376f
#else
Packit Service 20376f
	os_mask = 0777;
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	mask = p_umask(0);
Packit Service 20376f
	p_umask(mask);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(p_stat("pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.idx", &statbuf));
Packit Service 20376f
	cl_assert_equal_i(statbuf.st_mode & os_mask, (expected & ~mask) & os_mask);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(p_stat("pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.pack", &statbuf));
Packit Service 20376f
	cl_assert_equal_i(statbuf.st_mode & os_mask, (expected & ~mask) & os_mask);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_pack_packbuilder__permissions_standard(void)
Packit Service 20376f
{
Packit Service 20376f
	test_write_pack_permission(0, GIT_PACK_FILE_MODE);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_pack_packbuilder__permissions_readonly(void)
Packit Service 20376f
{
Packit Service 20376f
	test_write_pack_permission(0444, 0444);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_pack_packbuilder__permissions_readwrite(void)
Packit Service 20376f
{
Packit Service 20376f
	test_write_pack_permission(0666, 0666);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_pack_packbuilder__does_not_fsync_by_default(void)
Packit Service 20376f
{
Packit Service 20376f
	seed_packbuilder();
Packit Service 20376f
	git_packbuilder_write(_packbuilder, ".", 0666, NULL, NULL);
Packit Service 20376f
	cl_assert_equal_sz(0, p_fsync__cnt);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* We fsync the packfile and index.  On non-Windows, we also fsync
Packit Service 20376f
 * the parent directories.
Packit Service 20376f
 */
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
static int expected_fsyncs = 2;
Packit Service 20376f
#else
Packit Service 20376f
static int expected_fsyncs = 4;
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
void test_pack_packbuilder__fsync_global_setting(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_FSYNC_GITDIR, 1));
Packit Service 20376f
	p_fsync__cnt = 0;
Packit Service 20376f
	seed_packbuilder();
Packit Service 20376f
	git_packbuilder_write(_packbuilder, ".", 0666, NULL, NULL);
Packit Service 20376f
	cl_assert_equal_sz(expected_fsyncs, p_fsync__cnt);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_pack_packbuilder__fsync_repo_setting(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_repo_set_bool(_repo, "core.fsyncObjectFiles", true);
Packit Service 20376f
	p_fsync__cnt = 0;
Packit Service 20376f
	seed_packbuilder();
Packit Service 20376f
	git_packbuilder_write(_packbuilder, ".", 0666, NULL, NULL);
Packit Service 20376f
	cl_assert_equal_sz(expected_fsyncs, p_fsync__cnt);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int foreach_cb(void *buf, size_t len, void *payload)
Packit Service 20376f
{
Packit Service 20376f
	git_indexer *idx = (git_indexer *) payload;
Packit Service 20376f
	cl_git_pass(git_indexer_append(idx, buf, len, &_stats));
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_pack_packbuilder__foreach(void)
Packit Service 20376f
{
Packit Service 20376f
	git_indexer *idx;
Packit Service 20376f
Packit Service 20376f
	seed_packbuilder();
Packit Service 20376f
	cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL, NULL));
Packit Service 20376f
	cl_git_pass(git_packbuilder_foreach(_packbuilder, foreach_cb, idx));
Packit Service 20376f
	cl_git_pass(git_indexer_commit(idx, &_stats));
Packit Service 20376f
	git_indexer_free(idx);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int foreach_cancel_cb(void *buf, size_t len, void *payload)
Packit Service 20376f
{
Packit Service 20376f
	git_indexer *idx = (git_indexer *)payload;
Packit Service 20376f
	cl_git_pass(git_indexer_append(idx, buf, len, &_stats));
Packit Service 20376f
	return (_stats.total_objects > 2) ? -1111 : 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_pack_packbuilder__foreach_with_cancel(void)
Packit Service 20376f
{
Packit Service 20376f
	git_indexer *idx;
Packit Service 20376f
Packit Service 20376f
	seed_packbuilder();
Packit Service 20376f
	cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL, NULL));
Packit Service 20376f
	cl_git_fail_with(
Packit Service 20376f
		git_packbuilder_foreach(_packbuilder, foreach_cancel_cb, idx), -1111);
Packit Service 20376f
	git_indexer_free(idx);
Packit Service 20376f
}