Git 2.31.4
[git/debian.git] / t / t0020-crlf.sh
blobf25ae8b5e1f34dd8e58bbe65ee171cbf5c8aa14b
1 #!/bin/sh
3 test_description='CRLF conversion'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 . ./test-lib.sh
10 has_cr() {
11 tr '\015' Q <"$1" | grep Q >/dev/null
14 # add or remove CRs to disk file in-place
15 # usage: munge_cr <append|remove> <file>
16 munge_cr () {
17 "${1}_cr" <"$2" >tmp &&
18 mv tmp "$2"
21 test_expect_success setup '
23 git config core.autocrlf false &&
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 &&
28 for w in Oh here is NULQin text here; do echo $w; done | q_to_nul >three &&
29 git add . &&
31 git commit -m initial &&
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) &&
38 for w in Some extra lines here; do echo $w; done >>one &&
39 git diff >patch.file &&
40 patched=$(git hash-object --stdin <one) &&
41 git read-tree --reset -u HEAD
44 test_expect_success 'safecrlf: autocrlf=input, all CRLF' '
46 git config core.autocrlf input &&
47 git config core.safecrlf true &&
49 for w in I am all CRLF; do echo $w; done | append_cr >allcrlf &&
50 test_must_fail git add allcrlf
53 test_expect_success 'safecrlf: autocrlf=input, mixed LF/CRLF' '
55 git config core.autocrlf input &&
56 git config core.safecrlf true &&
58 for w in Oh here is CRLFQ in text; do echo $w; done | q_to_cr >mixed &&
59 test_must_fail git add mixed
62 test_expect_success 'safecrlf: autocrlf=true, all LF' '
64 git config core.autocrlf true &&
65 git config core.safecrlf true &&
67 for w in I am all LF; do echo $w; done >alllf &&
68 test_must_fail git add alllf
71 test_expect_success 'safecrlf: autocrlf=true mixed LF/CRLF' '
73 git config core.autocrlf true &&
74 git config core.safecrlf true &&
76 for w in Oh here is CRLFQ in text; do echo $w; done | q_to_cr >mixed &&
77 test_must_fail git add mixed
80 test_expect_success 'safecrlf: print warning only once' '
82 git config core.autocrlf input &&
83 git config core.safecrlf warn &&
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 &&
89 git add doublewarn 2>err &&
90 grep "CRLF will be replaced by LF" err >err.warnings &&
91 test_line_count = 1 err.warnings
95 test_expect_success 'safecrlf: git diff demotes safecrlf=true to warn' '
96 git config core.autocrlf input &&
97 git config core.safecrlf true &&
98 git diff HEAD
102 test_expect_success 'safecrlf: no warning with safecrlf=false' '
103 git config core.autocrlf input &&
104 git config core.safecrlf false &&
106 for w in I am all CRLF; do echo $w; done | append_cr >allcrlf &&
107 git add allcrlf 2>err &&
108 test_must_be_empty err
112 test_expect_success 'switch off autocrlf, safecrlf, reset HEAD' '
113 git config core.autocrlf false &&
114 git config core.safecrlf false &&
115 git reset --hard HEAD^
118 test_expect_success 'update with autocrlf=input' '
120 rm -f tmp one dir/two three &&
121 git read-tree --reset -u HEAD &&
122 git config core.autocrlf input &&
123 munge_cr append one &&
124 munge_cr append dir/two &&
125 git update-index -- one dir/two &&
126 differs=$(git diff-index --cached HEAD) &&
127 verbose test -z "$differs"
131 test_expect_success 'update with autocrlf=true' '
133 rm -f tmp one dir/two three &&
134 git read-tree --reset -u HEAD &&
135 git config core.autocrlf true &&
136 munge_cr append one &&
137 munge_cr append dir/two &&
138 git update-index -- one dir/two &&
139 differs=$(git diff-index --cached HEAD) &&
140 verbose test -z "$differs"
144 test_expect_success 'checkout with autocrlf=true' '
146 rm -f tmp one dir/two three &&
147 git config core.autocrlf true &&
148 git read-tree --reset -u HEAD &&
149 munge_cr remove one &&
150 munge_cr remove dir/two &&
151 git update-index -- one dir/two &&
152 test "$one" = $(git hash-object --stdin <one) &&
153 test "$two" = $(git hash-object --stdin <dir/two) &&
154 differs=$(git diff-index --cached HEAD) &&
155 verbose test -z "$differs"
158 test_expect_success 'checkout with autocrlf=input' '
160 rm -f tmp one dir/two three &&
161 git config core.autocrlf input &&
162 git read-tree --reset -u HEAD &&
163 ! has_cr one &&
164 ! has_cr dir/two &&
165 git update-index -- one dir/two &&
166 test "$one" = $(git hash-object --stdin <one) &&
167 test "$two" = $(git hash-object --stdin <dir/two) &&
168 differs=$(git diff-index --cached HEAD) &&
169 verbose test -z "$differs"
172 test_expect_success 'apply patch (autocrlf=input)' '
174 rm -f tmp one dir/two three &&
175 git config core.autocrlf input &&
176 git read-tree --reset -u HEAD &&
178 git apply patch.file &&
179 verbose test "$patched" = "$(git hash-object --stdin <one)"
182 test_expect_success 'apply patch --cached (autocrlf=input)' '
184 rm -f tmp one dir/two three &&
185 git config core.autocrlf input &&
186 git read-tree --reset -u HEAD &&
188 git apply --cached patch.file &&
189 verbose test "$patched" = $(git rev-parse :one)
192 test_expect_success 'apply patch --index (autocrlf=input)' '
194 rm -f tmp one dir/two three &&
195 git config core.autocrlf input &&
196 git read-tree --reset -u HEAD &&
198 git apply --index patch.file &&
199 verbose test "$patched" = $(git rev-parse :one) &&
200 verbose test "$patched" = $(git hash-object --stdin <one)
203 test_expect_success 'apply patch (autocrlf=true)' '
205 rm -f tmp one dir/two three &&
206 git config core.autocrlf true &&
207 git read-tree --reset -u HEAD &&
209 git apply patch.file &&
210 verbose test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
213 test_expect_success 'apply patch --cached (autocrlf=true)' '
215 rm -f tmp one dir/two three &&
216 git config core.autocrlf true &&
217 git read-tree --reset -u HEAD &&
219 git apply --cached patch.file &&
220 verbose test "$patched" = $(git rev-parse :one)
223 test_expect_success 'apply patch --index (autocrlf=true)' '
225 rm -f tmp one dir/two three &&
226 git config core.autocrlf true &&
227 git read-tree --reset -u HEAD &&
229 git apply --index patch.file &&
230 verbose test "$patched" = $(git rev-parse :one) &&
231 verbose test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
234 test_expect_success '.gitattributes says two is binary' '
236 rm -f tmp one dir/two three &&
237 echo "two -crlf" >.gitattributes &&
238 git config core.autocrlf true &&
239 git read-tree --reset -u HEAD &&
241 ! has_cr dir/two &&
242 verbose has_cr one &&
243 ! has_cr three
246 test_expect_success '.gitattributes says two is input' '
248 rm -f tmp one dir/two three &&
249 echo "two crlf=input" >.gitattributes &&
250 git read-tree --reset -u HEAD &&
252 ! has_cr dir/two
255 test_expect_success '.gitattributes says two and three are text' '
257 rm -f tmp one dir/two three &&
258 echo "t* crlf" >.gitattributes &&
259 git read-tree --reset -u HEAD &&
261 verbose has_cr dir/two &&
262 verbose has_cr three
265 test_expect_success 'in-tree .gitattributes (1)' '
267 echo "one -crlf" >>.gitattributes &&
268 git add .gitattributes &&
269 git commit -m "Add .gitattributes" &&
271 rm -rf tmp one dir .gitattributes patch.file three &&
272 git read-tree --reset -u HEAD &&
274 ! has_cr one &&
275 verbose has_cr three
278 test_expect_success 'in-tree .gitattributes (2)' '
280 rm -rf tmp one dir .gitattributes patch.file three &&
281 git read-tree --reset HEAD &&
282 git checkout-index -f -q -u -a &&
284 ! has_cr one &&
285 verbose has_cr three
288 test_expect_success 'in-tree .gitattributes (3)' '
290 rm -rf tmp one dir .gitattributes patch.file three &&
291 git read-tree --reset HEAD &&
292 git checkout-index -u .gitattributes &&
293 git checkout-index -u one dir/two three &&
295 ! has_cr one &&
296 verbose has_cr three
299 test_expect_success 'in-tree .gitattributes (4)' '
301 rm -rf tmp one dir .gitattributes patch.file three &&
302 git read-tree --reset HEAD &&
303 git checkout-index -u one dir/two three &&
304 git checkout-index -u .gitattributes &&
306 ! has_cr one &&
307 verbose has_cr three
310 test_expect_success 'checkout with existing .gitattributes' '
312 git config core.autocrlf true &&
313 git config --unset core.safecrlf &&
314 echo ".file2 -crlfQ" | q_to_cr >> .gitattributes &&
315 git add .gitattributes &&
316 git commit -m initial &&
317 echo ".file -crlfQ" | q_to_cr >> .gitattributes &&
318 echo "contents" > .file &&
319 git add .gitattributes .file &&
320 git commit -m second &&
322 git checkout main~1 &&
323 git checkout main &&
324 test "$(git diff-files --raw)" = ""
328 test_expect_success 'checkout when deleting .gitattributes' '
330 git rm .gitattributes &&
331 echo "contentsQ" | q_to_cr > .file2 &&
332 git add .file2 &&
333 git commit -m third &&
335 git checkout main~1 &&
336 git checkout main &&
337 has_cr .file2
341 test_expect_success 'invalid .gitattributes (must not crash)' '
343 echo "three +crlf" >>.gitattributes &&
344 git diff
347 # Some more tests here to add new autocrlf functionality.
348 # We want to have a known state here, so start a bit from scratch
350 test_expect_success 'setting up for new autocrlf tests' '
351 git config core.autocrlf false &&
352 git config core.safecrlf false &&
353 rm -rf .????* * &&
354 for w in I am all LF; do echo $w; done >alllf &&
355 for w in Oh here is CRLFQ in text; do echo $w; done | q_to_cr >mixed &&
356 for w in I am all CRLF; do echo $w; done | append_cr >allcrlf &&
357 git add -A . &&
358 git commit -m "alllf, allcrlf and mixed only" &&
359 git tag -a -m "message" autocrlf-checkpoint
362 test_expect_success 'report no change after setting autocrlf' '
363 git config core.autocrlf true &&
364 touch * &&
365 git diff --exit-code
368 test_expect_success 'files are clean after checkout' '
369 rm * &&
370 git checkout -f &&
371 git diff --exit-code
374 cr_to_Q_no_NL () {
375 tr '\015' Q | tr -d '\012'
378 test_expect_success 'LF only file gets CRLF with autocrlf' '
379 test "$(cr_to_Q_no_NL < alllf)" = "IQamQallQLFQ"
382 test_expect_success 'Mixed file is still mixed with autocrlf' '
383 test "$(cr_to_Q_no_NL < mixed)" = "OhhereisCRLFQintext"
386 test_expect_success 'CRLF only file has CRLF with autocrlf' '
387 test "$(cr_to_Q_no_NL < allcrlf)" = "IQamQallQCRLFQ"
390 test_expect_success 'New CRLF file gets LF in repo' '
391 tr -d "\015" < alllf | append_cr > alllf2 &&
392 git add alllf2 &&
393 git commit -m "alllf2 added" &&
394 git config core.autocrlf false &&
395 rm * &&
396 git checkout -f &&
397 test_cmp alllf alllf2
400 test_done