]> git.proxmox.com Git - libgit2.git/blob - tests/checkout/crlf.c
crlf: include utf8 resources in master branch
[libgit2.git] / tests / checkout / crlf.c
1 #include "clar_libgit2.h"
2 #include "checkout_helpers.h"
3 #include "../filter/crlf.h"
4
5 #include "git2/checkout.h"
6 #include "repository.h"
7 #include "index.h"
8 #include "posix.h"
9
10 static git_repository *g_repo;
11
12 void test_checkout_crlf__initialize(void)
13 {
14 g_repo = cl_git_sandbox_init("crlf");
15 }
16
17 void test_checkout_crlf__cleanup(void)
18 {
19 cl_git_sandbox_cleanup();
20 }
21
22 void test_checkout_crlf__detect_crlf_autocrlf_false(void)
23 {
24 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
25 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
26
27 cl_repo_set_bool(g_repo, "core.autocrlf", false);
28
29 git_checkout_head(g_repo, &opts);
30
31 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
32 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
33 }
34
35 void test_checkout_crlf__autocrlf_false_index_size_is_unfiltered_size(void)
36 {
37 git_index *index;
38 const git_index_entry *entry;
39 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
40 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
41
42 cl_repo_set_bool(g_repo, "core.autocrlf", false);
43
44 git_repository_index(&index, g_repo);
45 tick_index(index);
46
47 git_checkout_head(g_repo, &opts);
48
49 cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);
50 cl_assert(entry->file_size == strlen(ALL_LF_TEXT_RAW));
51
52 cl_assert((entry = git_index_get_bypath(index, "all-crlf", 0)) != NULL);
53 cl_assert(entry->file_size == strlen(ALL_CRLF_TEXT_RAW));
54
55 git_index_free(index);
56 }
57
58 void test_checkout_crlf__detect_crlf_autocrlf_true(void)
59 {
60 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
61 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
62
63 cl_repo_set_bool(g_repo, "core.autocrlf", true);
64
65 git_checkout_head(g_repo, &opts);
66
67 if (GIT_EOL_NATIVE == GIT_EOL_LF)
68 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
69 else
70 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
71
72 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
73 }
74
75 void test_checkout_crlf__more_lf_autocrlf_true(void)
76 {
77 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
78 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
79
80 cl_repo_set_bool(g_repo, "core.autocrlf", true);
81
82 git_checkout_head(g_repo, &opts);
83
84 check_file_contents("./crlf/more-lf", MORE_LF_TEXT_RAW);
85 }
86
87 void test_checkout_crlf__more_crlf_autocrlf_true(void)
88 {
89 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
90 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
91
92 cl_repo_set_bool(g_repo, "core.autocrlf", true);
93
94 git_checkout_head(g_repo, &opts);
95
96 check_file_contents("./crlf/more-crlf", MORE_CRLF_TEXT_RAW);
97 }
98
99 void test_checkout_crlf__all_crlf_autocrlf_true(void)
100 {
101 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
102 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
103
104 cl_repo_set_bool(g_repo, "core.autocrlf", true);
105
106 git_checkout_head(g_repo, &opts);
107
108 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
109 }
110
111 void test_checkout_crlf__detect_crlf_autocrlf_true_utf8(void)
112 {
113 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
114 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
115
116 cl_repo_set_bool(g_repo, "core.autocrlf", true);
117
118 git_repository_set_head(g_repo, "refs/heads/master");
119 git_checkout_head(g_repo, &opts);
120
121 if (GIT_EOL_NATIVE == GIT_EOL_LF)
122 {
123 check_file_contents("./crlf/few-utf8-chars-lf", FEW_UTF8_LF_RAW);
124 check_file_contents("./crlf/many-utf8-chars-lf", MANY_UTF8_LF_RAW);
125 }
126 else
127 {
128 check_file_contents("./crlf/few-utf8-chars-lf", FEW_UTF8_CRLF_RAW);
129 check_file_contents("./crlf/many-utf8-chars-lf", MANY_UTF8_CRLF_RAW);
130 }
131
132 check_file_contents("./crlf/few-utf8-chars-crlf", FEW_UTF8_CRLF_RAW);
133 check_file_contents("./crlf/many-utf8-chars-crlf", MANY_UTF8_CRLF_RAW);
134 }
135
136 void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void)
137 {
138 git_index *index;
139 const git_index_entry *entry;
140 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
141 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
142
143 cl_repo_set_bool(g_repo, "core.autocrlf", true);
144
145 git_repository_index(&index, g_repo);
146 tick_index(index);
147
148 git_checkout_head(g_repo, &opts);
149
150 cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);
151
152 if (GIT_EOL_NATIVE == GIT_EOL_LF)
153 cl_assert_equal_sz(strlen(ALL_LF_TEXT_RAW), entry->file_size);
154 else
155 cl_assert_equal_sz(strlen(ALL_LF_TEXT_AS_CRLF), entry->file_size);
156
157 cl_assert((entry = git_index_get_bypath(index, "all-crlf", 0)) != NULL);
158 cl_assert_equal_sz(strlen(ALL_CRLF_TEXT_RAW), entry->file_size);
159
160 git_index_free(index);
161 }
162
163 void test_checkout_crlf__with_ident(void)
164 {
165 git_index *index;
166 git_blob *blob;
167 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
168 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
169
170 cl_git_mkfile("crlf/.gitattributes",
171 "*.txt text\n*.bin binary\n"
172 "*.crlf text eol=crlf\n"
173 "*.lf text eol=lf\n"
174 "*.ident text ident\n"
175 "*.identcrlf ident text eol=crlf\n"
176 "*.identlf ident text eol=lf\n");
177
178 cl_repo_set_bool(g_repo, "core.autocrlf", true);
179
180 /* add files with $Id$ */
181
182 cl_git_mkfile("crlf/lf.ident", ALL_LF_TEXT_RAW "\n$Id: initial content$\n");
183 cl_git_mkfile("crlf/crlf.ident", ALL_CRLF_TEXT_RAW "\r\n$Id$\r\n\r\n");
184 cl_git_mkfile("crlf/more1.identlf", "$Id$\n" MORE_LF_TEXT_RAW);
185 cl_git_mkfile("crlf/more2.identcrlf", "\r\n$Id: \f$\r\n" MORE_CRLF_TEXT_RAW);
186
187 cl_git_pass(git_repository_index(&index, g_repo));
188 cl_git_pass(git_index_add_bypath(index, "lf.ident"));
189 cl_git_pass(git_index_add_bypath(index, "crlf.ident"));
190 cl_git_pass(git_index_add_bypath(index, "more1.identlf"));
191 cl_git_pass(git_index_add_bypath(index, "more2.identcrlf"));
192 cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "Some ident files\n");
193
194 git_checkout_head(g_repo, &opts);
195
196 /* check that blobs have $Id$ */
197
198 cl_git_pass(git_blob_lookup(&blob, g_repo,
199 & git_index_get_bypath(index, "lf.ident", 0)->id));
200 cl_assert_equal_s(
201 ALL_LF_TEXT_RAW "\n$Id$\n", git_blob_rawcontent(blob));
202 git_blob_free(blob);
203
204 cl_git_pass(git_blob_lookup(&blob, g_repo,
205 & git_index_get_bypath(index, "more2.identcrlf", 0)->id));
206 cl_assert_equal_s(
207 "\n$Id$\n" MORE_CRLF_TEXT_AS_LF, git_blob_rawcontent(blob));
208 git_blob_free(blob);
209
210 /* check that filesystem is initially untouched - matching core Git */
211
212 cl_assert_equal_file(
213 ALL_LF_TEXT_RAW "\n$Id: initial content$\n", 0, "crlf/lf.ident");
214
215 /* check that forced checkout rewrites correctly */
216
217 p_unlink("crlf/lf.ident");
218 p_unlink("crlf/crlf.ident");
219 p_unlink("crlf/more1.identlf");
220 p_unlink("crlf/more2.identcrlf");
221
222 git_checkout_head(g_repo, &opts);
223
224 if (GIT_EOL_NATIVE == GIT_EOL_LF) {
225 cl_assert_equal_file(
226 ALL_LF_TEXT_RAW
227 "\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467 $\n",
228 0, "crlf/lf.ident");
229 cl_assert_equal_file(
230 ALL_CRLF_TEXT_AS_LF
231 "\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857 $\n\n",
232 0, "crlf/crlf.ident");
233 } else {
234 cl_assert_equal_file(
235 ALL_LF_TEXT_AS_CRLF
236 "\r\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467 $\r\n",
237 0, "crlf/lf.ident");
238 cl_assert_equal_file(
239 ALL_CRLF_TEXT_RAW
240 "\r\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857 $\r\n\r\n",
241 0, "crlf/crlf.ident");
242 }
243
244 cl_assert_equal_file(
245 "$Id: f7830382dac1f1583422be5530fdfbd26289431b $\n"
246 MORE_LF_TEXT_AS_LF, 0, "crlf/more1.identlf");
247
248 cl_assert_equal_file(
249 "\r\n$Id: 74677a68413012ce8d7e7cfc3f12603df3a3eac4 $\r\n"
250 MORE_CRLF_TEXT_AS_CRLF, 0, "crlf/more2.identcrlf");
251
252 git_index_free(index);
253 }
254
255 void test_checkout_crlf__autocrlf_false_no_attrs(void)
256 {
257 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
258 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
259
260 cl_repo_set_bool(g_repo, "core.autocrlf", false);
261
262 git_checkout_head(g_repo, &opts);
263
264 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
265 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
266 }
267
268 void test_checkout_crlf__autocrlf_true_no_attrs(void)
269 {
270 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
271 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
272
273 cl_repo_set_bool(g_repo, "core.autocrlf", true);
274
275 git_checkout_head(g_repo, &opts);
276
277 if (GIT_EOL_NATIVE == GIT_EOL_CRLF) {
278 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
279 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
280 } else {
281 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
282 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
283 }
284 }
285
286 void test_checkout_crlf__autocrlf_input_no_attrs(void)
287 {
288 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
289 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
290
291 cl_repo_set_string(g_repo, "core.autocrlf", "input");
292
293 git_checkout_head(g_repo, &opts);
294
295 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
296 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
297 }
298
299 void test_checkout_crlf__autocrlf_false_text_auto_attr(void)
300 {
301 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
302 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
303
304 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
305
306 cl_repo_set_bool(g_repo, "core.autocrlf", false);
307
308 git_checkout_head(g_repo, &opts);
309
310 if (GIT_EOL_NATIVE == GIT_EOL_CRLF) {
311 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
312 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
313 } else {
314 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
315 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
316 }
317 }
318
319 void test_checkout_crlf__autocrlf_true_text_auto_attr(void)
320 {
321 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
322 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
323
324 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
325
326 cl_repo_set_bool(g_repo, "core.autocrlf", true);
327
328 git_checkout_head(g_repo, &opts);
329
330 if (GIT_EOL_NATIVE == GIT_EOL_CRLF) {
331 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
332 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
333 } else {
334 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
335 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
336 }
337 }
338
339 void test_checkout_crlf__autocrlf_input_text_auto_attr(void)
340 {
341 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
342 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
343
344 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
345
346 cl_repo_set_string(g_repo, "core.autocrlf", "input");
347
348 git_checkout_head(g_repo, &opts);
349
350 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
351 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
352 }
353
354 void test_checkout_crlf__can_write_empty_file(void)
355 {
356 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
357 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
358
359 cl_repo_set_bool(g_repo, "core.autocrlf", true);
360
361 git_repository_set_head(g_repo, "refs/heads/empty-files");
362 git_checkout_head(g_repo, &opts);
363
364 check_file_contents("./crlf/test1.txt", "");
365
366 if (GIT_EOL_NATIVE == GIT_EOL_LF)
367 check_file_contents("./crlf/test2.txt", "test2.txt's content\n");
368 else
369 check_file_contents("./crlf/test2.txt", "test2.txt's content\r\n");
370
371 check_file_contents("./crlf/test3.txt", "");
372 }