]> git.ipfire.org Git - thirdparty/git.git/blame - t/t0020-crlf.sh
l10n: it.po: update the Italian translation for Git 2.24.0 round #2
[thirdparty/git.git] / t / t0020-crlf.sh
CommitLineData
634ede32
JH
1#!/bin/sh
2
3test_description='CRLF conversion'
4
5. ./test-lib.sh
6
c4f3f551
SB
7has_cr() {
8 tr '\015' Q <"$1" | grep Q >/dev/null
634ede32
JH
9}
10
fd777141
JK
11# add or remove CRs to disk file in-place
12# usage: munge_cr <append|remove> <file>
13munge_cr () {
14 "${1}_cr" <"$2" >tmp &&
15 mv tmp "$2"
16}
17
634ede32
JH
18test_expect_success setup '
19
5c66d0d4 20 git config core.autocrlf false &&
634ede32
JH
21
22 for w in Hello world how are you; do echo $w; done >one &&
23 mkdir dir &&
24 for w in I am very very fine thank you; do echo $w; done >dir/two &&
163b9591 25 for w in Oh here is NULQin text here; do echo $w; done | q_to_nul >three &&
634ede32
JH
26 git add . &&
27
28 git commit -m initial &&
29
def226bd
EP
30 one=$(git rev-parse HEAD:one) &&
31 dir=$(git rev-parse HEAD:dir) &&
32 two=$(git rev-parse HEAD:dir/two) &&
33 three=$(git rev-parse HEAD:three) &&
634ede32
JH
34
35 for w in Some extra lines here; do echo $w; done >>one &&
36 git diff >patch.file &&
def226bd 37 patched=$(git hash-object --stdin <one) &&
be86fb3f 38 git read-tree --reset -u HEAD
634ede32
JH
39'
40
21e5ad50
SP
41test_expect_success 'safecrlf: autocrlf=input, all CRLF' '
42
43 git config core.autocrlf input &&
44 git config core.safecrlf true &&
45
46 for w in I am all CRLF; do echo $w; done | append_cr >allcrlf &&
d492b31c 47 test_must_fail git add allcrlf
21e5ad50
SP
48'
49
50test_expect_success 'safecrlf: autocrlf=input, mixed LF/CRLF' '
51
52 git config core.autocrlf input &&
53 git config core.safecrlf true &&
54
55 for w in Oh here is CRLFQ in text; do echo $w; done | q_to_cr >mixed &&
d492b31c 56 test_must_fail git add mixed
21e5ad50
SP
57'
58
59test_expect_success 'safecrlf: autocrlf=true, all LF' '
60
61 git config core.autocrlf true &&
62 git config core.safecrlf true &&
63
64 for w in I am all LF; do echo $w; done >alllf &&
d492b31c 65 test_must_fail git add alllf
21e5ad50
SP
66'
67
68test_expect_success 'safecrlf: autocrlf=true mixed LF/CRLF' '
69
70 git config core.autocrlf true &&
71 git config core.safecrlf true &&
72
73 for w in Oh here is CRLFQ in text; do echo $w; done | q_to_cr >mixed &&
d492b31c 74 test_must_fail git add mixed
21e5ad50
SP
75'
76
77test_expect_success 'safecrlf: print warning only once' '
78
79 git config core.autocrlf input &&
80 git config core.safecrlf warn &&
81
82 for w in I am all LF; do echo $w; done >doublewarn &&
83 git add doublewarn &&
84 git commit -m "nowarn" &&
85 for w in Oh here is CRLFQ in text; do echo $w; done | q_to_cr >doublewarn &&
87cb7845
VA
86 git add doublewarn 2>err &&
87 if test_have_prereq C_LOCALE_OUTPUT
88 then
89 test $(grep "CRLF will be replaced by LF" err | wc -l) = 1
90 fi
21e5ad50
SP
91'
92
5430bb28
JH
93
94test_expect_success 'safecrlf: git diff demotes safecrlf=true to warn' '
95 git config core.autocrlf input &&
96 git config core.safecrlf true &&
97 git diff HEAD
98'
99
100
6cb09125
AS
101test_expect_success 'safecrlf: no warning with safecrlf=false' '
102 git config core.autocrlf input &&
103 git config core.safecrlf false &&
104
105 for w in I am all CRLF; do echo $w; done | append_cr >allcrlf &&
106 git add allcrlf 2>err &&
107 test_must_be_empty err
108'
109
110
21e5ad50
SP
111test_expect_success 'switch off autocrlf, safecrlf, reset HEAD' '
112 git config core.autocrlf false &&
113 git config core.safecrlf false &&
114 git reset --hard HEAD^
115'
116
634ede32
JH
117test_expect_success 'update with autocrlf=input' '
118
163b9591 119 rm -f tmp one dir/two three &&
634ede32 120 git read-tree --reset -u HEAD &&
5c66d0d4 121 git config core.autocrlf input &&
fd777141
JK
122 munge_cr append one &&
123 munge_cr append dir/two &&
124 git update-index -- one dir/two &&
def226bd 125 differs=$(git diff-index --cached HEAD) &&
9157c5cb 126 verbose test -z "$differs"
634ede32
JH
127
128'
129
130test_expect_success 'update with autocrlf=true' '
131
163b9591 132 rm -f tmp one dir/two three &&
634ede32 133 git read-tree --reset -u HEAD &&
5c66d0d4 134 git config core.autocrlf true &&
fd777141
JK
135 munge_cr append one &&
136 munge_cr append dir/two &&
137 git update-index -- one dir/two &&
def226bd 138 differs=$(git diff-index --cached HEAD) &&
9157c5cb 139 verbose test -z "$differs"
634ede32
JH
140
141'
142
143test_expect_success 'checkout with autocrlf=true' '
144
163b9591 145 rm -f tmp one dir/two three &&
5c66d0d4 146 git config core.autocrlf true &&
634ede32 147 git read-tree --reset -u HEAD &&
fd777141
JK
148 munge_cr remove one &&
149 munge_cr remove dir/two &&
150 git update-index -- one dir/two &&
def226bd
EP
151 test "$one" = $(git hash-object --stdin <one) &&
152 test "$two" = $(git hash-object --stdin <dir/two) &&
153 differs=$(git diff-index --cached HEAD) &&
9157c5cb 154 verbose test -z "$differs"
634ede32
JH
155'
156
157test_expect_success 'checkout with autocrlf=input' '
158
163b9591 159 rm -f tmp one dir/two three &&
5c66d0d4 160 git config core.autocrlf input &&
634ede32 161 git read-tree --reset -u HEAD &&
fd777141 162 test_must_fail has_cr one &&
30612cb6 163 test_must_fail has_cr dir/two &&
fd777141 164 git update-index -- one dir/two &&
def226bd
EP
165 test "$one" = $(git hash-object --stdin <one) &&
166 test "$two" = $(git hash-object --stdin <dir/two) &&
167 differs=$(git diff-index --cached HEAD) &&
9157c5cb 168 verbose test -z "$differs"
634ede32
JH
169'
170
171test_expect_success 'apply patch (autocrlf=input)' '
172
163b9591 173 rm -f tmp one dir/two three &&
5c66d0d4 174 git config core.autocrlf input &&
634ede32
JH
175 git read-tree --reset -u HEAD &&
176
177 git apply patch.file &&
9157c5cb 178 verbose test "$patched" = "$(git hash-object --stdin <one)"
634ede32
JH
179'
180
181test_expect_success 'apply patch --cached (autocrlf=input)' '
182
163b9591 183 rm -f tmp one dir/two three &&
5c66d0d4 184 git config core.autocrlf input &&
634ede32
JH
185 git read-tree --reset -u HEAD &&
186
187 git apply --cached patch.file &&
9157c5cb 188 verbose test "$patched" = $(git rev-parse :one)
634ede32
JH
189'
190
191test_expect_success 'apply patch --index (autocrlf=input)' '
192
163b9591 193 rm -f tmp one dir/two three &&
5c66d0d4 194 git config core.autocrlf input &&
634ede32
JH
195 git read-tree --reset -u HEAD &&
196
197 git apply --index patch.file &&
9157c5cb
JK
198 verbose test "$patched" = $(git rev-parse :one) &&
199 verbose test "$patched" = $(git hash-object --stdin <one)
634ede32
JH
200'
201
202test_expect_success 'apply patch (autocrlf=true)' '
203
163b9591 204 rm -f tmp one dir/two three &&
5c66d0d4 205 git config core.autocrlf true &&
634ede32
JH
206 git read-tree --reset -u HEAD &&
207
634ede32 208 git apply patch.file &&
9157c5cb 209 verbose test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
634ede32
JH
210'
211
212test_expect_success 'apply patch --cached (autocrlf=true)' '
213
163b9591 214 rm -f tmp one dir/two three &&
5c66d0d4 215 git config core.autocrlf true &&
634ede32
JH
216 git read-tree --reset -u HEAD &&
217
218 git apply --cached patch.file &&
9157c5cb 219 verbose test "$patched" = $(git rev-parse :one)
634ede32
JH
220'
221
67160271
JH
222test_expect_success 'apply patch --index (autocrlf=true)' '
223
163b9591 224 rm -f tmp one dir/two three &&
5c66d0d4 225 git config core.autocrlf true &&
67160271
JH
226 git read-tree --reset -u HEAD &&
227
228 git apply --index patch.file &&
9157c5cb
JK
229 verbose test "$patched" = $(git rev-parse :one) &&
230 verbose test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
67160271
JH
231'
232
35ebfd6a
JH
233test_expect_success '.gitattributes says two is binary' '
234
163b9591 235 rm -f tmp one dir/two three &&
e4aee10a 236 echo "two -crlf" >.gitattributes &&
5c66d0d4 237 git config core.autocrlf true &&
35ebfd6a
JH
238 git read-tree --reset -u HEAD &&
239
be86fb3f
JK
240 test_must_fail has_cr dir/two &&
241 verbose has_cr one &&
242 test_must_fail has_cr three
163b9591
JH
243'
244
245test_expect_success '.gitattributes says two is input' '
246
247 rm -f tmp one dir/two three &&
248 echo "two crlf=input" >.gitattributes &&
249 git read-tree --reset -u HEAD &&
250
be86fb3f 251 test_must_fail has_cr dir/two
163b9591
JH
252'
253
254test_expect_success '.gitattributes says two and three are text' '
255
256 rm -f tmp one dir/two three &&
257 echo "t* crlf" >.gitattributes &&
258 git read-tree --reset -u HEAD &&
259
9157c5cb
JK
260 verbose has_cr dir/two &&
261 verbose has_cr three
35ebfd6a
JH
262'
263
1a9d7e9b
JH
264test_expect_success 'in-tree .gitattributes (1)' '
265
266 echo "one -crlf" >>.gitattributes &&
267 git add .gitattributes &&
268 git commit -m "Add .gitattributes" &&
269
270 rm -rf tmp one dir .gitattributes patch.file three &&
271 git read-tree --reset -u HEAD &&
272
9157c5cb
JK
273 test_must_fail has_cr one &&
274 verbose has_cr three
1a9d7e9b
JH
275'
276
277test_expect_success 'in-tree .gitattributes (2)' '
278
279 rm -rf tmp one dir .gitattributes patch.file three &&
280 git read-tree --reset HEAD &&
281 git checkout-index -f -q -u -a &&
282
9157c5cb
JK
283 test_must_fail has_cr one &&
284 verbose has_cr three
1a9d7e9b
JH
285'
286
287test_expect_success 'in-tree .gitattributes (3)' '
288
289 rm -rf tmp one dir .gitattributes patch.file three &&
290 git read-tree --reset HEAD &&
291 git checkout-index -u .gitattributes &&
292 git checkout-index -u one dir/two three &&
293
9157c5cb
JK
294 test_must_fail has_cr one &&
295 verbose has_cr three
1a9d7e9b
JH
296'
297
298test_expect_success 'in-tree .gitattributes (4)' '
299
300 rm -rf tmp one dir .gitattributes patch.file three &&
301 git read-tree --reset HEAD &&
302 git checkout-index -u one dir/two three &&
303 git checkout-index -u .gitattributes &&
304
9157c5cb
JK
305 test_must_fail has_cr one &&
306 verbose has_cr three
1a9d7e9b
JH
307'
308
b997045e
KA
309test_expect_success 'checkout with existing .gitattributes' '
310
311 git config core.autocrlf true &&
312 git config --unset core.safecrlf &&
313 echo ".file2 -crlfQ" | q_to_cr >> .gitattributes &&
314 git add .gitattributes &&
315 git commit -m initial &&
316 echo ".file -crlfQ" | q_to_cr >> .gitattributes &&
317 echo "contents" > .file &&
318 git add .gitattributes .file &&
319 git commit -m second &&
320
321 git checkout master~1 &&
322 git checkout master &&
323 test "$(git diff-files --raw)" = ""
324
325'
326
327test_expect_success 'checkout when deleting .gitattributes' '
328
329 git rm .gitattributes &&
330 echo "contentsQ" | q_to_cr > .file2 &&
331 git add .file2 &&
2dec68cf 332 git commit -m third &&
b997045e
KA
333
334 git checkout master~1 &&
335 git checkout master &&
c4f3f551 336 has_cr .file2
b997045e
KA
337
338'
339
d7b0a093
SP
340test_expect_success 'invalid .gitattributes (must not crash)' '
341
342 echo "three +crlf" >>.gitattributes &&
343 git diff
344
345'
c4805393
FAG
346# Some more tests here to add new autocrlf functionality.
347# We want to have a known state here, so start a bit from scratch
348
349test_expect_success 'setting up for new autocrlf tests' '
350 git config core.autocrlf false &&
351 git config core.safecrlf false &&
352 rm -rf .????* * &&
353 for w in I am all LF; do echo $w; done >alllf &&
354 for w in Oh here is CRLFQ in text; do echo $w; done | q_to_cr >mixed &&
355 for w in I am all CRLF; do echo $w; done | append_cr >allcrlf &&
356 git add -A . &&
357 git commit -m "alllf, allcrlf and mixed only" &&
358 git tag -a -m "message" autocrlf-checkpoint
359'
360
361test_expect_success 'report no change after setting autocrlf' '
362 git config core.autocrlf true &&
363 touch * &&
364 git diff --exit-code
365'
366
367test_expect_success 'files are clean after checkout' '
368 rm * &&
369 git checkout -f &&
370 git diff --exit-code
371'
372
373cr_to_Q_no_NL () {
374 tr '\015' Q | tr -d '\012'
375}
376
377test_expect_success 'LF only file gets CRLF with autocrlf' '
378 test "$(cr_to_Q_no_NL < alllf)" = "IQamQallQLFQ"
379'
380
381test_expect_success 'Mixed file is still mixed with autocrlf' '
382 test "$(cr_to_Q_no_NL < mixed)" = "OhhereisCRLFQintext"
383'
384
385test_expect_success 'CRLF only file has CRLF with autocrlf' '
386 test "$(cr_to_Q_no_NL < allcrlf)" = "IQamQallQCRLFQ"
387'
388
389test_expect_success 'New CRLF file gets LF in repo' '
390 tr -d "\015" < alllf | append_cr > alllf2 &&
391 git add alllf2 &&
392 git commit -m "alllf2 added" &&
393 git config core.autocrlf false &&
394 rm * &&
395 git checkout -f &&
396 test_cmp alllf alllf2
397'
d7b0a093 398
634ede32 399test_done