1 #include "clar_libgit2.h"
2 #include "checkout_helpers.h"
3 #include "../filter/crlf.h"
5 #include "git2/checkout.h"
6 #include "repository.h"
10 static git_repository
*g_repo
;
12 void test_checkout_crlf__initialize(void)
14 g_repo
= cl_git_sandbox_init("crlf");
17 void test_checkout_crlf__cleanup(void)
19 cl_git_sandbox_cleanup();
22 void test_checkout_crlf__detect_crlf_autocrlf_false(void)
24 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
25 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
27 cl_repo_set_bool(g_repo
, "core.autocrlf", false);
29 git_checkout_head(g_repo
, &opts
);
31 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
32 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
35 void test_checkout_crlf__autocrlf_false_index_size_is_unfiltered_size(void)
38 const git_index_entry
*entry
;
39 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
40 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
42 cl_repo_set_bool(g_repo
, "core.autocrlf", false);
44 git_repository_index(&index
, g_repo
);
47 git_checkout_head(g_repo
, &opts
);
49 cl_assert((entry
= git_index_get_bypath(index
, "all-lf", 0)) != NULL
);
50 cl_assert(entry
->file_size
== strlen(ALL_LF_TEXT_RAW
));
52 cl_assert((entry
= git_index_get_bypath(index
, "all-crlf", 0)) != NULL
);
53 cl_assert(entry
->file_size
== strlen(ALL_CRLF_TEXT_RAW
));
55 git_index_free(index
);
58 void test_checkout_crlf__detect_crlf_autocrlf_true(void)
60 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
61 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
63 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
65 git_checkout_head(g_repo
, &opts
);
67 if (GIT_EOL_NATIVE
== GIT_EOL_LF
)
68 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
70 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF
);
72 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
75 void test_checkout_crlf__more_lf_autocrlf_true(void)
77 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
78 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
80 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
82 git_checkout_head(g_repo
, &opts
);
84 check_file_contents("./crlf/more-lf", MORE_LF_TEXT_RAW
);
87 void test_checkout_crlf__more_crlf_autocrlf_true(void)
89 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
90 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
92 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
94 git_checkout_head(g_repo
, &opts
);
96 check_file_contents("./crlf/more-crlf", MORE_CRLF_TEXT_RAW
);
99 void test_checkout_crlf__all_crlf_autocrlf_true(void)
101 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
102 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
104 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
106 git_checkout_head(g_repo
, &opts
);
108 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
111 void test_checkout_crlf__detect_crlf_autocrlf_true_utf8(void)
113 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
114 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
116 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
118 git_repository_set_head(g_repo
, "refs/heads/master");
119 git_checkout_head(g_repo
, &opts
);
121 if (GIT_EOL_NATIVE
== GIT_EOL_LF
)
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
);
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
);
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
);
136 void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void)
139 const git_index_entry
*entry
;
140 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
141 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
143 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
145 git_repository_index(&index
, g_repo
);
148 git_checkout_head(g_repo
, &opts
);
150 cl_assert((entry
= git_index_get_bypath(index
, "all-lf", 0)) != NULL
);
152 if (GIT_EOL_NATIVE
== GIT_EOL_LF
)
153 cl_assert_equal_sz(strlen(ALL_LF_TEXT_RAW
), entry
->file_size
);
155 cl_assert_equal_sz(strlen(ALL_LF_TEXT_AS_CRLF
), entry
->file_size
);
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
);
160 git_index_free(index
);
163 void test_checkout_crlf__with_ident(void)
167 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
168 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
170 cl_git_mkfile("crlf/.gitattributes",
171 "*.txt text\n*.bin binary\n"
172 "*.crlf text eol=crlf\n"
174 "*.ident text ident\n"
175 "*.identcrlf ident text eol=crlf\n"
176 "*.identlf ident text eol=lf\n");
178 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
180 /* add files with $Id$ */
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
);
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");
194 git_checkout_head(g_repo
, &opts
);
196 /* check that blobs have $Id$ */
198 cl_git_pass(git_blob_lookup(&blob
, g_repo
,
199 & git_index_get_bypath(index
, "lf.ident", 0)->id
));
201 ALL_LF_TEXT_RAW
"\n$Id$\n", git_blob_rawcontent(blob
));
204 cl_git_pass(git_blob_lookup(&blob
, g_repo
,
205 & git_index_get_bypath(index
, "more2.identcrlf", 0)->id
));
207 "\n$Id$\n" MORE_CRLF_TEXT_AS_LF
, git_blob_rawcontent(blob
));
210 /* check that filesystem is initially untouched - matching core Git */
212 cl_assert_equal_file(
213 ALL_LF_TEXT_RAW
"\n$Id: initial content$\n", 0, "crlf/lf.ident");
215 /* check that forced checkout rewrites correctly */
217 p_unlink("crlf/lf.ident");
218 p_unlink("crlf/crlf.ident");
219 p_unlink("crlf/more1.identlf");
220 p_unlink("crlf/more2.identcrlf");
222 git_checkout_head(g_repo
, &opts
);
224 if (GIT_EOL_NATIVE
== GIT_EOL_LF
) {
225 cl_assert_equal_file(
227 "\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467 $\n",
229 cl_assert_equal_file(
231 "\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857 $\n\n",
232 0, "crlf/crlf.ident");
234 cl_assert_equal_file(
236 "\r\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467 $\r\n",
238 cl_assert_equal_file(
240 "\r\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857 $\r\n\r\n",
241 0, "crlf/crlf.ident");
244 cl_assert_equal_file(
245 "$Id: f7830382dac1f1583422be5530fdfbd26289431b $\n"
246 MORE_LF_TEXT_AS_LF
, 0, "crlf/more1.identlf");
248 cl_assert_equal_file(
249 "\r\n$Id: 74677a68413012ce8d7e7cfc3f12603df3a3eac4 $\r\n"
250 MORE_CRLF_TEXT_AS_CRLF
, 0, "crlf/more2.identcrlf");
252 git_index_free(index
);
255 void test_checkout_crlf__autocrlf_false_no_attrs(void)
257 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
258 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
260 cl_repo_set_bool(g_repo
, "core.autocrlf", false);
262 git_checkout_head(g_repo
, &opts
);
264 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
265 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
268 void test_checkout_crlf__autocrlf_true_no_attrs(void)
270 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
271 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
273 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
275 git_checkout_head(g_repo
, &opts
);
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
);
281 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
282 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
286 void test_checkout_crlf__autocrlf_input_no_attrs(void)
288 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
289 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
291 cl_repo_set_string(g_repo
, "core.autocrlf", "input");
293 git_checkout_head(g_repo
, &opts
);
295 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
296 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
299 void test_checkout_crlf__autocrlf_false_text_auto_attr(void)
301 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
302 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
304 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
306 cl_repo_set_bool(g_repo
, "core.autocrlf", false);
308 git_checkout_head(g_repo
, &opts
);
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
);
314 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
315 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
319 void test_checkout_crlf__autocrlf_true_text_auto_attr(void)
321 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
322 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
324 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
326 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
328 git_checkout_head(g_repo
, &opts
);
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
);
334 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
335 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
339 void test_checkout_crlf__autocrlf_input_text_auto_attr(void)
341 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
342 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
344 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
346 cl_repo_set_string(g_repo
, "core.autocrlf", "input");
348 git_checkout_head(g_repo
, &opts
);
350 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
351 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
354 void test_checkout_crlf__can_write_empty_file(void)
356 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
357 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
359 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
361 git_repository_set_head(g_repo
, "refs/heads/empty-files");
362 git_checkout_head(g_repo
, &opts
);
364 check_file_contents("./crlf/test1.txt", "");
366 if (GIT_EOL_NATIVE
== GIT_EOL_LF
)
367 check_file_contents("./crlf/test2.txt", "test2.txt's content\n");
369 check_file_contents("./crlf/test2.txt", "test2.txt's content\r\n");
371 check_file_contents("./crlf/test3.txt", "");