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_SAFE_CREATE
;
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_SAFE_CREATE
;
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_SAFE_CREATE
;
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_SAFE_CREATE
;
78 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
80 git_checkout_head(g_repo
, &opts
);
82 if (GIT_EOL_NATIVE
== GIT_EOL_LF
)
83 check_file_contents("./crlf/more-lf", MORE_LF_TEXT_RAW
);
85 check_file_contents("./crlf/more-lf", MORE_LF_TEXT_AS_CRLF
);
88 void test_checkout_crlf__more_crlf_autocrlf_true(void)
90 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
91 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE_CREATE
;
93 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
95 git_checkout_head(g_repo
, &opts
);
97 if (GIT_EOL_NATIVE
== GIT_EOL_LF
)
98 check_file_contents("./crlf/more-crlf", MORE_CRLF_TEXT_RAW
);
100 check_file_contents("./crlf/more-crlf", MORE_CRLF_TEXT_AS_CRLF
);
103 void test_checkout_crlf__all_crlf_autocrlf_true(void)
105 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
106 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE_CREATE
;
108 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
110 git_checkout_head(g_repo
, &opts
);
112 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
115 void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void)
118 const git_index_entry
*entry
;
119 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
120 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE_CREATE
;
122 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
124 git_checkout_head(g_repo
, &opts
);
126 git_repository_index(&index
, g_repo
);
128 cl_assert((entry
= git_index_get_bypath(index
, "all-lf", 0)) != NULL
);
130 if (GIT_EOL_NATIVE
== GIT_EOL_LF
)
131 cl_assert_equal_sz(strlen(ALL_LF_TEXT_RAW
), entry
->file_size
);
133 cl_assert_equal_sz(strlen(ALL_LF_TEXT_AS_CRLF
), entry
->file_size
);
135 cl_assert((entry
= git_index_get_bypath(index
, "all-crlf", 0)) != NULL
);
136 cl_assert_equal_sz(strlen(ALL_CRLF_TEXT_RAW
), entry
->file_size
);
138 git_index_free(index
);
141 void test_checkout_crlf__with_ident(void)
145 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
146 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE_CREATE
;
148 cl_git_mkfile("crlf/.gitattributes",
149 "*.txt text\n*.bin binary\n"
150 "*.crlf text eol=crlf\n"
152 "*.ident text ident\n"
153 "*.identcrlf ident text eol=crlf\n"
154 "*.identlf ident text eol=lf\n");
156 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
158 /* add files with $Id$ */
160 cl_git_mkfile("crlf/lf.ident", ALL_LF_TEXT_RAW
"\n$Id: initial content$\n");
161 cl_git_mkfile("crlf/crlf.ident", ALL_CRLF_TEXT_RAW
"\r\n$Id$\r\n\r\n");
162 cl_git_mkfile("crlf/more1.identlf", "$Id$\n" MORE_LF_TEXT_RAW
);
163 cl_git_mkfile("crlf/more2.identcrlf", "\r\n$Id: \f$\r\n" MORE_CRLF_TEXT_RAW
);
165 cl_git_pass(git_repository_index(&index
, g_repo
));
166 cl_git_pass(git_index_add_bypath(index
, "lf.ident"));
167 cl_git_pass(git_index_add_bypath(index
, "crlf.ident"));
168 cl_git_pass(git_index_add_bypath(index
, "more1.identlf"));
169 cl_git_pass(git_index_add_bypath(index
, "more2.identcrlf"));
170 cl_repo_commit_from_index(NULL
, g_repo
, NULL
, 0, "Some ident files\n");
172 git_checkout_head(g_repo
, &opts
);
174 /* check that blobs have $Id$ */
176 cl_git_pass(git_blob_lookup(&blob
, g_repo
,
177 & git_index_get_bypath(index
, "lf.ident", 0)->id
));
179 ALL_LF_TEXT_RAW
"\n$Id$\n", git_blob_rawcontent(blob
));
182 cl_git_pass(git_blob_lookup(&blob
, g_repo
,
183 & git_index_get_bypath(index
, "more2.identcrlf", 0)->id
));
185 "\n$Id$\n" MORE_CRLF_TEXT_AS_LF
, git_blob_rawcontent(blob
));
188 /* check that filesystem is initially untouched - matching core Git */
190 cl_assert_equal_file(
191 ALL_LF_TEXT_RAW
"\n$Id: initial content$\n", 0, "crlf/lf.ident");
193 /* check that forced checkout rewrites correctly */
195 p_unlink("crlf/lf.ident");
196 p_unlink("crlf/crlf.ident");
197 p_unlink("crlf/more1.identlf");
198 p_unlink("crlf/more2.identcrlf");
200 git_checkout_head(g_repo
, &opts
);
202 if (GIT_EOL_NATIVE
== GIT_EOL_LF
) {
203 cl_assert_equal_file(
205 "\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467$\n",
207 cl_assert_equal_file(
209 "\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857$\n\n",
210 0, "crlf/crlf.ident");
212 cl_assert_equal_file(
214 "\r\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467$\r\n",
216 cl_assert_equal_file(
218 "\r\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857$\r\n\r\n",
219 0, "crlf/crlf.ident");
222 cl_assert_equal_file(
223 "$Id: f7830382dac1f1583422be5530fdfbd26289431b$\n"
224 MORE_LF_TEXT_AS_LF
, 0, "crlf/more1.identlf");
226 cl_assert_equal_file(
227 "\r\n$Id: 74677a68413012ce8d7e7cfc3f12603df3a3eac4$\r\n"
228 MORE_CRLF_TEXT_AS_CRLF
, 0, "crlf/more2.identcrlf");
230 git_index_free(index
);
233 void test_checkout_crlf__autocrlf_false_no_attrs(void)
235 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
236 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE_CREATE
;
238 cl_repo_set_bool(g_repo
, "core.autocrlf", false);
240 git_checkout_head(g_repo
, &opts
);
242 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
243 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
246 void test_checkout_crlf__autocrlf_true_no_attrs(void)
248 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
249 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE_CREATE
;
251 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
253 git_checkout_head(g_repo
, &opts
);
255 if (GIT_EOL_NATIVE
== GIT_EOL_CRLF
) {
256 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF
);
257 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF
);
259 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
260 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
264 void test_checkout_crlf__autocrlf_input_no_attrs(void)
266 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
267 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE_CREATE
;
269 cl_repo_set_string(g_repo
, "core.autocrlf", "input");
271 git_checkout_head(g_repo
, &opts
);
273 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
274 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
277 void test_checkout_crlf__autocrlf_false_text_auto_attr(void)
279 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
280 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE_CREATE
;
282 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
284 cl_repo_set_bool(g_repo
, "core.autocrlf", false);
286 git_checkout_head(g_repo
, &opts
);
288 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
289 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
292 void test_checkout_crlf__autocrlf_true_text_auto_attr(void)
294 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
295 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE_CREATE
;
297 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
299 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
301 git_checkout_head(g_repo
, &opts
);
303 if (GIT_EOL_NATIVE
== GIT_EOL_CRLF
) {
304 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF
);
305 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF
);
307 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
308 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
312 void test_checkout_crlf__autocrlf_input_text_auto_attr(void)
314 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
315 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE_CREATE
;
317 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
319 cl_repo_set_string(g_repo
, "core.autocrlf", "input");
321 git_checkout_head(g_repo
, &opts
);
323 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
324 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);