Blame tests/clone/local.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
Packit Service 20376f
#include "git2/clone.h"
Packit Service 20376f
#include "clone.h"
Packit Service 20376f
#include "buffer.h"
Packit Service 20376f
#include "path.h"
Packit Service 20376f
#include "posix.h"
Packit Service 20376f
#include "fileops.h"
Packit Service 20376f
Packit Service 20376f
static int file_url(git_buf *buf, const char *host, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	if (path[0] == '/')
Packit Service 20376f
		path++;
Packit Service 20376f
Packit Service 20376f
	git_buf_clear(buf);
Packit Service 20376f
	return git_buf_printf(buf, "file://%s/%s", host, path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
static int git_style_unc_path(git_buf *buf, const char *host, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	git_buf_clear(buf);
Packit Service 20376f
Packit Service 20376f
	if (host)
Packit Service 20376f
		git_buf_printf(buf, "//%s/", host);
Packit Service 20376f
Packit Service 20376f
	if (path[0] == '/')
Packit Service 20376f
		path++;
Packit Service 20376f
Packit Service 20376f
	if (git__isalpha(path[0]) && path[1] == ':' && path[2] == '/') {
Packit Service 20376f
		git_buf_printf(buf, "%c$/", path[0]);
Packit Service 20376f
		path += 3;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_buf_puts(buf, path);
Packit Service 20376f
Packit Service 20376f
	return git_buf_oom(buf) ? -1 : 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int unc_path(git_buf *buf, const char *host, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	char *c;
Packit Service 20376f
Packit Service 20376f
	if (git_style_unc_path(buf, host, path) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	for (c = buf->ptr; *c; c++)
Packit Service 20376f
		if (*c == '/')
Packit Service 20376f
			*c = '\\';
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
void test_clone_local__should_clone_local(void)
Packit Service 20376f
{
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	/* we use a fixture path because it needs to exist for us to want to clone */
Packit Service 20376f
	const char *path = cl_fixture("testrepo.git");
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(file_url(&buf, "", path));
Packit Service 20376f
	cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_AUTO));
Packit Service 20376f
	cl_assert_equal_i(1,  git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL));
Packit Service 20376f
	cl_assert_equal_i(1,  git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_NO_LINKS));
Packit Service 20376f
	cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_NO_LOCAL));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(file_url(&buf, "localhost", path));
Packit Service 20376f
	cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_AUTO));
Packit Service 20376f
	cl_assert_equal_i(1,  git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL));
Packit Service 20376f
	cl_assert_equal_i(1,  git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_NO_LINKS));
Packit Service 20376f
	cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_NO_LOCAL));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(file_url(&buf, "other-host.mycompany.com", path));
Packit Service 20376f
	cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_AUTO));
Packit Service 20376f
	cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL));
Packit Service 20376f
	cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_NO_LINKS));
Packit Service 20376f
	cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_NO_LOCAL));
Packit Service 20376f
Packit Service 20376f
	/* Ensure that file:/// urls are percent decoded: .git == %2e%67%69%74 */
Packit Service 20376f
	cl_git_pass(file_url(&buf, "", path));
Packit Service 20376f
	git_buf_shorten(&buf, 4);
Packit Service 20376f
	cl_git_pass(git_buf_puts(&buf, "%2e%67%69%74"));
Packit Service 20376f
	cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_AUTO));
Packit Service 20376f
	cl_assert_equal_i(1,  git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL));
Packit Service 20376f
	cl_assert_equal_i(1,  git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_NO_LINKS));
Packit Service 20376f
	cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_NO_LOCAL));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(1,  git_clone__should_clone_local(path, GIT_CLONE_LOCAL_AUTO));
Packit Service 20376f
	cl_assert_equal_i(1,  git_clone__should_clone_local(path, GIT_CLONE_LOCAL));
Packit Service 20376f
	cl_assert_equal_i(1,  git_clone__should_clone_local(path, GIT_CLONE_LOCAL_NO_LINKS));
