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