Blame tests/submodule/init.c

Packit ae9e2a
#include "clar_libgit2.h"
Packit ae9e2a
#include "posix.h"
Packit ae9e2a
#include "path.h"
Packit ae9e2a
#include "submodule_helpers.h"
Packit ae9e2a
#include "fileops.h"
Packit ae9e2a
Packit ae9e2a
static git_repository *g_repo = NULL;
Packit ae9e2a
Packit ae9e2a
void test_submodule_init__cleanup(void)
Packit ae9e2a
{
Packit ae9e2a
	cl_git_sandbox_cleanup();
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
void test_submodule_init__absolute_url(void)
Packit ae9e2a
{
Packit ae9e2a
	git_submodule *sm;
Packit ae9e2a
	git_config *cfg;
Packit ae9e2a
	git_buf absolute_url = GIT_BUF_INIT;
Packit ae9e2a
	const char *config_url;
Packit ae9e2a
Packit ae9e2a
	g_repo = setup_fixture_submodule_simple();
Packit ae9e2a
Packit ae9e2a
	cl_assert(git_path_dirname_r(&absolute_url, git_repository_workdir(g_repo)) > 0);
Packit ae9e2a
	cl_git_pass(git_buf_joinpath(&absolute_url, absolute_url.ptr, "testrepo.git"));
Packit ae9e2a
Packit ae9e2a
	/* write the absolute url to the .gitmodules file*/
Packit ae9e2a
	cl_git_pass(git_submodule_set_url(g_repo, "testrepo", absolute_url.ptr));
Packit ae9e2a
Packit ae9e2a
	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
Packit ae9e2a
Packit ae9e2a
	/* verify that the .gitmodules is set with an absolute path*/
Packit ae9e2a
	cl_assert_equal_s(absolute_url.ptr, git_submodule_url(sm));
Packit ae9e2a
Packit ae9e2a
	/* init and verify that absolute path is written to .git/config */
Packit ae9e2a
	cl_git_pass(git_submodule_init(sm, false));
Packit ae9e2a
Packit ae9e2a
	cl_git_pass(git_repository_config_snapshot(&cfg, g_repo));
Packit ae9e2a
Packit ae9e2a
	cl_git_pass(git_config_get_string(&config_url, cfg, "submodule.testrepo.url"));
Packit ae9e2a
	cl_assert_equal_s(absolute_url.ptr, config_url);
Packit ae9e2a
Packit ae9e2a
	git_buf_free(&absolute_url);
Packit ae9e2a
	git_config_free(cfg);
Packit ae9e2a
	git_submodule_free(sm);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
void test_submodule_init__relative_url(void)
Packit ae9e2a
{
Packit ae9e2a
	git_submodule *sm;
Packit ae9e2a
	git_config *cfg;
Packit ae9e2a
	git_buf absolute_url = GIT_BUF_INIT;
Packit ae9e2a
	const char *config_url;
Packit ae9e2a
Packit ae9e2a
	g_repo = setup_fixture_submodule_simple();
Packit ae9e2a
Packit ae9e2a
	cl_assert(git_path_dirname_r(&absolute_url, git_repository_workdir(g_repo)) > 0);
Packit ae9e2a
	cl_git_pass(git_buf_joinpath(&absolute_url, absolute_url.ptr, "testrepo.git"));
Packit ae9e2a
Packit ae9e2a
	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
Packit ae9e2a
Packit ae9e2a
	/* verify that the .gitmodules is set with an absolute path*/
Packit ae9e2a
	cl_assert_equal_s("../testrepo.git", git_submodule_url(sm));
Packit ae9e2a
Packit ae9e2a
	/* init and verify that absolute path is written to .git/config */
Packit ae9e2a
	cl_git_pass(git_submodule_init(sm, false));
Packit ae9e2a
Packit ae9e2a
	cl_git_pass(git_repository_config_snapshot(&cfg, g_repo));
Packit ae9e2a
Packit ae9e2a
	cl_git_pass(git_config_get_string(&config_url, cfg, "submodule.testrepo.url"));
Packit ae9e2a
	cl_assert_equal_s(absolute_url.ptr, config_url);
Packit ae9e2a
Packit ae9e2a
	git_buf_free(&absolute_url);
Packit ae9e2a
	git_config_free(cfg);
Packit ae9e2a
	git_submodule_free(sm);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
void test_submodule_init__relative_url_detached_head(void)
Packit ae9e2a
{
Packit ae9e2a
	git_submodule *sm;
Packit ae9e2a
	git_config *cfg;
Packit ae9e2a
	git_buf absolute_url = GIT_BUF_INIT;
Packit ae9e2a
	const char *config_url;
Packit ae9e2a
	git_reference *head_ref = NULL;
Packit ae9e2a
	git_object *head_commit = NULL;
Packit ae9e2a
Packit ae9e2a
	g_repo = setup_fixture_submodule_simple();
Packit ae9e2a
Packit ae9e2a
	/* Put the parent repository into a detached head state. */
Packit ae9e2a
	cl_git_pass(git_repository_head(&head_ref, g_repo));
Packit ae9e2a
	cl_git_pass(git_reference_peel(&head_commit, head_ref, GIT_OBJ_COMMIT));
Packit ae9e2a
Packit ae9e2a
	cl_git_pass(git_repository_set_head_detached(g_repo, git_commit_id((git_commit *)head_commit)));
Packit ae9e2a
Packit ae9e2a
	cl_assert(git_path_dirname_r(&absolute_url, git_repository_workdir(g_repo)) > 0);
Packit ae9e2a
	cl_git_pass(git_buf_joinpath(&absolute_url, absolute_url.ptr, "testrepo.git"));
Packit ae9e2a
Packit ae9e2a
	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
Packit ae9e2a
Packit ae9e2a
	/* verify that the .gitmodules is set with an absolute path*/
Packit ae9e2a
	cl_assert_equal_s("../testrepo.git", git_submodule_url(sm));
Packit ae9e2a
Packit ae9e2a
	/* init and verify that absolute path is written to .git/config */
Packit ae9e2a
	cl_git_pass(git_submodule_init(sm, false));
Packit ae9e2a
Packit ae9e2a
	cl_git_pass(git_repository_config_snapshot(&cfg, g_repo));
Packit ae9e2a
Packit ae9e2a
	cl_git_pass(git_config_get_string(&config_url, cfg, "submodule.testrepo.url"));
Packit ae9e2a
	cl_assert_equal_s(absolute_url.ptr, config_url);
Packit ae9e2a
Packit ae9e2a
	git_buf_free(&absolute_url);
Packit ae9e2a
	git_config_free(cfg);
Packit ae9e2a
	git_object_free(head_commit);
Packit ae9e2a
	git_reference_free(head_ref);
Packit ae9e2a
	git_submodule_free(sm);
Packit ae9e2a
}