Blame tests/clar_libgit2.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
#include "posix.h"
Packit Service 20376f
#include "path.h"
Packit Service 20376f
#include "git2/sys/repository.h"
Packit Service 20376f
Packit Service 20376f
void cl_git_report_failure(
Packit Service 20376f
	int error, int expected, const char *file, int line, const char *fncall)
Packit Service 20376f
{
Packit Service 20376f
	char msg[4096];
Packit Service 20376f
	const git_error *last = giterr_last();
Packit Service 20376f
Packit Service 20376f
	if (expected)
Packit Service 20376f
		p_snprintf(msg, 4096, "error %d (expected %d) - %s",
Packit Service 20376f
			error, expected, last ? last->message : "<no message>");
Packit Service 20376f
	else if (error || last)
Packit Service 20376f
		p_snprintf(msg, 4096, "error %d - %s",
Packit Service 20376f
			error, last ? last->message : "<no message>");
Packit Service 20376f
	else
Packit Service 20376f
		p_snprintf(msg, 4096, "no error, expected non-zero return");
Packit Service 20376f
Packit Service 20376f
	clar__assert(0, file, line, fncall, msg, 1);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void cl_git_mkfile(const char *filename, const char *content)
Packit Service 20376f
{
Packit Service 20376f
	int fd;
Packit Service 20376f
Packit Service 20376f
	fd = p_creat(filename, 0666);
Packit Service 20376f
	cl_assert(fd != -1);
Packit Service 20376f
Packit Service 20376f
	if (content) {
Packit Service 20376f
		cl_must_pass(p_write(fd, content, strlen(content)));
Packit Service 20376f
	} else {
Packit Service 20376f
		cl_must_pass(p_write(fd, filename, strlen(filename)));
Packit Service 20376f
		cl_must_pass(p_write(fd, "\n", 1));
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(p_close(fd));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void cl_git_write2file(
Packit Service 20376f
	const char *path, const char *content, size_t content_len,
Packit Service 20376f
	int flags, unsigned int mode)
Packit Service 20376f
{
Packit Service 20376f
	int fd;
Packit Service 20376f
	cl_assert(path && content);
Packit Service 20376f
	cl_assert((fd = p_open(path, flags, mode)) >= 0);
Packit Service 20376f
	if (!content_len)
Packit Service 20376f
		content_len = strlen(content);
Packit Service 20376f
	cl_must_pass(p_write(fd, content, content_len));
Packit Service 20376f
	cl_must_pass(p_close(fd));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void cl_git_append2file(const char *path, const char *content)
Packit Service 20376f
{
Packit Service 20376f
	cl_git_write2file(path, content, 0, O_WRONLY | O_CREAT | O_APPEND, 0644);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void cl_git_rewritefile(const char *path, const char *content)
Packit Service 20376f
{
Packit Service 20376f
	cl_git_write2file(path, content, 0, O_WRONLY | O_CREAT | O_TRUNC, 0644);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void cl_git_rmfile(const char *filename)
Packit Service 20376f
{
Packit Service 20376f
	cl_must_pass(p_unlink(filename));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
char *cl_getenv(const char *name)
Packit Service 20376f
{
Packit Service 20376f
	git_buf out = GIT_BUF_INIT;
Packit Service 20376f
	int error = git__getenv(&out, name);
Packit Service 20376f
Packit Service 20376f
	cl_assert(error >= 0 || error == GIT_ENOTFOUND);
Packit Service 20376f
Packit Service 20376f
	if (error == GIT_ENOTFOUND)
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	if (out.size == 0) {
Packit Service 20376f
		char *dup = git__strdup("");
Packit Service 20376f
		cl_assert(dup);
Packit Service 20376f
Packit Service 20376f
		return dup;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return git_buf_detach(&out;;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
bool cl_is_env_set(const char *name)
Packit Service 20376f
{
Packit Service 20376f
	char *env = cl_getenv(name);
Packit Service 20376f
	bool result = (env != NULL);
Packit Service 20376f
	git__free(env);
Packit Service 20376f
	return result;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
Packit Service 20376f
#include "win32/utf-conv.h"
Packit Service 20376f
Packit Service 20376f
int cl_setenv(const char *name, const char *value)
Packit Service 20376f
{
Packit Service 20376f
	wchar_t *wide_name, *wide_value = NULL;
Packit Service 20376f
Packit Service 20376f
	cl_assert(git__utf8_to_16_alloc(&wide_name, name) >= 0);
Packit Service 20376f
Packit Service 20376f
	if (value) {
Packit Service 20376f
		cl_assert(git__utf8_to_16_alloc(&wide_value, value) >= 0);
Packit Service 20376f
		cl_assert(SetEnvironmentVariableW(wide_name, wide_value));
Packit Service 20376f
	} else {
Packit Service 20376f
		/* Windows XP returns 0 (failed) when passing NULL for lpValue when
Packit Service 20376f
		* lpName does not exist in the environment block. This behavior
Packit Service 20376f
		* seems to have changed in later versions. Don't check the return value
Packit Service 20376f
		* of SetEnvironmentVariable when passing NULL for lpValue. */
Packit Service 20376f
		SetEnvironmentVariableW(wide_name, NULL);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git__free(wide_name);
Packit Service 20376f
	git__free(wide_value);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* This function performs retries on calls to MoveFile in order
Packit Service 20376f
 * to provide enhanced reliability in the face of antivirus
Packit Service 20376f
 * agents that may be scanning the source (or in the case that
Packit Service 20376f
 * the source is a directory, a child of the source). */
Packit Service 20376f
int cl_rename(const char *source, const char *dest)
Packit Service 20376f
{
Packit Service 20376f
	git_win32_path source_utf16;
Packit Service 20376f
	git_win32_path dest_utf16;
Packit Service 20376f
	unsigned retries = 1;
Packit Service 20376f
Packit Service 20376f
	cl_assert(git_win32_path_from_utf8(source_utf16, source) >= 0);
Packit Service 20376f
	cl_assert(git_win32_path_from_utf8(dest_utf16, dest) >= 0);
Packit Service 20376f
Packit Service 20376f
	while (!MoveFileW(source_utf16, dest_utf16)) {
Packit Service 20376f
		/* Only retry if the error is ERROR_ACCESS_DENIED;
Packit Service 20376f
		 * this may indicate that an antivirus agent is
Packit Service 20376f
		 * preventing the rename from source to target */
Packit Service 20376f
		if (retries > 5 ||
Packit Service 20376f
			ERROR_ACCESS_DENIED != GetLastError())
Packit Service 20376f
			return -1;
Packit Service 20376f
Packit Service 20376f
		/* With 5 retries and a coefficient of 10ms, the maximum
Packit Service 20376f
		 * delay here is 550 ms */
Packit Service 20376f
		Sleep(10 * retries * retries);
Packit Service 20376f
		retries++;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#else
Packit Service 20376f
Packit Service 20376f
#include <stdlib.h>
Packit Service 20376f
Packit Service 20376f
int cl_setenv(const char *name, const char *value)
Packit Service 20376f
{
Packit Service 20376f
	return (value == NULL) ? unsetenv(name) : setenv(name, value, 1);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int cl_rename(const char *source, const char *dest)
Packit Service 20376f
{
Packit Service 20376f
	return p_rename(source, dest);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
static const char *_cl_sandbox = NULL;
Packit Service 20376f
static git_repository *_cl_repo = NULL;
Packit Service 20376f
Packit Service 20376f
git_repository *cl_git_sandbox_init(const char *sandbox)
Packit Service 20376f
{
Packit Service 20376f
	/* Copy the whole sandbox folder from our fixtures to our test sandbox
Packit Service 20376f
	 * area.  After this it can be accessed with `./sandbox`
Packit Service 20376f
	 */
Packit Service 20376f
	cl_fixture_sandbox(sandbox);
Packit Service 20376f
	_cl_sandbox = sandbox;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(p_chdir(sandbox));
Packit Service 20376f
Packit Service 20376f
	/* If this is not a bare repo, then rename `sandbox/.gitted` to
Packit Service 20376f
	 * `sandbox/.git` which must be done since we cannot store a folder
Packit Service 20376f
	 * named `.git` inside the fixtures folder of our libgit2 repo.
Packit Service 20376f
	 */
Packit Service 20376f
	if (p_access(".gitted", F_OK) == 0)
Packit Service 20376f
		cl_git_pass(cl_rename(".gitted", ".git"));
Packit Service 20376f
Packit Service 20376f
	/* If we have `gitattributes`, rename to `.gitattributes`.  This may
Packit Service 20376f
	 * be necessary if we don't want the attributes to be applied in the
Packit Service 20376f
	 * libgit2 repo, but just during testing.
Packit Service 20376f
	 */
Packit Service 20376f
	if (p_access("gitattributes", F_OK) == 0)
Packit Service 20376f
		cl_git_pass(cl_rename("gitattributes", ".gitattributes"));
Packit Service 20376f
Packit Service 20376f
	/* As with `gitattributes`, we may need `gitignore` just for testing. */
Packit Service 20376f
	if (p_access("gitignore", F_OK) == 0)
Packit Service 20376f
		cl_git_pass(cl_rename("gitignore", ".gitignore"));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(p_chdir(".."));
Packit Service 20376f
Packit Service 20376f
	/* Now open the sandbox repository and make it available for tests */
Packit Service 20376f
	cl_git_pass(git_repository_open(&_cl_repo, sandbox));
Packit Service 20376f
Packit Service 20376f
	/* Adjust configs after copying to new filesystem */
Packit Service 20376f
	cl_git_pass(git_repository_reinit_filesystem(_cl_repo, 0));
Packit Service 20376f
Packit Service 20376f
	return _cl_repo;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
git_repository *cl_git_sandbox_init_new(const char *sandbox)
Packit Service 20376f
{
Packit Service 20376f
	cl_git_pass(git_repository_init(&_cl_repo, sandbox, false));
Packit Service 20376f
	_cl_sandbox = sandbox;
Packit Service 20376f
Packit Service 20376f
	return _cl_repo;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
git_repository *cl_git_sandbox_reopen(void)
Packit Service 20376f
{
Packit Service 20376f
	if (_cl_repo) {
Packit Service 20376f
		git_repository_free(_cl_repo);
Packit Service 20376f
		_cl_repo = NULL;
Packit Service 20376f
Packit Service 20376f
		cl_git_pass(git_repository_open(&_cl_repo, _cl_sandbox));
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return _cl_repo;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void cl_git_sandbox_cleanup(void)
Packit Service 20376f
{
Packit Service 20376f
	if (_cl_repo) {
Packit Service 20376f
		git_repository_free(_cl_repo);
Packit Service 20376f
		_cl_repo = NULL;
Packit Service 20376f
	}
Packit Service 20376f
	if (_cl_sandbox) {
Packit Service 20376f
		cl_fixture_cleanup(_cl_sandbox);
Packit Service 20376f
		_cl_sandbox = NULL;
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
bool cl_toggle_filemode(const char *filename)
Packit Service 20376f
{
Packit Service 20376f
	struct stat st1, st2;
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(p_stat(filename, &st1));
Packit Service 20376f
	cl_must_pass(p_chmod(filename, st1.st_mode ^ 0100));
Packit Service 20376f
	cl_must_pass(p_stat(filename, &st2));
Packit Service 20376f
Packit Service 20376f
	return (st1.st_mode != st2.st_mode);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
bool cl_is_chmod_supported(void)
Packit Service 20376f
{
Packit Service 20376f
	static int _is_supported = -1;
Packit Service 20376f
Packit Service 20376f
	if (_is_supported < 0) {
Packit Service 20376f
		cl_git_mkfile("filemode.t", "Test if filemode can be modified");
Packit Service 20376f
		_is_supported = cl_toggle_filemode("filemode.t");
Packit Service 20376f
		cl_must_pass(p_unlink("filemode.t"));
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return _is_supported;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
const char* cl_git_fixture_url(const char *fixturename)
Packit Service 20376f
{
Packit Service 20376f
	return cl_git_path_url(cl_fixture(fixturename));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
const char* cl_git_path_url(const char *path)
Packit Service 20376f
{
Packit Service 20376f
	static char url[4096];
Packit Service 20376f
Packit Service 20376f
	const char *in_buf;
Packit Service 20376f
	git_buf path_buf = GIT_BUF_INIT;
Packit Service 20376f
	git_buf url_buf = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_path_prettify_dir(&path_buf, path, NULL));
Packit Service 20376f
	cl_git_pass(git_buf_puts(&url_buf, "file://"));
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	/*
Packit Service 20376f
	 * A FILE uri matches the following format: file://[host]/path
Packit Service 20376f
	 * where "host" can be empty and "path" is an absolute path to the resource.
Packit Service 20376f
	 *
Packit Service 20376f
	 * In this test, no hostname is used, but we have to ensure the leading triple slashes:
Packit Service 20376f
	 *
Packit Service 20376f
	 * *nix: file:///usr/home/...
Packit Service 20376f
	 * Windows: file:///C:/Users/...
Packit Service 20376f
	 */
Packit Service 20376f
	cl_git_pass(git_buf_putc(&url_buf, '/'));
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	in_buf = git_buf_cstr(&path_buf);
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * A very hacky Url encoding that only takes care of escaping the spaces
Packit Service 20376f
	 */
Packit Service 20376f
	while (*in_buf) {
Packit Service 20376f
		if (*in_buf == ' ')
Packit Service 20376f
			cl_git_pass(git_buf_puts(&url_buf, "%20"));
Packit Service 20376f
		else
Packit Service 20376f
			cl_git_pass(git_buf_putc(&url_buf, *in_buf));
Packit Service 20376f
Packit Service 20376f
		in_buf++;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	cl_assert(url_buf.size < 4096);
Packit Service 20376f
Packit Service 20376f
	strncpy(url, git_buf_cstr(&url_buf), 4096);
Packit Service 20376f
	git_buf_free(&url_buf);
Packit Service 20376f
	git_buf_free(&path_buf);
Packit Service 20376f
	return url;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
typedef struct {
Packit Service 20376f
	const char *filename;
Packit Service 20376f
	size_t filename_len;
Packit Service 20376f
} remove_data;
Packit Service 20376f
Packit Service 20376f
static int remove_placeholders_recurs(void *_data, git_buf *path)
Packit Service 20376f
{
Packit Service 20376f
	remove_data *data = (remove_data *)_data;
Packit Service 20376f
	size_t pathlen;
Packit Service 20376f
Packit Service 20376f
	if (git_path_isdir(path->ptr) == true)
Packit Service 20376f
		return git_path_direach(path, 0, remove_placeholders_recurs, data);
Packit Service 20376f
Packit Service 20376f
	pathlen = path->size;
Packit Service 20376f
Packit Service 20376f
	if (pathlen < data->filename_len)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	/* if path ends in '/'+filename (or equals filename) */
Packit Service 20376f
	if (!strcmp(data->filename, path->ptr + pathlen - data->filename_len) &&
Packit Service 20376f
		(pathlen == data->filename_len ||
Packit Service 20376f
		 path->ptr[pathlen - data->filename_len - 1] == '/'))
Packit Service 20376f
		return p_unlink(path->ptr);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int cl_git_remove_placeholders(const char *directory_path, const char *filename)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	remove_data data;
Packit Service 20376f
	git_buf buffer = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	if (git_path_isdir(directory_path) == false)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (git_buf_sets(&buffer, directory_path) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	data.filename = filename;
Packit Service 20376f
	data.filename_len = strlen(filename);
Packit Service 20376f
Packit Service 20376f
	error = remove_placeholders_recurs(&data, &buffer);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&buffer);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define CL_COMMIT_NAME "Libgit2 Tester"
Packit Service 20376f
#define CL_COMMIT_EMAIL "libgit2-test@github.com"
Packit Service 20376f
#define CL_COMMIT_MSG "Test commit of tree "
Packit Service 20376f
Packit Service 20376f
void cl_repo_commit_from_index(
Packit Service 20376f
	git_oid *out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	git_signature *sig,
Packit Service 20376f
	git_time_t time,
Packit Service 20376f
	const char *msg)
Packit Service 20376f
{
Packit Service 20376f
	git_index *index;
Packit Service 20376f
	git_oid commit_id, tree_id;
Packit Service 20376f
	git_object *parent = NULL;
Packit Service 20376f
	git_reference *ref = NULL;
Packit Service 20376f
	git_tree *tree = NULL;
Packit Service 20376f
	char buf[128];
Packit Service 20376f
	int free_sig = (sig == NULL);
Packit Service 20376f
Packit Service 20376f
	/* it is fine if looking up HEAD fails - we make this the first commit */
Packit Service 20376f
	git_revparse_ext(&parent, &ref, repo, "HEAD");
Packit Service 20376f
Packit Service 20376f
	/* write the index content as a tree */
Packit Service 20376f
	cl_git_pass(git_repository_index(&index, repo));
Packit Service 20376f
	cl_git_pass(git_index_write_tree(&tree_id, index));
Packit Service 20376f
	cl_git_pass(git_index_write(index));
Packit Service 20376f
	git_index_free(index);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_tree_lookup(&tree, repo, &tree_id));
Packit Service 20376f
Packit Service 20376f
	if (sig)
Packit Service 20376f
		cl_assert(sig->name && sig->email);
Packit Service 20376f
	else if (!time)
Packit Service 20376f
		cl_git_pass(git_signature_now(&sig, CL_COMMIT_NAME, CL_COMMIT_EMAIL));
Packit Service 20376f
	else
Packit Service 20376f
		cl_git_pass(git_signature_new(
Packit Service 20376f
			&sig, CL_COMMIT_NAME, CL_COMMIT_EMAIL, time, 0));
Packit Service 20376f
Packit Service 20376f
	if (!msg) {
Packit Service 20376f
		strcpy(buf, CL_COMMIT_MSG);
Packit Service 20376f
		git_oid_tostr(buf + strlen(CL_COMMIT_MSG),
Packit Service 20376f
			sizeof(buf) - strlen(CL_COMMIT_MSG), &tree_id);
Packit Service 20376f
		msg = buf;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_commit_create_v(
Packit Service 20376f
		&commit_id, repo, ref ? git_reference_name(ref) : "HEAD",
Packit Service 20376f
		sig, sig, NULL, msg, tree, parent ? 1 : 0, parent));
Packit Service 20376f
Packit Service 20376f
	if (out)
Packit Service 20376f
		git_oid_cpy(out, &commit_id);
Packit Service 20376f
Packit Service 20376f
	git_object_free(parent);
Packit Service 20376f
	git_reference_free(ref);
Packit Service 20376f
	if (free_sig)
Packit Service 20376f
		git_signature_free(sig);
Packit Service 20376f
	git_tree_free(tree);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void cl_repo_set_bool(git_repository *repo, const char *cfg, int value)
Packit Service 20376f
{
Packit Service 20376f
	git_config *config;
Packit Service 20376f
	cl_git_pass(git_repository_config(&config, repo));
Packit Service 20376f
	cl_git_pass(git_config_set_bool(config, cfg, value != 0));
Packit Service 20376f
	git_config_free(config);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int cl_repo_get_bool(git_repository *repo, const char *cfg)
Packit Service 20376f
{
Packit Service 20376f
	int val = 0;
Packit Service 20376f
	git_config *config;
Packit Service 20376f
	cl_git_pass(git_repository_config(&config, repo));
Packit Service 20376f
	if (git_config_get_bool(&val, config, cfg) < 0)
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
	git_config_free(config);
Packit Service 20376f
	return val;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void cl_repo_set_string(git_repository *repo, const char *cfg, const char *value)
Packit Service 20376f
{
Packit Service 20376f
	git_config *config;
Packit Service 20376f
	cl_git_pass(git_repository_config(&config, repo));
Packit Service 20376f
	cl_git_pass(git_config_set_string(config, cfg, value));
Packit Service 20376f
	git_config_free(config);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* this is essentially the code from git__unescape modified slightly */
Packit Service 20376f
static size_t strip_cr_from_buf(char *start, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	char *scan, *trail, *end = start + len;
Packit Service 20376f
Packit Service 20376f
	for (scan = trail = start; scan < end; trail++, scan++) {
Packit Service 20376f
		while (*scan == '\r')
Packit Service 20376f
			scan++; /* skip '\r' */
Packit Service 20376f
Packit Service 20376f
		if (trail != scan)
Packit Service 20376f
			*trail = *scan;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*trail = '\0';
Packit Service 20376f
Packit Service 20376f
	return (trail - start);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void clar__assert_equal_file(
Packit Service 20376f
	const char *expected_data,
Packit Service 20376f
	size_t expected_bytes,
Packit Service 20376f
	int ignore_cr,
Packit Service 20376f
	const char *path,
Packit Service 20376f
	const char *file,
Packit Service 20376f
	int line)
Packit Service 20376f
{
Packit Service 20376f
	char buf[4000];
Packit Service 20376f
	ssize_t bytes, total_bytes = 0;
Packit Service 20376f
	int fd = p_open(path, O_RDONLY | O_BINARY);
Packit Service 20376f
	cl_assert(fd >= 0);
Packit Service 20376f
Packit Service 20376f
	if (expected_data && !expected_bytes)
Packit Service 20376f
		expected_bytes = strlen(expected_data);
Packit Service 20376f
Packit Service 20376f
	while ((bytes = p_read(fd, buf, sizeof(buf))) != 0) {
Packit Service 20376f
		clar__assert(
Packit Service 20376f
			bytes > 0, file, line, "error reading from file", path, 1);
Packit Service 20376f
Packit Service 20376f
		if (ignore_cr)
Packit Service 20376f
			bytes = strip_cr_from_buf(buf, bytes);
Packit Service 20376f
Packit Service 20376f
		if (memcmp(expected_data, buf, bytes) != 0) {
Packit Service 20376f
			int pos;
Packit Service 20376f
			for (pos = 0; pos < bytes && expected_data[pos] == buf[pos]; ++pos)
Packit Service 20376f
				/* find differing byte offset */;
Packit Service 20376f
			p_snprintf(
Packit Service 20376f
				buf, sizeof(buf), "file content mismatch at byte %"PRIdZ,
Packit Service 20376f
				(ssize_t)(total_bytes + pos));
Packit Service 20376f
			p_close(fd);
Packit Service 20376f
			clar__fail(file, line, path, buf, 1);
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		expected_data += bytes;
Packit Service 20376f
		total_bytes   += bytes;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	p_close(fd);
Packit Service 20376f
Packit Service 20376f
	clar__assert(!bytes, file, line, "error reading from file", path, 1);
Packit Service 20376f
	clar__assert_equal(file, line, "mismatched file length", 1, "%"PRIuZ,
Packit Service 20376f
		(size_t)expected_bytes, (size_t)total_bytes);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static char *_cl_restore_home = NULL;
Packit Service 20376f
Packit Service 20376f
void cl_fake_home_cleanup(void *payload)
Packit Service 20376f
{
Packit Service 20376f
	char *restore = _cl_restore_home;
Packit Service 20376f
	_cl_restore_home = NULL;
Packit Service 20376f
Packit Service 20376f
	GIT_UNUSED(payload);
Packit Service 20376f
Packit Service 20376f
	if (restore) {
Packit Service 20376f
		cl_git_pass(git_libgit2_opts(
Packit Service 20376f
			GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, restore));
Packit Service 20376f
		git__free(restore);
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void cl_fake_home(void)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_libgit2_opts(
Packit Service 20376f
		GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &path));
Packit Service 20376f
Packit Service 20376f
	_cl_restore_home = git_buf_detach(&path);
Packit Service 20376f
	cl_set_cleanup(cl_fake_home_cleanup, NULL);
Packit Service 20376f
Packit Service 20376f
	if (!git_path_exists("home"))
Packit Service 20376f
		cl_must_pass(p_mkdir("home", 0777));
Packit Service 20376f
	cl_git_pass(git_path_prettify(&path, "home", NULL));
Packit Service 20376f
	cl_git_pass(git_libgit2_opts(
Packit Service 20376f
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void cl_sandbox_set_search_path_defaults(void)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	git_buf_joinpath(&path, clar_sandbox_path(), "__config");
Packit Service 20376f
Packit Service 20376f
	if (!git_path_exists(path.ptr))
Packit Service 20376f
		cl_must_pass(p_mkdir(path.ptr, 0777));
Packit Service 20376f
Packit Service 20376f
	git_libgit2_opts(
Packit Service 20376f
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr);
Packit Service 20376f
	git_libgit2_opts(
Packit Service 20376f
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr);
Packit Service 20376f
	git_libgit2_opts(
Packit Service 20376f
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr);
Packit Service 20376f
	git_libgit2_opts(
Packit Service 20376f
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_PROGRAMDATA, path.ptr);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
bool cl_sandbox_supports_8dot3(void)
Packit Service 20376f
{
Packit Service 20376f
	git_buf longpath = GIT_BUF_INIT;
Packit Service 20376f
	char *shortname;
Packit Service 20376f
	bool supported;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(
Packit Service 20376f
		git_buf_joinpath(&longpath, clar_sandbox_path(), "longer_than_8dot3"));
Packit Service 20376f
Packit Service 20376f
	cl_git_write2file(longpath.ptr, "", 0, O_RDWR|O_CREAT, 0666);
Packit Service 20376f
	shortname = git_win32_path_8dot3_name(longpath.ptr);
Packit Service 20376f
Packit Service 20376f
	supported = (shortname != NULL);
Packit Service 20376f
Packit Service 20376f
	git__free(shortname);
Packit Service 20376f
	git_buf_free(&longpath);
Packit Service 20376f
Packit Service 20376f
	return supported;
Packit Service 20376f
}
Packit Service 20376f
#endif
Packit Service 20376f