3 test_description='CRLF conversion'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
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>
17 "${1}_cr" <"$2" >tmp &&
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
242 verbose has_cr one &&
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
333 git commit -m third &&
335 git checkout main~1 &&
341 test_expect_success 'invalid .gitattributes (must not crash)' '
343 echo "three +crlf" >>.gitattributes &&
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 &&
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 &&
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 &&
368 test_expect_success 'files are clean after checkout' '
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 &&
393 git commit -m "alllf2 added" &&
394 git config core.autocrlf false &&
397 test_cmp alllf alllf2