Merge branch 'da/difftool' into maint
[git.git] / t / t0020-crlf.sh
blobf94120a8940a37c48fbb97abd85002da95e3abe9
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 # add or remove CRs to disk file in-place
12 # usage: munge_cr <append|remove> <file>
13 munge_cr () {
14 "${1}_cr" <"$2" >tmp &&
15 mv tmp "$2"
18 test_expect_success setup '
20 git config core.autocrlf false &&
22 for w in Hello world how are you; do echo $w; done >one &&
23 mkdir dir &&
24 for w in I am very very fine thank you; do echo $w; done >dir/two &&
25 for w in Oh here is NULQin text here; do echo $w; done | q_to_nul >three &&
26 git add . &&
28 git commit -m initial &&
30 one=$(git rev-parse HEAD:one) &&
31 dir=$(git rev-parse HEAD:dir) &&
32 two=$(git rev-parse HEAD:dir/two) &&
33 three=$(git rev-parse HEAD:three) &&
35 for w in Some extra lines here; do echo $w; done >>one &&
36 git diff >patch.file &&
37 patched=$(git hash-object --stdin <one) &&
38 git read-tree --reset -u HEAD
41 test_expect_success 'safecrlf: autocrlf=input, all CRLF' '
43 git config core.autocrlf input &&
44 git config core.safecrlf true &&
46 for w in I am all CRLF; do echo $w; done | append_cr >allcrlf &&
47 test_must_fail git add allcrlf
50 test_expect_success 'safecrlf: autocrlf=input, mixed LF/CRLF' '
52 git config core.autocrlf input &&
53 git config core.safecrlf true &&
55 for w in Oh here is CRLFQ in text; do echo $w; done | q_to_cr >mixed &&
56 test_must_fail git add mixed
59 test_expect_success 'safecrlf: autocrlf=true, all LF' '
61 git config core.autocrlf true &&
62 git config core.safecrlf true &&
64 for w in I am all LF; do echo $w; done >alllf &&
65 test_must_fail git add alllf
68 test_expect_success 'safecrlf: autocrlf=true mixed LF/CRLF' '
70 git config core.autocrlf true &&
71 git config core.safecrlf true &&
73 for w in Oh here is CRLFQ in text; do echo $w; done | q_to_cr >mixed &&
74 test_must_fail git add mixed
77 test_expect_success 'safecrlf: print warning only once' '
79 git config core.autocrlf input &&
80 git config core.safecrlf warn &&
82 for w in I am all LF; do echo $w; done >doublewarn &&
83 git add doublewarn &&
84 git commit -m "nowarn" &&
85 for w in Oh here is CRLFQ in text; do echo $w; done | q_to_cr >doublewarn &&
86 test $(git add doublewarn 2>&1 | grep "CRLF will be replaced by LF" | wc -l) = 1
90 test_expect_success 'safecrlf: git diff demotes safecrlf=true to warn' '
91 git config core.autocrlf input &&
92 git config core.safecrlf true &&
93 git diff HEAD
97 test_expect_success 'switch off autocrlf, safecrlf, reset HEAD' '
98 git config core.autocrlf false &&
99 git config core.safecrlf false &&
100 git reset --hard HEAD^
103 test_expect_success 'update with autocrlf=input' '
105 rm -f tmp one dir/two three &&
106 git read-tree --reset -u HEAD &&
107 git config core.autocrlf input &&
108 munge_cr append one &&
109 munge_cr append dir/two &&
110 git update-index -- one dir/two &&
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 &&
121 munge_cr append one &&
122 munge_cr append dir/two &&
123 git update-index -- one dir/two &&
124 differs=$(git diff-index --cached HEAD) &&
125 verbose test -z "$differs"
129 test_expect_success 'checkout with autocrlf=true' '
131 rm -f tmp one dir/two three &&
132 git config core.autocrlf true &&
133 git read-tree --reset -u HEAD &&
134 munge_cr remove one &&
135 munge_cr remove dir/two &&
136 git update-index -- one dir/two &&
137 test "$one" = $(git hash-object --stdin <one) &&
138 test "$two" = $(git hash-object --stdin <dir/two) &&
139 differs=$(git diff-index --cached HEAD) &&
140 verbose test -z "$differs"
143 test_expect_success 'checkout with autocrlf=input' '
145 rm -f tmp one dir/two three &&
146 git config core.autocrlf input &&
147 git read-tree --reset -u HEAD &&
148 test_must_fail has_cr one &&
149 test_must_fail has_cr two &&
150 git update-index -- one dir/two &&
151 test "$one" = $(git hash-object --stdin <one) &&
152 test "$two" = $(git hash-object --stdin <dir/two) &&
153 differs=$(git diff-index --cached HEAD) &&
154 verbose test -z "$differs"
157 test_expect_success 'apply patch (autocrlf=input)' '
159 rm -f tmp one dir/two three &&
160 git config core.autocrlf input &&
161 git read-tree --reset -u HEAD &&
163 git apply patch.file &&
164 verbose test "$patched" = "$(git hash-object --stdin <one)"
167 test_expect_success 'apply patch --cached (autocrlf=input)' '
169 rm -f tmp one dir/two three &&
170 git config core.autocrlf input &&
171 git read-tree --reset -u HEAD &&
173 git apply --cached patch.file &&
174 verbose test "$patched" = $(git rev-parse :one)
177 test_expect_success 'apply patch --index (autocrlf=input)' '
179 rm -f tmp one dir/two three &&
180 git config core.autocrlf input &&
181 git read-tree --reset -u HEAD &&
183 git apply --index patch.file &&
184 verbose test "$patched" = $(git rev-parse :one) &&
185 verbose test "$patched" = $(git hash-object --stdin <one)
188 test_expect_success 'apply patch (autocrlf=true)' '
190 rm -f tmp one dir/two three &&
191 git config core.autocrlf true &&
192 git read-tree --reset -u HEAD &&
194 git apply patch.file &&
195 verbose test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
198 test_expect_success 'apply patch --cached (autocrlf=true)' '
200 rm -f tmp one dir/two three &&
201 git config core.autocrlf true &&
202 git read-tree --reset -u HEAD &&
204 git apply --cached patch.file &&
205 verbose test "$patched" = $(git rev-parse :one)
208 test_expect_success 'apply patch --index (autocrlf=true)' '
210 rm -f tmp one dir/two three &&
211 git config core.autocrlf true &&
212 git read-tree --reset -u HEAD &&
214 git apply --index patch.file &&
215 verbose test "$patched" = $(git rev-parse :one) &&
216 verbose test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
219 test_expect_success '.gitattributes says two is binary' '
221 rm -f tmp one dir/two three &&
222 echo "two -crlf" >.gitattributes &&
223 git config core.autocrlf true &&
224 git read-tree --reset -u HEAD &&
226 test_must_fail has_cr dir/two &&
227 verbose has_cr one &&
228 test_must_fail has_cr three
231 test_expect_success '.gitattributes says two is input' '
233 rm -f tmp one dir/two three &&
234 echo "two crlf=input" >.gitattributes &&
235 git read-tree --reset -u HEAD &&
237 test_must_fail has_cr dir/two
240 test_expect_success '.gitattributes says two and three are text' '
242 rm -f tmp one dir/two three &&
243 echo "t* crlf" >.gitattributes &&
244 git read-tree --reset -u HEAD &&
246 verbose has_cr dir/two &&
247 verbose has_cr three
250 test_expect_success 'in-tree .gitattributes (1)' '
252 echo "one -crlf" >>.gitattributes &&
253 git add .gitattributes &&
254 git commit -m "Add .gitattributes" &&
256 rm -rf tmp one dir .gitattributes patch.file three &&
257 git read-tree --reset -u HEAD &&
259 test_must_fail has_cr one &&
260 verbose has_cr three
263 test_expect_success 'in-tree .gitattributes (2)' '
265 rm -rf tmp one dir .gitattributes patch.file three &&
266 git read-tree --reset HEAD &&
267 git checkout-index -f -q -u -a &&
269 test_must_fail has_cr one &&
270 verbose has_cr three
273 test_expect_success 'in-tree .gitattributes (3)' '
275 rm -rf tmp one dir .gitattributes patch.file three &&
276 git read-tree --reset HEAD &&
277 git checkout-index -u .gitattributes &&
278 git checkout-index -u one dir/two three &&
280 test_must_fail has_cr one &&
281 verbose has_cr three
284 test_expect_success 'in-tree .gitattributes (4)' '
286 rm -rf tmp one dir .gitattributes patch.file three &&
287 git read-tree --reset HEAD &&
288 git checkout-index -u one dir/two three &&
289 git checkout-index -u .gitattributes &&
291 test_must_fail has_cr one &&
292 verbose has_cr three
295 test_expect_success 'checkout with existing .gitattributes' '
297 git config core.autocrlf true &&
298 git config --unset core.safecrlf &&
299 echo ".file2 -crlfQ" | q_to_cr >> .gitattributes &&
300 git add .gitattributes &&
301 git commit -m initial &&
302 echo ".file -crlfQ" | q_to_cr >> .gitattributes &&
303 echo "contents" > .file &&
304 git add .gitattributes .file &&
305 git commit -m second &&
307 git checkout master~1 &&
308 git checkout master &&
309 test "$(git diff-files --raw)" = ""
313 test_expect_success 'checkout when deleting .gitattributes' '
315 git rm .gitattributes &&
316 echo "contentsQ" | q_to_cr > .file2 &&
317 git add .file2 &&
318 git commit -m third &&
320 git checkout master~1 &&
321 git checkout master &&
322 has_cr .file2
326 test_expect_success 'invalid .gitattributes (must not crash)' '
328 echo "three +crlf" >>.gitattributes &&
329 git diff
332 # Some more tests here to add new autocrlf functionality.
333 # We want to have a known state here, so start a bit from scratch
335 test_expect_success 'setting up for new autocrlf tests' '
336 git config core.autocrlf false &&
337 git config core.safecrlf false &&
338 rm -rf .????* * &&
339 for w in I am all LF; do echo $w; done >alllf &&
340 for w in Oh here is CRLFQ in text; do echo $w; done | q_to_cr >mixed &&
341 for w in I am all CRLF; do echo $w; done | append_cr >allcrlf &&
342 git add -A . &&
343 git commit -m "alllf, allcrlf and mixed only" &&
344 git tag -a -m "message" autocrlf-checkpoint
347 test_expect_success 'report no change after setting autocrlf' '
348 git config core.autocrlf true &&
349 touch * &&
350 git diff --exit-code
353 test_expect_success 'files are clean after checkout' '
354 rm * &&
355 git checkout -f &&
356 git diff --exit-code
359 cr_to_Q_no_NL () {
360 tr '\015' Q | tr -d '\012'
363 test_expect_success 'LF only file gets CRLF with autocrlf' '
364 test "$(cr_to_Q_no_NL < alllf)" = "IQamQallQLFQ"
367 test_expect_success 'Mixed file is still mixed with autocrlf' '
368 test "$(cr_to_Q_no_NL < mixed)" = "OhhereisCRLFQintext"
371 test_expect_success 'CRLF only file has CRLF with autocrlf' '
372 test "$(cr_to_Q_no_NL < allcrlf)" = "IQamQallQCRLFQ"
375 test_expect_success 'New CRLF file gets LF in repo' '
376 tr -d "\015" < alllf | append_cr > alllf2 &&
377 git add alllf2 &&
378 git commit -m "alllf2 added" &&
379 git config core.autocrlf false &&
380 rm * &&
381 git checkout -f &&
382 test_cmp alllf alllf2
385 test_done