]> git.proxmox.com Git - libgit2.git/blob - tests/checkout/crlf.c
Merge pull request #3127 from libgit2/cmn/remote-fixups
[libgit2.git] / tests / checkout / crlf.c
1 #include "clar_libgit2.h"
2 #include "checkout_helpers.h"
3 #include "../filter/crlf.h"
4
5 #include "git2/checkout.h"
6 #include "repository.h"
7 #include "posix.h"
8
9 static git_repository *g_repo;
10
11 void test_checkout_crlf__initialize(void)
12 {
13 g_repo = cl_git_sandbox_init("crlf");
14 }
15
16 void test_checkout_crlf__cleanup(void)
17 {
18 cl_git_sandbox_cleanup();
19 }
20
21 void test_checkout_crlf__detect_crlf_autocrlf_false(void)
22 {
23 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
24 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
25
26 cl_repo_set_bool(g_repo, "core.autocrlf", false);
27
28 git_checkout_head(g_repo, &opts);
29
30 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
31 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
32 }
33
34 void test_checkout_crlf__autocrlf_false_index_size_is_unfiltered_size(void)
35 {
36 git_index *index;
37 const git_index_entry *entry;
38 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
39 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
40
41 cl_repo_set_bool(g_repo, "core.autocrlf", false);
42
43 git_checkout_head(g_repo, &opts);
44
45 git_repository_index(&index, g_repo);
46
47 cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);
48 cl_assert(entry->file_size == strlen(ALL_LF_TEXT_RAW));
49
50 cl_assert((entry = git_index_get_bypath(index, "all-crlf", 0)) != NULL);
51 cl_assert(entry->file_size == strlen(ALL_CRLF_TEXT_RAW));
52
53 git_index_free(index);
54 }
55
56 void test_checkout_crlf__detect_crlf_autocrlf_true(void)
57 {
58 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
59 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
60
61 cl_repo_set_bool(g_repo, "core.autocrlf", true);
62
63 git_checkout_head(g_repo, &opts);
64
65 if (GIT_EOL_NATIVE == GIT_EOL_LF)
66 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
67 else
68 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
69
70 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
71 }
72
73 void test_checkout_crlf__more_lf_autocrlf_true(void)
74 {
75 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
76 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
77
78 cl_repo_set_bool(g_repo, "core.autocrlf", true);
79
80 git_checkout_head(g_repo, &opts);
81
82 check_file_contents("./crlf/more-lf", MORE_LF_TEXT_RAW);
83 }
84
85 void test_checkout_crlf__more_crlf_autocrlf_true(void)
86 {
87 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
88 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
89
90 cl_repo_set_bool(g_repo, "core.autocrlf", true);
91
92 git_checkout_head(g_repo, &opts);
93
94 check_file_contents("./crlf/more-crlf", MORE_CRLF_TEXT_RAW);
95 }
96
97 void test_checkout_crlf__all_crlf_autocrlf_true(void)
98 {
99 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
100 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
101
102 cl_repo_set_bool(g_repo, "core.autocrlf", true);
103
104 git_checkout_head(g_repo, &opts);
105
106 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
107 }
108
109 void test_checkout_crlf__detect_crlf_autocrlf_true_utf8(void)
110 {
111 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
112 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
113
114 cl_repo_set_bool(g_repo, "core.autocrlf", true);
115
116 git_repository_set_head(g_repo, "refs/heads/utf8");
117 git_checkout_head(g_repo, &opts);
118
119 if (GIT_EOL_NATIVE == GIT_EOL_LF)
120 {
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);
123 }
124 else
125 {
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);
128 }
129
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);
132 }
133
134 void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void)
135 {
136 git_index *index;
137 const git_index_entry *entry;
138 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
139 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
140
141 cl_repo_set_bool(g_repo, "core.autocrlf", true);
142
143 git_checkout_head(g_repo, &opts);
144
145 git_repository_index(&index, g_repo);
146
147 cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);
148
149 if (GIT_EOL_NATIVE == GIT_EOL_LF)
150 cl_assert_equal_sz(strlen(ALL_LF_TEXT_RAW), entry->file_size);
151 else
152 cl_assert_equal_sz(strlen(ALL_LF_TEXT_AS_CRLF), entry->file_size);
153
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);
156
157 git_index_free(index);
158 }
159
160 void test_checkout_crlf__with_ident(void)
161 {
162 git_index *index;
163 git_blob *blob;
164 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
165 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
166
167 cl_git_mkfile("crlf/.gitattributes",
168 "*.txt text\n*.bin binary\n"
169 "*.crlf text eol=crlf\n"
170 "*.lf text eol=lf\n"
171 "*.ident text ident\n"
172 "*.identcrlf ident text eol=crlf\n"
173 "*.identlf ident text eol=lf\n");
174
175 cl_repo_set_bool(g_repo, "core.autocrlf", true);
176
177 /* add files with $Id$ */
178
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);
183
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");
190
191 git_checkout_head(g_repo, &opts);
192
193 /* check that blobs have $Id$ */
194
195 cl_git_pass(git_blob_lookup(&blob, g_repo,
196 & git_index_get_bypath(index, "lf.ident", 0)->id));
197 cl_assert_equal_s(
198 ALL_LF_TEXT_RAW "\n$Id$\n", git_blob_rawcontent(blob));
199 git_blob_free(blob);
200
201 cl_git_pass(git_blob_lookup(&blob, g_repo,
202 & git_index_get_bypath(index, "more2.identcrlf", 0)->id));
203 cl_assert_equal_s(
204 "\n$Id$\n" MORE_CRLF_TEXT_AS_LF, git_blob_rawcontent(blob));
205 git_blob_free(blob);
206
207 /* check that filesystem is initially untouched - matching core Git */
208
209 cl_assert_equal_file(
210 ALL_LF_TEXT_RAW "\n$Id: initial content$\n", 0, "crlf/lf.ident");
211
212 /* check that forced checkout rewrites correctly */
213
214 p_unlink("crlf/lf.ident");
215 p_unlink("crlf/crlf.ident");
216 p_unlink("crlf/more1.identlf");
217 p_unlink("crlf/more2.identcrlf");
218
219 git_checkout_head(g_repo, &opts);
220
221 if (GIT_EOL_NATIVE == GIT_EOL_LF) {
222 cl_assert_equal_file(
223 ALL_LF_TEXT_RAW
224 "\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467 $\n",
225 0, "crlf/lf.ident");
226 cl_assert_equal_file(
227 ALL_CRLF_TEXT_AS_LF
228 "\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857 $\n\n",
229 0, "crlf/crlf.ident");
230 } else {
231 cl_assert_equal_file(
232 ALL_LF_TEXT_AS_CRLF
233 "\r\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467 $\r\n",
234 0, "crlf/lf.ident");
235 cl_assert_equal_file(
236 ALL_CRLF_TEXT_RAW
237 "\r\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857 $\r\n\r\n",
238 0, "crlf/crlf.ident");
239 }
240
241 cl_assert_equal_file(
242 "$Id: f7830382dac1f1583422be5530fdfbd26289431b $\n"
243 MORE_LF_TEXT_AS_LF, 0, "crlf/more1.identlf");
244
245 cl_assert_equal_file(
246 "\r\n$Id: 74677a68413012ce8d7e7cfc3f12603df3a3eac4 $\r\n"
247 MORE_CRLF_TEXT_AS_CRLF, 0, "crlf/more2.identcrlf");
248
249 git_index_free(index);
250 }
251
252 void test_checkout_crlf__autocrlf_false_no_attrs(void)
253 {
254 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
255 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
256
257 cl_repo_set_bool(g_repo, "core.autocrlf", false);
258
259 git_checkout_head(g_repo, &opts);
260
261 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
262 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
263 }
264
265 void test_checkout_crlf__autocrlf_true_no_attrs(void)
266 {
267 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
268 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
269
270 cl_repo_set_bool(g_repo, "core.autocrlf", true);
271
272 git_checkout_head(g_repo, &opts);
273
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);
277 } else {
278 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
279 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
280 }
281 }
282
283 void test_checkout_crlf__autocrlf_input_no_attrs(void)
284 {
285 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
286 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
287
288 cl_repo_set_string(g_repo, "core.autocrlf", "input");
289
290 git_checkout_head(g_repo, &opts);
291
292 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
293 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
294 }
295
296 void test_checkout_crlf__autocrlf_false_text_auto_attr(void)
297 {
298 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
299 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
300
301 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
302
303 cl_repo_set_bool(g_repo, "core.autocrlf", false);
304
305 git_checkout_head(g_repo, &opts);
306
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);
310 } else {
311 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
312 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
313 }
314 }
315
316 void test_checkout_crlf__autocrlf_true_text_auto_attr(void)
317 {
318 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
319 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
320
321 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
322
323 cl_repo_set_bool(g_repo, "core.autocrlf", true);
324
325 git_checkout_head(g_repo, &opts);
326
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);
330 } else {
331 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
332 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
333 }
334 }
335
336 void test_checkout_crlf__autocrlf_input_text_auto_attr(void)
337 {
338 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
339 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
340
341 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
342
343 cl_repo_set_string(g_repo, "core.autocrlf", "input");
344
345 git_checkout_head(g_repo, &opts);
346
347 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
348 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
349 }
350
351 void test_checkout_crlf__can_write_empty_file(void)
352 {
353 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
354 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
355
356 cl_repo_set_bool(g_repo, "core.autocrlf", true);
357
358 git_repository_set_head(g_repo, "refs/heads/empty-files");
359 git_checkout_head(g_repo, &opts);
360
361 check_file_contents("./crlf/test1.txt", "");
362
363 if (GIT_EOL_NATIVE == GIT_EOL_LF)
364 check_file_contents("./crlf/test2.txt", "test2.txt's content\n");
365 else
366 check_file_contents("./crlf/test2.txt", "test2.txt's content\r\n");
367
368 check_file_contents("./crlf/test3.txt", "");
369 }