]> git.proxmox.com Git - libgit2.git/blob - tests/checkout/crlf.c
Merge branch 'cmn/zlib-update' into cmn/update-zlib
[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_SAFE_CREATE;
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_SAFE_CREATE;
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_SAFE_CREATE;
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_SAFE_CREATE;
77
78 cl_repo_set_bool(g_repo, "core.autocrlf", true);
79
80 git_checkout_head(g_repo, &opts);
81
82 if (GIT_EOL_NATIVE == GIT_EOL_LF)
83 check_file_contents("./crlf/more-lf", MORE_LF_TEXT_RAW);
84 else
85 check_file_contents("./crlf/more-lf", MORE_LF_TEXT_AS_CRLF);
86 }
87
88 void test_checkout_crlf__more_crlf_autocrlf_true(void)
89 {
90 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
91 opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
92
93 cl_repo_set_bool(g_repo, "core.autocrlf", true);
94
95 git_checkout_head(g_repo, &opts);
96
97 if (GIT_EOL_NATIVE == GIT_EOL_LF)
98 check_file_contents("./crlf/more-crlf", MORE_CRLF_TEXT_RAW);
99 else
100 check_file_contents("./crlf/more-crlf", MORE_CRLF_TEXT_AS_CRLF);
101 }
102
103 void test_checkout_crlf__all_crlf_autocrlf_true(void)
104 {
105 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
106 opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
107
108 cl_repo_set_bool(g_repo, "core.autocrlf", true);
109
110 git_checkout_head(g_repo, &opts);
111
112 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
113 }
114
115 void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void)
116 {
117 git_index *index;
118 const git_index_entry *entry;
119 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
120 opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
121
122 cl_repo_set_bool(g_repo, "core.autocrlf", true);
123
124 git_checkout_head(g_repo, &opts);
125
126 git_repository_index(&index, g_repo);
127
128 cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);
129
130 if (GIT_EOL_NATIVE == GIT_EOL_LF)
131 cl_assert_equal_sz(strlen(ALL_LF_TEXT_RAW), entry->file_size);
132 else
133 cl_assert_equal_sz(strlen(ALL_LF_TEXT_AS_CRLF), entry->file_size);
134
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);
137
138 git_index_free(index);
139 }
140
141 void test_checkout_crlf__with_ident(void)
142 {
143 git_index *index;
144 git_blob *blob;
145 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
146 opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
147
148 cl_git_mkfile("crlf/.gitattributes",
149 "*.txt text\n*.bin binary\n"
150 "*.crlf text eol=crlf\n"
151 "*.lf text eol=lf\n"
152 "*.ident text ident\n"
153 "*.identcrlf ident text eol=crlf\n"
154 "*.identlf ident text eol=lf\n");
155
156 cl_repo_set_bool(g_repo, "core.autocrlf", true);
157
158 /* add files with $Id$ */
159
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);
164
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");
171
172 git_checkout_head(g_repo, &opts);
173
174 /* check that blobs have $Id$ */
175
176 cl_git_pass(git_blob_lookup(&blob, g_repo,
177 & git_index_get_bypath(index, "lf.ident", 0)->id));
178 cl_assert_equal_s(
179 ALL_LF_TEXT_RAW "\n$Id$\n", git_blob_rawcontent(blob));
180 git_blob_free(blob);
181
182 cl_git_pass(git_blob_lookup(&blob, g_repo,
183 & git_index_get_bypath(index, "more2.identcrlf", 0)->id));
184 cl_assert_equal_s(
185 "\n$Id$\n" MORE_CRLF_TEXT_AS_LF, git_blob_rawcontent(blob));
186 git_blob_free(blob);
187
188 /* check that filesystem is initially untouched - matching core Git */
189
190 cl_assert_equal_file(
191 ALL_LF_TEXT_RAW "\n$Id: initial content$\n", 0, "crlf/lf.ident");
192
193 /* check that forced checkout rewrites correctly */
194
195 p_unlink("crlf/lf.ident");
196 p_unlink("crlf/crlf.ident");
197 p_unlink("crlf/more1.identlf");
198 p_unlink("crlf/more2.identcrlf");
199
200 git_checkout_head(g_repo, &opts);
201
202 if (GIT_EOL_NATIVE == GIT_EOL_LF) {
203 cl_assert_equal_file(
204 ALL_LF_TEXT_RAW
205 "\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467$\n",
206 0, "crlf/lf.ident");
207 cl_assert_equal_file(
208 ALL_CRLF_TEXT_AS_LF
209 "\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857$\n\n",
210 0, "crlf/crlf.ident");
211 } else {
212 cl_assert_equal_file(
213 ALL_LF_TEXT_AS_CRLF
214 "\r\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467$\r\n",
215 0, "crlf/lf.ident");
216 cl_assert_equal_file(
217 ALL_CRLF_TEXT_RAW
218 "\r\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857$\r\n\r\n",
219 0, "crlf/crlf.ident");
220 }
221
222 cl_assert_equal_file(
223 "$Id: f7830382dac1f1583422be5530fdfbd26289431b$\n"
224 MORE_LF_TEXT_AS_LF, 0, "crlf/more1.identlf");
225
226 cl_assert_equal_file(
227 "\r\n$Id: 74677a68413012ce8d7e7cfc3f12603df3a3eac4$\r\n"
228 MORE_CRLF_TEXT_AS_CRLF, 0, "crlf/more2.identcrlf");
229
230 git_index_free(index);
231 }
232
233 void test_checkout_crlf__autocrlf_false_no_attrs(void)
234 {
235 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
236 opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
237
238 cl_repo_set_bool(g_repo, "core.autocrlf", false);
239
240 git_checkout_head(g_repo, &opts);
241
242 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
243 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
244 }
245
246 void test_checkout_crlf__autocrlf_true_no_attrs(void)
247 {
248 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
249 opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
250
251 cl_repo_set_bool(g_repo, "core.autocrlf", true);
252
253 git_checkout_head(g_repo, &opts);
254
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);
258 } else {
259 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
260 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
261 }
262 }
263
264 void test_checkout_crlf__autocrlf_input_no_attrs(void)
265 {
266 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
267 opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
268
269 cl_repo_set_string(g_repo, "core.autocrlf", "input");
270
271 git_checkout_head(g_repo, &opts);
272
273 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
274 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
275 }
276
277 void test_checkout_crlf__autocrlf_false_text_auto_attr(void)
278 {
279 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
280 opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
281
282 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
283
284 cl_repo_set_bool(g_repo, "core.autocrlf", false);
285
286 git_checkout_head(g_repo, &opts);
287
288 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
289 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
290 }
291
292 void test_checkout_crlf__autocrlf_true_text_auto_attr(void)
293 {
294 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
295 opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
296
297 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
298
299 cl_repo_set_bool(g_repo, "core.autocrlf", true);
300
301 git_checkout_head(g_repo, &opts);
302
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);
306 } else {
307 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
308 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
309 }
310 }
311
312 void test_checkout_crlf__autocrlf_input_text_auto_attr(void)
313 {
314 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
315 opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
316
317 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
318
319 cl_repo_set_string(g_repo, "core.autocrlf", "input");
320
321 git_checkout_head(g_repo, &opts);
322
323 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
324 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
325 }