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