]> git.ipfire.org Git - thirdparty/git.git/blob - t/t0020-crlf.sh
tests: mark tests relying on the current default for `init.defaultBranch`
[thirdparty/git.git] / t / t0020-crlf.sh
1 #!/bin/sh
2
3 test_description='CRLF conversion'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
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 if test_have_prereq C_LOCALE_OUTPUT
91 then
92 test $(grep "CRLF will be replaced by LF" err | wc -l) = 1
93 fi
94 '
95
96
97 test_expect_success 'safecrlf: git diff demotes safecrlf=true to warn' '
98 git config core.autocrlf input &&
99 git config core.safecrlf true &&
100 git diff HEAD
101 '
102
103
104 test_expect_success 'safecrlf: no warning with safecrlf=false' '
105 git config core.autocrlf input &&
106 git config core.safecrlf false &&
107
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
111 '
112
113
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^
118 '
119
120 test_expect_success 'update with autocrlf=input' '
121
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"
130
131 '
132
133 test_expect_success 'update with autocrlf=true' '
134
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"
143
144 '
145
146 test_expect_success 'checkout with autocrlf=true' '
147
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"
158 '
159
160 test_expect_success 'checkout with autocrlf=input' '
161
162 rm -f tmp one dir/two three &&
163 git config core.autocrlf input &&
164 git read-tree --reset -u HEAD &&
165 ! has_cr one &&
166 ! has_cr dir/two &&
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"
172 '
173
174 test_expect_success 'apply patch (autocrlf=input)' '
175
176 rm -f tmp one dir/two three &&
177 git config core.autocrlf input &&
178 git read-tree --reset -u HEAD &&
179
180 git apply patch.file &&
181 verbose test "$patched" = "$(git hash-object --stdin <one)"
182 '
183
184 test_expect_success 'apply patch --cached (autocrlf=input)' '
185
186 rm -f tmp one dir/two three &&
187 git config core.autocrlf input &&
188 git read-tree --reset -u HEAD &&
189
190 git apply --cached patch.file &&
191 verbose test "$patched" = $(git rev-parse :one)
192 '
193
194 test_expect_success 'apply patch --index (autocrlf=input)' '
195
196 rm -f tmp one dir/two three &&
197 git config core.autocrlf input &&
198 git read-tree --reset -u HEAD &&
199
200 git apply --index patch.file &&
201 verbose test "$patched" = $(git rev-parse :one) &&
202 verbose test "$patched" = $(git hash-object --stdin <one)
203 '
204
205 test_expect_success 'apply patch (autocrlf=true)' '
206
207 rm -f tmp one dir/two three &&
208 git config core.autocrlf true &&
209 git read-tree --reset -u HEAD &&
210
211 git apply patch.file &&
212 verbose test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
213 '
214
215 test_expect_success 'apply patch --cached (autocrlf=true)' '
216
217 rm -f tmp one dir/two three &&
218 git config core.autocrlf true &&
219 git read-tree --reset -u HEAD &&
220
221 git apply --cached patch.file &&
222 verbose test "$patched" = $(git rev-parse :one)
223 '
224
225 test_expect_success 'apply patch --index (autocrlf=true)' '
226
227 rm -f tmp one dir/two three &&
228 git config core.autocrlf true &&
229 git read-tree --reset -u HEAD &&
230
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)"
234 '
235
236 test_expect_success '.gitattributes says two is binary' '
237
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 &&
242
243 ! has_cr dir/two &&
244 verbose has_cr one &&
245 ! has_cr three
246 '
247
248 test_expect_success '.gitattributes says two is input' '
249
250 rm -f tmp one dir/two three &&
251 echo "two crlf=input" >.gitattributes &&
252 git read-tree --reset -u HEAD &&
253
254 ! has_cr dir/two
255 '
256
257 test_expect_success '.gitattributes says two and three are text' '
258
259 rm -f tmp one dir/two three &&
260 echo "t* crlf" >.gitattributes &&
261 git read-tree --reset -u HEAD &&
262
263 verbose has_cr dir/two &&
264 verbose has_cr three
265 '
266
267 test_expect_success 'in-tree .gitattributes (1)' '
268
269 echo "one -crlf" >>.gitattributes &&
270 git add .gitattributes &&
271 git commit -m "Add .gitattributes" &&
272
273 rm -rf tmp one dir .gitattributes patch.file three &&
274 git read-tree --reset -u HEAD &&
275
276 ! has_cr one &&
277 verbose has_cr three
278 '
279
280 test_expect_success 'in-tree .gitattributes (2)' '
281
282 rm -rf tmp one dir .gitattributes patch.file three &&
283 git read-tree --reset HEAD &&
284 git checkout-index -f -q -u -a &&
285
286 ! has_cr one &&
287 verbose has_cr three
288 '
289
290 test_expect_success 'in-tree .gitattributes (3)' '
291
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 &&
296
297 ! has_cr one &&
298 verbose has_cr three
299 '
300
301 test_expect_success 'in-tree .gitattributes (4)' '
302
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 &&
307
308 ! has_cr one &&
309 verbose has_cr three
310 '
311
312 test_expect_success 'checkout with existing .gitattributes' '
313
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 &&
323
324 git checkout master~1 &&
325 git checkout master &&
326 test "$(git diff-files --raw)" = ""
327
328 '
329
330 test_expect_success 'checkout when deleting .gitattributes' '
331
332 git rm .gitattributes &&
333 echo "contentsQ" | q_to_cr > .file2 &&
334 git add .file2 &&
335 git commit -m third &&
336
337 git checkout master~1 &&
338 git checkout master &&
339 has_cr .file2
340
341 '
342
343 test_expect_success 'invalid .gitattributes (must not crash)' '
344
345 echo "three +crlf" >>.gitattributes &&
346 git diff
347
348 '
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
351
352 test_expect_success 'setting up for new autocrlf tests' '
353 git config core.autocrlf false &&
354 git config core.safecrlf false &&
355 rm -rf .????* * &&
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 &&
359 git add -A . &&
360 git commit -m "alllf, allcrlf and mixed only" &&
361 git tag -a -m "message" autocrlf-checkpoint
362 '
363
364 test_expect_success 'report no change after setting autocrlf' '
365 git config core.autocrlf true &&
366 touch * &&
367 git diff --exit-code
368 '
369
370 test_expect_success 'files are clean after checkout' '
371 rm * &&
372 git checkout -f &&
373 git diff --exit-code
374 '
375
376 cr_to_Q_no_NL () {
377 tr '\015' Q | tr -d '\012'
378 }
379
380 test_expect_success 'LF only file gets CRLF with autocrlf' '
381 test "$(cr_to_Q_no_NL < alllf)" = "IQamQallQLFQ"
382 '
383
384 test_expect_success 'Mixed file is still mixed with autocrlf' '
385 test "$(cr_to_Q_no_NL < mixed)" = "OhhereisCRLFQintext"
386 '
387
388 test_expect_success 'CRLF only file has CRLF with autocrlf' '
389 test "$(cr_to_Q_no_NL < allcrlf)" = "IQamQallQCRLFQ"
390 '
391
392 test_expect_success 'New CRLF file gets LF in repo' '
393 tr -d "\015" < alllf | append_cr > alllf2 &&
394 git add alllf2 &&
395 git commit -m "alllf2 added" &&
396 git config core.autocrlf false &&
397 rm * &&
398 git checkout -f &&
399 test_cmp alllf alllf2
400 '
401
402 test_done