]>
git.proxmox.com Git - libgit2.git/blob - tests/checkout/crlf.c
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__autocrlf_false_index_size_is_unfiltered_size(void)
24 const git_index_entry
*entry
;
25 git_checkout_opts opts
= GIT_CHECKOUT_OPTS_INIT
;
26 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE_CREATE
;
28 cl_repo_set_bool(g_repo
, "core.autocrlf", false);
30 git_checkout_head(g_repo
, &opts
);
32 git_repository_index(&index
, g_repo
);
34 cl_assert((entry
= git_index_get_bypath(index
, "all-lf", 0)) != NULL
);
35 cl_assert(entry
->file_size
== strlen(ALL_LF_TEXT_RAW
));
37 cl_assert((entry
= git_index_get_bypath(index
, "all-crlf", 0)) != NULL
);
38 cl_assert(entry
->file_size
== strlen(ALL_CRLF_TEXT_RAW
));
40 git_index_free(index
);
43 void test_checkout_crlf__detect_crlf_autocrlf_true(void)
45 git_checkout_opts opts
= GIT_CHECKOUT_OPTS_INIT
;
46 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE_CREATE
;
48 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
50 git_checkout_head(g_repo
, &opts
);
52 if (GIT_EOL_NATIVE
== GIT_EOL_LF
)
53 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
55 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF
);
57 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
60 void test_checkout_crlf__more_lf_autocrlf_true(void)
62 git_checkout_opts opts
= GIT_CHECKOUT_OPTS_INIT
;
63 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE_CREATE
;
65 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
67 git_checkout_head(g_repo
, &opts
);
69 if (GIT_EOL_NATIVE
== GIT_EOL_LF
)
70 check_file_contents("./crlf/more-lf", MORE_LF_TEXT_RAW
);
72 check_file_contents("./crlf/more-lf", MORE_LF_TEXT_AS_CRLF
);
75 void test_checkout_crlf__more_crlf_autocrlf_true(void)
77 git_checkout_opts opts
= GIT_CHECKOUT_OPTS_INIT
;
78 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE_CREATE
;
80 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
82 git_checkout_head(g_repo
, &opts
);
84 if (GIT_EOL_NATIVE
== GIT_EOL_LF
)
85 check_file_contents("./crlf/more-crlf", MORE_CRLF_TEXT_RAW
);
87 check_file_contents("./crlf/more-crlf", MORE_CRLF_TEXT_AS_CRLF
);
90 void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void)
93 const git_index_entry
*entry
;
94 git_checkout_opts opts
= GIT_CHECKOUT_OPTS_INIT
;
95 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE_CREATE
;
97 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
99 git_checkout_head(g_repo
, &opts
);
101 git_repository_index(&index
, g_repo
);
103 cl_assert((entry
= git_index_get_bypath(index
, "all-lf", 0)) != NULL
);
105 if (GIT_EOL_NATIVE
== GIT_EOL_LF
)
106 cl_assert_equal_sz(strlen(ALL_LF_TEXT_RAW
), entry
->file_size
);
108 cl_assert_equal_sz(strlen(ALL_LF_TEXT_AS_CRLF
), entry
->file_size
);
110 cl_assert((entry
= git_index_get_bypath(index
, "all-crlf", 0)) != NULL
);
111 cl_assert_equal_sz(strlen(ALL_CRLF_TEXT_RAW
), entry
->file_size
);
113 git_index_free(index
);
116 void test_checkout_crlf__with_ident(void)
120 git_checkout_opts opts
= GIT_CHECKOUT_OPTS_INIT
;
121 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE_CREATE
;
123 cl_git_mkfile("crlf/.gitattributes",
124 "*.txt text\n*.bin binary\n"
125 "*.crlf text eol=crlf\n"
127 "*.ident text ident\n"
128 "*.identcrlf ident text eol=crlf\n"
129 "*.identlf ident text eol=lf\n");
131 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
133 /* add files with $Id$ */
135 cl_git_mkfile("crlf/lf.ident", ALL_LF_TEXT_RAW
"\n$Id: initial content$\n");
136 cl_git_mkfile("crlf/crlf.ident", ALL_CRLF_TEXT_RAW
"\r\n$Id$\r\n\r\n");
137 cl_git_mkfile("crlf/more1.identlf", "$Id$\n" MORE_LF_TEXT_RAW
);
138 cl_git_mkfile("crlf/more2.identcrlf", "\r\n$Id: \f$\r\n" MORE_CRLF_TEXT_RAW
);
140 cl_git_pass(git_repository_index(&index
, g_repo
));
141 cl_git_pass(git_index_add_bypath(index
, "lf.ident"));
142 cl_git_pass(git_index_add_bypath(index
, "crlf.ident"));
143 cl_git_pass(git_index_add_bypath(index
, "more1.identlf"));
144 cl_git_pass(git_index_add_bypath(index
, "more2.identcrlf"));
145 cl_repo_commit_from_index(NULL
, g_repo
, NULL
, 0, "Some ident files\n");
147 git_checkout_head(g_repo
, &opts
);
149 /* check that blobs have $Id$ */
151 cl_git_pass(git_blob_lookup(&blob
, g_repo
,
152 & git_index_get_bypath(index
, "lf.ident", 0)->id
));
154 ALL_LF_TEXT_RAW
"\n$Id$\n", git_blob_rawcontent(blob
));
157 cl_git_pass(git_blob_lookup(&blob
, g_repo
,
158 & git_index_get_bypath(index
, "more2.identcrlf", 0)->id
));
160 "\n$Id$\n" MORE_CRLF_TEXT_AS_LF
, git_blob_rawcontent(blob
));
163 /* check that filesystem is initially untouched - matching core Git */
165 cl_assert_equal_file(
166 ALL_LF_TEXT_RAW
"\n$Id: initial content$\n", 0, "crlf/lf.ident");
168 /* check that forced checkout rewrites correctly */
170 p_unlink("crlf/lf.ident");
171 p_unlink("crlf/crlf.ident");
172 p_unlink("crlf/more1.identlf");
173 p_unlink("crlf/more2.identcrlf");
175 git_checkout_head(g_repo
, &opts
);
177 if (GIT_EOL_NATIVE
== GIT_EOL_LF
) {
178 cl_assert_equal_file(
180 "\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467$\n",
182 cl_assert_equal_file(
184 "\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857$\n\n",
185 0, "crlf/crlf.ident");
187 cl_assert_equal_file(
189 "\r\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467$\r\n",
191 cl_assert_equal_file(
193 "\r\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857$\r\n\r\n",
194 0, "crlf/crlf.ident");
197 cl_assert_equal_file(
198 "$Id: f7830382dac1f1583422be5530fdfbd26289431b$\n"
199 MORE_LF_TEXT_AS_LF
, 0, "crlf/more1.identlf");
201 cl_assert_equal_file(
202 "\r\n$Id: 74677a68413012ce8d7e7cfc3f12603df3a3eac4$\r\n"
203 MORE_CRLF_TEXT_AS_CRLF
, 0, "crlf/more2.identcrlf");
205 git_index_free(index
);
208 void test_checkout_crlf__autocrlf_false_no_attrs(void)
210 git_checkout_opts opts
= GIT_CHECKOUT_OPTS_INIT
;
211 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE_CREATE
;
213 cl_repo_set_bool(g_repo
, "core.autocrlf", false);
215 git_checkout_head(g_repo
, &opts
);
217 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
218 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
221 void test_checkout_crlf__autocrlf_true_no_attrs(void)
223 git_checkout_opts opts
= GIT_CHECKOUT_OPTS_INIT
;
224 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE_CREATE
;
226 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
228 git_checkout_head(g_repo
, &opts
);
230 if (GIT_EOL_NATIVE
== GIT_EOL_CRLF
) {
231 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF
);
232 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF
);
234 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
235 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
239 void test_checkout_crlf__autocrlf_input_no_attrs(void)
241 git_checkout_opts opts
= GIT_CHECKOUT_OPTS_INIT
;
242 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE_CREATE
;
244 cl_repo_set_string(g_repo
, "core.autocrlf", "input");
246 git_checkout_head(g_repo
, &opts
);
248 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
249 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
252 void test_checkout_crlf__autocrlf_false_text_auto_attr(void)
254 git_checkout_opts opts
= GIT_CHECKOUT_OPTS_INIT
;
255 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE_CREATE
;
257 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
259 cl_repo_set_bool(g_repo
, "core.autocrlf", false);
261 git_checkout_head(g_repo
, &opts
);
263 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
264 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
267 void test_checkout_crlf__autocrlf_true_text_auto_attr(void)
269 git_checkout_opts opts
= GIT_CHECKOUT_OPTS_INIT
;
270 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE_CREATE
;
272 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
274 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
276 git_checkout_head(g_repo
, &opts
);
278 if (GIT_EOL_NATIVE
== GIT_EOL_CRLF
) {
279 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF
);
280 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF
);
282 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
283 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);
287 void test_checkout_crlf__autocrlf_input_text_auto_attr(void)
289 git_checkout_opts opts
= GIT_CHECKOUT_OPTS_INIT
;
290 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE_CREATE
;
292 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
294 cl_repo_set_string(g_repo
, "core.autocrlf", "input");
296 git_checkout_head(g_repo
, &opts
);
298 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW
);
299 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW
);