t0020: use modern test_* helpers
[git/mingw/j6t.git] / t / t0020-crlf.sh
blob9fa26df729fe45ad81d4c80d791da0fd7eb1bc02
1 #!/bin/sh
3 test_description='CRLF conversion'
5 . ./test-lib.sh
7 has_cr() {
8 tr '\015' Q <"$1" | grep Q >/dev/null
11 test_expect_success setup '
13 git config core.autocrlf false &&
15 for w in Hello world how are you; do echo $w; done >one &&
16 mkdir dir &&
17 for w in I am very very fine thank you; do echo $w; done >dir/two &&
18 for w in Oh here is NULQin text here; do echo $w; done | q_to_nul >three &&
19 git add . &&
21 git commit -m initial &&
23 one=$(git rev-parse HEAD:one) &&
24 dir=$(git rev-parse HEAD:dir) &&
25 two=$(git rev-parse HEAD:dir/two) &&
26 three=$(git rev-parse HEAD:three) &&
28 for w in Some extra lines here; do echo $w; done >>one &&
29 git diff >patch.file &&
30 patched=$(git hash-object --stdin <one) &&
31 git read-tree --reset -u HEAD &&
33 echo happy.
36 test_expect_success 'safecrlf: autocrlf=input, all CRLF' '
38 git config core.autocrlf input &&
39 git config core.safecrlf true &&
41 for w in I am all CRLF; do echo $w; done | append_cr >allcrlf &&
42 test_must_fail git add allcrlf
45 test_expect_success 'safecrlf: autocrlf=input, mixed LF/CRLF' '
47 git config core.autocrlf input &&
48 git config core.safecrlf true &&
50 for w in Oh here is CRLFQ in text; do echo $w; done | q_to_cr >mixed &&
51 test_must_fail git add mixed
54 test_expect_success 'safecrlf: autocrlf=true, all LF' '
56 git config core.autocrlf true &&
57 git config core.safecrlf true &&
59 for w in I am all LF; do echo $w; done >alllf &&
60 test_must_fail git add alllf
63 test_expect_success 'safecrlf: autocrlf=true mixed LF/CRLF' '
65 git config core.autocrlf true &&
66 git config core.safecrlf true &&
68 for w in Oh here is CRLFQ in text; do echo $w; done | q_to_cr >mixed &&
69 test_must_fail git add mixed
72 test_expect_success 'safecrlf: print warning only once' '
74 git config core.autocrlf input &&
75 git config core.safecrlf warn &&
77 for w in I am all LF; do echo $w; done >doublewarn &&
78 git add doublewarn &&
79 git commit -m "nowarn" &&
80 for w in Oh here is CRLFQ in text; do echo $w; done | q_to_cr >doublewarn &&
81 test $(git add doublewarn 2>&1 | grep "CRLF will be replaced by LF" | wc -l) = 1
85 test_expect_success 'safecrlf: git diff demotes safecrlf=true to warn' '
86 git config core.autocrlf input &&
87 git config core.safecrlf true &&
88 git diff HEAD
92 test_expect_success 'switch off autocrlf, safecrlf, reset HEAD' '
93 git config core.autocrlf false &&
94 git config core.safecrlf false &&
95 git reset --hard HEAD^
98 test_expect_success 'update with autocrlf=input' '
100 rm -f tmp one dir/two three &&
101 git read-tree --reset -u HEAD &&
102 git config core.autocrlf input &&
104 for f in one dir/two
106 append_cr <$f >tmp && mv -f tmp $f &&
107 git update-index -- $f ||
108 break
109 done &&
111 differs=$(git diff-index --cached HEAD) &&
112 verbose test -z "$differs"
116 test_expect_success 'update with autocrlf=true' '
118 rm -f tmp one dir/two three &&
119 git read-tree --reset -u HEAD &&
120 git config core.autocrlf true &&
122 for f in one dir/two
124 append_cr <$f >tmp && mv -f tmp $f &&
125 git update-index -- $f ||
126 break
127 done &&
129 differs=$(git diff-index --cached HEAD) &&
130 verbose test -z "$differs"
134 test_expect_success 'checkout with autocrlf=true' '
136 rm -f tmp one dir/two three &&
137 git config core.autocrlf true &&
138 git read-tree --reset -u HEAD &&
140 for f in one dir/two
142 remove_cr <"$f" >tmp && mv -f tmp $f &&
143 verbose git update-index -- $f ||
144 break
145 done &&
146 test "$one" = $(git hash-object --stdin <one) &&
147 test "$two" = $(git hash-object --stdin <dir/two) &&
148 differs=$(git diff-index --cached HEAD) &&
149 verbose test -z "$differs"
152 test_expect_success 'checkout with autocrlf=input' '
154 rm -f tmp one dir/two three &&
155 git config core.autocrlf input &&
156 git read-tree --reset -u HEAD &&
158 for f in one dir/two
160 if has_cr "$f"
161 then
162 echo "Eh? $f"
163 false
164 break
165 else
166 git update-index -- $f
168 done &&
169 test "$one" = $(git hash-object --stdin <one) &&
170 test "$two" = $(git hash-object --stdin <dir/two) &&
171 differs=$(git diff-index --cached HEAD) &&
172 verbose test -z "$differs"
175 test_expect_success 'apply patch (autocrlf=input)' '
177 rm -f tmp one dir/two three &&
178 git config core.autocrlf input &&
179 git read-tree --reset -u HEAD &&
181 git apply patch.file &&
182 verbose test "$patched" = "$(git hash-object --stdin <one)"
185 test_expect_success 'apply patch --cached (autocrlf=input)' '
187 rm -f tmp one dir/two three &&
188 git config core.autocrlf input &&
189 git read-tree --reset -u HEAD &&
191 git apply --cached patch.file &&
192 verbose test "$patched" = $(git rev-parse :one)
195 test_expect_success 'apply patch --index (autocrlf=input)' '
197 rm -f tmp one dir/two three &&
198 git config core.autocrlf input &&
199 git read-tree --reset -u HEAD &&
201 git apply --index patch.file &&
202 verbose test "$patched" = $(git rev-parse :one) &&
203 verbose test "$patched" = $(git hash-object --stdin <one)
206 test_expect_success 'apply patch (autocrlf=true)' '
208 rm -f tmp one dir/two three &&
209 git config core.autocrlf true &&
210 git read-tree --reset -u HEAD &&
212 git apply patch.file &&
213 verbose test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
216 test_expect_success 'apply patch --cached (autocrlf=true)' '
218 rm -f tmp one dir/two three &&
219 git config core.autocrlf true &&
220 git read-tree --reset -u HEAD &&
222 git apply --cached patch.file &&
223 verbose test "$patched" = $(git rev-parse :one)
226 test_expect_success 'apply patch --index (autocrlf=true)' '
228 rm -f tmp one dir/two three &&
229 git config core.autocrlf true &&
230 git read-tree --reset -u HEAD &&
232 git apply --index patch.file &&
233 verbose test "$patched" = $(git rev-parse :one) &&
234 verbose test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
237 test_expect_success '.gitattributes says two is binary' '
239 rm -f tmp one dir/two three &&
240 echo "two -crlf" >.gitattributes &&
241 git config core.autocrlf true &&
242 git read-tree --reset -u HEAD &&
244 if has_cr dir/two
245 then
246 echo "Huh?"
247 false
248 else
249 : happy
250 fi &&
252 if has_cr one
253 then
254 : happy
255 else
256 echo "Huh?"
257 false
258 fi &&
260 if has_cr three
261 then
262 echo "Huh?"
263 false
264 else
265 : happy
269 test_expect_success '.gitattributes says two is input' '
271 rm -f tmp one dir/two three &&
272 echo "two crlf=input" >.gitattributes &&
273 git read-tree --reset -u HEAD &&
275 if has_cr dir/two
276 then
277 echo "Huh?"
278 false
279 else
280 : happy
284 test_expect_success '.gitattributes says two and three are text' '
286 rm -f tmp one dir/two three &&
287 echo "t* crlf" >.gitattributes &&
288 git read-tree --reset -u HEAD &&
290 verbose has_cr dir/two &&
291 verbose has_cr three
294 test_expect_success 'in-tree .gitattributes (1)' '
296 echo "one -crlf" >>.gitattributes &&
297 git add .gitattributes &&
298 git commit -m "Add .gitattributes" &&
300 rm -rf tmp one dir .gitattributes patch.file three &&
301 git read-tree --reset -u HEAD &&
303 test_must_fail has_cr one &&
304 verbose has_cr three
307 test_expect_success 'in-tree .gitattributes (2)' '
309 rm -rf tmp one dir .gitattributes patch.file three &&
310 git read-tree --reset HEAD &&
311 git checkout-index -f -q -u -a &&
313 test_must_fail has_cr one &&
314 verbose has_cr three
317 test_expect_success 'in-tree .gitattributes (3)' '
319 rm -rf tmp one dir .gitattributes patch.file three &&
320 git read-tree --reset HEAD &&
321 git checkout-index -u .gitattributes &&
322 git checkout-index -u one dir/two three &&
324 test_must_fail has_cr one &&
325 verbose has_cr three
328 test_expect_success 'in-tree .gitattributes (4)' '
330 rm -rf tmp one dir .gitattributes patch.file three &&
331 git read-tree --reset HEAD &&
332 git checkout-index -u one dir/two three &&
333 git checkout-index -u .gitattributes &&
335 test_must_fail has_cr one &&
336 verbose has_cr three
339 test_expect_success 'checkout with existing .gitattributes' '
341 git config core.autocrlf true &&
342 git config --unset core.safecrlf &&
343 echo ".file2 -crlfQ" | q_to_cr >> .gitattributes &&
344 git add .gitattributes &&
345 git commit -m initial &&
346 echo ".file -crlfQ" | q_to_cr >> .gitattributes &&
347 echo "contents" > .file &&
348 git add .gitattributes .file &&
349 git commit -m second &&
351 git checkout master~1 &&
352 git checkout master &&
353 test "$(git diff-files --raw)" = ""
357 test_expect_success 'checkout when deleting .gitattributes' '
359 git rm .gitattributes &&
360 echo "contentsQ" | q_to_cr > .file2 &&
361 git add .file2 &&
362 git commit -m third &&
364 git checkout master~1 &&
365 git checkout master &&
366 has_cr .file2
370 test_expect_success 'invalid .gitattributes (must not crash)' '
372 echo "three +crlf" >>.gitattributes &&
373 git diff
376 # Some more tests here to add new autocrlf functionality.
377 # We want to have a known state here, so start a bit from scratch
379 test_expect_success 'setting up for new autocrlf tests' '
380 git config core.autocrlf false &&
381 git config core.safecrlf false &&
382 rm -rf .????* * &&
383 for w in I am all LF; do echo $w; done >alllf &&
384 for w in Oh here is CRLFQ in text; do echo $w; done | q_to_cr >mixed &&
385 for w in I am all CRLF; do echo $w; done | append_cr >allcrlf &&
386 git add -A . &&
387 git commit -m "alllf, allcrlf and mixed only" &&
388 git tag -a -m "message" autocrlf-checkpoint
391 test_expect_success 'report no change after setting autocrlf' '
392 git config core.autocrlf true &&
393 touch * &&
394 git diff --exit-code
397 test_expect_success 'files are clean after checkout' '
398 rm * &&
399 git checkout -f &&
400 git diff --exit-code
403 cr_to_Q_no_NL () {
404 tr '\015' Q | tr -d '\012'
407 test_expect_success 'LF only file gets CRLF with autocrlf' '
408 test "$(cr_to_Q_no_NL < alllf)" = "IQamQallQLFQ"
411 test_expect_success 'Mixed file is still mixed with autocrlf' '
412 test "$(cr_to_Q_no_NL < mixed)" = "OhhereisCRLFQintext"
415 test_expect_success 'CRLF only file has CRLF with autocrlf' '
416 test "$(cr_to_Q_no_NL < allcrlf)" = "IQamQallQCRLFQ"
419 test_expect_success 'New CRLF file gets LF in repo' '
420 tr -d "\015" < alllf | append_cr > alllf2 &&
421 git add alllf2 &&
422 git commit -m "alllf2 added" &&
423 git config core.autocrlf false &&
424 rm * &&
425 git checkout -f &&
426 test_cmp alllf alllf2
429 test_done