Merge branch 'ps/ci-fix-detection-of-ubuntu-20' into maint-2.45
[alt-git.git] / t / t0020-crlf.sh
blob81946e87ccb9b478789164a6db54d9a80278288d
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_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
11 has_cr() {
12 tr '\015' Q <"$1" | grep Q >/dev/null
15 # add or remove CRs to disk file in-place
16 # usage: munge_cr <append|remove> <file>
17 munge_cr () {
18 "${1}_cr" <"$2" >tmp &&
19 mv tmp "$2"
22 test_expect_success setup '
24 git config core.autocrlf false &&
26 test_write_lines Hello world how are you >one &&
27 mkdir dir &&
28 test_write_lines I am very very fine thank you >dir/two &&
29 test_write_lines Oh here is NULQin text here | q_to_nul >three &&
30 git add . &&
32 git commit -m initial &&
34 one=$(git rev-parse HEAD:one) &&
35 dir=$(git rev-parse HEAD:dir) &&
36 two=$(git rev-parse HEAD:dir/two) &&
37 three=$(git rev-parse HEAD:three) &&
39 test_write_lines Some extra lines here >>one &&
40 git diff >patch.file &&
41 patched=$(git hash-object --stdin <one) &&
42 git read-tree --reset -u HEAD
45 test_expect_success 'safecrlf: autocrlf=input, all CRLF' '
47 git config core.autocrlf input &&
48 git config core.safecrlf true &&
50 test_write_lines I am all CRLF | append_cr >allcrlf &&
51 test_must_fail git add allcrlf
54 test_expect_success 'safecrlf: autocrlf=input, mixed LF/CRLF' '
56 git config core.autocrlf input &&
57 git config core.safecrlf true &&
59 test_write_lines Oh here is CRLFQ in text | q_to_cr >mixed &&
60 test_must_fail git add mixed
63 test_expect_success 'safecrlf: autocrlf=true, all LF' '
65 git config core.autocrlf true &&
66 git config core.safecrlf true &&
68 test_write_lines I am all LF >alllf &&
69 test_must_fail git add alllf
72 test_expect_success 'safecrlf: autocrlf=true mixed LF/CRLF' '
74 git config core.autocrlf true &&
75 git config core.safecrlf true &&
77 test_write_lines Oh here is CRLFQ in text | q_to_cr >mixed &&
78 test_must_fail git add mixed
81 test_expect_success 'safecrlf: print warning only once' '
83 git config core.autocrlf input &&
84 git config core.safecrlf warn &&
86 test_write_lines I am all LF >doublewarn &&
87 git add doublewarn &&
88 git commit -m "nowarn" &&
89 test_write_lines Oh here is CRLFQ in text | q_to_cr >doublewarn &&
90 git add doublewarn 2>err &&
91 grep "CRLF will be replaced by LF" err >err.warnings &&
92 test_line_count = 1 err.warnings
96 test_expect_success 'safecrlf: git diff demotes safecrlf=true to warn' '
97 git config core.autocrlf input &&
98 git config core.safecrlf true &&
99 git diff HEAD
103 test_expect_success 'safecrlf: no warning with safecrlf=false' '
104 git config core.autocrlf input &&
105 git config core.safecrlf false &&
107 test_write_lines I am all CRLF | append_cr >allcrlf &&
108 git add allcrlf 2>err &&
109 test_must_be_empty err
113 test_expect_success 'switch off autocrlf, safecrlf, reset HEAD' '
114 git config core.autocrlf false &&
115 git config core.safecrlf false &&
116 git reset --hard HEAD^
119 test_expect_success 'update with autocrlf=input' '
121 rm -f tmp one dir/two three &&
122 git read-tree --reset -u HEAD &&
123 git config core.autocrlf input &&
124 munge_cr append one &&
125 munge_cr append dir/two &&
126 git update-index -- one dir/two &&
127 differs=$(git diff-index --cached HEAD) &&
128 test -z "$differs"
132 test_expect_success 'update with autocrlf=true' '
134 rm -f tmp one dir/two three &&
135 git read-tree --reset -u HEAD &&
136 git config core.autocrlf true &&
137 munge_cr append one &&
138 munge_cr append dir/two &&
139 git update-index -- one dir/two &&
140 differs=$(git diff-index --cached HEAD) &&
141 test -z "$differs"
145 test_expect_success 'checkout with autocrlf=true' '
147 rm -f tmp one dir/two three &&
148 git config core.autocrlf true &&
149 git read-tree --reset -u HEAD &&
150 munge_cr remove one &&
151 munge_cr remove dir/two &&
152 git update-index -- one dir/two &&
153 test "$one" = $(git hash-object --stdin <one) &&
154 test "$two" = $(git hash-object --stdin <dir/two) &&
155 differs=$(git diff-index --cached HEAD) &&
156 test -z "$differs"
159 test_expect_success 'checkout with autocrlf=input' '
161 rm -f tmp one dir/two three &&
162 git config core.autocrlf input &&
163 git read-tree --reset -u HEAD &&
164 ! has_cr one &&
165 ! has_cr dir/two &&
166 git update-index -- one dir/two &&
167 test "$one" = $(git hash-object --stdin <one) &&
168 test "$two" = $(git hash-object --stdin <dir/two) &&
169 differs=$(git diff-index --cached HEAD) &&
170 test -z "$differs"
173 test_expect_success 'apply patch (autocrlf=input)' '
175 rm -f tmp one dir/two three &&
176 git config core.autocrlf input &&
177 git read-tree --reset -u HEAD &&
179 git apply patch.file &&
180 test "$patched" = "$(git hash-object --stdin <one)"
183 test_expect_success 'apply patch --cached (autocrlf=input)' '
185 rm -f tmp one dir/two three &&
186 git config core.autocrlf input &&
187 git read-tree --reset -u HEAD &&
189 git apply --cached patch.file &&
190 test "$patched" = $(git rev-parse :one)
193 test_expect_success 'apply patch --index (autocrlf=input)' '
195 rm -f tmp one dir/two three &&
196 git config core.autocrlf input &&
197 git read-tree --reset -u HEAD &&
199 git apply --index patch.file &&
200 test "$patched" = $(git rev-parse :one) &&
201 test "$patched" = $(git hash-object --stdin <one)
204 test_expect_success 'apply patch (autocrlf=true)' '
206 rm -f tmp one dir/two three &&
207 git config core.autocrlf true &&
208 git read-tree --reset -u HEAD &&
210 git apply patch.file &&
211 test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
214 test_expect_success 'apply patch --cached (autocrlf=true)' '
216 rm -f tmp one dir/two three &&
217 git config core.autocrlf true &&
218 git read-tree --reset -u HEAD &&
220 git apply --cached patch.file &&
221 test "$patched" = $(git rev-parse :one)
224 test_expect_success 'apply patch --index (autocrlf=true)' '
226 rm -f tmp one dir/two three &&
227 git config core.autocrlf true &&
228 git read-tree --reset -u HEAD &&
230 git apply --index patch.file &&
231 test "$patched" = $(git rev-parse :one) &&
232 test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
235 test_expect_success '.gitattributes says two is binary' '
237 rm -f tmp one dir/two three &&
238 echo "two -crlf" >.gitattributes &&
239 git config core.autocrlf true &&
240 git read-tree --reset -u HEAD &&
242 ! has_cr dir/two &&
243 has_cr one &&
244 ! has_cr three
247 test_expect_success '.gitattributes says two is input' '
249 rm -f tmp one dir/two three &&
250 echo "two crlf=input" >.gitattributes &&
251 git read-tree --reset -u HEAD &&
253 ! has_cr dir/two
256 test_expect_success '.gitattributes says two and three are text' '
258 rm -f tmp one dir/two three &&
259 echo "t* crlf" >.gitattributes &&
260 git read-tree --reset -u HEAD &&
262 has_cr dir/two &&
263 has_cr three
266 test_expect_success 'in-tree .gitattributes (1)' '
268 echo "one -crlf" >>.gitattributes &&
269 git add .gitattributes &&
270 git commit -m "Add .gitattributes" &&
272 rm -rf tmp one dir .gitattributes patch.file three &&
273 git read-tree --reset -u HEAD &&
275 ! has_cr one &&
276 has_cr three
279 test_expect_success 'in-tree .gitattributes (2)' '
281 rm -rf tmp one dir .gitattributes patch.file three &&
282 git read-tree --reset HEAD &&
283 git checkout-index -f -q -u -a &&
285 ! has_cr one &&
286 has_cr three
289 test_expect_success 'in-tree .gitattributes (3)' '
291 rm -rf tmp one dir .gitattributes patch.file three &&
292 git read-tree --reset HEAD &&
293 git checkout-index -u .gitattributes &&
294 git checkout-index -u one dir/two three &&
296 ! has_cr one &&
297 has_cr three
300 test_expect_success 'in-tree .gitattributes (4)' '
302 rm -rf tmp one dir .gitattributes patch.file three &&
303 git read-tree --reset HEAD &&
304 git checkout-index -u one dir/two three &&
305 git checkout-index -u .gitattributes &&
307 ! has_cr one &&
308 has_cr three
311 test_expect_success 'checkout with existing .gitattributes' '
313 git config core.autocrlf true &&
314 git config --unset core.safecrlf &&
315 echo ".file2 -crlfQ" | q_to_cr >> .gitattributes &&
316 git add .gitattributes &&
317 git commit -m initial &&
318 echo ".file -crlfQ" | q_to_cr >> .gitattributes &&
319 echo "contents" > .file &&
320 git add .gitattributes .file &&
321 git commit -m second &&
323 git checkout main~1 &&
324 git checkout main &&
325 test "$(git diff-files --raw)" = ""
329 test_expect_success 'checkout when deleting .gitattributes' '
331 git rm .gitattributes &&
332 echo "contentsQ" | q_to_cr > .file2 &&
333 git add .file2 &&
334 git commit -m third &&
336 git checkout main~1 &&
337 git checkout main &&
338 has_cr .file2
342 test_expect_success 'invalid .gitattributes (must not crash)' '
344 echo "three +crlf" >>.gitattributes &&
345 git diff
348 # Some more tests here to add new autocrlf functionality.
349 # We want to have a known state here, so start a bit from scratch
351 test_expect_success 'setting up for new autocrlf tests' '
352 git config core.autocrlf false &&
353 git config core.safecrlf false &&
354 rm -rf .????* * &&
355 test_write_lines I am all LF >alllf &&
356 test_write_lines Oh here is CRLFQ in text | q_to_cr >mixed &&
357 test_write_lines I am all CRLF | append_cr >allcrlf &&
358 git add -A . &&
359 git commit -m "alllf, allcrlf and mixed only" &&
360 git tag -a -m "message" autocrlf-checkpoint
363 test_expect_success 'report no change after setting autocrlf' '
364 git config core.autocrlf true &&
365 touch * &&
366 git diff --exit-code
369 test_expect_success 'files are clean after checkout' '
370 rm * &&
371 git checkout -f &&
372 git diff --exit-code
375 cr_to_Q_no_NL () {
376 tr '\015' Q | tr -d '\012'
379 test_expect_success 'LF only file gets CRLF with autocrlf' '
380 test "$(cr_to_Q_no_NL < alllf)" = "IQamQallQLFQ"
383 test_expect_success 'Mixed file is still mixed with autocrlf' '
384 test "$(cr_to_Q_no_NL < mixed)" = "OhhereisCRLFQintext"
387 test_expect_success 'CRLF only file has CRLF with autocrlf' '
388 test "$(cr_to_Q_no_NL < allcrlf)" = "IQamQallQCRLFQ"
391 test_expect_success 'New CRLF file gets LF in repo' '
392 tr -d "\015" < alllf | append_cr > alllf2 &&
393 git add alllf2 &&
394 git commit -m "alllf2 added" &&
395 git config core.autocrlf false &&
396 rm * &&
397 git checkout -f &&
398 test_cmp alllf alllf2
401 test_done