restore: replace --force with --ignore-unmerged
[git.git] / t / t0020-crlf.sh
blob854da0ae16f8c437c239a0a4e16f1549d87521a2
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 git add doublewarn 2>err &&
87 if test_have_prereq C_LOCALE_OUTPUT
88 then
89 test $(grep "CRLF will be replaced by LF" err | wc -l) = 1
94 test_expect_success 'safecrlf: git diff demotes safecrlf=true to warn' '
95 git config core.autocrlf input &&
96 git config core.safecrlf true &&
97 git diff HEAD
101 test_expect_success 'safecrlf: no warning with safecrlf=false' '
102 git config core.autocrlf input &&
103 git config core.safecrlf false &&
105 for w in I am all CRLF; do echo $w; done | append_cr >allcrlf &&
106 git add allcrlf 2>err &&
107 test_must_be_empty err
111 test_expect_success 'switch off autocrlf, safecrlf, reset HEAD' '
112 git config core.autocrlf false &&
113 git config core.safecrlf false &&
114 git reset --hard HEAD^
117 test_expect_success 'update with autocrlf=input' '
119 rm -f tmp one dir/two three &&
120 git read-tree --reset -u HEAD &&
121 git config core.autocrlf input &&
122 munge_cr append one &&
123 munge_cr append dir/two &&
124 git update-index -- one dir/two &&
125 differs=$(git diff-index --cached HEAD) &&
126 verbose test -z "$differs"
130 test_expect_success 'update with autocrlf=true' '
132 rm -f tmp one dir/two three &&
133 git read-tree --reset -u HEAD &&
134 git config core.autocrlf true &&
135 munge_cr append one &&
136 munge_cr append dir/two &&
137 git update-index -- one dir/two &&
138 differs=$(git diff-index --cached HEAD) &&
139 verbose test -z "$differs"
143 test_expect_success 'checkout with autocrlf=true' '
145 rm -f tmp one dir/two three &&
146 git config core.autocrlf true &&
147 git read-tree --reset -u HEAD &&
148 munge_cr remove one &&
149 munge_cr remove dir/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 'checkout with autocrlf=input' '
159 rm -f tmp one dir/two three &&
160 git config core.autocrlf input &&
161 git read-tree --reset -u HEAD &&
162 test_must_fail has_cr one &&
163 test_must_fail has_cr dir/two &&
164 git update-index -- one dir/two &&
165 test "$one" = $(git hash-object --stdin <one) &&
166 test "$two" = $(git hash-object --stdin <dir/two) &&
167 differs=$(git diff-index --cached HEAD) &&
168 verbose test -z "$differs"
171 test_expect_success 'apply patch (autocrlf=input)' '
173 rm -f tmp one dir/two three &&
174 git config core.autocrlf input &&
175 git read-tree --reset -u HEAD &&
177 git apply patch.file &&
178 verbose test "$patched" = "$(git hash-object --stdin <one)"
181 test_expect_success 'apply patch --cached (autocrlf=input)' '
183 rm -f tmp one dir/two three &&
184 git config core.autocrlf input &&
185 git read-tree --reset -u HEAD &&
187 git apply --cached patch.file &&
188 verbose test "$patched" = $(git rev-parse :one)
191 test_expect_success 'apply patch --index (autocrlf=input)' '
193 rm -f tmp one dir/two three &&
194 git config core.autocrlf input &&
195 git read-tree --reset -u HEAD &&
197 git apply --index patch.file &&
198 verbose test "$patched" = $(git rev-parse :one) &&
199 verbose test "$patched" = $(git hash-object --stdin <one)
202 test_expect_success 'apply patch (autocrlf=true)' '
204 rm -f tmp one dir/two three &&
205 git config core.autocrlf true &&
206 git read-tree --reset -u HEAD &&
208 git apply patch.file &&
209 verbose test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
212 test_expect_success 'apply patch --cached (autocrlf=true)' '
214 rm -f tmp one dir/two three &&
215 git config core.autocrlf true &&
216 git read-tree --reset -u HEAD &&
218 git apply --cached patch.file &&
219 verbose test "$patched" = $(git rev-parse :one)
222 test_expect_success 'apply patch --index (autocrlf=true)' '
224 rm -f tmp one dir/two three &&
225 git config core.autocrlf true &&
226 git read-tree --reset -u HEAD &&
228 git apply --index patch.file &&
229 verbose test "$patched" = $(git rev-parse :one) &&
230 verbose test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
233 test_expect_success '.gitattributes says two is binary' '
235 rm -f tmp one dir/two three &&
236 echo "two -crlf" >.gitattributes &&
237 git config core.autocrlf true &&
238 git read-tree --reset -u HEAD &&
240 test_must_fail has_cr dir/two &&
241 verbose has_cr one &&
242 test_must_fail has_cr three
245 test_expect_success '.gitattributes says two is input' '
247 rm -f tmp one dir/two three &&
248 echo "two crlf=input" >.gitattributes &&
249 git read-tree --reset -u HEAD &&
251 test_must_fail has_cr dir/two
254 test_expect_success '.gitattributes says two and three are text' '
256 rm -f tmp one dir/two three &&
257 echo "t* crlf" >.gitattributes &&
258 git read-tree --reset -u HEAD &&
260 verbose has_cr dir/two &&
261 verbose has_cr three
264 test_expect_success 'in-tree .gitattributes (1)' '
266 echo "one -crlf" >>.gitattributes &&
267 git add .gitattributes &&
268 git commit -m "Add .gitattributes" &&
270 rm -rf tmp one dir .gitattributes patch.file three &&
271 git read-tree --reset -u HEAD &&
273 test_must_fail has_cr one &&
274 verbose has_cr three
277 test_expect_success 'in-tree .gitattributes (2)' '
279 rm -rf tmp one dir .gitattributes patch.file three &&
280 git read-tree --reset HEAD &&
281 git checkout-index -f -q -u -a &&
283 test_must_fail has_cr one &&
284 verbose has_cr three
287 test_expect_success 'in-tree .gitattributes (3)' '
289 rm -rf tmp one dir .gitattributes patch.file three &&
290 git read-tree --reset HEAD &&
291 git checkout-index -u .gitattributes &&
292 git checkout-index -u one dir/two three &&
294 test_must_fail has_cr one &&
295 verbose has_cr three
298 test_expect_success 'in-tree .gitattributes (4)' '
300 rm -rf tmp one dir .gitattributes patch.file three &&
301 git read-tree --reset HEAD &&
302 git checkout-index -u one dir/two three &&
303 git checkout-index -u .gitattributes &&
305 test_must_fail has_cr one &&
306 verbose has_cr three
309 test_expect_success 'checkout with existing .gitattributes' '
311 git config core.autocrlf true &&
312 git config --unset core.safecrlf &&
313 echo ".file2 -crlfQ" | q_to_cr >> .gitattributes &&
314 git add .gitattributes &&
315 git commit -m initial &&
316 echo ".file -crlfQ" | q_to_cr >> .gitattributes &&
317 echo "contents" > .file &&
318 git add .gitattributes .file &&
319 git commit -m second &&
321 git checkout master~1 &&
322 git checkout master &&
323 test "$(git diff-files --raw)" = ""
327 test_expect_success 'checkout when deleting .gitattributes' '
329 git rm .gitattributes &&
330 echo "contentsQ" | q_to_cr > .file2 &&
331 git add .file2 &&
332 git commit -m third &&
334 git checkout master~1 &&
335 git checkout master &&
336 has_cr .file2
340 test_expect_success 'invalid .gitattributes (must not crash)' '
342 echo "three +crlf" >>.gitattributes &&
343 git diff
346 # Some more tests here to add new autocrlf functionality.
347 # We want to have a known state here, so start a bit from scratch
349 test_expect_success 'setting up for new autocrlf tests' '
350 git config core.autocrlf false &&
351 git config core.safecrlf false &&
352 rm -rf .????* * &&
353 for w in I am all LF; do echo $w; done >alllf &&
354 for w in Oh here is CRLFQ in text; do echo $w; done | q_to_cr >mixed &&
355 for w in I am all CRLF; do echo $w; done | append_cr >allcrlf &&
356 git add -A . &&
357 git commit -m "alllf, allcrlf and mixed only" &&
358 git tag -a -m "message" autocrlf-checkpoint
361 test_expect_success 'report no change after setting autocrlf' '
362 git config core.autocrlf true &&
363 touch * &&
364 git diff --exit-code
367 test_expect_success 'files are clean after checkout' '
368 rm * &&
369 git checkout -f &&
370 git diff --exit-code
373 cr_to_Q_no_NL () {
374 tr '\015' Q | tr -d '\012'
377 test_expect_success 'LF only file gets CRLF with autocrlf' '
378 test "$(cr_to_Q_no_NL < alllf)" = "IQamQallQLFQ"
381 test_expect_success 'Mixed file is still mixed with autocrlf' '
382 test "$(cr_to_Q_no_NL < mixed)" = "OhhereisCRLFQintext"
385 test_expect_success 'CRLF only file has CRLF with autocrlf' '
386 test "$(cr_to_Q_no_NL < allcrlf)" = "IQamQallQCRLFQ"
389 test_expect_success 'New CRLF file gets LF in repo' '
390 tr -d "\015" < alllf | append_cr > alllf2 &&
391 git add alllf2 &&
392 git commit -m "alllf2 added" &&
393 git config core.autocrlf false &&
394 rm * &&
395 git checkout -f &&
396 test_cmp alllf alllf2
399 test_done