1 #include "clar_libgit2.h"
2 #include "checkout_helpers.h"
3 #include "../filter/crlf.h"
5 #include "git2/checkout.h"
6 #include "repository.h"
9 static git_repository
*g_repo
;
11 void test_checkout_crlf__initialize(void)
13 g_repo
= cl_git_sandbox_init("crlf");
16 void test_checkout_crlf__cleanup(void)
18 cl_git_sandbox_cleanup();
21 void test_checkout_crlf__detect_crlf_autocrlf_false(void)
23 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
24 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
26 cl_repo_set_bool(g_repo
, "core.autocrlf", false);
28 git_checkout_head(g_repo
, &opts
);
30 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
31 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
34 void test_checkout_crlf__autocrlf_false_index_size_is_unfiltered_size(void)
37 const git_index_entry
*entry
;
38 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
39 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
41 cl_repo_set_bool(g_repo
, "core.autocrlf", false);
43 git_checkout_head(g_repo
, &opts
);
45 git_repository_index(&index
, g_repo
);
47 cl_assert((entry
= git_index_get_bypath(index
, "all-lf", 0)) != NULL
);
48 cl_assert(entry
->file_size
== strlen(ALL_LF_TEXT_RAW
));
50 cl_assert((entry
= git_index_get_bypath(index
, "all-crlf", 0)) != NULL
);
51 cl_assert(entry
->file_size
== strlen(ALL_CRLF_TEXT_RAW
));
53 git_index_free(index
);
56 void test_checkout_crlf__detect_crlf_autocrlf_true(void)
58 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
59 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
61 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
63 git_checkout_head(g_repo
, &opts
);
65 if (GIT_EOL_NATIVE
== GIT_EOL_LF
)
66 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
68 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF
);
70 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
73 void test_checkout_crlf__more_lf_autocrlf_true(void)
75 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
76 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
78 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
80 git_checkout_head(g_repo
, &opts
);
82 check_file_contents("./crlf/more-lf", MORE_LF_TEXT_RAW
);
85 void test_checkout_crlf__more_crlf_autocrlf_true(void)
87 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
88 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
90 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
92 git_checkout_head(g_repo
, &opts
);
94 check_file_contents("./crlf/more-crlf", MORE_CRLF_TEXT_RAW
);
97 void test_checkout_crlf__all_crlf_autocrlf_true(void)
99 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
100 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
102 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
104 git_checkout_head(g_repo
, &opts
);
106 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
109 void test_checkout_crlf__detect_crlf_autocrlf_true_utf8(void)
111 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
112 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
114 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
116 git_repository_set_head(g_repo
, "refs/heads/utf8");
117 git_checkout_head(g_repo
, &opts
);
119 if (GIT_EOL_NATIVE
== GIT_EOL_LF
)
121 check_file_contents("./crlf/few-utf8-chars-lf.txt", FEW_UTF8_LF_RAW
);
122 check_file_contents("./crlf/many-utf8-chars-lf.txt", MANY_UTF8_LF_RAW
);
126 check_file_contents("./crlf/few-utf8-chars-lf.txt", FEW_UTF8_CRLF_RAW
);
127 check_file_contents("./crlf/many-utf8-chars-lf.txt", MANY_UTF8_CRLF_RAW
);
130 check_file_contents("./crlf/few-utf8-chars-crlf.txt", FEW_UTF8_CRLF_RAW
);
131 check_file_contents("./crlf/many-utf8-chars-crlf.txt", MANY_UTF8_CRLF_RAW
);
134 void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void)
137 const git_index_entry
*entry
;
138 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
139 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
141 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
143 git_checkout_head(g_repo
, &opts
);
145 git_repository_index(&index
, g_repo
);
147 cl_assert((entry
= git_index_get_bypath(index
, "all-lf", 0)) != NULL
);
149 if (GIT_EOL_NATIVE
== GIT_EOL_LF
)
150 cl_assert_equal_sz(strlen(ALL_LF_TEXT_RAW
), entry
->file_size
);
152 cl_assert_equal_sz(strlen(ALL_LF_TEXT_AS_CRLF
), entry
->file_size
);
154 cl_assert((entry
= git_index_get_bypath(index
, "all-crlf", 0)) != NULL
);
155 cl_assert_equal_sz(strlen(ALL_CRLF_TEXT_RAW
), entry
->file_size
);
157 git_index_free(index
);
160 void test_checkout_crlf__with_ident(void)
164 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
165 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
167 cl_git_mkfile("crlf/.gitattributes",
168 "*.txt text\n*.bin binary\n"
169 "*.crlf text eol=crlf\n"
171 "*.ident text ident\n"
172 "*.identcrlf ident text eol=crlf\n"
173 "*.identlf ident text eol=lf\n");
175 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
177 /* add files with $Id$ */
179 cl_git_mkfile("crlf/lf.ident", ALL_LF_TEXT_RAW
"\n$Id: initial content$\n");
180 cl_git_mkfile("crlf/crlf.ident", ALL_CRLF_TEXT_RAW
"\r\n$Id$\r\n\r\n");
181 cl_git_mkfile("crlf/more1.identlf", "$Id$\n" MORE_LF_TEXT_RAW
);
182 cl_git_mkfile("crlf/more2.identcrlf", "\r\n$Id: \f$\r\n" MORE_CRLF_TEXT_RAW
);
184 cl_git_pass(git_repository_index(&index
, g_repo
));
185 cl_git_pass(git_index_add_bypath(index
, "lf.ident"));
186 cl_git_pass(git_index_add_bypath(index
, "crlf.ident"));
187 cl_git_pass(git_index_add_bypath(index
, "more1.identlf"));
188 cl_git_pass(git_index_add_bypath(index
, "more2.identcrlf"));
189 cl_repo_commit_from_index(NULL
, g_repo
, NULL
, 0, "Some ident files\n");
191 git_checkout_head(g_repo
, &opts
);
193 /* check that blobs have $Id$ */
195 cl_git_pass(git_blob_lookup(&blob
, g_repo
,
196 & git_index_get_bypath(index
, "lf.ident", 0)->id
));
198 ALL_LF_TEXT_RAW
"\n$Id$\n", git_blob_rawcontent(blob
));
201 cl_git_pass(git_blob_lookup(&blob
, g_repo
,
202 & git_index_get_bypath(index
, "more2.identcrlf", 0)->id
));
204 "\n$Id$\n" MORE_CRLF_TEXT_AS_LF
, git_blob_rawcontent(blob
));
207 /* check that filesystem is initially untouched - matching core Git */
209 cl_assert_equal_file(
210 ALL_LF_TEXT_RAW
"\n$Id: initial content$\n", 0, "crlf/lf.ident");
212 /* check that forced checkout rewrites correctly */
214 p_unlink("crlf/lf.ident");
215 p_unlink("crlf/crlf.ident");
216 p_unlink("crlf/more1.identlf");
217 p_unlink("crlf/more2.identcrlf");
219 git_checkout_head(g_repo
, &opts
);
221 if (GIT_EOL_NATIVE
== GIT_EOL_LF
) {
222 cl_assert_equal_file(
224 "\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467 $\n",
226 cl_assert_equal_file(
228 "\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857 $\n\n",
229 0, "crlf/crlf.ident");
231 cl_assert_equal_file(
233 "\r\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467 $\r\n",
235 cl_assert_equal_file(
237 "\r\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857 $\r\n\r\n",
238 0, "crlf/crlf.ident");
241 cl_assert_equal_file(
242 "$Id: f7830382dac1f1583422be5530fdfbd26289431b $\n"
243 MORE_LF_TEXT_AS_LF
, 0, "crlf/more1.identlf");
245 cl_assert_equal_file(
246 "\r\n$Id: 74677a68413012ce8d7e7cfc3f12603df3a3eac4 $\r\n"
247 MORE_CRLF_TEXT_AS_CRLF
, 0, "crlf/more2.identcrlf");
249 git_index_free(index
);
252 void test_checkout_crlf__autocrlf_false_no_attrs(void)
254 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
255 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
257 cl_repo_set_bool(g_repo
, "core.autocrlf", false);
259 git_checkout_head(g_repo
, &opts
);
261 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
262 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
265 void test_checkout_crlf__autocrlf_true_no_attrs(void)
267 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
268 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
270 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
272 git_checkout_head(g_repo
, &opts
);
274 if (GIT_EOL_NATIVE
== GIT_EOL_CRLF
) {
275 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF
);
276 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF
);
278 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
279 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
283 void test_checkout_crlf__autocrlf_input_no_attrs(void)
285 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
286 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
288 cl_repo_set_string(g_repo
, "core.autocrlf", "input");
290 git_checkout_head(g_repo
, &opts
);
292 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
293 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
296 void test_checkout_crlf__autocrlf_false_text_auto_attr(void)
298 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
299 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
301 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
303 cl_repo_set_bool(g_repo
, "core.autocrlf", false);
305 git_checkout_head(g_repo
, &opts
);
307 if (GIT_EOL_NATIVE
== GIT_EOL_CRLF
) {
308 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF
);
309 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF
);
311 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
312 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
316 void test_checkout_crlf__autocrlf_true_text_auto_attr(void)
318 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
319 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
321 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
323 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
325 git_checkout_head(g_repo
, &opts
);
327 if (GIT_EOL_NATIVE
== GIT_EOL_CRLF
) {
328 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF
);
329 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF
);
331 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
332 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
336 void test_checkout_crlf__autocrlf_input_text_auto_attr(void)
338 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
339 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
341 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
343 cl_repo_set_string(g_repo
, "core.autocrlf", "input");
345 git_checkout_head(g_repo
, &opts
);
347 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
348 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
351 void test_checkout_crlf__can_write_empty_file(void)
353 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
354 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
356 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
358 git_repository_set_head(g_repo
, "refs/heads/empty-files");
359 git_checkout_head(g_repo
, &opts
);
361 check_file_contents("./crlf/test1.txt", "");
363 if (GIT_EOL_NATIVE
== GIT_EOL_LF
)
364 check_file_contents("./crlf/test2.txt", "test2.txt's content\n");
366 check_file_contents("./crlf/test2.txt", "test2.txt's content\r\n");
368 check_file_contents("./crlf/test3.txt", "");