1 #include "clar_libgit2.h"
2 #include "checkout_helpers.h"
3 #include "../filter/crlf.h"
6 #include "git2/checkout.h"
7 #include "repository.h"
11 static git_repository
*g_repo
;
13 static const char *systype
;
14 static git_buf expected_fixture
= GIT_BUF_INIT
;
16 void test_checkout_crlf__initialize(void)
18 g_repo
= cl_git_sandbox_init("crlf");
20 if (GIT_EOL_NATIVE
== GIT_EOL_CRLF
)
26 void test_checkout_crlf__cleanup(void)
28 cl_git_sandbox_cleanup();
30 if (expected_fixture
.size
) {
31 cl_fixture_cleanup(expected_fixture
.ptr
);
32 git_buf_free(&expected_fixture
);
43 static int compare_file(void *payload
, git_buf
*actual_path
)
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
;
50 int cmp_git
, cmp_gitattributes
;
53 basename
= git_path_basename(actual_path
->ptr
);
54 cmp_git
= strcmp(basename
, ".git");
55 cmp_gitattributes
= strcmp(basename
, ".gitattributes");
57 if (cmp_git
== 0 || cmp_gitattributes
== 0) {
62 cl_git_pass(git_buf_joinpath(&expected_path
, cd
->dirname
, basename
));
64 if (!git_path_isfile(expected_path
.ptr
) ||
65 !git_path_isfile(actual_path
->ptr
))
68 if (git_futils_readbuffer(&actual_contents
, actual_path
->ptr
) < 0 ||
69 git_futils_readbuffer(&expected_contents
, expected_path
.ptr
) < 0)
72 if (actual_contents
.size
!= expected_contents
.size
)
75 if (memcmp(actual_contents
.ptr
, expected_contents
.ptr
, expected_contents
.size
) != 0)
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
);
91 git_buf_free(&expected_contents
);
92 git_buf_free(&actual_contents
);
93 git_buf_free(&expected_path
);
98 static void test_checkout(const char *autocrlf
, const char *attrs
)
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
};
108 git_buf_puts(&reponame
, "crlf");
110 git_buf_puts(&sandboxname
, "autocrlf_");
111 git_buf_puts(&sandboxname
, autocrlf
);
114 git_buf_puts(&sandboxname
, ",");
116 for (c
= attrs
; *c
; c
++) {
118 git_buf_putc(&sandboxname
, ',');
120 git_buf_putc(&sandboxname
, '_');
122 git_buf_putc(&sandboxname
, *c
);
125 git_buf_printf(&attrbuf
, "* %s\n", attrs
);
126 cl_git_mkfile("crlf/.gitattributes", attrbuf
.ptr
);
129 cl_repo_set_string(g_repo
, "core.autocrlf", autocrlf
);
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
);
135 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
136 git_checkout_head(g_repo
, &opts
);
138 compare_data
.dirname
= sandboxname
.ptr
;
139 cl_git_pass(git_path_direach(&reponame
, 0, compare_file
, &compare_data
));
141 cl_fixture_cleanup(expected_fixture
.ptr
);
142 git_buf_free(&expected_fixture
);
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
);
151 static void empty_workdir(const char *name
)
153 git_vector contents
= GIT_VECTOR_INIT
;
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);
168 git_vector_free_deep(&contents
);
171 void test_checkout_crlf__matches_core_git(void)
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",
180 for (a
= autocrlf
; *a
; a
++) {
181 for (b
= attrs
; *b
; b
++) {
182 empty_workdir("crlf");
183 test_checkout(*a
, *b
);
188 void test_checkout_crlf__detect_crlf_autocrlf_false(void)
190 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
191 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
193 cl_repo_set_bool(g_repo
, "core.autocrlf", false);
195 git_checkout_head(g_repo
, &opts
);
197 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
198 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
201 void test_checkout_crlf__autocrlf_false_index_size_is_unfiltered_size(void)
204 const git_index_entry
*entry
;
205 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
206 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
208 cl_repo_set_bool(g_repo
, "core.autocrlf", false);
210 git_repository_index(&index
, g_repo
);
213 git_checkout_head(g_repo
, &opts
);
215 cl_assert((entry
= git_index_get_bypath(index
, "all-lf", 0)) != NULL
);
216 cl_assert(entry
->file_size
== strlen(ALL_LF_TEXT_RAW
));
218 cl_assert((entry
= git_index_get_bypath(index
, "all-crlf", 0)) != NULL
);
219 cl_assert(entry
->file_size
== strlen(ALL_CRLF_TEXT_RAW
));
221 git_index_free(index
);
224 void test_checkout_crlf__detect_crlf_autocrlf_true(void)
226 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
227 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
229 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
231 git_checkout_head(g_repo
, &opts
);
233 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF
);
234 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
237 void test_checkout_crlf__detect_crlf_autocrlf_true_utf8(void)
239 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
240 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
242 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
244 git_repository_set_head(g_repo
, "refs/heads/master");
245 git_checkout_head(g_repo
, &opts
);
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
);
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
);
254 void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void)
257 const git_index_entry
*entry
;
258 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
259 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
261 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
263 git_repository_index(&index
, g_repo
);
266 git_checkout_head(g_repo
, &opts
);
268 cl_assert((entry
= git_index_get_bypath(index
, "all-lf", 0)) != NULL
);
270 cl_assert_equal_sz(strlen(ALL_LF_TEXT_AS_CRLF
), entry
->file_size
);
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
);
275 git_index_free(index
);
278 void test_checkout_crlf__with_ident(void)
281 git_index_entry
*entry
;
283 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
284 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
286 cl_git_mkfile("crlf/.gitattributes",
287 "*.txt text\n*.bin binary\n"
288 "*.crlf text eol=crlf\n"
290 "*.ident text ident\n"
291 "*.identcrlf ident text eol=crlf\n"
292 "*.identlf ident text eol=lf\n");
294 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
296 /* add files with $Id$ */
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
);
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");
310 git_checkout_head(g_repo
, &opts
);
312 /* check that blobs have $Id$ */
314 cl_assert((entry
= git_index_get_bypath(index
, "lf.ident", 0)));
315 cl_git_pass(git_blob_lookup(&blob
, g_repo
, &entry
->id
));
317 ALL_LF_TEXT_RAW
"\n$Id$\n", git_blob_rawcontent(blob
));
320 cl_assert((entry
= git_index_get_bypath(index
, "more2.identcrlf", 0)));
321 cl_git_pass(git_blob_lookup(&blob
, g_repo
, &entry
->id
));
323 "\n$Id$\n" MORE_CRLF_TEXT_AS_LF
, git_blob_rawcontent(blob
));
326 /* check that filesystem is initially untouched - matching core Git */
328 cl_assert_equal_file(
329 ALL_LF_TEXT_RAW
"\n$Id: initial content$\n", 0, "crlf/lf.ident");
331 /* check that forced checkout rewrites correctly */
333 p_unlink("crlf/lf.ident");
334 p_unlink("crlf/crlf.ident");
335 p_unlink("crlf/more1.identlf");
336 p_unlink("crlf/more2.identcrlf");
338 git_checkout_head(g_repo
, &opts
);
340 cl_assert_equal_file(
342 "\r\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467 $\r\n",
344 cl_assert_equal_file(
346 "\r\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857 $\r\n\r\n",
347 0, "crlf/crlf.ident");
349 cl_assert_equal_file(
350 "$Id: f7830382dac1f1583422be5530fdfbd26289431b $\n"
351 MORE_LF_TEXT_AS_LF
, 0, "crlf/more1.identlf");
353 cl_assert_equal_file(
354 "\r\n$Id: 74677a68413012ce8d7e7cfc3f12603df3a3eac4 $\r\n"
355 MORE_CRLF_TEXT_AS_CRLF
, 0, "crlf/more2.identcrlf");
357 git_index_free(index
);
360 void test_checkout_crlf__autocrlf_false_no_attrs(void)
362 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
363 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
365 cl_repo_set_bool(g_repo
, "core.autocrlf", false);
367 git_checkout_head(g_repo
, &opts
);
369 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
370 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
373 void test_checkout_crlf__autocrlf_true_no_attrs(void)
375 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
376 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
378 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
380 git_checkout_head(g_repo
, &opts
);
382 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF
);
383 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF
);
386 void test_checkout_crlf__autocrlf_input_no_attrs(void)
388 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
389 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
391 cl_repo_set_string(g_repo
, "core.autocrlf", "input");
393 git_checkout_head(g_repo
, &opts
);
395 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
396 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
399 void test_checkout_crlf__autocrlf_false_text_auto_attr(void)
401 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
402 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
404 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
406 cl_repo_set_bool(g_repo
, "core.autocrlf", false);
408 git_checkout_head(g_repo
, &opts
);
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
);
414 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
415 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
419 void test_checkout_crlf__autocrlf_true_text_auto_attr(void)
421 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
422 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
424 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
426 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
428 git_checkout_head(g_repo
, &opts
);
430 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF
);
431 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF
);
434 void test_checkout_crlf__autocrlf_input_text_auto_attr(void)
436 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
437 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
439 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
441 cl_repo_set_string(g_repo
, "core.autocrlf", "input");
443 git_checkout_head(g_repo
, &opts
);
445 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
446 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
449 void test_checkout_crlf__can_write_empty_file(void)
451 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
452 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
454 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
456 git_repository_set_head(g_repo
, "refs/heads/empty-files");
457 git_checkout_head(g_repo
, &opts
);
459 check_file_contents("./crlf/test1.txt", "");
461 check_file_contents("./crlf/test2.txt", "test2.txt's content\r\n");
463 check_file_contents("./crlf/test3.txt", "");