]> git.proxmox.com Git - libgit2.git/blob - tests/checkout/crlf.c
checkout::crlf test: don't crash when no idx entry
[libgit2.git] / tests / checkout / crlf.c
1 #include "clar_libgit2.h"
2 #include "checkout_helpers.h"
3 #include "../filter/crlf.h"
4 #include "fileops.h"
5
6 #include "git2/checkout.h"
7 #include "repository.h"
8 #include "index.h"
9 #include "posix.h"
10
11 static git_repository *g_repo;
12
13 static const char *systype;
14 static git_buf expected_fixture = GIT_BUF_INIT;
15
16 void test_checkout_crlf__initialize(void)
17 {
18 g_repo = cl_git_sandbox_init("crlf");
19
20 if (GIT_EOL_NATIVE == GIT_EOL_CRLF)
21 systype = "windows";
22 else
23 systype = "posix";
24 }
25
26 void test_checkout_crlf__cleanup(void)
27 {
28 cl_git_sandbox_cleanup();
29
30 if (expected_fixture.size) {
31 cl_fixture_cleanup(expected_fixture.ptr);
32 git_buf_free(&expected_fixture);
33 }
34 }
35
36 struct compare_data
37 {
38 const char *dirname;
39 const char *autocrlf;
40 const char *attrs;
41 };
42
43 static 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;
50 int cmp_git, cmp_gitattributes;
51 char *basename;
52
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) {
58 failed = false;
59 goto done;
60 }
61
62 cl_git_pass(git_buf_joinpath(&expected_path, cd->dirname, basename));
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
80 done:
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
90 git__free(basename);
91 git_buf_free(&expected_contents);
92 git_buf_free(&actual_contents);
93 git_buf_free(&expected_path);
94
95 return 0;
96 }
97
98 static 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
151 static 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) {
159 char *basename = git_path_basename(fn);
160 int cmp = strncasecmp(basename, ".git", 4);
161
162 git__free(basename);
163
164 if (cmp == 0)
165 continue;
166 p_unlink(fn);
167 }
168 git_vector_free_deep(&contents);
169 }
170
171 void 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 }
186 }
187
188 void test_checkout_crlf__detect_crlf_autocrlf_false(void)
189 {
190 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
191 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
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
201 void test_checkout_crlf__autocrlf_false_index_size_is_unfiltered_size(void)
202 {
203 git_index *index;
204 const git_index_entry *entry;
205 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
206 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
207
208 cl_repo_set_bool(g_repo, "core.autocrlf", false);
209
210 git_repository_index(&index, g_repo);
211 tick_index(index);
212
213 git_checkout_head(g_repo, &opts);
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
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
221 git_index_free(index);
222 }
223
224 void test_checkout_crlf__detect_crlf_autocrlf_true(void)
225 {
226 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
227 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
228
229 cl_repo_set_bool(g_repo, "core.autocrlf", true);
230
231 git_checkout_head(g_repo, &opts);
232
233 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
234 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
235 }
236
237 void test_checkout_crlf__detect_crlf_autocrlf_true_utf8(void)
238 {
239 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
240 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
241
242 cl_repo_set_bool(g_repo, "core.autocrlf", true);
243
244 git_repository_set_head(g_repo, "refs/heads/master");
245 git_checkout_head(g_repo, &opts);
246
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);
249
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);
252 }
253
254 void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void)
255 {
256 git_index *index;
257 const git_index_entry *entry;
258 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
259 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
260
261 cl_repo_set_bool(g_repo, "core.autocrlf", true);
262
263 git_repository_index(&index, g_repo);
264 tick_index(index);
265
266 git_checkout_head(g_repo, &opts);
267
268 cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);
269
270 cl_assert_equal_sz(strlen(ALL_LF_TEXT_AS_CRLF), entry->file_size);
271
272 cl_assert((entry = git_index_get_bypath(index, "all-crlf", 0)) != NULL);
273 cl_assert_equal_sz(strlen(ALL_CRLF_TEXT_RAW), entry->file_size);
274
275 git_index_free(index);
276 }
277
278 void test_checkout_crlf__with_ident(void)
279 {
280 git_index *index;
281 git_index_entry *entry;
282 git_blob *blob;
283 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
284 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
285
286 cl_git_mkfile("crlf/.gitattributes",
287 "*.txt text\n*.bin binary\n"
288 "*.crlf text eol=crlf\n"
289 "*.lf text eol=lf\n"
290 "*.ident text ident\n"
291 "*.identcrlf ident text eol=crlf\n"
292 "*.identlf ident text eol=lf\n");
293
294 cl_repo_set_bool(g_repo, "core.autocrlf", true);
295
296 /* add files with $Id$ */
297
298 cl_git_mkfile("crlf/lf.ident", ALL_LF_TEXT_RAW "\n$Id: initial content$\n");
299 cl_git_mkfile("crlf/crlf.ident", ALL_CRLF_TEXT_RAW "\r\n$Id$\r\n\r\n");
300 cl_git_mkfile("crlf/more1.identlf", "$Id$\n" MORE_LF_TEXT_RAW);
301 cl_git_mkfile("crlf/more2.identcrlf", "\r\n$Id: \f$\r\n" MORE_CRLF_TEXT_RAW);
302
303 cl_git_pass(git_repository_index(&index, g_repo));
304 cl_git_pass(git_index_add_bypath(index, "lf.ident"));
305 cl_git_pass(git_index_add_bypath(index, "crlf.ident"));
306 cl_git_pass(git_index_add_bypath(index, "more1.identlf"));
307 cl_git_pass(git_index_add_bypath(index, "more2.identcrlf"));
308 cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "Some ident files\n");
309
310 git_checkout_head(g_repo, &opts);
311
312 /* check that blobs have $Id$ */
313
314 cl_assert((entry = git_index_get_bypath(index, "lf.ident", 0)));
315 cl_git_pass(git_blob_lookup(&blob, g_repo, &entry->id));
316 cl_assert_equal_s(
317 ALL_LF_TEXT_RAW "\n$Id$\n", git_blob_rawcontent(blob));
318 git_blob_free(blob);
319
320 cl_assert((entry = git_index_get_bypath(index, "more2.identcrlf", 0)));
321 cl_git_pass(git_blob_lookup(&blob, g_repo, &entry->id));
322 cl_assert_equal_s(
323 "\n$Id$\n" MORE_CRLF_TEXT_AS_LF, git_blob_rawcontent(blob));
324 git_blob_free(blob);
325
326 /* check that filesystem is initially untouched - matching core Git */
327
328 cl_assert_equal_file(
329 ALL_LF_TEXT_RAW "\n$Id: initial content$\n", 0, "crlf/lf.ident");
330
331 /* check that forced checkout rewrites correctly */
332
333 p_unlink("crlf/lf.ident");
334 p_unlink("crlf/crlf.ident");
335 p_unlink("crlf/more1.identlf");
336 p_unlink("crlf/more2.identcrlf");
337
338 git_checkout_head(g_repo, &opts);
339
340 cl_assert_equal_file(
341 ALL_LF_TEXT_AS_CRLF
342 "\r\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467 $\r\n",
343 0, "crlf/lf.ident");
344 cl_assert_equal_file(
345 ALL_CRLF_TEXT_RAW
346 "\r\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857 $\r\n\r\n",
347 0, "crlf/crlf.ident");
348
349 cl_assert_equal_file(
350 "$Id: f7830382dac1f1583422be5530fdfbd26289431b $\n"
351 MORE_LF_TEXT_AS_LF, 0, "crlf/more1.identlf");
352
353 cl_assert_equal_file(
354 "\r\n$Id: 74677a68413012ce8d7e7cfc3f12603df3a3eac4 $\r\n"
355 MORE_CRLF_TEXT_AS_CRLF, 0, "crlf/more2.identcrlf");
356
357 git_index_free(index);
358 }
359
360 void test_checkout_crlf__autocrlf_false_no_attrs(void)
361 {
362 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
363 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
364
365 cl_repo_set_bool(g_repo, "core.autocrlf", false);
366
367 git_checkout_head(g_repo, &opts);
368
369 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
370 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
371 }
372
373 void test_checkout_crlf__autocrlf_true_no_attrs(void)
374 {
375 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
376 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
377
378 cl_repo_set_bool(g_repo, "core.autocrlf", true);
379
380 git_checkout_head(g_repo, &opts);
381
382 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
383 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
384 }
385
386 void test_checkout_crlf__autocrlf_input_no_attrs(void)
387 {
388 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
389 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
390
391 cl_repo_set_string(g_repo, "core.autocrlf", "input");
392
393 git_checkout_head(g_repo, &opts);
394
395 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
396 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
397 }
398
399 void test_checkout_crlf__autocrlf_false_text_auto_attr(void)
400 {
401 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
402 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
403
404 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
405
406 cl_repo_set_bool(g_repo, "core.autocrlf", false);
407
408 git_checkout_head(g_repo, &opts);
409
410 if (GIT_EOL_NATIVE == GIT_EOL_CRLF) {
411 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
412 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
413 } else {
414 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
415 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
416 }
417 }
418
419 void test_checkout_crlf__autocrlf_true_text_auto_attr(void)
420 {
421 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
422 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
423
424 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
425
426 cl_repo_set_bool(g_repo, "core.autocrlf", true);
427
428 git_checkout_head(g_repo, &opts);
429
430 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
431 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
432 }
433
434 void test_checkout_crlf__autocrlf_input_text_auto_attr(void)
435 {
436 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
437 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
438
439 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
440
441 cl_repo_set_string(g_repo, "core.autocrlf", "input");
442
443 git_checkout_head(g_repo, &opts);
444
445 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
446 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
447 }
448
449 void test_checkout_crlf__can_write_empty_file(void)
450 {
451 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
452 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
453
454 cl_repo_set_bool(g_repo, "core.autocrlf", true);
455
456 git_repository_set_head(g_repo, "refs/heads/empty-files");
457 git_checkout_head(g_repo, &opts);
458
459 check_file_contents("./crlf/test1.txt", "");
460
461 check_file_contents("./crlf/test2.txt", "test2.txt's content\r\n");
462
463 check_file_contents("./crlf/test3.txt", "");
464 }