]> git.proxmox.com Git - libgit2.git/blame - tests/libgit2/filter/crlf.c
Merge https://salsa.debian.org/debian/libgit2 into proxmox/bullseye
[libgit2.git] / tests / libgit2 / filter / crlf.c
CommitLineData
40cb40fa
RB
1#include "clar_libgit2.h"
2#include "git2/sys/filter.h"
3
4static git_repository *g_repo = NULL;
5
6void test_filter_crlf__initialize(void)
7{
8 g_repo = cl_git_sandbox_init("crlf");
9
10 cl_git_mkfile("crlf/.gitattributes",
11 "*.txt text\n*.bin binary\n*.crlf text eol=crlf\n*.lf text eol=lf\n");
b47349b8 12
8427757f 13 cl_repo_set_bool(g_repo, "core.autocrlf", true);
40cb40fa
RB
14}
15
16void test_filter_crlf__cleanup(void)
17{
18 cl_git_sandbox_cleanup();
19}
20
21void test_filter_crlf__to_worktree(void)
22{
23 git_filter_list *fl;
24 git_filter *crlf;
c25aa7cd
PP
25 git_buf out = GIT_BUF_INIT;
26 const char *in;
27 size_t in_len;
40cb40fa 28
5269008c
RB
29 cl_git_pass(git_filter_list_new(
30 &fl, g_repo, GIT_FILTER_TO_WORKTREE, 0));
40cb40fa
RB
31
32 crlf = git_filter_lookup(GIT_FILTER_CRLF);
33 cl_assert(crlf != NULL);
34
35 cl_git_pass(git_filter_list_push(fl, crlf, NULL));
36
c25aa7cd
PP
37 in = "Some text\nRight here\n";
38 in_len = strlen(in);
40cb40fa 39
c25aa7cd 40 cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
40cb40fa 41
40cb40fa 42 cl_assert_equal_s("Some text\r\nRight here\r\n", out.ptr);
40cb40fa
RB
43
44 git_filter_list_free(fl);
ac3d33df 45 git_buf_dispose(&out);
40cb40fa
RB
46}
47
48void test_filter_crlf__to_odb(void)
49{
50 git_filter_list *fl;
51 git_filter *crlf;
c25aa7cd
PP
52 git_buf out = GIT_BUF_INIT;
53 const char *in;
54 size_t in_len;
40cb40fa 55
5269008c
RB
56 cl_git_pass(git_filter_list_new(
57 &fl, g_repo, GIT_FILTER_TO_ODB, 0));
40cb40fa
RB
58
59 crlf = git_filter_lookup(GIT_FILTER_CRLF);
60 cl_assert(crlf != NULL);
61
62 cl_git_pass(git_filter_list_push(fl, crlf, NULL));
63
c25aa7cd
PP
64 in = "Some text\r\nRight here\r\n";
65 in_len = strlen(in);
40cb40fa 66
c25aa7cd 67 cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
40cb40fa
RB
68
69 cl_assert_equal_s("Some text\nRight here\n", out.ptr);
70
71 git_filter_list_free(fl);
ac3d33df 72 git_buf_dispose(&out);
40cb40fa 73}
855c66de
ET
74
75void test_filter_crlf__with_safecrlf(void)
76{
77 git_filter_list *fl;
78 git_filter *crlf;
c25aa7cd
PP
79 git_buf out = GIT_BUF_INIT;
80 const char *in;
81 size_t in_len;
855c66de
ET
82
83 cl_repo_set_bool(g_repo, "core.safecrlf", true);
84
5269008c
RB
85 cl_git_pass(git_filter_list_new(
86 &fl, g_repo, GIT_FILTER_TO_ODB, 0));
855c66de
ET
87
88 crlf = git_filter_lookup(GIT_FILTER_CRLF);
89 cl_assert(crlf != NULL);
90
91 cl_git_pass(git_filter_list_push(fl, crlf, NULL));
92
93 /* Normalized \r\n succeeds with safecrlf */
c25aa7cd
PP
94 in = "Normal\r\nCRLF\r\nline-endings.\r\n";
95 in_len = strlen(in);
855c66de 96
c25aa7cd 97 cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
855c66de
ET
98 cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr);
99
100 /* Mix of line endings fails with safecrlf */
c25aa7cd
PP
101 in = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
102 in_len = strlen(in);
855c66de 103
c25aa7cd 104 cl_git_fail(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
ac3d33df 105 cl_assert_equal_i(git_error_last()->klass, GIT_ERROR_FILTER);
855c66de 106
ac3d33df 107 /* Normalized \n fails for autocrlf=true when safecrlf=true */
c25aa7cd
PP
108 in = "Normal\nLF\nonly\nline-endings.\n";
109 in_len = strlen(in);
855c66de 110
c25aa7cd 111 cl_git_fail(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
ac3d33df
JK
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 */
c25aa7cd
PP
115 in = "Normal\nCR only\rand some more\nline-endings.\n";
116 in_len = strlen(in);
ac3d33df 117
c25aa7cd 118 cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
ac3d33df 119 cl_assert_equal_s("Normal\nCR only\rand some more\nline-endings.\n", out.ptr);
855c66de
ET
120
121 git_filter_list_free(fl);
ac3d33df 122 git_buf_dispose(&out);
855c66de
ET
123}
124
5269008c
RB
125void test_filter_crlf__with_safecrlf_and_unsafe_allowed(void)
126{
127 git_filter_list *fl;
128 git_filter *crlf;
c25aa7cd
PP
129 git_buf out = GIT_BUF_INIT;
130 const char *in;
131 size_t in_len;
5269008c
RB
132
133 cl_repo_set_bool(g_repo, "core.safecrlf", true);
134
135 cl_git_pass(git_filter_list_new(
795eaccd 136 &fl, g_repo, GIT_FILTER_TO_ODB, GIT_FILTER_ALLOW_UNSAFE));
5269008c
RB
137
138 crlf = git_filter_lookup(GIT_FILTER_CRLF);
139 cl_assert(crlf != NULL);
140
141 cl_git_pass(git_filter_list_push(fl, crlf, NULL));
142
143 /* Normalized \r\n succeeds with safecrlf */
c25aa7cd
PP
144 in = "Normal\r\nCRLF\r\nline-endings.\r\n";
145 in_len = strlen(in);
5269008c 146
c25aa7cd 147 cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
5269008c
RB
148 cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr);
149
150 /* Mix of line endings fails with safecrlf, but allowed to pass */
c25aa7cd
PP
151 in = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
152 in_len = strlen(in);
5269008c 153
c25aa7cd 154 cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
5269008c
RB
155 /* TODO: check for warning */
156 cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out.ptr);
157
158 /* Normalized \n fails with safecrlf, but allowed to pass */
c25aa7cd
PP
159 in = "Normal\nLF\nonly\nline-endings.\n";
160 in_len = strlen(in);
5269008c 161
c25aa7cd 162 cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
5269008c
RB
163 /* TODO: check for warning */
164 cl_assert_equal_s("Normal\nLF\nonly\nline-endings.\n", out.ptr);
165
166 git_filter_list_free(fl);
ac3d33df 167 git_buf_dispose(&out);
5269008c
RB
168}
169
855c66de
ET
170void test_filter_crlf__no_safecrlf(void)
171{
172 git_filter_list *fl;
173 git_filter *crlf;
c25aa7cd
PP
174 git_buf out = GIT_BUF_INIT;
175 const char *in;
176 size_t in_len;
855c66de 177
5269008c
RB
178 cl_git_pass(git_filter_list_new(
179 &fl, g_repo, GIT_FILTER_TO_ODB, 0));
855c66de
ET
180
181 crlf = git_filter_lookup(GIT_FILTER_CRLF);
182 cl_assert(crlf != NULL);
183
184 cl_git_pass(git_filter_list_push(fl, crlf, NULL));
185
186 /* Normalized \r\n succeeds with safecrlf */
c25aa7cd
PP
187 in = "Normal\r\nCRLF\r\nline-endings.\r\n";
188 in_len = strlen(in);
855c66de 189
c25aa7cd 190 cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
855c66de
ET
191 cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr);
192
193 /* Mix of line endings fails with safecrlf */
c25aa7cd
PP
194 in = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
195 in_len = strlen(in);
855c66de 196
c25aa7cd 197 cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
855c66de
ET
198 cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out.ptr);
199
200 /* Normalized \n fails with safecrlf */
c25aa7cd
PP
201 in = "Normal\nLF\nonly\nline-endings.\n";
202 in_len = strlen(in);
855c66de 203
c25aa7cd 204 cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
855c66de
ET
205 cl_assert_equal_s("Normal\nLF\nonly\nline-endings.\n", out.ptr);
206
207 git_filter_list_free(fl);
ac3d33df 208 git_buf_dispose(&out);
855c66de
ET
209}
210
49837fd4
ET
211void test_filter_crlf__safecrlf_warn(void)
212{
213 git_filter_list *fl;
214 git_filter *crlf;
c25aa7cd
PP
215 git_buf out = GIT_BUF_INIT;
216 const char *in;
217 size_t in_len;
49837fd4
ET
218
219 cl_repo_set_string(g_repo, "core.safecrlf", "warn");
220
221 cl_git_pass(git_filter_list_new(
222 &fl, g_repo, GIT_FILTER_TO_ODB, 0));
223
224 crlf = git_filter_lookup(GIT_FILTER_CRLF);
225 cl_assert(crlf != NULL);
226
227 cl_git_pass(git_filter_list_push(fl, crlf, NULL));
228
229 /* Normalized \r\n succeeds with safecrlf=warn */
c25aa7cd
PP
230 in = "Normal\r\nCRLF\r\nline-endings.\r\n";
231 in_len = strlen(in);
49837fd4 232
c25aa7cd 233 cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
49837fd4
ET
234 cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr);
235
236 /* Mix of line endings succeeds with safecrlf=warn */
c25aa7cd
PP
237 in = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
238 in_len = strlen(in);
49837fd4 239
c25aa7cd 240 cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
49837fd4
ET
241 /* TODO: check for warning */
242 cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out.ptr);
243
244 /* Normalized \n is reversible, so does not fail with safecrlf=warn */
c25aa7cd
PP
245 in = "Normal\nLF\nonly\nline-endings.\n";
246 in_len = strlen(in);
49837fd4 247
c25aa7cd
PP
248 cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
249 cl_assert_equal_s(in, out.ptr);
49837fd4
ET
250
251 git_filter_list_free(fl);
ac3d33df 252 git_buf_dispose(&out);
49837fd4 253}