]> git.ipfire.org Git - thirdparty/git.git/blame - t/t0020-crlf.sh
The third batch
[thirdparty/git.git] / t / t0020-crlf.sh
CommitLineData
634ede32
JH
1#!/bin/sh
2
3test_description='CRLF conversion'
4
06d53148 5GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
6export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
9081a421 8TEST_PASSES_SANITIZE_LEAK=true
634ede32
JH
9. ./test-lib.sh
10
c4f3f551
SB
11has_cr() {
12 tr '\015' Q <"$1" | grep Q >/dev/null
634ede32
JH
13}
14
fd777141
JK
15# add or remove CRs to disk file in-place
16# usage: munge_cr <append|remove> <file>
17munge_cr () {
18 "${1}_cr" <"$2" >tmp &&
19 mv tmp "$2"
20}
21
634ede32
JH
22test_expect_success setup '
23
5c66d0d4 24 git config core.autocrlf false &&
634ede32 25
08495412 26 test_write_lines Hello world how are you >one &&
634ede32 27 mkdir dir &&
08495412
ES
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 &&
634ede32
JH
30 git add . &&
31
32 git commit -m initial &&
33
def226bd
EP
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) &&
634ede32 38
08495412 39 test_write_lines Some extra lines here >>one &&
634ede32 40 git diff >patch.file &&
def226bd 41 patched=$(git hash-object --stdin <one) &&
be86fb3f 42 git read-tree --reset -u HEAD
634ede32
JH
43'
44
21e5ad50
SP
45test_expect_success 'safecrlf: autocrlf=input, all CRLF' '
46
47 git config core.autocrlf input &&
48 git config core.safecrlf true &&
49
08495412 50 test_write_lines I am all CRLF | append_cr >allcrlf &&
d492b31c 51 test_must_fail git add allcrlf
21e5ad50
SP
52'
53
54test_expect_success 'safecrlf: autocrlf=input, mixed LF/CRLF' '
55
56 git config core.autocrlf input &&
57 git config core.safecrlf true &&
58
08495412 59 test_write_lines Oh here is CRLFQ in text | q_to_cr >mixed &&
d492b31c 60 test_must_fail git add mixed
21e5ad50
SP
61'
62
63test_expect_success 'safecrlf: autocrlf=true, all LF' '
64
65 git config core.autocrlf true &&
66 git config core.safecrlf true &&
67
08495412 68 test_write_lines I am all LF >alllf &&
d492b31c 69 test_must_fail git add alllf
21e5ad50
SP
70'
71
72test_expect_success 'safecrlf: autocrlf=true mixed LF/CRLF' '
73
74 git config core.autocrlf true &&
75 git config core.safecrlf true &&
76
08495412 77 test_write_lines Oh here is CRLFQ in text | q_to_cr >mixed &&
d492b31c 78 test_must_fail git add mixed
21e5ad50
SP
79'
80
81test_expect_success 'safecrlf: print warning only once' '
82
83 git config core.autocrlf input &&
84 git config core.safecrlf warn &&
85
08495412 86 test_write_lines I am all LF >doublewarn &&
21e5ad50
SP
87 git add doublewarn &&
88 git commit -m "nowarn" &&
08495412 89 test_write_lines Oh here is CRLFQ in text | q_to_cr >doublewarn &&
87cb7845 90 git add doublewarn 2>err &&
b1e07980
ÆAB
91 grep "CRLF will be replaced by LF" err >err.warnings &&
92 test_line_count = 1 err.warnings
21e5ad50
SP
93'
94
5430bb28
JH
95
96test_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
6cb09125
AS
103test_expect_success 'safecrlf: no warning with safecrlf=false' '
104 git config core.autocrlf input &&
105 git config core.safecrlf false &&
106
08495412 107 test_write_lines I am all CRLF | append_cr >allcrlf &&
6cb09125
AS
108 git add allcrlf 2>err &&
109 test_must_be_empty err
110'
111
112
21e5ad50
SP
113test_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
634ede32
JH
119test_expect_success 'update with autocrlf=input' '
120
163b9591 121 rm -f tmp one dir/two three &&
634ede32 122 git read-tree --reset -u HEAD &&
5c66d0d4 123 git config core.autocrlf input &&
fd777141
JK
124 munge_cr append one &&
125 munge_cr append dir/two &&
126 git update-index -- one dir/two &&
def226bd 127 differs=$(git diff-index --cached HEAD) &&
8ddfce71 128 test -z "$differs"
634ede32
JH
129
130'
131
132test_expect_success 'update with autocrlf=true' '
133
163b9591 134 rm -f tmp one dir/two three &&
634ede32 135 git read-tree --reset -u HEAD &&
5c66d0d4 136 git config core.autocrlf true &&
fd777141
JK
137 munge_cr append one &&
138 munge_cr append dir/two &&
139 git update-index -- one dir/two &&
def226bd 140 differs=$(git diff-index --cached HEAD) &&
8ddfce71 141 test -z "$differs"
634ede32
JH
142
143'
144
145test_expect_success 'checkout with autocrlf=true' '
146
163b9591 147 rm -f tmp one dir/two three &&
5c66d0d4 148 git config core.autocrlf true &&
634ede32 149 git read-tree --reset -u HEAD &&
fd777141
JK
150 munge_cr remove one &&
151 munge_cr remove dir/two &&
152 git update-index -- one dir/two &&
def226bd
EP
153 test "$one" = $(git hash-object --stdin <one) &&
154 test "$two" = $(git hash-object --stdin <dir/two) &&
155 differs=$(git diff-index --cached HEAD) &&
8ddfce71 156 test -z "$differs"
634ede32
JH
157'
158
159test_expect_success 'checkout with autocrlf=input' '
160
163b9591 161 rm -f tmp one dir/two three &&
5c66d0d4 162 git config core.autocrlf input &&
634ede32 163 git read-tree --reset -u HEAD &&
f6041abd
DL
164 ! has_cr one &&
165 ! has_cr dir/two &&
fd777141 166 git update-index -- one dir/two &&
def226bd
EP
167 test "$one" = $(git hash-object --stdin <one) &&
168 test "$two" = $(git hash-object --stdin <dir/two) &&
169 differs=$(git diff-index --cached HEAD) &&
8ddfce71 170 test -z "$differs"
634ede32
JH
171'
172
173test_expect_success 'apply patch (autocrlf=input)' '
174
163b9591 175 rm -f tmp one dir/two three &&
5c66d0d4 176 git config core.autocrlf input &&
634ede32
JH
177 git read-tree --reset -u HEAD &&
178
179 git apply patch.file &&
8ddfce71 180 test "$patched" = "$(git hash-object --stdin <one)"
634ede32
JH
181'
182
183test_expect_success 'apply patch --cached (autocrlf=input)' '
184
163b9591 185 rm -f tmp one dir/two three &&
5c66d0d4 186 git config core.autocrlf input &&
634ede32
JH
187 git read-tree --reset -u HEAD &&
188
189 git apply --cached patch.file &&
8ddfce71 190 test "$patched" = $(git rev-parse :one)
634ede32
JH
191'
192
193test_expect_success 'apply patch --index (autocrlf=input)' '
194
163b9591 195 rm -f tmp one dir/two three &&
5c66d0d4 196 git config core.autocrlf input &&
634ede32
JH
197 git read-tree --reset -u HEAD &&
198
199 git apply --index patch.file &&
8ddfce71
JK
200 test "$patched" = $(git rev-parse :one) &&
201 test "$patched" = $(git hash-object --stdin <one)
634ede32
JH
202'
203
204test_expect_success 'apply patch (autocrlf=true)' '
205
163b9591 206 rm -f tmp one dir/two three &&
5c66d0d4 207 git config core.autocrlf true &&
634ede32
JH
208 git read-tree --reset -u HEAD &&
209
634ede32 210 git apply patch.file &&
8ddfce71 211 test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
634ede32
JH
212'
213
214test_expect_success 'apply patch --cached (autocrlf=true)' '
215
163b9591 216 rm -f tmp one dir/two three &&
5c66d0d4 217 git config core.autocrlf true &&
634ede32
JH
218 git read-tree --reset -u HEAD &&
219
220 git apply --cached patch.file &&
8ddfce71 221 test "$patched" = $(git rev-parse :one)
634ede32
JH
222'
223
67160271
JH
224test_expect_success 'apply patch --index (autocrlf=true)' '
225
163b9591 226 rm -f tmp one dir/two three &&
5c66d0d4 227 git config core.autocrlf true &&
67160271
JH
228 git read-tree --reset -u HEAD &&
229
230 git apply --index patch.file &&
8ddfce71
JK
231 test "$patched" = $(git rev-parse :one) &&
232 test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
67160271
JH
233'
234
35ebfd6a
JH
235test_expect_success '.gitattributes says two is binary' '
236
163b9591 237 rm -f tmp one dir/two three &&
e4aee10a 238 echo "two -crlf" >.gitattributes &&
5c66d0d4 239 git config core.autocrlf true &&
35ebfd6a
JH
240 git read-tree --reset -u HEAD &&
241
f6041abd 242 ! has_cr dir/two &&
8ddfce71 243 has_cr one &&
f6041abd 244 ! has_cr three
163b9591
JH
245'
246
247test_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
f6041abd 253 ! has_cr dir/two
163b9591
JH
254'
255
256test_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
8ddfce71
JK
262 has_cr dir/two &&
263 has_cr three
35ebfd6a
JH
264'
265
1a9d7e9b
JH
266test_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
f6041abd 275 ! has_cr one &&
8ddfce71 276 has_cr three
1a9d7e9b
JH
277'
278
279test_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
f6041abd 285 ! has_cr one &&
8ddfce71 286 has_cr three
1a9d7e9b
JH
287'
288
289test_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
f6041abd 296 ! has_cr one &&
8ddfce71 297 has_cr three
1a9d7e9b
JH
298'
299
300test_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
f6041abd 307 ! has_cr one &&
8ddfce71 308 has_cr three
1a9d7e9b
JH
309'
310
b997045e
KA
311test_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
06d53148
JS
323 git checkout main~1 &&
324 git checkout main &&
b997045e
KA
325 test "$(git diff-files --raw)" = ""
326
327'
328
329test_expect_success 'checkout when deleting .gitattributes' '
330
331 git rm .gitattributes &&
332 echo "contentsQ" | q_to_cr > .file2 &&
333 git add .file2 &&
2dec68cf 334 git commit -m third &&
b997045e 335
06d53148
JS
336 git checkout main~1 &&
337 git checkout main &&
c4f3f551 338 has_cr .file2
b997045e
KA
339
340'
341
d7b0a093
SP
342test_expect_success 'invalid .gitattributes (must not crash)' '
343
344 echo "three +crlf" >>.gitattributes &&
345 git diff
346
347'
c4805393
FAG
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
351test_expect_success 'setting up for new autocrlf tests' '
352 git config core.autocrlf false &&
353 git config core.safecrlf false &&
354 rm -rf .????* * &&
08495412
ES
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 &&
c4805393
FAG
358 git add -A . &&
359 git commit -m "alllf, allcrlf and mixed only" &&
360 git tag -a -m "message" autocrlf-checkpoint
361'
362
363test_expect_success 'report no change after setting autocrlf' '
364 git config core.autocrlf true &&
365 touch * &&
366 git diff --exit-code
367'
368
369test_expect_success 'files are clean after checkout' '
370 rm * &&
371 git checkout -f &&
372 git diff --exit-code
373'
374
375cr_to_Q_no_NL () {
376 tr '\015' Q | tr -d '\012'
377}
378
379test_expect_success 'LF only file gets CRLF with autocrlf' '
380 test "$(cr_to_Q_no_NL < alllf)" = "IQamQallQLFQ"
381'
382
383test_expect_success 'Mixed file is still mixed with autocrlf' '
384 test "$(cr_to_Q_no_NL < mixed)" = "OhhereisCRLFQintext"
385'
386
387test_expect_success 'CRLF only file has CRLF with autocrlf' '
388 test "$(cr_to_Q_no_NL < allcrlf)" = "IQamQallQCRLFQ"
389'
390
391test_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'
d7b0a093 400
634ede32 401test_done