Blame tests/odb/alternates.c

Packit ae9e2a
#include "clar_libgit2.h"
Packit ae9e2a
#include "odb.h"
Packit ae9e2a
#include "filebuf.h"
Packit ae9e2a
Packit ae9e2a
static git_buf destpath, filepath;
Packit ae9e2a
static const char *paths[] = {
Packit ae9e2a
	"A.git", "B.git", "C.git", "D.git", "E.git", "F.git", "G.git"
Packit ae9e2a
};
Packit ae9e2a
static git_filebuf file;
Packit ae9e2a
static git_repository *repo;
Packit ae9e2a
Packit ae9e2a
void test_odb_alternates__cleanup(void)
Packit ae9e2a
{
Packit ae9e2a
	size_t i;
Packit ae9e2a
Packit ae9e2a
	git_buf_free(&destpath);
Packit ae9e2a
	git_buf_free(&filepath);
Packit ae9e2a
Packit ae9e2a
	for (i = 0; i < ARRAY_SIZE(paths); i++)
Packit ae9e2a
		cl_fixture_cleanup(paths[i]);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static void init_linked_repo(const char *path, const char *alternate)
Packit ae9e2a
{
Packit ae9e2a
	git_buf_clear(&destpath);
Packit ae9e2a
	git_buf_clear(&filepath);
Packit ae9e2a
Packit ae9e2a
	cl_git_pass(git_repository_init(&repo, path, 1));
Packit ae9e2a
	cl_git_pass(git_path_prettify(&destpath, alternate, NULL));
Packit ae9e2a
	cl_git_pass(git_buf_joinpath(&destpath, destpath.ptr, "objects"));
Packit ae9e2a
	cl_git_pass(git_buf_joinpath(&filepath, git_repository_path(repo), "objects/info"));
Packit ae9e2a
	cl_git_pass(git_futils_mkdir(filepath.ptr, 0755, GIT_MKDIR_PATH));
Packit ae9e2a
	cl_git_pass(git_buf_joinpath(&filepath, filepath.ptr , "alternates"));
Packit ae9e2a
Packit ae9e2a
	cl_git_pass(git_filebuf_open(&file, git_buf_cstr(&filepath), 0, 0666));
Packit ae9e2a
	git_filebuf_printf(&file, "%s\n", git_buf_cstr(&destpath));
Packit ae9e2a
	cl_git_pass(git_filebuf_commit(&file));
Packit ae9e2a
Packit ae9e2a
	git_repository_free(repo);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
void test_odb_alternates__chained(void)
Packit ae9e2a
{
Packit ae9e2a
	git_commit *commit;
Packit ae9e2a
	git_oid oid;
Packit ae9e2a
Packit ae9e2a
	/* Set the alternate A -> testrepo.git */
Packit ae9e2a
	init_linked_repo(paths[0], cl_fixture("testrepo.git"));
Packit ae9e2a
Packit ae9e2a
	/* Set the alternate B -> A */
Packit ae9e2a
	init_linked_repo(paths[1], paths[0]);
Packit ae9e2a
Packit ae9e2a
	/* Now load B and see if we can find an object from testrepo.git */
Packit ae9e2a
	cl_git_pass(git_repository_open(&repo, paths[1]));
Packit ae9e2a
	git_oid_fromstr(&oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
Packit ae9e2a
	cl_git_pass(git_commit_lookup(&commit, repo, &oid));
Packit ae9e2a
	git_commit_free(commit);
Packit ae9e2a
	git_repository_free(repo);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
void test_odb_alternates__long_chain(void)
Packit ae9e2a
{
Packit ae9e2a
	git_commit *commit;
Packit ae9e2a
	git_oid oid;
Packit ae9e2a
	size_t i;
Packit ae9e2a
Packit ae9e2a
	/* Set the alternate A -> testrepo.git */
Packit ae9e2a
	init_linked_repo(paths[0], cl_fixture("testrepo.git"));
Packit ae9e2a
Packit ae9e2a
	/* Set up the five-element chain */
Packit ae9e2a
	for (i = 1; i < ARRAY_SIZE(paths); i++) {
Packit ae9e2a
		init_linked_repo(paths[i], paths[i-1]);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	/* Now load the last one and see if we can find an object from testrepo.git */
Packit ae9e2a
	cl_git_pass(git_repository_open(&repo, paths[ARRAY_SIZE(paths)-1]));
Packit ae9e2a
	git_oid_fromstr(&oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
Packit ae9e2a
	cl_git_fail(git_commit_lookup(&commit, repo, &oid));
Packit ae9e2a
	git_repository_free(repo);
Packit ae9e2a
}