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 if test_have_prereq C_LOCALE_OUTPUT
92 test $(grep "CRLF will be replaced by LF" err | wc -l) = 1
97 test_expect_success
'safecrlf: git diff demotes safecrlf=true to warn' '
98 git config core.autocrlf input &&
99 git config core.safecrlf true &&
104 test_expect_success
'safecrlf: no warning with safecrlf=false' '
105 git config core.autocrlf input &&
106 git config core.safecrlf false &&
108 for w in I am all CRLF; do echo $w; done | append_cr >allcrlf &&
109 git add allcrlf 2>err &&
110 test_must_be_empty err
114 test_expect_success
'switch off autocrlf, safecrlf, reset HEAD' '
115 git config core.autocrlf false &&
116 git config core.safecrlf false &&
117 git reset --hard HEAD^
120 test_expect_success
'update with autocrlf=input' '
122 rm -f tmp one dir/two three &&
123 git read-tree --reset -u HEAD &&
124 git config core.autocrlf input &&
125 munge_cr append one &&
126 munge_cr append dir/two &&
127 git update-index -- one dir/two &&
128 differs=$(git diff-index --cached HEAD) &&
129 verbose test -z "$differs"
133 test_expect_success
'update with autocrlf=true' '
135 rm -f tmp one dir/two three &&
136 git read-tree --reset -u HEAD &&
137 git config core.autocrlf true &&
138 munge_cr append one &&
139 munge_cr append dir/two &&
140 git update-index -- one dir/two &&
141 differs=$(git diff-index --cached HEAD) &&
142 verbose test -z "$differs"
146 test_expect_success
'checkout with autocrlf=true' '
148 rm -f tmp one dir/two three &&
149 git config core.autocrlf true &&
150 git read-tree --reset -u HEAD &&
151 munge_cr remove one &&
152 munge_cr remove dir/two &&
153 git update-index -- one dir/two &&
154 test "$one" = $(git hash-object --stdin <one) &&
155 test "$two" = $(git hash-object --stdin <dir/two) &&
156 differs=$(git diff-index --cached HEAD) &&
157 verbose test -z "$differs"
160 test_expect_success
'checkout with autocrlf=input' '
162 rm -f tmp one dir/two three &&
163 git config core.autocrlf input &&
164 git read-tree --reset -u HEAD &&
167 git update-index -- one dir/two &&
168 test "$one" = $(git hash-object --stdin <one) &&
169 test "$two" = $(git hash-object --stdin <dir/two) &&
170 differs=$(git diff-index --cached HEAD) &&
171 verbose test -z "$differs"
174 test_expect_success
'apply patch (autocrlf=input)' '
176 rm -f tmp one dir/two three &&
177 git config core.autocrlf input &&
178 git read-tree --reset -u HEAD &&
180 git apply patch.file &&
181 verbose test "$patched" = "$(git hash-object --stdin <one)"
184 test_expect_success
'apply patch --cached (autocrlf=input)' '
186 rm -f tmp one dir/two three &&
187 git config core.autocrlf input &&
188 git read-tree --reset -u HEAD &&
190 git apply --cached patch.file &&
191 verbose test "$patched" = $(git rev-parse :one)
194 test_expect_success
'apply patch --index (autocrlf=input)' '
196 rm -f tmp one dir/two three &&
197 git config core.autocrlf input &&
198 git read-tree --reset -u HEAD &&
200 git apply --index patch.file &&
201 verbose test "$patched" = $(git rev-parse :one) &&
202 verbose test "$patched" = $(git hash-object --stdin <one)
205 test_expect_success
'apply patch (autocrlf=true)' '
207 rm -f tmp one dir/two three &&
208 git config core.autocrlf true &&
209 git read-tree --reset -u HEAD &&
211 git apply patch.file &&
212 verbose test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
215 test_expect_success
'apply patch --cached (autocrlf=true)' '
217 rm -f tmp one dir/two three &&
218 git config core.autocrlf true &&
219 git read-tree --reset -u HEAD &&
221 git apply --cached patch.file &&
222 verbose test "$patched" = $(git rev-parse :one)
225 test_expect_success
'apply patch --index (autocrlf=true)' '
227 rm -f tmp one dir/two three &&
228 git config core.autocrlf true &&
229 git read-tree --reset -u HEAD &&
231 git apply --index patch.file &&
232 verbose test "$patched" = $(git rev-parse :one) &&
233 verbose test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
236 test_expect_success
'.gitattributes says two is binary' '
238 rm -f tmp one dir/two three &&
239 echo "two -crlf" >.gitattributes &&
240 git config core.autocrlf true &&
241 git read-tree --reset -u HEAD &&
244 verbose has_cr one &&
248 test_expect_success
'.gitattributes says two is input' '
250 rm -f tmp one dir/two three &&
251 echo "two crlf=input" >.gitattributes &&
252 git read-tree --reset -u HEAD &&
257 test_expect_success
'.gitattributes says two and three are text' '
259 rm -f tmp one dir/two three &&
260 echo "t* crlf" >.gitattributes &&
261 git read-tree --reset -u HEAD &&
263 verbose has_cr dir/two &&
267 test_expect_success
'in-tree .gitattributes (1)' '
269 echo "one -crlf" >>.gitattributes &&
270 git add .gitattributes &&
271 git commit -m "Add .gitattributes" &&
273 rm -rf tmp one dir .gitattributes patch.file three &&
274 git read-tree --reset -u HEAD &&
280 test_expect_success
'in-tree .gitattributes (2)' '
282 rm -rf tmp one dir .gitattributes patch.file three &&
283 git read-tree --reset HEAD &&
284 git checkout-index -f -q -u -a &&
290 test_expect_success
'in-tree .gitattributes (3)' '
292 rm -rf tmp one dir .gitattributes patch.file three &&
293 git read-tree --reset HEAD &&
294 git checkout-index -u .gitattributes &&
295 git checkout-index -u one dir/two three &&
301 test_expect_success
'in-tree .gitattributes (4)' '
303 rm -rf tmp one dir .gitattributes patch.file three &&
304 git read-tree --reset HEAD &&
305 git checkout-index -u one dir/two three &&
306 git checkout-index -u .gitattributes &&
312 test_expect_success
'checkout with existing .gitattributes' '
314 git config core.autocrlf true &&
315 git config --unset core.safecrlf &&
316 echo ".file2 -crlfQ" | q_to_cr >> .gitattributes &&
317 git add .gitattributes &&
318 git commit -m initial &&
319 echo ".file -crlfQ" | q_to_cr >> .gitattributes &&
320 echo "contents" > .file &&
321 git add .gitattributes .file &&
322 git commit -m second &&
324 git checkout main~1 &&
326 test "$(git diff-files --raw)" = ""
330 test_expect_success
'checkout when deleting .gitattributes' '
332 git rm .gitattributes &&
333 echo "contentsQ" | q_to_cr > .file2 &&
335 git commit -m third &&
337 git checkout main~1 &&
343 test_expect_success
'invalid .gitattributes (must not crash)' '
345 echo "three +crlf" >>.gitattributes &&
349 # Some more tests here to add new autocrlf functionality.
350 # We want to have a known state here, so start a bit from scratch
352 test_expect_success
'setting up for new autocrlf tests' '
353 git config core.autocrlf false &&
354 git config core.safecrlf false &&
356 for w in I am all LF; do echo $w; done >alllf &&
357 for w in Oh here is CRLFQ in text; do echo $w; done | q_to_cr >mixed &&
358 for w in I am all CRLF; do echo $w; done | append_cr >allcrlf &&
360 git commit -m "alllf, allcrlf and mixed only" &&
361 git tag -a -m "message" autocrlf-checkpoint
364 test_expect_success
'report no change after setting autocrlf' '
365 git config core.autocrlf true &&
370 test_expect_success
'files are clean after checkout' '
377 tr '\015' Q |
tr -d '\012'
380 test_expect_success
'LF only file gets CRLF with autocrlf' '
381 test "$(cr_to_Q_no_NL < alllf)" = "IQamQallQLFQ"
384 test_expect_success
'Mixed file is still mixed with autocrlf' '
385 test "$(cr_to_Q_no_NL < mixed)" = "OhhereisCRLFQintext"
388 test_expect_success
'CRLF only file has CRLF with autocrlf' '
389 test "$(cr_to_Q_no_NL < allcrlf)" = "IQamQallQCRLFQ"
392 test_expect_success
'New CRLF file gets LF in repo' '
393 tr -d "\015" < alllf | append_cr > alllf2 &&
395 git commit -m "alllf2 added" &&
396 git config core.autocrlf false &&
399 test_cmp alllf alllf2