]> git.ipfire.org Git - thirdparty/git.git/blob - t/t0020-crlf.sh
The third batch
[thirdparty/git.git] / t / t0020-crlf.sh
1 #!/bin/sh
2
3 test_description='CRLF conversion'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
10
11 has_cr() {
12 tr '\015' Q <"$1" | grep Q >/dev/null
13 }
14
15 # add or remove CRs to disk file in-place
16 # usage: munge_cr <append|remove> <file>
17 munge_cr () {
18 "${1}_cr" <"$2" >tmp &&
19 mv tmp "$2"
20 }
21
22 test_expect_success setup '
23
24 git config core.autocrlf false &&
25
26 test_write_lines Hello world how are you >one &&
27 mkdir dir &&
28 test_write_lines I am very very fine thank you >dir/two &&
29 test_write_lines Oh here is NULQin text here | q_to_nul >three &&
30 git add . &&
31
32 git commit -m initial &&
33
34 one=$(git rev-parse HEAD:one) &&
35 dir=$(git rev-parse HEAD:dir) &&
36 two=$(git rev-parse HEAD:dir/two) &&
37 three=$(git rev-parse HEAD:three) &&
38
39 test_write_lines Some extra lines here >>one &&
40 git diff >patch.file &&
41 patched=$(git hash-object --stdin <one) &&
42 git read-tree --reset -u HEAD
43 '
44
45 test_expect_success 'safecrlf: autocrlf=input, all CRLF' '
46
47 git config core.autocrlf input &&
48 git config core.safecrlf true &&
49
50 test_write_lines I am all CRLF | append_cr >allcrlf &&
51 test_must_fail git add allcrlf
52 '
53
54 test_expect_success 'safecrlf: autocrlf=input, mixed LF/CRLF' '
55
56 git config core.autocrlf input &&
57 git config core.safecrlf true &&
58
59 test_write_lines Oh here is CRLFQ in text | q_to_cr >mixed &&
60 test_must_fail git add mixed
61 '
62
63 test_expect_success 'safecrlf: autocrlf=true, all LF' '
64
65 git config core.autocrlf true &&
66 git config core.safecrlf true &&
67
68 test_write_lines I am all LF >alllf &&
69 test_must_fail git add alllf
70 '
71
72 test_expect_success 'safecrlf: autocrlf=true mixed LF/CRLF' '
73
74 git config core.autocrlf true &&
75 git config core.safecrlf true &&
76
77 test_write_lines Oh here is CRLFQ in text | q_to_cr >mixed &&
78 test_must_fail git add mixed
79 '
80
81 test_expect_success 'safecrlf: print warning only once' '
82
83 git config core.autocrlf input &&
84 git config core.safecrlf warn &&
85
86 test_write_lines I am all LF >doublewarn &&
87 git add doublewarn &&
88 git commit -m "nowarn" &&
89 test_write_lines Oh here is CRLFQ in text | q_to_cr >doublewarn &&
90 git add doublewarn 2>err &&
91 grep "CRLF will be replaced by LF" err >err.warnings &&
92 test_line_count = 1 err.warnings
93 '
94
95
96 test_expect_success 'safecrlf: git diff demotes safecrlf=true to warn' '
97 git config core.autocrlf input &&
98 git config core.safecrlf true &&
99 git diff HEAD
100 '
101
102
103 test_expect_success 'safecrlf: no warning with safecrlf=false' '
104 git config core.autocrlf input &&
105 git config core.safecrlf false &&
106
107 test_write_lines I am all CRLF | append_cr >allcrlf &&
108 git add allcrlf 2>err &&
109 test_must_be_empty err
110 '
111
112
113 test_expect_success 'switch off autocrlf, safecrlf, reset HEAD' '
114 git config core.autocrlf false &&
115 git config core.safecrlf false &&
116 git reset --hard HEAD^
117 '
118
119 test_expect_success 'update with autocrlf=input' '
120
121 rm -f tmp one dir/two three &&
122 git read-tree --reset -u HEAD &&
123 git config core.autocrlf input &&
124 munge_cr append one &&
125 munge_cr append dir/two &&
126 git update-index -- one dir/two &&
127 differs=$(git diff-index --cached HEAD) &&
128 test -z "$differs"
129
130 '
131
132 test_expect_success 'update with autocrlf=true' '
133
134 rm -f tmp one dir/two three &&
135 git read-tree --reset -u HEAD &&
136 git config core.autocrlf true &&
137 munge_cr append one &&
138 munge_cr append dir/two &&
139 git update-index -- one dir/two &&
140 differs=$(git diff-index --cached HEAD) &&
141 test -z "$differs"
142
143 '
144
145 test_expect_success 'checkout with autocrlf=true' '
146
147 rm -f tmp one dir/two three &&
148 git config core.autocrlf true &&
149 git read-tree --reset -u HEAD &&
150 munge_cr remove one &&
151 munge_cr remove dir/two &&
152 git update-index -- one dir/two &&
153 test "$one" = $(git hash-object --stdin <one) &&
154 test "$two" = $(git hash-object --stdin <dir/two) &&
155 differs=$(git diff-index --cached HEAD) &&
156 test -z "$differs"
157 '
158
159 test_expect_success 'checkout with autocrlf=input' '
160
161 rm -f tmp one dir/two three &&
162 git config core.autocrlf input &&
163 git read-tree --reset -u HEAD &&
164 ! has_cr one &&
165 ! has_cr dir/two &&
166 git update-index -- one dir/two &&
167 test "$one" = $(git hash-object --stdin <one) &&
168 test "$two" = $(git hash-object --stdin <dir/two) &&
169 differs=$(git diff-index --cached HEAD) &&
170 test -z "$differs"
171 '
172
173 test_expect_success 'apply patch (autocrlf=input)' '
174
175 rm -f tmp one dir/two three &&
176 git config core.autocrlf input &&
177 git read-tree --reset -u HEAD &&
178
179 git apply patch.file &&
180 test "$patched" = "$(git hash-object --stdin <one)"
181 '
182
183 test_expect_success 'apply patch --cached (autocrlf=input)' '
184
185 rm -f tmp one dir/two three &&
186 git config core.autocrlf input &&
187 git read-tree --reset -u HEAD &&
188
189 git apply --cached patch.file &&
190 test "$patched" = $(git rev-parse :one)
191 '
192
193 test_expect_success 'apply patch --index (autocrlf=input)' '
194
195 rm -f tmp one dir/two three &&
196 git config core.autocrlf input &&
197 git read-tree --reset -u HEAD &&
198
199 git apply --index patch.file &&
200 test "$patched" = $(git rev-parse :one) &&
201 test "$patched" = $(git hash-object --stdin <one)
202 '
203
204 test_expect_success 'apply patch (autocrlf=true)' '
205
206 rm -f tmp one dir/two three &&
207 git config core.autocrlf true &&
208 git read-tree --reset -u HEAD &&
209
210 git apply patch.file &&
211 test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
212 '
213
214 test_expect_success 'apply patch --cached (autocrlf=true)' '
215
216 rm -f tmp one dir/two three &&
217 git config core.autocrlf true &&
218 git read-tree --reset -u HEAD &&
219
220 git apply --cached patch.file &&
221 test "$patched" = $(git rev-parse :one)
222 '
223
224 test_expect_success 'apply patch --index (autocrlf=true)' '
225
226 rm -f tmp one dir/two three &&
227 git config core.autocrlf true &&
228 git read-tree --reset -u HEAD &&
229
230 git apply --index patch.file &&
231 test "$patched" = $(git rev-parse :one) &&
232 test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
233 '
234
235 test_expect_success '.gitattributes says two is binary' '
236
237 rm -f tmp one dir/two three &&
238 echo "two -crlf" >.gitattributes &&
239 git config core.autocrlf true &&
240 git read-tree --reset -u HEAD &&
241
242 ! has_cr dir/two &&
243 has_cr one &&
244 ! has_cr three
245 '
246
247 test_expect_success '.gitattributes says two is input' '
248
249 rm -f tmp one dir/two three &&
250 echo "two crlf=input" >.gitattributes &&
251 git read-tree --reset -u HEAD &&
252
253 ! has_cr dir/two
254 '
255
256 test_expect_success '.gitattributes says two and three are text' '
257
258 rm -f tmp one dir/two three &&
259 echo "t* crlf" >.gitattributes &&
260 git read-tree --reset -u HEAD &&
261
262 has_cr dir/two &&
263 has_cr three
264 '
265
266 test_expect_success 'in-tree .gitattributes (1)' '
267
268 echo "one -crlf" >>.gitattributes &&
269 git add .gitattributes &&
270 git commit -m "Add .gitattributes" &&
271
272 rm -rf tmp one dir .gitattributes patch.file three &&
273 git read-tree --reset -u HEAD &&
274
275 ! has_cr one &&
276 has_cr three
277 '
278
279 test_expect_success 'in-tree .gitattributes (2)' '
280
281 rm -rf tmp one dir .gitattributes patch.file three &&
282 git read-tree --reset HEAD &&
283 git checkout-index -f -q -u -a &&
284
285 ! has_cr one &&
286 has_cr three
287 '
288
289 test_expect_success 'in-tree .gitattributes (3)' '
290
291 rm -rf tmp one dir .gitattributes patch.file three &&
292 git read-tree --reset HEAD &&
293 git checkout-index -u .gitattributes &&
294 git checkout-index -u one dir/two three &&
295
296 ! has_cr one &&
297 has_cr three
298 '
299
300 test_expect_success 'in-tree .gitattributes (4)' '
301
302 rm -rf tmp one dir .gitattributes patch.file three &&
303 git read-tree --reset HEAD &&
304 git checkout-index -u one dir/two three &&
305 git checkout-index -u .gitattributes &&
306
307 ! has_cr one &&
308 has_cr three
309 '
310
311 test_expect_success 'checkout with existing .gitattributes' '
312
313 git config core.autocrlf true &&
314 git config --unset core.safecrlf &&
315 echo ".file2 -crlfQ" | q_to_cr >> .gitattributes &&
316 git add .gitattributes &&
317 git commit -m initial &&
318 echo ".file -crlfQ" | q_to_cr >> .gitattributes &&
319 echo "contents" > .file &&
320 git add .gitattributes .file &&
321 git commit -m second &&
322
323 git checkout main~1 &&
324 git checkout main &&
325 test "$(git diff-files --raw)" = ""
326
327 '
328
329 test_expect_success 'checkout when deleting .gitattributes' '
330
331 git rm .gitattributes &&
332 echo "contentsQ" | q_to_cr > .file2 &&
333 git add .file2 &&
334 git commit -m third &&
335
336 git checkout main~1 &&
337 git checkout main &&
338 has_cr .file2
339
340 '
341
342 test_expect_success 'invalid .gitattributes (must not crash)' '
343
344 echo "three +crlf" >>.gitattributes &&
345 git diff
346
347 '
348 # Some more tests here to add new autocrlf functionality.
349 # We want to have a known state here, so start a bit from scratch
350
351 test_expect_success 'setting up for new autocrlf tests' '
352 git config core.autocrlf false &&
353 git config core.safecrlf false &&
354 rm -rf .????* * &&
355 test_write_lines I am all LF >alllf &&
356 test_write_lines Oh here is CRLFQ in text | q_to_cr >mixed &&
357 test_write_lines I am all CRLF | append_cr >allcrlf &&
358 git add -A . &&
359 git commit -m "alllf, allcrlf and mixed only" &&
360 git tag -a -m "message" autocrlf-checkpoint
361 '
362
363 test_expect_success 'report no change after setting autocrlf' '
364 git config core.autocrlf true &&
365 touch * &&
366 git diff --exit-code
367 '
368
369 test_expect_success 'files are clean after checkout' '
370 rm * &&
371 git checkout -f &&
372 git diff --exit-code
373 '
374
375 cr_to_Q_no_NL () {
376 tr '\015' Q | tr -d '\012'
377 }
378
379 test_expect_success 'LF only file gets CRLF with autocrlf' '
380 test "$(cr_to_Q_no_NL < alllf)" = "IQamQallQLFQ"
381 '
382
383 test_expect_success 'Mixed file is still mixed with autocrlf' '
384 test "$(cr_to_Q_no_NL < mixed)" = "OhhereisCRLFQintext"
385 '
386
387 test_expect_success 'CRLF only file has CRLF with autocrlf' '
388 test "$(cr_to_Q_no_NL < allcrlf)" = "IQamQallQCRLFQ"
389 '
390
391 test_expect_success 'New CRLF file gets LF in repo' '
392 tr -d "\015" < alllf | append_cr > alllf2 &&
393 git add alllf2 &&
394 git commit -m "alllf2 added" &&
395 git config core.autocrlf false &&
396 rm * &&
397 git checkout -f &&
398 test_cmp alllf alllf2
399 '
400
401 test_done