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