]>
Commit | Line | Data |
---|---|---|
634ede32 JH |
1 | #!/bin/sh |
2 | ||
3 | test_description='CRLF conversion' | |
4 | ||
06d53148 | 5 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
334afbc7 JS |
6 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
7 | ||
634ede32 JH |
8 | . ./test-lib.sh |
9 | ||
c4f3f551 SB |
10 | has_cr() { |
11 | tr '\015' Q <"$1" | grep Q >/dev/null | |
634ede32 JH |
12 | } |
13 | ||
fd777141 JK |
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 | ||
634ede32 JH |
21 | test_expect_success setup ' |
22 | ||
5c66d0d4 | 23 | git config core.autocrlf false && |
634ede32 JH |
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 && | |
163b9591 | 28 | for w in Oh here is NULQin text here; do echo $w; done | q_to_nul >three && |
634ede32 JH |
29 | git add . && |
30 | ||
31 | git commit -m initial && | |
32 | ||
def226bd EP |
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) && | |
634ede32 JH |
37 | |
38 | for w in Some extra lines here; do echo $w; done >>one && | |
39 | git diff >patch.file && | |
def226bd | 40 | patched=$(git hash-object --stdin <one) && |
be86fb3f | 41 | git read-tree --reset -u HEAD |
634ede32 JH |
42 | ' |
43 | ||
21e5ad50 SP |
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 && | |
d492b31c | 50 | test_must_fail git add allcrlf |
21e5ad50 SP |
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 && | |
d492b31c | 59 | test_must_fail git add mixed |
21e5ad50 SP |
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 && | |
d492b31c | 68 | test_must_fail git add alllf |
21e5ad50 SP |
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 && | |
d492b31c | 77 | test_must_fail git add mixed |
21e5ad50 SP |
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 && | |
87cb7845 | 89 | git add doublewarn 2>err && |
b1e07980 ÆAB |
90 | grep "CRLF will be replaced by LF" err >err.warnings && |
91 | test_line_count = 1 err.warnings | |
21e5ad50 SP |
92 | ' |
93 | ||
5430bb28 JH |
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 | ||
6cb09125 AS |
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 | ||
21e5ad50 SP |
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 | ||
634ede32 JH |
118 | test_expect_success 'update with autocrlf=input' ' |
119 | ||
163b9591 | 120 | rm -f tmp one dir/two three && |
634ede32 | 121 | git read-tree --reset -u HEAD && |
5c66d0d4 | 122 | git config core.autocrlf input && |
fd777141 JK |
123 | munge_cr append one && |
124 | munge_cr append dir/two && | |
125 | git update-index -- one dir/two && | |
def226bd | 126 | differs=$(git diff-index --cached HEAD) && |
9157c5cb | 127 | verbose test -z "$differs" |
634ede32 JH |
128 | |
129 | ' | |
130 | ||
131 | test_expect_success 'update with autocrlf=true' ' | |
132 | ||
163b9591 | 133 | rm -f tmp one dir/two three && |
634ede32 | 134 | git read-tree --reset -u HEAD && |
5c66d0d4 | 135 | git config core.autocrlf true && |
fd777141 JK |
136 | munge_cr append one && |
137 | munge_cr append dir/two && | |
138 | git update-index -- one dir/two && | |
def226bd | 139 | differs=$(git diff-index --cached HEAD) && |
9157c5cb | 140 | verbose test -z "$differs" |
634ede32 JH |
141 | |
142 | ' | |
143 | ||
144 | test_expect_success 'checkout with autocrlf=true' ' | |
145 | ||
163b9591 | 146 | rm -f tmp one dir/two three && |
5c66d0d4 | 147 | git config core.autocrlf true && |
634ede32 | 148 | git read-tree --reset -u HEAD && |
fd777141 JK |
149 | munge_cr remove one && |
150 | munge_cr remove dir/two && | |
151 | git update-index -- one dir/two && | |
def226bd EP |
152 | test "$one" = $(git hash-object --stdin <one) && |
153 | test "$two" = $(git hash-object --stdin <dir/two) && | |
154 | differs=$(git diff-index --cached HEAD) && | |
9157c5cb | 155 | verbose test -z "$differs" |
634ede32 JH |
156 | ' |
157 | ||
158 | test_expect_success 'checkout with autocrlf=input' ' | |
159 | ||
163b9591 | 160 | rm -f tmp one dir/two three && |
5c66d0d4 | 161 | git config core.autocrlf input && |
634ede32 | 162 | git read-tree --reset -u HEAD && |
f6041abd DL |
163 | ! has_cr one && |
164 | ! has_cr dir/two && | |
fd777141 | 165 | git update-index -- one dir/two && |
def226bd EP |
166 | test "$one" = $(git hash-object --stdin <one) && |
167 | test "$two" = $(git hash-object --stdin <dir/two) && | |
168 | differs=$(git diff-index --cached HEAD) && | |
9157c5cb | 169 | verbose test -z "$differs" |
634ede32 JH |
170 | ' |
171 | ||
172 | test_expect_success 'apply patch (autocrlf=input)' ' | |
173 | ||
163b9591 | 174 | rm -f tmp one dir/two three && |
5c66d0d4 | 175 | git config core.autocrlf input && |
634ede32 JH |
176 | git read-tree --reset -u HEAD && |
177 | ||
178 | git apply patch.file && | |
9157c5cb | 179 | verbose test "$patched" = "$(git hash-object --stdin <one)" |
634ede32 JH |
180 | ' |
181 | ||
182 | test_expect_success 'apply patch --cached (autocrlf=input)' ' | |
183 | ||
163b9591 | 184 | rm -f tmp one dir/two three && |
5c66d0d4 | 185 | git config core.autocrlf input && |
634ede32 JH |
186 | git read-tree --reset -u HEAD && |
187 | ||
188 | git apply --cached patch.file && | |
9157c5cb | 189 | verbose test "$patched" = $(git rev-parse :one) |
634ede32 JH |
190 | ' |
191 | ||
192 | test_expect_success 'apply patch --index (autocrlf=input)' ' | |
193 | ||
163b9591 | 194 | rm -f tmp one dir/two three && |
5c66d0d4 | 195 | git config core.autocrlf input && |
634ede32 JH |
196 | git read-tree --reset -u HEAD && |
197 | ||
198 | git apply --index patch.file && | |
9157c5cb JK |
199 | verbose test "$patched" = $(git rev-parse :one) && |
200 | verbose test "$patched" = $(git hash-object --stdin <one) | |
634ede32 JH |
201 | ' |
202 | ||
203 | test_expect_success 'apply patch (autocrlf=true)' ' | |
204 | ||
163b9591 | 205 | rm -f tmp one dir/two three && |
5c66d0d4 | 206 | git config core.autocrlf true && |
634ede32 JH |
207 | git read-tree --reset -u HEAD && |
208 | ||
634ede32 | 209 | git apply patch.file && |
9157c5cb | 210 | verbose test "$patched" = "$(remove_cr <one | git hash-object --stdin)" |
634ede32 JH |
211 | ' |
212 | ||
213 | test_expect_success 'apply patch --cached (autocrlf=true)' ' | |
214 | ||
163b9591 | 215 | rm -f tmp one dir/two three && |
5c66d0d4 | 216 | git config core.autocrlf true && |
634ede32 JH |
217 | git read-tree --reset -u HEAD && |
218 | ||
219 | git apply --cached patch.file && | |
9157c5cb | 220 | verbose test "$patched" = $(git rev-parse :one) |
634ede32 JH |
221 | ' |
222 | ||
67160271 JH |
223 | test_expect_success 'apply patch --index (autocrlf=true)' ' |
224 | ||
163b9591 | 225 | rm -f tmp one dir/two three && |
5c66d0d4 | 226 | git config core.autocrlf true && |
67160271 JH |
227 | git read-tree --reset -u HEAD && |
228 | ||
229 | git apply --index patch.file && | |
9157c5cb JK |
230 | verbose test "$patched" = $(git rev-parse :one) && |
231 | verbose test "$patched" = "$(remove_cr <one | git hash-object --stdin)" | |
67160271 JH |
232 | ' |
233 | ||
35ebfd6a JH |
234 | test_expect_success '.gitattributes says two is binary' ' |
235 | ||
163b9591 | 236 | rm -f tmp one dir/two three && |
e4aee10a | 237 | echo "two -crlf" >.gitattributes && |
5c66d0d4 | 238 | git config core.autocrlf true && |
35ebfd6a JH |
239 | git read-tree --reset -u HEAD && |
240 | ||
f6041abd | 241 | ! has_cr dir/two && |
be86fb3f | 242 | verbose has_cr one && |
f6041abd | 243 | ! has_cr three |
163b9591 JH |
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 | ||
f6041abd | 252 | ! has_cr dir/two |
163b9591 JH |
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 | ||
9157c5cb JK |
261 | verbose has_cr dir/two && |
262 | verbose has_cr three | |
35ebfd6a JH |
263 | ' |
264 | ||
1a9d7e9b JH |
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 | ||
f6041abd | 274 | ! has_cr one && |
9157c5cb | 275 | verbose has_cr three |
1a9d7e9b JH |
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 | ||
f6041abd | 284 | ! has_cr one && |
9157c5cb | 285 | verbose has_cr three |
1a9d7e9b JH |
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 | ||
f6041abd | 295 | ! has_cr one && |
9157c5cb | 296 | verbose has_cr three |
1a9d7e9b JH |
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 | ||
f6041abd | 306 | ! has_cr one && |
9157c5cb | 307 | verbose has_cr three |
1a9d7e9b JH |
308 | ' |
309 | ||
b997045e KA |
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 | ||
06d53148 JS |
322 | git checkout main~1 && |
323 | git checkout main && | |
b997045e KA |
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 && | |
2dec68cf | 333 | git commit -m third && |
b997045e | 334 | |
06d53148 JS |
335 | git checkout main~1 && |
336 | git checkout main && | |
c4f3f551 | 337 | has_cr .file2 |
b997045e KA |
338 | |
339 | ' | |
340 | ||
d7b0a093 SP |
341 | test_expect_success 'invalid .gitattributes (must not crash)' ' |
342 | ||
343 | echo "three +crlf" >>.gitattributes && | |
344 | git diff | |
345 | ||
346 | ' | |
c4805393 FAG |
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 | ' | |
d7b0a093 | 399 | |
634ede32 | 400 | test_done |