]> git.ipfire.org Git - thirdparty/git.git/blob - t/t0020-crlf.sh
Merge branch 'ah/commit-graph-leakplug'
[thirdparty/git.git] / t / t0020-crlf.sh
1 #!/bin/sh
2
3 test_description='CRLF conversion'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 . ./test-lib.sh
9
10 has_cr() {
11 tr '\015' Q <"$1" | grep Q >/dev/null
12 }
13
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"
19 }
20
21 test_expect_success setup '
22
23 git config core.autocrlf false &&
24
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 . &&
30
31 git commit -m initial &&
32
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) &&
37
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
42 '
43
44 test_expect_success 'safecrlf: autocrlf=input, all CRLF' '
45
46 git config core.autocrlf input &&
47 git config core.safecrlf true &&
48
49 for w in I am all CRLF; do echo $w; done | append_cr >allcrlf &&
50 test_must_fail git add allcrlf
51 '
52
53 test_expect_success 'safecrlf: autocrlf=input, mixed LF/CRLF' '
54
55 git config core.autocrlf input &&
56 git config core.safecrlf true &&
57
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
60 '
61
62 test_expect_success 'safecrlf: autocrlf=true, all LF' '
63
64 git config core.autocrlf true &&
65 git config core.safecrlf true &&
66
67 for w in I am all LF; do echo $w; done >alllf &&
68 test_must_fail git add alllf
69 '
70
71 test_expect_success 'safecrlf: autocrlf=true mixed LF/CRLF' '
72
73 git config core.autocrlf true &&
74 git config core.safecrlf true &&
75
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
78 '
79
80 test_expect_success 'safecrlf: print warning only once' '
81
82 git config core.autocrlf input &&
83 git config core.safecrlf warn &&
84
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
92 '
93
94
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
99 '
100
101
102 test_expect_success 'safecrlf: no warning with safecrlf=false' '
103 git config core.autocrlf input &&
104 git config core.safecrlf false &&
105
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
109 '
110
111
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^
116 '
117
118 test_expect_success 'update with autocrlf=input' '
119
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"
128
129 '
130
131 test_expect_success 'update with autocrlf=true' '
132
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"
141
142 '
143
144 test_expect_success 'checkout with autocrlf=true' '
145
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"
156 '
157
158 test_expect_success 'checkout with autocrlf=input' '
159
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"
170 '
171
172 test_expect_success 'apply patch (autocrlf=input)' '
173
174 rm -f tmp one dir/two three &&
175 git config core.autocrlf input &&
176 git read-tree --reset -u HEAD &&
177
178 git apply patch.file &&
179 verbose test "$patched" = "$(git hash-object --stdin <one)"
180 '
181
182 test_expect_success 'apply patch --cached (autocrlf=input)' '
183
184 rm -f tmp one dir/two three &&
185 git config core.autocrlf input &&
186 git read-tree --reset -u HEAD &&
187
188 git apply --cached patch.file &&
189 verbose test "$patched" = $(git rev-parse :one)
190 '
191
192 test_expect_success 'apply patch --index (autocrlf=input)' '
193
194 rm -f tmp one dir/two three &&
195 git config core.autocrlf input &&
196 git read-tree --reset -u HEAD &&
197
198 git apply --index patch.file &&
199 verbose test "$patched" = $(git rev-parse :one) &&
200 verbose test "$patched" = $(git hash-object --stdin <one)
201 '
202
203 test_expect_success 'apply patch (autocrlf=true)' '
204
205 rm -f tmp one dir/two three &&
206 git config core.autocrlf true &&
207 git read-tree --reset -u HEAD &&
208
209 git apply patch.file &&
210 verbose test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
211 '
212
213 test_expect_success 'apply patch --cached (autocrlf=true)' '
214
215 rm -f tmp one dir/two three &&
216 git config core.autocrlf true &&
217 git read-tree --reset -u HEAD &&
218
219 git apply --cached patch.file &&
220 verbose test "$patched" = $(git rev-parse :one)
221 '
222
223 test_expect_success 'apply patch --index (autocrlf=true)' '
224
225 rm -f tmp one dir/two three &&
226 git config core.autocrlf true &&
227 git read-tree --reset -u HEAD &&
228
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)"
232 '
233
234 test_expect_success '.gitattributes says two is binary' '
235
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 &&
240
241 ! has_cr dir/two &&
242 verbose has_cr one &&
243 ! has_cr three
244 '
245
246 test_expect_success '.gitattributes says two is input' '
247
248 rm -f tmp one dir/two three &&
249 echo "two crlf=input" >.gitattributes &&
250 git read-tree --reset -u HEAD &&
251
252 ! has_cr dir/two
253 '
254
255 test_expect_success '.gitattributes says two and three are text' '
256
257 rm -f tmp one dir/two three &&
258 echo "t* crlf" >.gitattributes &&
259 git read-tree --reset -u HEAD &&
260
261 verbose has_cr dir/two &&
262 verbose has_cr three
263 '
264
265 test_expect_success 'in-tree .gitattributes (1)' '
266
267 echo "one -crlf" >>.gitattributes &&
268 git add .gitattributes &&
269 git commit -m "Add .gitattributes" &&
270
271 rm -rf tmp one dir .gitattributes patch.file three &&
272 git read-tree --reset -u HEAD &&
273
274 ! has_cr one &&
275 verbose has_cr three
276 '
277
278 test_expect_success 'in-tree .gitattributes (2)' '
279
280 rm -rf tmp one dir .gitattributes patch.file three &&
281 git read-tree --reset HEAD &&
282 git checkout-index -f -q -u -a &&
283
284 ! has_cr one &&
285 verbose has_cr three
286 '
287
288 test_expect_success 'in-tree .gitattributes (3)' '
289
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 &&
294
295 ! has_cr one &&
296 verbose has_cr three
297 '
298
299 test_expect_success 'in-tree .gitattributes (4)' '
300
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 &&
305
306 ! has_cr one &&
307 verbose has_cr three
308 '
309
310 test_expect_success 'checkout with existing .gitattributes' '
311
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 &&
321
322 git checkout main~1 &&
323 git checkout main &&
324 test "$(git diff-files --raw)" = ""
325
326 '
327
328 test_expect_success 'checkout when deleting .gitattributes' '
329
330 git rm .gitattributes &&
331 echo "contentsQ" | q_to_cr > .file2 &&
332 git add .file2 &&
333 git commit -m third &&
334
335 git checkout main~1 &&
336 git checkout main &&
337 has_cr .file2
338
339 '
340
341 test_expect_success 'invalid .gitattributes (must not crash)' '
342
343 echo "three +crlf" >>.gitattributes &&
344 git diff
345
346 '
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
349
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
360 '
361
362 test_expect_success 'report no change after setting autocrlf' '
363 git config core.autocrlf true &&
364 touch * &&
365 git diff --exit-code
366 '
367
368 test_expect_success 'files are clean after checkout' '
369 rm * &&
370 git checkout -f &&
371 git diff --exit-code
372 '
373
374 cr_to_Q_no_NL () {
375 tr '\015' Q | tr -d '\012'
376 }
377
378 test_expect_success 'LF only file gets CRLF with autocrlf' '
379 test "$(cr_to_Q_no_NL < alllf)" = "IQamQallQLFQ"
380 '
381
382 test_expect_success 'Mixed file is still mixed with autocrlf' '
383 test "$(cr_to_Q_no_NL < mixed)" = "OhhereisCRLFQintext"
384 '
385
386 test_expect_success 'CRLF only file has CRLF with autocrlf' '
387 test "$(cr_to_Q_no_NL < allcrlf)" = "IQamQallQCRLFQ"
388 '
389
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
398 '
399
400 test_done