blob: 81946e87ccb9b478789164a6db54d9a80278288d [file] [log] [blame]
Junio C Hamano634ede32007-02-14 14:54:00 -08001#!/bin/sh
2
3test_description='CRLF conversion'
4
Johannes Schindelin06d53142020-11-18 23:44:21 +00005GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
Johannes Schindelin334afbc2020-11-18 23:44:19 +00006export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01008TEST_PASSES_SANITIZE_LEAK=true
Junio C Hamano634ede32007-02-14 14:54:00 -08009. ./test-lib.sh
10
Stephen Boydc4f3f552010-01-25 16:33:57 -080011has_cr() {
12 tr '\015' Q <"$1" | grep Q >/dev/null
Junio C Hamano634ede32007-02-14 14:54:00 -080013}
14
Jeff Kingfd777142015-03-25 01:28:44 -040015# add or remove CRs to disk file in-place
16# usage: munge_cr <append|remove> <file>
17munge_cr () {
18 "${1}_cr" <"$2" >tmp &&
19 mv tmp "$2"
20}
21
Junio C Hamano634ede32007-02-14 14:54:00 -080022test_expect_success setup '
23
Junio C Hamano5c66d0d2008-01-17 22:52:40 -080024 git config core.autocrlf false &&
Junio C Hamano634ede32007-02-14 14:54:00 -080025
Eric Sunshine08495412021-12-09 00:11:05 -050026 test_write_lines Hello world how are you >one &&
Junio C Hamano634ede32007-02-14 14:54:00 -080027 mkdir dir &&
Eric Sunshine08495412021-12-09 00:11:05 -050028 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 &&
Junio C Hamano634ede32007-02-14 14:54:00 -080030 git add . &&
31
32 git commit -m initial &&
33
Elia Pintodef226b2014-04-28 05:57:26 -070034 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) &&
Junio C Hamano634ede32007-02-14 14:54:00 -080038
Eric Sunshine08495412021-12-09 00:11:05 -050039 test_write_lines Some extra lines here >>one &&
Junio C Hamano634ede32007-02-14 14:54:00 -080040 git diff >patch.file &&
Elia Pintodef226b2014-04-28 05:57:26 -070041 patched=$(git hash-object --stdin <one) &&
Jeff Kingbe86fb32015-03-25 01:31:41 -040042 git read-tree --reset -u HEAD
Junio C Hamano634ede32007-02-14 14:54:00 -080043'
44
Steffen Prohaska21e5ad52008-02-06 12:25:58 +010045test_expect_success 'safecrlf: autocrlf=input, all CRLF' '
46
47 git config core.autocrlf input &&
48 git config core.safecrlf true &&
49
Eric Sunshine08495412021-12-09 00:11:05 -050050 test_write_lines I am all CRLF | append_cr >allcrlf &&
Stephan Beyerd492b312008-07-12 17:47:52 +020051 test_must_fail git add allcrlf
Steffen Prohaska21e5ad52008-02-06 12:25:58 +010052'
53
54test_expect_success 'safecrlf: autocrlf=input, mixed LF/CRLF' '
55
56 git config core.autocrlf input &&
57 git config core.safecrlf true &&
58
Eric Sunshine08495412021-12-09 00:11:05 -050059 test_write_lines Oh here is CRLFQ in text | q_to_cr >mixed &&
Stephan Beyerd492b312008-07-12 17:47:52 +020060 test_must_fail git add mixed
Steffen Prohaska21e5ad52008-02-06 12:25:58 +010061'
62
63test_expect_success 'safecrlf: autocrlf=true, all LF' '
64
65 git config core.autocrlf true &&
66 git config core.safecrlf true &&
67
Eric Sunshine08495412021-12-09 00:11:05 -050068 test_write_lines I am all LF >alllf &&
Stephan Beyerd492b312008-07-12 17:47:52 +020069 test_must_fail git add alllf
Steffen Prohaska21e5ad52008-02-06 12:25:58 +010070'
71
72test_expect_success 'safecrlf: autocrlf=true mixed LF/CRLF' '
73
74 git config core.autocrlf true &&
75 git config core.safecrlf true &&
76
Eric Sunshine08495412021-12-09 00:11:05 -050077 test_write_lines Oh here is CRLFQ in text | q_to_cr >mixed &&
Stephan Beyerd492b312008-07-12 17:47:52 +020078 test_must_fail git add mixed
Steffen Prohaska21e5ad52008-02-06 12:25:58 +010079'
80
81test_expect_success 'safecrlf: print warning only once' '
82
83 git config core.autocrlf input &&
84 git config core.safecrlf warn &&
85
Eric Sunshine08495412021-12-09 00:11:05 -050086 test_write_lines I am all LF >doublewarn &&
Steffen Prohaska21e5ad52008-02-06 12:25:58 +010087 git add doublewarn &&
88 git commit -m "nowarn" &&
Eric Sunshine08495412021-12-09 00:11:05 -050089 test_write_lines Oh here is CRLFQ in text | q_to_cr >doublewarn &&
Vasco Almeida87cb7842016-10-17 13:15:27 +000090 git add doublewarn 2>err &&
Ævar Arnfjörð Bjarmasonb1e07982021-02-11 02:53:52 +010091 grep "CRLF will be replaced by LF" err >err.warnings &&
92 test_line_count = 1 err.warnings
Steffen Prohaska21e5ad52008-02-06 12:25:58 +010093'
94
Junio C Hamano5430bb22013-06-24 14:35:04 -070095
96test_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
100'
101
102
Anthony Sottile6cb09122018-06-04 13:17:42 -0700103test_expect_success 'safecrlf: no warning with safecrlf=false' '
104 git config core.autocrlf input &&
105 git config core.safecrlf false &&
106
Eric Sunshine08495412021-12-09 00:11:05 -0500107 test_write_lines I am all CRLF | append_cr >allcrlf &&
Anthony Sottile6cb09122018-06-04 13:17:42 -0700108 git add allcrlf 2>err &&
109 test_must_be_empty err
110'
111
112
Steffen Prohaska21e5ad52008-02-06 12:25:58 +0100113test_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^
117'
118
Junio C Hamano634ede32007-02-14 14:54:00 -0800119test_expect_success 'update with autocrlf=input' '
120
Junio C Hamano163b9592007-04-19 22:37:19 -0700121 rm -f tmp one dir/two three &&
Junio C Hamano634ede32007-02-14 14:54:00 -0800122 git read-tree --reset -u HEAD &&
Junio C Hamano5c66d0d2008-01-17 22:52:40 -0800123 git config core.autocrlf input &&
Jeff Kingfd777142015-03-25 01:28:44 -0400124 munge_cr append one &&
125 munge_cr append dir/two &&
126 git update-index -- one dir/two &&
Elia Pintodef226b2014-04-28 05:57:26 -0700127 differs=$(git diff-index --cached HEAD) &&
Jeff King8ddfce72023-05-08 15:04:57 -0400128 test -z "$differs"
Junio C Hamano634ede32007-02-14 14:54:00 -0800129
130'
131
132test_expect_success 'update with autocrlf=true' '
133
Junio C Hamano163b9592007-04-19 22:37:19 -0700134 rm -f tmp one dir/two three &&
Junio C Hamano634ede32007-02-14 14:54:00 -0800135 git read-tree --reset -u HEAD &&
Junio C Hamano5c66d0d2008-01-17 22:52:40 -0800136 git config core.autocrlf true &&
Jeff Kingfd777142015-03-25 01:28:44 -0400137 munge_cr append one &&
138 munge_cr append dir/two &&
139 git update-index -- one dir/two &&
Elia Pintodef226b2014-04-28 05:57:26 -0700140 differs=$(git diff-index --cached HEAD) &&
Jeff King8ddfce72023-05-08 15:04:57 -0400141 test -z "$differs"
Junio C Hamano634ede32007-02-14 14:54:00 -0800142
143'
144
145test_expect_success 'checkout with autocrlf=true' '
146
Junio C Hamano163b9592007-04-19 22:37:19 -0700147 rm -f tmp one dir/two three &&
Junio C Hamano5c66d0d2008-01-17 22:52:40 -0800148 git config core.autocrlf true &&
Junio C Hamano634ede32007-02-14 14:54:00 -0800149 git read-tree --reset -u HEAD &&
Jeff Kingfd777142015-03-25 01:28:44 -0400150 munge_cr remove one &&
151 munge_cr remove dir/two &&
152 git update-index -- one dir/two &&
Elia Pintodef226b2014-04-28 05:57:26 -0700153 test "$one" = $(git hash-object --stdin <one) &&
154 test "$two" = $(git hash-object --stdin <dir/two) &&
155 differs=$(git diff-index --cached HEAD) &&
Jeff King8ddfce72023-05-08 15:04:57 -0400156 test -z "$differs"
Junio C Hamano634ede32007-02-14 14:54:00 -0800157'
158
159test_expect_success 'checkout with autocrlf=input' '
160
Junio C Hamano163b9592007-04-19 22:37:19 -0700161 rm -f tmp one dir/two three &&
Junio C Hamano5c66d0d2008-01-17 22:52:40 -0800162 git config core.autocrlf input &&
Junio C Hamano634ede32007-02-14 14:54:00 -0800163 git read-tree --reset -u HEAD &&
Denton Liuf6041ab2019-12-20 10:15:53 -0800164 ! has_cr one &&
165 ! has_cr dir/two &&
Jeff Kingfd777142015-03-25 01:28:44 -0400166 git update-index -- one dir/two &&
Elia Pintodef226b2014-04-28 05:57:26 -0700167 test "$one" = $(git hash-object --stdin <one) &&
168 test "$two" = $(git hash-object --stdin <dir/two) &&
169 differs=$(git diff-index --cached HEAD) &&
Jeff King8ddfce72023-05-08 15:04:57 -0400170 test -z "$differs"
Junio C Hamano634ede32007-02-14 14:54:00 -0800171'
172
173test_expect_success 'apply patch (autocrlf=input)' '
174
Junio C Hamano163b9592007-04-19 22:37:19 -0700175 rm -f tmp one dir/two three &&
Junio C Hamano5c66d0d2008-01-17 22:52:40 -0800176 git config core.autocrlf input &&
Junio C Hamano634ede32007-02-14 14:54:00 -0800177 git read-tree --reset -u HEAD &&
178
179 git apply patch.file &&
Jeff King8ddfce72023-05-08 15:04:57 -0400180 test "$patched" = "$(git hash-object --stdin <one)"
Junio C Hamano634ede32007-02-14 14:54:00 -0800181'
182
183test_expect_success 'apply patch --cached (autocrlf=input)' '
184
Junio C Hamano163b9592007-04-19 22:37:19 -0700185 rm -f tmp one dir/two three &&
Junio C Hamano5c66d0d2008-01-17 22:52:40 -0800186 git config core.autocrlf input &&
Junio C Hamano634ede32007-02-14 14:54:00 -0800187 git read-tree --reset -u HEAD &&
188
189 git apply --cached patch.file &&
Jeff King8ddfce72023-05-08 15:04:57 -0400190 test "$patched" = $(git rev-parse :one)
Junio C Hamano634ede32007-02-14 14:54:00 -0800191'
192
193test_expect_success 'apply patch --index (autocrlf=input)' '
194
Junio C Hamano163b9592007-04-19 22:37:19 -0700195 rm -f tmp one dir/two three &&
Junio C Hamano5c66d0d2008-01-17 22:52:40 -0800196 git config core.autocrlf input &&
Junio C Hamano634ede32007-02-14 14:54:00 -0800197 git read-tree --reset -u HEAD &&
198
199 git apply --index patch.file &&
Jeff King8ddfce72023-05-08 15:04:57 -0400200 test "$patched" = $(git rev-parse :one) &&
201 test "$patched" = $(git hash-object --stdin <one)
Junio C Hamano634ede32007-02-14 14:54:00 -0800202'
203
204test_expect_success 'apply patch (autocrlf=true)' '
205
Junio C Hamano163b9592007-04-19 22:37:19 -0700206 rm -f tmp one dir/two three &&
Junio C Hamano5c66d0d2008-01-17 22:52:40 -0800207 git config core.autocrlf true &&
Junio C Hamano634ede32007-02-14 14:54:00 -0800208 git read-tree --reset -u HEAD &&
209
Junio C Hamano634ede32007-02-14 14:54:00 -0800210 git apply patch.file &&
Jeff King8ddfce72023-05-08 15:04:57 -0400211 test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
Junio C Hamano634ede32007-02-14 14:54:00 -0800212'
213
214test_expect_success 'apply patch --cached (autocrlf=true)' '
215
Junio C Hamano163b9592007-04-19 22:37:19 -0700216 rm -f tmp one dir/two three &&
Junio C Hamano5c66d0d2008-01-17 22:52:40 -0800217 git config core.autocrlf true &&
Junio C Hamano634ede32007-02-14 14:54:00 -0800218 git read-tree --reset -u HEAD &&
219
220 git apply --cached patch.file &&
Jeff King8ddfce72023-05-08 15:04:57 -0400221 test "$patched" = $(git rev-parse :one)
Junio C Hamano634ede32007-02-14 14:54:00 -0800222'
223
Junio C Hamano67160272007-02-17 12:37:25 -0800224test_expect_success 'apply patch --index (autocrlf=true)' '
225
Junio C Hamano163b9592007-04-19 22:37:19 -0700226 rm -f tmp one dir/two three &&
Junio C Hamano5c66d0d2008-01-17 22:52:40 -0800227 git config core.autocrlf true &&
Junio C Hamano67160272007-02-17 12:37:25 -0800228 git read-tree --reset -u HEAD &&
229
230 git apply --index patch.file &&
Jeff King8ddfce72023-05-08 15:04:57 -0400231 test "$patched" = $(git rev-parse :one) &&
232 test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
Junio C Hamano67160272007-02-17 12:37:25 -0800233'
234
Junio C Hamano35ebfd62007-04-12 22:30:05 -0700235test_expect_success '.gitattributes says two is binary' '
236
Junio C Hamano163b9592007-04-19 22:37:19 -0700237 rm -f tmp one dir/two three &&
Junio C Hamanoe4aee102007-04-15 14:56:09 -0700238 echo "two -crlf" >.gitattributes &&
Junio C Hamano5c66d0d2008-01-17 22:52:40 -0800239 git config core.autocrlf true &&
Junio C Hamano35ebfd62007-04-12 22:30:05 -0700240 git read-tree --reset -u HEAD &&
241
Denton Liuf6041ab2019-12-20 10:15:53 -0800242 ! has_cr dir/two &&
Jeff King8ddfce72023-05-08 15:04:57 -0400243 has_cr one &&
Denton Liuf6041ab2019-12-20 10:15:53 -0800244 ! has_cr three
Junio C Hamano163b9592007-04-19 22:37:19 -0700245'
246
247test_expect_success '.gitattributes says two is input' '
248
249 rm -f tmp one dir/two three &&
250 echo "two crlf=input" >.gitattributes &&
251 git read-tree --reset -u HEAD &&
252
Denton Liuf6041ab2019-12-20 10:15:53 -0800253 ! has_cr dir/two
Junio C Hamano163b9592007-04-19 22:37:19 -0700254'
255
256test_expect_success '.gitattributes says two and three are text' '
257
258 rm -f tmp one dir/two three &&
259 echo "t* crlf" >.gitattributes &&
260 git read-tree --reset -u HEAD &&
261
Jeff King8ddfce72023-05-08 15:04:57 -0400262 has_cr dir/two &&
263 has_cr three
Junio C Hamano35ebfd62007-04-12 22:30:05 -0700264'
265
Junio C Hamano1a9d7e92007-08-14 01:41:02 -0700266test_expect_success 'in-tree .gitattributes (1)' '
267
268 echo "one -crlf" >>.gitattributes &&
269 git add .gitattributes &&
270 git commit -m "Add .gitattributes" &&
271
272 rm -rf tmp one dir .gitattributes patch.file three &&
273 git read-tree --reset -u HEAD &&
274
Denton Liuf6041ab2019-12-20 10:15:53 -0800275 ! has_cr one &&
Jeff King8ddfce72023-05-08 15:04:57 -0400276 has_cr three
Junio C Hamano1a9d7e92007-08-14 01:41:02 -0700277'
278
279test_expect_success 'in-tree .gitattributes (2)' '
280
281 rm -rf tmp one dir .gitattributes patch.file three &&
282 git read-tree --reset HEAD &&
283 git checkout-index -f -q -u -a &&
284
Denton Liuf6041ab2019-12-20 10:15:53 -0800285 ! has_cr one &&
Jeff King8ddfce72023-05-08 15:04:57 -0400286 has_cr three
Junio C Hamano1a9d7e92007-08-14 01:41:02 -0700287'
288
289test_expect_success 'in-tree .gitattributes (3)' '
290
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 &&
295
Denton Liuf6041ab2019-12-20 10:15:53 -0800296 ! has_cr one &&
Jeff King8ddfce72023-05-08 15:04:57 -0400297 has_cr three
Junio C Hamano1a9d7e92007-08-14 01:41:02 -0700298'
299
300test_expect_success 'in-tree .gitattributes (4)' '
301
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 &&
306
Denton Liuf6041ab2019-12-20 10:15:53 -0800307 ! has_cr one &&
Jeff King8ddfce72023-05-08 15:04:57 -0400308 has_cr three
Junio C Hamano1a9d7e92007-08-14 01:41:02 -0700309'
310
Kristian Amlieb9970452009-03-20 10:32:09 +0100311test_expect_success 'checkout with existing .gitattributes' '
312
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 &&
322
Johannes Schindelin06d53142020-11-18 23:44:21 +0000323 git checkout main~1 &&
324 git checkout main &&
Kristian Amlieb9970452009-03-20 10:32:09 +0100325 test "$(git diff-files --raw)" = ""
326
327'
328
329test_expect_success 'checkout when deleting .gitattributes' '
330
331 git rm .gitattributes &&
332 echo "contentsQ" | q_to_cr > .file2 &&
333 git add .file2 &&
Jonathan Nieder2dec68c2010-10-31 02:30:58 -0500334 git commit -m third &&
Kristian Amlieb9970452009-03-20 10:32:09 +0100335
Johannes Schindelin06d53142020-11-18 23:44:21 +0000336 git checkout main~1 &&
337 git checkout main &&
Stephen Boydc4f3f552010-01-25 16:33:57 -0800338 has_cr .file2
Kristian Amlieb9970452009-03-20 10:32:09 +0100339
340'
341
Steffen Prohaskad7b0a092007-10-18 22:02:35 +0200342test_expect_success 'invalid .gitattributes (must not crash)' '
343
344 echo "three +crlf" >>.gitattributes &&
345 git diff
346
347'
Finn Arne Gangstadc4805392010-05-12 00:37:57 +0200348# Some more tests here to add new autocrlf functionality.
349# We want to have a known state here, so start a bit from scratch
350
351test_expect_success 'setting up for new autocrlf tests' '
352 git config core.autocrlf false &&
353 git config core.safecrlf false &&
354 rm -rf .????* * &&
Eric Sunshine08495412021-12-09 00:11:05 -0500355 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 &&
Finn Arne Gangstadc4805392010-05-12 00:37:57 +0200358 git add -A . &&
359 git commit -m "alllf, allcrlf and mixed only" &&
360 git tag -a -m "message" autocrlf-checkpoint
361'
362
363test_expect_success 'report no change after setting autocrlf' '
364 git config core.autocrlf true &&
365 touch * &&
366 git diff --exit-code
367'
368
369test_expect_success 'files are clean after checkout' '
370 rm * &&
371 git checkout -f &&
372 git diff --exit-code
373'
374
375cr_to_Q_no_NL () {
376 tr '\015' Q | tr -d '\012'
377}
378
379test_expect_success 'LF only file gets CRLF with autocrlf' '
380 test "$(cr_to_Q_no_NL < alllf)" = "IQamQallQLFQ"
381'
382
383test_expect_success 'Mixed file is still mixed with autocrlf' '
384 test "$(cr_to_Q_no_NL < mixed)" = "OhhereisCRLFQintext"
385'
386
387test_expect_success 'CRLF only file has CRLF with autocrlf' '
388 test "$(cr_to_Q_no_NL < allcrlf)" = "IQamQallQCRLFQ"
389'
390
391test_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
399'
Steffen Prohaskad7b0a092007-10-18 22:02:35 +0200400
Junio C Hamano634ede32007-02-14 14:54:00 -0800401test_done