]> git.proxmox.com Git - libgit2.git/blame - tests/checkout/crlf.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / checkout / crlf.c
CommitLineData
6e959708
ET
1#include "clar_libgit2.h"
2#include "checkout_helpers.h"
0cf77103 3#include "../filter/crlf.h"
22a2d3d5 4#include "futils.h"
6e959708
ET
5
6#include "git2/checkout.h"
7#include "repository.h"
c4e6ab5f 8#include "index.h"
37f9e409 9#include "posix.h"
6e959708 10
6e959708
ET
11static git_repository *g_repo;
12
3d92b9ab 13static const char *systype;
e579e0f7 14static git_str expected_fixture = GIT_STR_INIT;
3d92b9ab 15
e579e0f7 16static int unlink_file(void *payload, git_str *path)
ac3d33df
JK
17{
18 char *fn;
19
e579e0f7 20 cl_assert(fn = git_fs_path_basename(path->ptr));
ac3d33df
JK
21
22 GIT_UNUSED(payload);
23
24 if (strcmp(fn, ".git"))
25 cl_must_pass(p_unlink(path->ptr));
26
27 git__free(fn);
28 return 0;
29}
30
6e959708
ET
31void test_checkout_crlf__initialize(void)
32{
e579e0f7 33 git_str reponame = GIT_STR_INIT;
ac3d33df 34
6e959708 35 g_repo = cl_git_sandbox_init("crlf");
3d92b9ab 36
ac3d33df
JK
37 /*
38 * remove the contents of the working directory so that we can
39 * check out over it.
40 */
e579e0f7
MB
41 cl_git_pass(git_str_puts(&reponame, "crlf"));
42 cl_git_pass(git_fs_path_direach(&reponame, 0, unlink_file, NULL));
ac3d33df 43
3d92b9ab
ET
44 if (GIT_EOL_NATIVE == GIT_EOL_CRLF)
45 systype = "windows";
46 else
47 systype = "posix";
ac3d33df 48
e579e0f7 49 git_str_dispose(&reponame);
6e959708
ET
50}
51
52void test_checkout_crlf__cleanup(void)
53{
54 cl_git_sandbox_cleanup();
3d92b9ab
ET
55
56 if (expected_fixture.size) {
57 cl_fixture_cleanup(expected_fixture.ptr);
e579e0f7 58 git_str_dispose(&expected_fixture);
3d92b9ab
ET
59 }
60}
61
62struct compare_data
63{
64 const char *dirname;
65 const char *autocrlf;
66 const char *attrs;
67};
68
e579e0f7 69static int compare_file(void *payload, git_str *actual_path)
3d92b9ab 70{
e579e0f7
MB
71 git_str expected_path = GIT_STR_INIT;
72 git_str actual_contents = GIT_STR_INIT;
73 git_str expected_contents = GIT_STR_INIT;
3d92b9ab
ET
74 struct compare_data *cd = payload;
75 bool failed = true;
afd8a94e
CMN
76 int cmp_git, cmp_gitattributes;
77 char *basename;
3d92b9ab 78
e579e0f7 79 basename = git_fs_path_basename(actual_path->ptr);
afd8a94e
CMN
80 cmp_git = strcmp(basename, ".git");
81 cmp_gitattributes = strcmp(basename, ".gitattributes");
82
83 if (cmp_git == 0 || cmp_gitattributes == 0) {
3d92b9ab
ET
84 failed = false;
85 goto done;
86 }
87
e579e0f7 88 cl_git_pass(git_str_joinpath(&expected_path, cd->dirname, basename));
3d92b9ab 89
e579e0f7
MB
90 if (!git_fs_path_isfile(expected_path.ptr) ||
91 !git_fs_path_isfile(actual_path->ptr))
3d92b9ab
ET
92 goto done;
93
94 if (git_futils_readbuffer(&actual_contents, actual_path->ptr) < 0 ||
95 git_futils_readbuffer(&expected_contents, expected_path.ptr) < 0)
96 goto done;
97
98 if (actual_contents.size != expected_contents.size)
99 goto done;
100
101 if (memcmp(actual_contents.ptr, expected_contents.ptr, expected_contents.size) != 0)
102 goto done;
103
104 failed = false;
105
106done:
107 if (failed) {
e579e0f7
MB
108 git_str details = GIT_STR_INIT;
109 git_str_printf(&details, "filename=%s, system=%s, autocrlf=%s, attrs={%s}",
110 git_fs_path_basename(actual_path->ptr), systype, cd->autocrlf, cd->attrs);
22a2d3d5 111 clar__fail(__FILE__, __func__, __LINE__,
3d92b9ab 112 "checked out contents did not match expected", details.ptr, 0);
e579e0f7 113 git_str_dispose(&details);
3d92b9ab
ET
114 }
115
afd8a94e 116 git__free(basename);
e579e0f7
MB
117 git_str_dispose(&expected_contents);
118 git_str_dispose(&actual_contents);
119 git_str_dispose(&expected_path);
3d92b9ab
ET
120
121 return 0;
122}
123
124static void test_checkout(const char *autocrlf, const char *attrs)
125{
e579e0f7
MB
126 git_str attrbuf = GIT_STR_INIT;
127 git_str expected_dirname = GIT_STR_INIT;
128 git_str systype_and_direction = GIT_STR_INIT;
129 git_str sandboxname = GIT_STR_INIT;
130 git_str reponame = GIT_STR_INIT;
3d92b9ab
ET
131 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
132 struct compare_data compare_data = { NULL, autocrlf, attrs };
133 const char *c;
134
e579e0f7 135 cl_git_pass(git_str_puts(&reponame, "crlf"));
3d92b9ab 136
e579e0f7
MB
137 cl_git_pass(git_str_puts(&systype_and_direction, systype));
138 cl_git_pass(git_str_puts(&systype_and_direction, "_to_workdir"));
ac3d33df 139
e579e0f7
MB
140 cl_git_pass(git_str_puts(&sandboxname, "autocrlf_"));
141 cl_git_pass(git_str_puts(&sandboxname, autocrlf));
3d92b9ab
ET
142
143 if (*attrs) {
e579e0f7 144 cl_git_pass(git_str_puts(&sandboxname, ","));
3d92b9ab
ET
145
146 for (c = attrs; *c; c++) {
147 if (*c == ' ')
e579e0f7 148 cl_git_pass(git_str_putc(&sandboxname, ','));
3d92b9ab 149 else if (*c == '=')
e579e0f7 150 cl_git_pass(git_str_putc(&sandboxname, '_'));
3d92b9ab 151 else
e579e0f7 152 cl_git_pass(git_str_putc(&sandboxname, *c));
3d92b9ab
ET
153 }
154
e579e0f7 155 cl_git_pass(git_str_printf(&attrbuf, "* %s\n", attrs));
3d92b9ab
ET
156 cl_git_mkfile("crlf/.gitattributes", attrbuf.ptr);
157 }
158
159 cl_repo_set_string(g_repo, "core.autocrlf", autocrlf);
160
e579e0f7
MB
161 cl_git_pass(git_str_joinpath(&expected_dirname, systype_and_direction.ptr, sandboxname.ptr));
162 cl_git_pass(git_str_joinpath(&expected_fixture, "crlf_data", expected_dirname.ptr));
3d92b9ab
ET
163 cl_fixture_sandbox(expected_fixture.ptr);
164
165 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
ac3d33df 166 cl_git_pass(git_checkout_head(g_repo, &opts));
3d92b9ab
ET
167
168 compare_data.dirname = sandboxname.ptr;
e579e0f7 169 cl_git_pass(git_fs_path_direach(&reponame, 0, compare_file, &compare_data));
3d92b9ab
ET
170
171 cl_fixture_cleanup(expected_fixture.ptr);
e579e0f7
MB
172 git_str_dispose(&expected_fixture);
173
174 git_str_dispose(&attrbuf);
175 git_str_dispose(&expected_fixture);
176 git_str_dispose(&expected_dirname);
177 git_str_dispose(&sandboxname);
178 git_str_dispose(&systype_and_direction);
179 git_str_dispose(&reponame);
3d92b9ab
ET
180}
181
182static void empty_workdir(const char *name)
183{
184 git_vector contents = GIT_VECTOR_INIT;
ac3d33df
JK
185 char *basename;
186 int cmp;
3d92b9ab
ET
187 size_t i;
188 const char *fn;
189
e579e0f7 190 cl_git_pass(git_fs_path_dirload(&contents, name, 0, 0));
3d92b9ab 191 git_vector_foreach(&contents, i, fn) {
e579e0f7 192 cl_assert(basename = git_fs_path_basename(fn));
ac3d33df 193 cmp = strncasecmp(basename, ".git", 4);
afd8a94e
CMN
194
195 git__free(basename);
196
ac3d33df
JK
197 if (cmp)
198 cl_git_pass(p_unlink(fn));
3d92b9ab
ET
199 }
200 git_vector_free_deep(&contents);
201}
202
203void test_checkout_crlf__matches_core_git(void)
204{
205 const char *autocrlf[] = { "true", "false", "input", NULL };
206 const char *attrs[] = { "", "-crlf", "-text", "eol=crlf", "eol=lf",
207 "text", "text eol=crlf", "text eol=lf",
ac3d33df 208 "text=auto", "text=auto eol=crlf", "text=auto eol=lf",
3d92b9ab
ET
209 NULL };
210 const char **a, **b;
211
212 for (a = autocrlf; *a; a++) {
213 for (b = attrs; *b; b++) {
214 empty_workdir("crlf");
215 test_checkout(*a, *b);
216 }
217 }
6e959708
ET
218}
219
6affd71f
BS
220void test_checkout_crlf__detect_crlf_autocrlf_false(void)
221{
222 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
96b82b11 223 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
6affd71f
BS
224
225 cl_repo_set_bool(g_repo, "core.autocrlf", false);
226
227 git_checkout_head(g_repo, &opts);
228
229 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
230 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
231}
232
6e959708
ET
233void test_checkout_crlf__autocrlf_false_index_size_is_unfiltered_size(void)
234{
235 git_index *index;
236 const git_index_entry *entry;
6affd71f 237 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
96b82b11 238 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
6e959708 239
1323c6d1 240 cl_repo_set_bool(g_repo, "core.autocrlf", false);
6e959708 241
e579e0f7 242 cl_git_pass(git_repository_index(&index, g_repo));
c4e6ab5f
CMN
243 tick_index(index);
244
e579e0f7 245 cl_git_pass(git_checkout_head(g_repo, &opts));
6e959708
ET
246
247 cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);
248 cl_assert(entry->file_size == strlen(ALL_LF_TEXT_RAW));
249
b8acb775
SS
250 cl_assert((entry = git_index_get_bypath(index, "all-crlf", 0)) != NULL);
251 cl_assert(entry->file_size == strlen(ALL_CRLF_TEXT_RAW));
252
6e959708
ET
253 git_index_free(index);
254}
255
256void test_checkout_crlf__detect_crlf_autocrlf_true(void)
257{
6affd71f 258 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
96b82b11 259 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
6e959708 260
1323c6d1 261 cl_repo_set_bool(g_repo, "core.autocrlf", true);
6e959708
ET
262
263 git_checkout_head(g_repo, &opts);
264
1e46d545 265 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
050ab995 266 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
6e959708
ET
267}
268
b4c6a9da
L
269void test_checkout_crlf__detect_crlf_autocrlf_true_utf8(void)
270{
271 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
96b82b11 272 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
b4c6a9da
L
273
274 cl_repo_set_bool(g_repo, "core.autocrlf", true);
275
bd5e59ee 276 git_repository_set_head(g_repo, "refs/heads/master");
b4c6a9da
L
277 git_checkout_head(g_repo, &opts);
278
1e46d545
ET
279 check_file_contents("./crlf/few-utf8-chars-lf", FEW_UTF8_CRLF_RAW);
280 check_file_contents("./crlf/many-utf8-chars-lf", MANY_UTF8_CRLF_RAW);
b4c6a9da 281
bd5e59ee
ET
282 check_file_contents("./crlf/few-utf8-chars-crlf", FEW_UTF8_CRLF_RAW);
283 check_file_contents("./crlf/many-utf8-chars-crlf", MANY_UTF8_CRLF_RAW);
b4c6a9da
L
284}
285
6e959708
ET
286void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void)
287{
288 git_index *index;
289 const git_index_entry *entry;
6affd71f 290 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
96b82b11 291 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
6e959708 292
1323c6d1 293 cl_repo_set_bool(g_repo, "core.autocrlf", true);
6e959708 294
6e959708 295 git_repository_index(&index, g_repo);
c4e6ab5f
CMN
296 tick_index(index);
297
298 git_checkout_head(g_repo, &opts);
6e959708
ET
299
300 cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);
050ab995 301
1e46d545 302 cl_assert_equal_sz(strlen(ALL_LF_TEXT_AS_CRLF), entry->file_size);
6e959708 303
b8acb775 304 cl_assert((entry = git_index_get_bypath(index, "all-crlf", 0)) != NULL);
050ab995 305 cl_assert_equal_sz(strlen(ALL_CRLF_TEXT_RAW), entry->file_size);
b8acb775 306
6e959708
ET
307 git_index_free(index);
308}
37f9e409
RB
309
310void test_checkout_crlf__with_ident(void)
311{
312 git_index *index;
87428c55 313 const git_index_entry *entry;
37f9e409 314 git_blob *blob;
6affd71f 315 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
96b82b11 316 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
37f9e409
RB
317
318 cl_git_mkfile("crlf/.gitattributes",
319 "*.txt text\n*.bin binary\n"
320 "*.crlf text eol=crlf\n"
321 "*.lf text eol=lf\n"
322 "*.ident text ident\n"
323 "*.identcrlf ident text eol=crlf\n"
324 "*.identlf ident text eol=lf\n");
325
326 cl_repo_set_bool(g_repo, "core.autocrlf", true);
327
328 /* add files with $Id$ */
329
330 cl_git_mkfile("crlf/lf.ident", ALL_LF_TEXT_RAW "\n$Id: initial content$\n");
331 cl_git_mkfile("crlf/crlf.ident", ALL_CRLF_TEXT_RAW "\r\n$Id$\r\n\r\n");
eab3746b
RB
332 cl_git_mkfile("crlf/more1.identlf", "$Id$\n" MORE_LF_TEXT_RAW);
333 cl_git_mkfile("crlf/more2.identcrlf", "\r\n$Id: \f$\r\n" MORE_CRLF_TEXT_RAW);
37f9e409
RB
334
335 cl_git_pass(git_repository_index(&index, g_repo));
336 cl_git_pass(git_index_add_bypath(index, "lf.ident"));
337 cl_git_pass(git_index_add_bypath(index, "crlf.ident"));
eab3746b
RB
338 cl_git_pass(git_index_add_bypath(index, "more1.identlf"));
339 cl_git_pass(git_index_add_bypath(index, "more2.identcrlf"));
37f9e409
RB
340 cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "Some ident files\n");
341
342 git_checkout_head(g_repo, &opts);
343
eab3746b 344 /* check that blobs have $Id$ */
37f9e409 345
de999f26
ET
346 cl_assert((entry = git_index_get_bypath(index, "lf.ident", 0)));
347 cl_git_pass(git_blob_lookup(&blob, g_repo, &entry->id));
37f9e409
RB
348 cl_assert_equal_s(
349 ALL_LF_TEXT_RAW "\n$Id$\n", git_blob_rawcontent(blob));
eab3746b 350 git_blob_free(blob);
37f9e409 351
de999f26
ET
352 cl_assert((entry = git_index_get_bypath(index, "more2.identcrlf", 0)));
353 cl_git_pass(git_blob_lookup(&blob, g_repo, &entry->id));
eab3746b
RB
354 cl_assert_equal_s(
355 "\n$Id$\n" MORE_CRLF_TEXT_AS_LF, git_blob_rawcontent(blob));
37f9e409
RB
356 git_blob_free(blob);
357
358 /* check that filesystem is initially untouched - matching core Git */
359
360 cl_assert_equal_file(
361 ALL_LF_TEXT_RAW "\n$Id: initial content$\n", 0, "crlf/lf.ident");
362
363 /* check that forced checkout rewrites correctly */
364
365 p_unlink("crlf/lf.ident");
eab3746b
RB
366 p_unlink("crlf/crlf.ident");
367 p_unlink("crlf/more1.identlf");
368 p_unlink("crlf/more2.identcrlf");
37f9e409 369
ac3d33df 370 cl_git_pass(git_checkout_head(g_repo, &opts));
37f9e409 371
1e46d545
ET
372 cl_assert_equal_file(
373 ALL_LF_TEXT_AS_CRLF
374 "\r\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467 $\r\n",
375 0, "crlf/lf.ident");
376 cl_assert_equal_file(
377 ALL_CRLF_TEXT_RAW
378 "\r\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857 $\r\n\r\n",
379 0, "crlf/crlf.ident");
eab3746b
RB
380
381 cl_assert_equal_file(
1ecbcd8e 382 "$Id: f7830382dac1f1583422be5530fdfbd26289431b $\n"
eab3746b
RB
383 MORE_LF_TEXT_AS_LF, 0, "crlf/more1.identlf");
384
385 cl_assert_equal_file(
1ecbcd8e 386 "\r\n$Id: 74677a68413012ce8d7e7cfc3f12603df3a3eac4 $\r\n"
eab3746b 387 MORE_CRLF_TEXT_AS_CRLF, 0, "crlf/more2.identcrlf");
37f9e409
RB
388
389 git_index_free(index);
390}
f77127da
ET
391
392void test_checkout_crlf__autocrlf_false_no_attrs(void)
393{
6affd71f 394 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
96b82b11 395 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
f77127da
ET
396
397 cl_repo_set_bool(g_repo, "core.autocrlf", false);
398
ac3d33df 399 cl_git_pass(git_checkout_head(g_repo, &opts));
f77127da
ET
400
401 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
402 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
403}
404
405void test_checkout_crlf__autocrlf_true_no_attrs(void)
406{
6affd71f 407 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
96b82b11 408 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
f77127da
ET
409
410 cl_repo_set_bool(g_repo, "core.autocrlf", true);
411
ac3d33df 412 cl_git_pass(git_checkout_head(g_repo, &opts));
f77127da 413
1e46d545
ET
414 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
415 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
f77127da
ET
416}
417
418void test_checkout_crlf__autocrlf_input_no_attrs(void)
419{
6affd71f 420 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
96b82b11 421 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
f77127da
ET
422
423 cl_repo_set_string(g_repo, "core.autocrlf", "input");
424
ac3d33df 425 cl_git_pass(git_checkout_head(g_repo, &opts));
f77127da
ET
426
427 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
428 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
429}
430
431void test_checkout_crlf__autocrlf_false_text_auto_attr(void)
432{
6affd71f 433 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
96b82b11 434 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
f77127da
ET
435
436 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
437
438 cl_repo_set_bool(g_repo, "core.autocrlf", false);
439
ac3d33df 440 cl_git_pass(git_checkout_head(g_repo, &opts));
f77127da 441
d412165f
ET
442 if (GIT_EOL_NATIVE == GIT_EOL_CRLF) {
443 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
444 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
445 } else {
446 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
447 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
448 }
f77127da
ET
449}
450
451void test_checkout_crlf__autocrlf_true_text_auto_attr(void)
452{
6affd71f 453 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
96b82b11 454 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
f77127da
ET
455
456 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
457
458 cl_repo_set_bool(g_repo, "core.autocrlf", true);
459
ac3d33df 460 cl_git_pass(git_checkout_head(g_repo, &opts));
f77127da 461
1e46d545
ET
462 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
463 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
f77127da
ET
464}
465
466void test_checkout_crlf__autocrlf_input_text_auto_attr(void)
467{
6affd71f 468 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
96b82b11 469 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
f77127da
ET
470
471 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
472
473 cl_repo_set_string(g_repo, "core.autocrlf", "input");
474
ac3d33df 475 cl_git_pass(git_checkout_head(g_repo, &opts));
f77127da
ET
476
477 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
478 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
479}
7c2b9e06
JG
480
481void test_checkout_crlf__can_write_empty_file(void)
482{
483 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
484 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
485
486 cl_repo_set_bool(g_repo, "core.autocrlf", true);
487
ac3d33df
JK
488 cl_git_pass(git_repository_set_head(g_repo, "refs/heads/empty-files"));
489 cl_git_pass(git_checkout_head(g_repo, &opts));
7c2b9e06
JG
490
491 check_file_contents("./crlf/test1.txt", "");
492
1e46d545 493 check_file_contents("./crlf/test2.txt", "test2.txt's content\r\n");
7c2b9e06
JG
494
495 check_file_contents("./crlf/test3.txt", "");
496}