crlf.c 1.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#include "clar_libgit2.h"
#include "git2/sys/filter.h"

static git_repository *g_repo = NULL;

void test_filter_crlf__initialize(void)
{
	g_repo = cl_git_sandbox_init("crlf");

	cl_git_mkfile("crlf/.gitattributes",
		"*.txt text\n*.bin binary\n*.crlf text eol=crlf\n*.lf text eol=lf\n");
Russell Belfer committed
12

13
	cl_repo_set_bool(g_repo, "core.autocrlf", true);
14 15 16 17 18 19 20 21 22 23 24
}

void test_filter_crlf__cleanup(void)
{
	cl_git_sandbox_cleanup();
}

void test_filter_crlf__to_worktree(void)
{
	git_filter_list *fl;
	git_filter *crlf;
25
	git_buf in = { 0 }, out = { 0 };
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

	cl_git_pass(git_filter_list_new(&fl, g_repo, GIT_FILTER_TO_WORKTREE));

	crlf = git_filter_lookup(GIT_FILTER_CRLF);
	cl_assert(crlf != NULL);

	cl_git_pass(git_filter_list_push(fl, crlf, NULL));

	in.ptr = "Some text\nRight here\n";
	in.size = strlen(in.ptr);

	cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));

#ifdef GIT_WIN32
	cl_assert_equal_s("Some text\r\nRight here\r\n", out.ptr);
#else
	cl_assert_equal_s("Some text\nRight here\n", out.ptr);
#endif

	git_filter_list_free(fl);
46
	git_buf_free(&out);
47 48 49 50 51 52
}

void test_filter_crlf__to_odb(void)
{
	git_filter_list *fl;
	git_filter *crlf;
53
	git_buf in = { 0 }, out = { 0 };
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

	cl_git_pass(git_filter_list_new(&fl, g_repo, GIT_FILTER_TO_ODB));

	crlf = git_filter_lookup(GIT_FILTER_CRLF);
	cl_assert(crlf != NULL);

	cl_git_pass(git_filter_list_push(fl, crlf, NULL));

	in.ptr = "Some text\r\nRight here\r\n";
	in.size = strlen(in.ptr);

	cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));

	cl_assert_equal_s("Some text\nRight here\n", out.ptr);

	git_filter_list_free(fl);
70
	git_buf_free(&out);
71
}