]> git.proxmox.com Git - libgit2.git/blob - tests/checkout/crlf.c
Merge pull request #2144 from linquize/branch-f-current
[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__autocrlf_false_index_size_is_unfiltered_size(void)
22 {
23 git_index *index;
24 const git_index_entry *entry;
25 git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
26 opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
27
28 cl_repo_set_bool(g_repo, "core.autocrlf", false);
29
30 git_checkout_head(g_repo, &opts);
31
32 git_repository_index(&index, g_repo);
33
34 cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);
35 cl_assert(entry->file_size == strlen(ALL_LF_TEXT_RAW));
36
37 cl_assert((entry = git_index_get_bypath(index, "all-crlf", 0)) != NULL);
38 cl_assert(entry->file_size == strlen(ALL_CRLF_TEXT_RAW));
39
40 git_index_free(index);
41 }
42
43 void test_checkout_crlf__detect_crlf_autocrlf_true(void)
44 {
45 git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
46 opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
47
48 cl_repo_set_bool(g_repo, "core.autocrlf", true);
49
50 git_checkout_head(g_repo, &opts);
51
52 if (GIT_EOL_NATIVE == GIT_EOL_LF)
53 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
54 else
55 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
56
57 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
58 }
59
60 void test_checkout_crlf__more_lf_autocrlf_true(void)
61 {
62 git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
63 opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
64
65 cl_repo_set_bool(g_repo, "core.autocrlf", true);
66
67 git_checkout_head(g_repo, &opts);
68
69 if (GIT_EOL_NATIVE == GIT_EOL_LF)
70 check_file_contents("./crlf/more-lf", MORE_LF_TEXT_RAW);
71 else
72 check_file_contents("./crlf/more-lf", MORE_LF_TEXT_AS_CRLF);
73 }
74
75 void test_checkout_crlf__more_crlf_autocrlf_true(void)
76 {
77 git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
78 opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
79
80 cl_repo_set_bool(g_repo, "core.autocrlf", true);
81
82 git_checkout_head(g_repo, &opts);
83
84 if (GIT_EOL_NATIVE == GIT_EOL_LF)
85 check_file_contents("./crlf/more-crlf", MORE_CRLF_TEXT_RAW);
86 else
87 check_file_contents("./crlf/more-crlf", MORE_CRLF_TEXT_AS_CRLF);
88 }
89
90 void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void)
91 {
92 git_index *index;
93 const git_index_entry *entry;
94 git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
95 opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
96
97 cl_repo_set_bool(g_repo, "core.autocrlf", true);
98
99 git_checkout_head(g_repo, &opts);
100
101 git_repository_index(&index, g_repo);
102
103 cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);
104
105 if (GIT_EOL_NATIVE == GIT_EOL_LF)
106 cl_assert_equal_sz(strlen(ALL_LF_TEXT_RAW), entry->file_size);
107 else
108 cl_assert_equal_sz(strlen(ALL_LF_TEXT_AS_CRLF), entry->file_size);
109
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);
112
113 git_index_free(index);
114 }
115
116 void test_checkout_crlf__with_ident(void)
117 {
118 git_index *index;
119 git_blob *blob;
120 git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
121 opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
122
123 cl_git_mkfile("crlf/.gitattributes",
124 "*.txt text\n*.bin binary\n"
125 "*.crlf text eol=crlf\n"
126 "*.lf text eol=lf\n"
127 "*.ident text ident\n"
128 "*.identcrlf ident text eol=crlf\n"
129 "*.identlf ident text eol=lf\n");
130
131 cl_repo_set_bool(g_repo, "core.autocrlf", true);
132
133 /* add files with $Id$ */
134
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);
139
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");
146
147 git_checkout_head(g_repo, &opts);
148
149 /* check that blobs have $Id$ */
150
151 cl_git_pass(git_blob_lookup(&blob, g_repo,
152 & git_index_get_bypath(index, "lf.ident", 0)->id));
153 cl_assert_equal_s(
154 ALL_LF_TEXT_RAW "\n$Id$\n", git_blob_rawcontent(blob));
155 git_blob_free(blob);
156
157 cl_git_pass(git_blob_lookup(&blob, g_repo,
158 & git_index_get_bypath(index, "more2.identcrlf", 0)->id));
159 cl_assert_equal_s(
160 "\n$Id$\n" MORE_CRLF_TEXT_AS_LF, git_blob_rawcontent(blob));
161 git_blob_free(blob);
162
163 /* check that filesystem is initially untouched - matching core Git */
164
165 cl_assert_equal_file(
166 ALL_LF_TEXT_RAW "\n$Id: initial content$\n", 0, "crlf/lf.ident");
167
168 /* check that forced checkout rewrites correctly */
169
170 p_unlink("crlf/lf.ident");
171 p_unlink("crlf/crlf.ident");
172 p_unlink("crlf/more1.identlf");
173 p_unlink("crlf/more2.identcrlf");
174
175 git_checkout_head(g_repo, &opts);
176
177 if (GIT_EOL_NATIVE == GIT_EOL_LF) {
178 cl_assert_equal_file(
179 ALL_LF_TEXT_RAW
180 "\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467$\n",
181 0, "crlf/lf.ident");
182 cl_assert_equal_file(
183 ALL_CRLF_TEXT_AS_LF
184 "\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857$\n\n",
185 0, "crlf/crlf.ident");
186 } else {
187 cl_assert_equal_file(
188 ALL_LF_TEXT_AS_CRLF
189 "\r\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467$\r\n",
190 0, "crlf/lf.ident");
191 cl_assert_equal_file(
192 ALL_CRLF_TEXT_RAW
193 "\r\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857$\r\n\r\n",
194 0, "crlf/crlf.ident");
195 }
196
197 cl_assert_equal_file(
198 "$Id: f7830382dac1f1583422be5530fdfbd26289431b$\n"
199 MORE_LF_TEXT_AS_LF, 0, "crlf/more1.identlf");
200
201 cl_assert_equal_file(
202 "\r\n$Id: 74677a68413012ce8d7e7cfc3f12603df3a3eac4$\r\n"
203 MORE_CRLF_TEXT_AS_CRLF, 0, "crlf/more2.identcrlf");
204
205 git_index_free(index);
206 }
207
208 void test_checkout_crlf__autocrlf_false_no_attrs(void)
209 {
210 git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
211 opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
212
213 cl_repo_set_bool(g_repo, "core.autocrlf", false);
214
215 git_checkout_head(g_repo, &opts);
216
217 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
218 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
219 }
220
221 void test_checkout_crlf__autocrlf_true_no_attrs(void)
222 {
223 git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
224 opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
225
226 cl_repo_set_bool(g_repo, "core.autocrlf", true);
227
228 git_checkout_head(g_repo, &opts);
229
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);
233 } else {
234 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
235 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
236 }
237 }
238
239 void test_checkout_crlf__autocrlf_input_no_attrs(void)
240 {
241 git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
242 opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
243
244 cl_repo_set_string(g_repo, "core.autocrlf", "input");
245
246 git_checkout_head(g_repo, &opts);
247
248 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
249 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
250 }
251
252 void test_checkout_crlf__autocrlf_false_text_auto_attr(void)
253 {
254 git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
255 opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
256
257 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
258
259 cl_repo_set_bool(g_repo, "core.autocrlf", false);
260
261 git_checkout_head(g_repo, &opts);
262
263 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
264 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
265 }
266
267 void test_checkout_crlf__autocrlf_true_text_auto_attr(void)
268 {
269 git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
270 opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
271
272 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
273
274 cl_repo_set_bool(g_repo, "core.autocrlf", true);
275
276 git_checkout_head(g_repo, &opts);
277
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);
281 } else {
282 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
283 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
284 }
285 }
286
287 void test_checkout_crlf__autocrlf_input_text_auto_attr(void)
288 {
289 git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
290 opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
291
292 cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
293
294 cl_repo_set_string(g_repo, "core.autocrlf", "input");
295
296 git_checkout_head(g_repo, &opts);
297
298 check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
299 check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
300 }