Packit Service 20376f
	cl_assert_equal_i(0, git_clone__should_clone_local(path, GIT_CLONE_NO_LOCAL));
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_clone_local__hardlinks(void)
Packit Service 20376f
{
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
	git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT;
Packit Service 20376f
	struct stat st;
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * In this first clone, we just copy over, since the temp dir
Packit Service 20376f
	 * will often be in a different filesystem, so we cannot
Packit Service 20376f
	 * link. It also allows us to control the number of links
Packit Service 20376f
	 */
Packit Service 20376f
	opts.bare = true;
Packit Service 20376f
	opts.local = GIT_CLONE_LOCAL_NO_LINKS;
Packit Service 20376f
	cl_git_pass(git_clone(&repo, cl_fixture("testrepo.git"), "./clone.git", &opts));
Packit Service 20376f
	git_repository_free(repo);
Packit Service 20376f
Packit Service 20376f
	/* This second clone is in the same filesystem, so we can hardlink */
Packit Service 20376f
Packit Service 20376f
	opts.local = GIT_CLONE_LOCAL;
Packit Service 20376f
	cl_git_pass(git_clone(&repo, cl_git_path_url("clone.git"), "./clone2.git", &opts));
Packit Service 20376f
Packit Service 20376f
#ifndef GIT_WIN32
Packit Service 20376f
	git_buf_clear(&buf;;
Packit Service 20376f
	cl_git_pass(git_buf_join_n(&buf, '/', 4, git_repository_path(repo), "objects", "08", "b041783f40edfe12bb406c9c9a8a040177c125"));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(p_stat(buf.ptr, &st);;
Packit Service 20376f
	cl_assert_equal_i(2, st.st_nlink);
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	git_repository_free(repo);
Packit Service 20376f
	git_buf_clear(&buf;;
Packit Service 20376f
Packit Service 20376f
	opts.local = GIT_CLONE_LOCAL_NO_LINKS;
Packit Service 20376f
	cl_git_pass(git_clone(&repo, cl_git_path_url("clone.git"), "./clone3.git", &opts));
Packit Service 20376f
Packit Service 20376f
	git_buf_clear(&buf;;
Packit Service 20376f
	cl_git_pass(git_buf_join_n(&buf, '/', 4, git_repository_path(repo), "objects", "08", "b041783f40edfe12bb406c9c9a8a040177c125"));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(p_stat(buf.ptr, &st);;
Packit Service 20376f
	cl_assert_equal_i(1, st.st_nlink);
Packit Service 20376f
Packit Service 20376f
	git_repository_free(repo);
Packit Service 20376f
Packit Service 20376f
	/* this one should automatically use links */
Packit Service 20376f
	cl_git_pass(git_clone(&repo, "./clone.git", "./clone4.git", NULL));
Packit Service 20376f
Packit Service 20376f
#ifndef GIT_WIN32
Packit Service 20376f
	git_buf_clear(&buf;;
Packit Service 20376f
	cl_git_pass(git_buf_join_n(&buf, '/', 4, git_repository_path(repo), "objects", "08", "b041783f40edfe12bb406c9c9a8a040177c125"));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(p_stat(buf.ptr, &st);;
Packit Service 20376f
	cl_assert_equal_i(3, st.st_nlink);
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
	git_repository_free(repo);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_futils_rmdir_r("./clone.git", NULL, GIT_RMDIR_REMOVE_FILES));
Packit Service 20376f
	cl_git_pass(git_futils_rmdir_r("./clone2.git", NULL, GIT_RMDIR_REMOVE_FILES));
Packit Service 20376f
	cl_git_pass(git_futils_rmdir_r("./clone3.git", NULL, GIT_RMDIR_REMOVE_FILES));
Packit Service 20376f
	cl_git_pass(git_futils_rmdir_r("./clone4.git", NULL, GIT_RMDIR_REMOVE_FILES));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_clone_local__standard_unc_paths_are_written_git_style(void)
Packit Service 20376f
{
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
	git_remote *remote;
Packit Service 20376f
	git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
Packit Service 20376f
	git_buf unc = GIT_BUF_INIT, git_unc = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	/* we use a fixture path because it needs to exist for us to want to clone */
Packit Service 20376f
	const char *path = cl_fixture("testrepo.git");
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(unc_path(&unc, "localhost", path));
Packit Service 20376f
	cl_git_pass(git_style_unc_path(&git_unc, "localhost", path));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_clone(&repo, unc.ptr, "./clone.git", &opts));
Packit Service 20376f
	cl_git_pass(git_remote_lookup(&remote, repo, "origin"));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_s(git_unc.ptr, git_remote_url(remote));
Packit Service 20376f
Packit Service 20376f
	git_remote_free(remote);
Packit Service 20376f
	git_repository_free(repo);
Packit Service 20376f
	git_buf_free(&unc);
Packit Service 20376f
	git_buf_free(&git_unc);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_futils_rmdir_r("./clone.git", NULL, GIT_RMDIR_REMOVE_FILES));
Packit Service 20376f
#endif
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_clone_local__git_style_unc_paths(void)
Packit Service 20376f
{
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
	git_remote *remote;
Packit Service 20376f
	git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
Packit Service 20376f
	git_buf git_unc = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	/* we use a fixture path because it needs to exist for us to want to clone */
Packit Service 20376f
	const char *path = cl_fixture("testrepo.git");
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_style_unc_path(&git_unc, "localhost", path));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_clone(&repo, git_unc.ptr, "./clone.git", &opts));
Packit Service 20376f
	cl_git_pass(git_remote_lookup(&remote, repo, "origin"));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_s(git_unc.ptr, git_remote_url(remote));
Packit Service 20376f
Packit Service 20376f
	git_remote_free(remote);
Packit Service 20376f
	git_repository_free(repo);
Packit Service 20376f
	git_buf_free(&git_unc);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_futils_rmdir_r("./clone.git", NULL, GIT_RMDIR_REMOVE_FILES));
Packit Service 20376f
#endif
Packit Service 20376f
}