]> git.proxmox.com Git - libgit2.git/blob - tests/filter/crlf.c
Merge pull request #2359 from e45lee/chmod-fix
[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 #ifdef GIT_WIN32
42 cl_assert_equal_s("Some text\r\nRight here\r\n", out.ptr);
43 #else
44 cl_assert_equal_s("Some text\nRight here\n", out.ptr);
45 #endif
46
47 git_filter_list_free(fl);
48 git_buf_free(&out);
49 }
50
51 void test_filter_crlf__to_odb(void)
52 {
53 git_filter_list *fl;
54 git_filter *crlf;
55 git_buf in = { 0 }, out = { 0 };
56
57 cl_git_pass(git_filter_list_new(
58 &fl, g_repo, GIT_FILTER_TO_ODB, 0));
59
60 crlf = git_filter_lookup(GIT_FILTER_CRLF);
61 cl_assert(crlf != NULL);
62
63 cl_git_pass(git_filter_list_push(fl, crlf, NULL));
64
65 in.ptr = "Some text\r\nRight here\r\n";
66 in.size = strlen(in.ptr);
67
68 cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
69
70 cl_assert_equal_s("Some text\nRight here\n", out.ptr);
71
72 git_filter_list_free(fl);
73 git_buf_free(&out);
74 }
75
76 void test_filter_crlf__with_safecrlf(void)
77 {
78 git_filter_list *fl;
79 git_filter *crlf;
80 git_buf in = {0}, out = GIT_BUF_INIT;
81
82 cl_repo_set_bool(g_repo, "core.safecrlf", true);
83
84 cl_git_pass(git_filter_list_new(
85 &fl, g_repo, GIT_FILTER_TO_ODB, 0));
86
87 crlf = git_filter_lookup(GIT_FILTER_CRLF);
88 cl_assert(crlf != NULL);
89
90 cl_git_pass(git_filter_list_push(fl, crlf, NULL));
91
92 /* Normalized \r\n succeeds with safecrlf */
93 in.ptr = "Normal\r\nCRLF\r\nline-endings.\r\n";
94 in.size = strlen(in.ptr);
95
96 cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
97 cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr);
98
99 /* Mix of line endings fails with safecrlf */
100 in.ptr = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
101 in.size = strlen(in.ptr);
102
103 cl_git_fail(git_filter_list_apply_to_data(&out, fl, &in));
104 cl_assert_equal_i(giterr_last()->klass, GITERR_FILTER);
105
106 /* Normalized \n is reversible, so does not fail with safecrlf */
107 in.ptr = "Normal\nLF\nonly\nline-endings.\n";
108 in.size = strlen(in.ptr);
109
110 cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
111 cl_assert_equal_s(in.ptr, out.ptr);
112
113 git_filter_list_free(fl);
114 git_buf_free(&out);
115 }
116
117 void test_filter_crlf__with_safecrlf_and_unsafe_allowed(void)
118 {
119 git_filter_list *fl;
120 git_filter *crlf;
121 git_buf in = {0}, out = GIT_BUF_INIT;
122
123 cl_repo_set_bool(g_repo, "core.safecrlf", true);
124
125 cl_git_pass(git_filter_list_new(
126 &fl, g_repo, GIT_FILTER_TO_ODB, GIT_FILTER_OPT_ALLOW_UNSAFE));
127
128 crlf = git_filter_lookup(GIT_FILTER_CRLF);
129 cl_assert(crlf != NULL);
130
131 cl_git_pass(git_filter_list_push(fl, crlf, NULL));
132
133 /* Normalized \r\n succeeds with safecrlf */
134 in.ptr = "Normal\r\nCRLF\r\nline-endings.\r\n";
135 in.size = strlen(in.ptr);
136
137 cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
138 cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr);
139
140 /* Mix of line endings fails with safecrlf, but allowed to pass */
141 in.ptr = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
142 in.size = strlen(in.ptr);
143
144 cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
145 /* TODO: check for warning */
146 cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out.ptr);
147
148 /* Normalized \n fails with safecrlf, but allowed to pass */
149 in.ptr = "Normal\nLF\nonly\nline-endings.\n";
150 in.size = strlen(in.ptr);
151
152 cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
153 /* TODO: check for warning */
154 cl_assert_equal_s("Normal\nLF\nonly\nline-endings.\n", out.ptr);
155
156 git_filter_list_free(fl);
157 git_buf_free(&out);
158 }
159
160 void test_filter_crlf__no_safecrlf(void)
161 {
162 git_filter_list *fl;
163 git_filter *crlf;
164 git_buf in = {0}, out = GIT_BUF_INIT;
165
166 cl_git_pass(git_filter_list_new(
167 &fl, g_repo, GIT_FILTER_TO_ODB, 0));
168
169 crlf = git_filter_lookup(GIT_FILTER_CRLF);
170 cl_assert(crlf != NULL);
171
172 cl_git_pass(git_filter_list_push(fl, crlf, NULL));
173
174 /* Normalized \r\n succeeds with safecrlf */
175 in.ptr = "Normal\r\nCRLF\r\nline-endings.\r\n";
176 in.size = strlen(in.ptr);
177
178 cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
179 cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr);
180
181 /* Mix of line endings fails with safecrlf */
182 in.ptr = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
183 in.size = strlen(in.ptr);
184
185 cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
186 cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out.ptr);
187
188 /* Normalized \n fails with safecrlf */
189 in.ptr = "Normal\nLF\nonly\nline-endings.\n";
190 in.size = strlen(in.ptr);
191
192 cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
193 cl_assert_equal_s("Normal\nLF\nonly\nline-endings.\n", out.ptr);
194
195 git_filter_list_free(fl);
196 git_buf_free(&out);
197 }
198