crlf.c 6.71 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 26 27
	git_buf out = GIT_BUF_INIT;
	const char *in;
	size_t in_len;
28

29 30
	cl_git_pass(git_filter_list_new(
		&fl, g_repo, GIT_FILTER_TO_WORKTREE, 0));
31 32 33 34 35 36

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

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

37 38
	in = "Some text\nRight here\n";
	in_len = strlen(in);
39

40
	cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
41 42 43 44

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

	git_filter_list_free(fl);
45
	git_buf_dispose(&out);
46 47 48 49 50 51
}

void test_filter_crlf__to_odb(void)
{
	git_filter_list *fl;
	git_filter *crlf;
52 53 54
	git_buf out = GIT_BUF_INIT;
	const char *in;
	size_t in_len;
55

56 57
	cl_git_pass(git_filter_list_new(
		&fl, g_repo, GIT_FILTER_TO_ODB, 0));
58 59 60 61 62 63

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

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

64 65
	in = "Some text\r\nRight here\r\n";
	in_len = strlen(in);
66

67
	cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
68 69 70 71

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

	git_filter_list_free(fl);
72
	git_buf_dispose(&out);
73
}
74 75 76 77 78

void test_filter_crlf__with_safecrlf(void)
{
	git_filter_list *fl;
	git_filter *crlf;
79 80 81
	git_buf out = GIT_BUF_INIT;
	const char *in;
	size_t in_len;
82 83 84

	cl_repo_set_bool(g_repo, "core.safecrlf", true);

85 86
	cl_git_pass(git_filter_list_new(
		&fl, g_repo, GIT_FILTER_TO_ODB, 0));
87 88 89 90 91 92 93

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

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

	/* Normalized \r\n succeeds with safecrlf */
94 95
	in = "Normal\r\nCRLF\r\nline-endings.\r\n";
	in_len = strlen(in);
96

97
	cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
98 99 100
	cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr);

	/* Mix of line endings fails with safecrlf */
101 102
	in = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
	in_len = strlen(in);
103

104
	cl_git_fail(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
105
	cl_assert_equal_i(git_error_last()->klass, GIT_ERROR_FILTER);
106

107
	/* Normalized \n fails for autocrlf=true when safecrlf=true */
108 109
	in = "Normal\nLF\nonly\nline-endings.\n";
	in_len = strlen(in);
110

111
	cl_git_fail(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
112
	cl_assert_equal_i(git_error_last()->klass, GIT_ERROR_FILTER);
113 114

	/* String with \r but without \r\n does not fail with safecrlf */
115 116
	in = "Normal\nCR only\rand some more\nline-endings.\n";
	in_len = strlen(in);
117

118
	cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
119
	cl_assert_equal_s("Normal\nCR only\rand some more\nline-endings.\n", out.ptr);
120 121

	git_filter_list_free(fl);
122
	git_buf_dispose(&out);
123 124
}

125 126 127 128
void test_filter_crlf__with_safecrlf_and_unsafe_allowed(void)
{
	git_filter_list *fl;
	git_filter *crlf;
129 130 131
	git_buf out = GIT_BUF_INIT;
	const char *in;
	size_t in_len;
132 133 134 135

	cl_repo_set_bool(g_repo, "core.safecrlf", true);

	cl_git_pass(git_filter_list_new(
136
		&fl, g_repo, GIT_FILTER_TO_ODB, GIT_FILTER_ALLOW_UNSAFE));
137 138 139 140 141 142 143

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

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

	/* Normalized \r\n succeeds with safecrlf */
144 145
	in = "Normal\r\nCRLF\r\nline-endings.\r\n";
	in_len = strlen(in);
146

147
	cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
148 149 150
	cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr);

	/* Mix of line endings fails with safecrlf, but allowed to pass */
151 152
	in = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
	in_len = strlen(in);
153

154
	cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
155 156 157 158
	/* TODO: check for warning */
	cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out.ptr);

	/* Normalized \n fails with safecrlf, but allowed to pass */
159 160
	in = "Normal\nLF\nonly\nline-endings.\n";
	in_len = strlen(in);
161

162
	cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
163 164 165 166
	/* TODO: check for warning */
	cl_assert_equal_s("Normal\nLF\nonly\nline-endings.\n", out.ptr);

	git_filter_list_free(fl);
167
	git_buf_dispose(&out);
168 169
}

170 171 172 173
void test_filter_crlf__no_safecrlf(void)
{
	git_filter_list *fl;
	git_filter *crlf;
174 175 176
	git_buf out = GIT_BUF_INIT;
	const char *in;
	size_t in_len;
177

178 179
	cl_git_pass(git_filter_list_new(
		&fl, g_repo, GIT_FILTER_TO_ODB, 0));
180 181 182 183 184 185 186

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

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

	/* Normalized \r\n succeeds with safecrlf */
187 188
	in = "Normal\r\nCRLF\r\nline-endings.\r\n";
	in_len = strlen(in);
189

190
	cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
191 192 193
	cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr);

	/* Mix of line endings fails with safecrlf */
194 195
	in = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
	in_len = strlen(in);
196

197
	cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
198 199 200
	cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out.ptr);

	/* Normalized \n fails with safecrlf */
201 202
	in = "Normal\nLF\nonly\nline-endings.\n";
	in_len = strlen(in);
203

204
	cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
205 206 207
	cl_assert_equal_s("Normal\nLF\nonly\nline-endings.\n", out.ptr);

	git_filter_list_free(fl);
208
	git_buf_dispose(&out);
209 210
}

211 212 213 214
void test_filter_crlf__safecrlf_warn(void)
{
	git_filter_list *fl;
	git_filter *crlf;
215 216 217
	git_buf out = GIT_BUF_INIT;
	const char *in;
	size_t in_len;
218 219 220 221 222 223 224 225 226 227 228 229

	cl_repo_set_string(g_repo, "core.safecrlf", "warn");

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

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

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

	/* Normalized \r\n succeeds with safecrlf=warn */
230 231
	in = "Normal\r\nCRLF\r\nline-endings.\r\n";
	in_len = strlen(in);
232

233
	cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
234 235 236
	cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr);

	/* Mix of line endings succeeds with safecrlf=warn */
237 238
	in = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
	in_len = strlen(in);
239

240
	cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
241 242 243 244
	/* TODO: check for warning */
	cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out.ptr);

	/* Normalized \n is reversible, so does not fail with safecrlf=warn */
245 246
	in = "Normal\nLF\nonly\nline-endings.\n";
	in_len = strlen(in);
247

248 249
	cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
	cl_assert_equal_s(in, out.ptr);
250 251

	git_filter_list_free(fl);
252
	git_buf_dispose(&out);
253
}