]>
Commit | Line | Data |
---|---|---|
2344d47f JS |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2006 Johannes E. Schindelin | |
4 | # | |
5 | ||
6 | test_description='Test special whitespace in diff engine. | |
7 | ||
8 | ' | |
9 | . ./test-lib.sh | |
bfdbee98 | 10 | . "$TEST_DIRECTORY"/diff-lib.sh |
2344d47f JS |
11 | |
12 | # Ray Lehtiniemi's example | |
13 | ||
14 | cat << EOF > x | |
15 | do { | |
16 | nothing; | |
17 | } while (0); | |
18 | EOF | |
19 | ||
5be60078 | 20 | git update-index --add x |
2344d47f JS |
21 | |
22 | cat << EOF > x | |
23 | do | |
24 | { | |
25 | nothing; | |
26 | } | |
27 | while (0); | |
28 | EOF | |
29 | ||
30 | cat << EOF > expect | |
31 | diff --git a/x b/x | |
32 | index adf3937..6edc172 100644 | |
33 | --- a/x | |
34 | +++ b/x | |
35 | @@ -1,3 +1,5 @@ | |
36 | -do { | |
37 | +do | |
38 | +{ | |
39 | nothing; | |
40 | -} while (0); | |
41 | +} | |
42 | +while (0); | |
43 | EOF | |
44 | ||
5be60078 | 45 | git diff > out |
3af82863 | 46 | test_expect_success "Ray's example without options" 'test_cmp expect out' |
2344d47f | 47 | |
5be60078 | 48 | git diff -w > out |
3af82863 | 49 | test_expect_success "Ray's example with -w" 'test_cmp expect out' |
2344d47f | 50 | |
5be60078 | 51 | git diff -b > out |
3af82863 | 52 | test_expect_success "Ray's example with -b" 'test_cmp expect out' |
2344d47f | 53 | |
4035b46e | 54 | tr 'Q' '\015' << EOF > x |
2344d47f JS |
55 | whitespace at beginning |
56 | whitespace change | |
57 | whitespace in the middle | |
58 | whitespace at end | |
59 | unchanged line | |
4035b46e | 60 | CR at endQ |
2344d47f JS |
61 | EOF |
62 | ||
5be60078 | 63 | git update-index x |
2344d47f | 64 | |
74f16b0c | 65 | tr '_' ' ' << EOF > x |
2344d47f JS |
66 | whitespace at beginning |
67 | whitespace change | |
68 | white space in the middle | |
74f16b0c | 69 | whitespace at end__ |
2344d47f JS |
70 | unchanged line |
71 | CR at end | |
72 | EOF | |
73 | ||
74f16b0c | 74 | tr 'Q_' '\015 ' << EOF > expect |
2344d47f JS |
75 | diff --git a/x b/x |
76 | index d99af23..8b32fb5 100644 | |
77 | --- a/x | |
78 | +++ b/x | |
79 | @@ -1,6 +1,6 @@ | |
80 | -whitespace at beginning | |
81 | -whitespace change | |
82 | -whitespace in the middle | |
83 | -whitespace at end | |
84 | + whitespace at beginning | |
85 | +whitespace change | |
86 | +white space in the middle | |
74f16b0c | 87 | +whitespace at end__ |
2344d47f | 88 | unchanged line |
4035b46e | 89 | -CR at endQ |
2344d47f JS |
90 | +CR at end |
91 | EOF | |
5be60078 | 92 | git diff > out |
3af82863 | 93 | test_expect_success 'another test, without options' 'test_cmp expect out' |
2344d47f JS |
94 | |
95 | cat << EOF > expect | |
2344d47f | 96 | EOF |
5be60078 | 97 | git diff -w > out |
3af82863 | 98 | test_expect_success 'another test, with -w' 'test_cmp expect out' |
7a383291 | 99 | git diff -w -b > out |
6d12acef | 100 | test_expect_success 'another test, with -w -b' 'test_cmp expect out' |
7a383291 | 101 | git diff -w --ignore-space-at-eol > out |
6d12acef | 102 | test_expect_success 'another test, with -w --ignore-space-at-eol' 'test_cmp expect out' |
7a383291 | 103 | git diff -w -b --ignore-space-at-eol > out |
6d12acef | 104 | test_expect_success 'another test, with -w -b --ignore-space-at-eol' 'test_cmp expect out' |
2344d47f | 105 | |
4035b46e | 106 | tr 'Q' '\015' << EOF > expect |
2344d47f JS |
107 | diff --git a/x b/x |
108 | index d99af23..8b32fb5 100644 | |
109 | --- a/x | |
110 | +++ b/x | |
111 | @@ -1,6 +1,6 @@ | |
112 | -whitespace at beginning | |
113 | + whitespace at beginning | |
114 | whitespace change | |
115 | -whitespace in the middle | |
2344d47f | 116 | +white space in the middle |
c7c24889 | 117 | whitespace at end |
2344d47f | 118 | unchanged line |
c7c24889 | 119 | CR at endQ |
2344d47f | 120 | EOF |
5be60078 | 121 | git diff -b > out |
3af82863 | 122 | test_expect_success 'another test, with -b' 'test_cmp expect out' |
7a383291 | 123 | git diff -b --ignore-space-at-eol > out |
6d12acef | 124 | test_expect_success 'another test, with -b --ignore-space-at-eol' 'test_cmp expect out' |
7a383291 KC |
125 | |
126 | tr 'Q' '\015' << EOF > expect | |
127 | diff --git a/x b/x | |
128 | index d99af23..8b32fb5 100644 | |
129 | --- a/x | |
130 | +++ b/x | |
131 | @@ -1,6 +1,6 @@ | |
132 | -whitespace at beginning | |
133 | -whitespace change | |
134 | -whitespace in the middle | |
135 | + whitespace at beginning | |
136 | +whitespace change | |
137 | +white space in the middle | |
138 | whitespace at end | |
139 | unchanged line | |
140 | CR at endQ | |
141 | EOF | |
142 | git diff --ignore-space-at-eol > out | |
143 | test_expect_success 'another test, with --ignore-space-at-eol' 'test_cmp expect out' | |
2344d47f | 144 | |
86f8c236 WC |
145 | test_expect_success 'check mixed spaces and tabs in indent' ' |
146 | ||
147 | # This is indented with SP HT SP. | |
148 | echo " foo();" > x && | |
420f4f04 | 149 | git diff --check | grep "space before tab in indent" |
86f8c236 WC |
150 | |
151 | ' | |
152 | ||
9afa2d4a BF |
153 | test_expect_success 'check mixed tabs and spaces in indent' ' |
154 | ||
155 | # This is indented with HT SP HT. | |
156 | echo " foo();" > x && | |
157 | git diff --check | grep "space before tab in indent" | |
158 | ||
159 | ' | |
160 | ||
62c64895 WC |
161 | test_expect_success 'check with no whitespace errors' ' |
162 | ||
163 | git commit -m "snapshot" && | |
164 | echo "foo();" > x && | |
165 | git diff --check | |
166 | ||
167 | ' | |
168 | ||
f8175466 | 169 | test_expect_success 'check with trailing whitespace' ' |
62c64895 WC |
170 | |
171 | echo "foo(); " > x && | |
d492b31c | 172 | test_must_fail git diff --check |
62c64895 WC |
173 | |
174 | ' | |
175 | ||
f8175466 | 176 | test_expect_success 'check with space before tab in indent' ' |
62c64895 WC |
177 | |
178 | # indent has space followed by hard tab | |
179 | echo " foo();" > x && | |
d492b31c | 180 | test_must_fail git diff --check |
62c64895 WC |
181 | |
182 | ' | |
183 | ||
da31b358 | 184 | test_expect_success '--check and --exit-code are not exclusive' ' |
62c64895 WC |
185 | |
186 | git checkout x && | |
187 | git diff --check --exit-code | |
188 | ||
189 | ' | |
190 | ||
da31b358 | 191 | test_expect_success '--check and --quiet are not exclusive' ' |
62c64895 WC |
192 | |
193 | git diff --check --quiet | |
194 | ||
195 | ' | |
196 | ||
197 | test_expect_success 'check staged with no whitespace errors' ' | |
198 | ||
199 | echo "foo();" > x && | |
200 | git add x && | |
201 | git diff --cached --check | |
202 | ||
203 | ' | |
204 | ||
f8175466 | 205 | test_expect_success 'check staged with trailing whitespace' ' |
62c64895 WC |
206 | |
207 | echo "foo(); " > x && | |
208 | git add x && | |
d492b31c | 209 | test_must_fail git diff --cached --check |
62c64895 WC |
210 | |
211 | ' | |
212 | ||
f8175466 | 213 | test_expect_success 'check staged with space before tab in indent' ' |
62c64895 WC |
214 | |
215 | # indent has space followed by hard tab | |
216 | echo " foo();" > x && | |
217 | git add x && | |
d492b31c | 218 | test_must_fail git diff --cached --check |
62c64895 WC |
219 | |
220 | ' | |
221 | ||
222 | test_expect_success 'check with no whitespace errors (diff-index)' ' | |
223 | ||
224 | echo "foo();" > x && | |
225 | git add x && | |
226 | git diff-index --check HEAD | |
227 | ||
228 | ' | |
229 | ||
f8175466 | 230 | test_expect_success 'check with trailing whitespace (diff-index)' ' |
62c64895 WC |
231 | |
232 | echo "foo(); " > x && | |
233 | git add x && | |
d492b31c | 234 | test_must_fail git diff-index --check HEAD |
62c64895 WC |
235 | |
236 | ' | |
237 | ||
f8175466 | 238 | test_expect_success 'check with space before tab in indent (diff-index)' ' |
62c64895 WC |
239 | |
240 | # indent has space followed by hard tab | |
241 | echo " foo();" > x && | |
242 | git add x && | |
d492b31c | 243 | test_must_fail git diff-index --check HEAD |
62c64895 WC |
244 | |
245 | ' | |
246 | ||
247 | test_expect_success 'check staged with no whitespace errors (diff-index)' ' | |
248 | ||
249 | echo "foo();" > x && | |
250 | git add x && | |
251 | git diff-index --cached --check HEAD | |
252 | ||
253 | ' | |
254 | ||
f8175466 | 255 | test_expect_success 'check staged with trailing whitespace (diff-index)' ' |
62c64895 WC |
256 | |
257 | echo "foo(); " > x && | |
258 | git add x && | |
d492b31c | 259 | test_must_fail git diff-index --cached --check HEAD |
62c64895 WC |
260 | |
261 | ' | |
262 | ||
f8175466 | 263 | test_expect_success 'check staged with space before tab in indent (diff-index)' ' |
62c64895 WC |
264 | |
265 | # indent has space followed by hard tab | |
266 | echo " foo();" > x && | |
267 | git add x && | |
d492b31c | 268 | test_must_fail git diff-index --cached --check HEAD |
62c64895 WC |
269 | |
270 | ' | |
271 | ||
272 | test_expect_success 'check with no whitespace errors (diff-tree)' ' | |
273 | ||
274 | echo "foo();" > x && | |
275 | git commit -m "new commit" x && | |
276 | git diff-tree --check HEAD^ HEAD | |
277 | ||
278 | ' | |
279 | ||
f8175466 | 280 | test_expect_success 'check with trailing whitespace (diff-tree)' ' |
62c64895 WC |
281 | |
282 | echo "foo(); " > x && | |
283 | git commit -m "another commit" x && | |
d492b31c | 284 | test_must_fail git diff-tree --check HEAD^ HEAD |
62c64895 WC |
285 | |
286 | ' | |
287 | ||
f8175466 | 288 | test_expect_success 'check with space before tab in indent (diff-tree)' ' |
62c64895 WC |
289 | |
290 | # indent has space followed by hard tab | |
291 | echo " foo();" > x && | |
292 | git commit -m "yet another" x && | |
d492b31c | 293 | test_must_fail git diff-tree --check HEAD^ HEAD |
f8175466 JH |
294 | |
295 | ' | |
296 | ||
297 | test_expect_success 'check trailing whitespace (trailing-space: off)' ' | |
298 | ||
299 | git config core.whitespace "-trailing-space" && | |
300 | echo "foo (); " > x && | |
301 | git diff --check | |
302 | ||
303 | ' | |
304 | ||
305 | test_expect_success 'check trailing whitespace (trailing-space: on)' ' | |
306 | ||
307 | git config core.whitespace "trailing-space" && | |
308 | echo "foo (); " > x && | |
d492b31c | 309 | test_must_fail git diff --check |
f8175466 JH |
310 | |
311 | ' | |
312 | ||
313 | test_expect_success 'check space before tab in indent (space-before-tab: off)' ' | |
314 | ||
315 | # indent contains space followed by HT | |
316 | git config core.whitespace "-space-before-tab" && | |
317 | echo " foo ();" > x && | |
318 | git diff --check | |
319 | ||
320 | ' | |
321 | ||
322 | test_expect_success 'check space before tab in indent (space-before-tab: on)' ' | |
323 | ||
324 | # indent contains space followed by HT | |
325 | git config core.whitespace "space-before-tab" && | |
326 | echo " foo (); " > x && | |
d492b31c | 327 | test_must_fail git diff --check |
f8175466 JH |
328 | |
329 | ' | |
330 | ||
331 | test_expect_success 'check spaces as indentation (indent-with-non-tab: off)' ' | |
332 | ||
333 | git config core.whitespace "-indent-with-non-tab" | |
4d9697c7 | 334 | echo " foo ();" > x && |
f8175466 JH |
335 | git diff --check |
336 | ||
337 | ' | |
338 | ||
339 | test_expect_success 'check spaces as indentation (indent-with-non-tab: on)' ' | |
340 | ||
341 | git config core.whitespace "indent-with-non-tab" && | |
4d9697c7 | 342 | echo " foo ();" > x && |
d492b31c | 343 | test_must_fail git diff --check |
62c64895 WC |
344 | |
345 | ' | |
346 | ||
9afa2d4a BF |
347 | test_expect_success 'check tabs and spaces as indentation (indent-with-non-tab: on)' ' |
348 | ||
349 | git config core.whitespace "indent-with-non-tab" && | |
350 | echo " foo ();" > x && | |
d492b31c | 351 | test_must_fail git diff --check |
9afa2d4a BF |
352 | |
353 | ' | |
0ef617f4 | 354 | |
b27eb499 CW |
355 | test_expect_success 'check tabs as indentation (tab-in-indent: off)' ' |
356 | ||
357 | git config core.whitespace "-tab-in-indent" && | |
358 | echo " foo ();" > x && | |
359 | git diff --check | |
360 | ||
361 | ' | |
362 | ||
363 | test_expect_success 'check tabs as indentation (tab-in-indent: on)' ' | |
364 | ||
365 | git config core.whitespace "tab-in-indent" && | |
366 | echo " foo ();" > x && | |
367 | test_must_fail git diff --check | |
368 | ||
369 | ' | |
370 | ||
371 | test_expect_success 'check tabs and spaces as indentation (tab-in-indent: on)' ' | |
372 | ||
373 | git config core.whitespace "tab-in-indent" && | |
374 | echo " foo ();" > x && | |
375 | test_must_fail git diff --check | |
376 | ||
377 | ' | |
378 | ||
379 | test_expect_success 'check tab-in-indent and indent-with-non-tab conflict' ' | |
380 | ||
381 | git config core.whitespace "tab-in-indent,indent-with-non-tab" && | |
382 | echo "foo ();" > x && | |
383 | test_must_fail git diff --check | |
384 | ||
385 | ' | |
386 | ||
387 | test_expect_success 'check tab-in-indent excluded from wildcard whitespace attribute' ' | |
388 | ||
389 | git config --unset core.whitespace && | |
390 | echo "x whitespace" > .gitattributes && | |
391 | echo " foo ();" > x && | |
392 | git diff --check && | |
393 | rm -f .gitattributes | |
394 | ||
395 | ' | |
396 | ||
0ef617f4 JH |
397 | test_expect_success 'line numbers in --check output are correct' ' |
398 | ||
399 | echo "" > x && | |
400 | echo "foo(); " >> x && | |
401 | git diff --check | grep "x:2:" | |
402 | ||
403 | ' | |
404 | ||
5b5061ef | 405 | test_expect_success 'checkdiff detects new trailing blank lines (1)' ' |
877f23cc JH |
406 | echo "foo();" >x && |
407 | echo "" >>x && | |
5b5061ef | 408 | git diff --check | grep "new blank line" |
877f23cc JH |
409 | ' |
410 | ||
467babf8 JH |
411 | test_expect_success 'checkdiff detects new trailing blank lines (2)' ' |
412 | { echo a; echo b; echo; echo; } >x && | |
413 | git add x && | |
414 | { echo a; echo; echo; echo; echo; } >x && | |
415 | git diff --check | grep "new blank line" | |
877f23cc JH |
416 | ' |
417 | ||
c35539eb JH |
418 | test_expect_success 'checkdiff allows new blank lines' ' |
419 | git checkout x && | |
420 | mv x y && | |
421 | ( | |
422 | echo "/* This is new */" && | |
423 | echo "" && | |
424 | cat y | |
425 | ) >x && | |
426 | git diff --check | |
427 | ' | |
428 | ||
3e97c7c6 GB |
429 | cat <<EOF >expect |
430 | EOF | |
431 | test_expect_success 'whitespace-only changes not reported' ' | |
432 | git reset --hard && | |
433 | echo >x "hello world" && | |
434 | git add x && | |
435 | git commit -m "hello 1" && | |
436 | echo >x "hello world" && | |
437 | git diff -b >actual && | |
438 | test_cmp expect actual | |
439 | ' | |
440 | ||
296c6bb2 CC |
441 | cat <<EOF >expect |
442 | diff --git a/x b/z | |
443 | similarity index NUM% | |
444 | rename from x | |
445 | rename to z | |
446 | index 380c32a..a97b785 100644 | |
447 | EOF | |
448 | test_expect_success 'whitespace-only changes reported across renames' ' | |
449 | git reset --hard && | |
450 | for i in 1 2 3 4 5 6 7 8 9; do echo "$i$i$i$i$i$i"; done >x && | |
451 | git add x && | |
452 | git commit -m "base" && | |
453 | sed -e "5s/^/ /" x >z && | |
454 | git rm x && | |
455 | git add z && | |
456 | git diff -w -M --cached | | |
457 | sed -e "/^similarity index /s/[0-9][0-9]*/NUM/" >actual && | |
458 | test_cmp expect actual | |
459 | ' | |
460 | ||
461 | cat >expected <<\EOF | |
462 | diff --git a/empty b/void | |
463 | similarity index 100% | |
464 | rename from empty | |
465 | rename to void | |
466 | EOF | |
467 | ||
468 | test_expect_success 'rename empty' ' | |
469 | git reset --hard && | |
470 | >empty && | |
471 | git add empty && | |
472 | git commit -m empty && | |
473 | git mv empty void && | |
474 | git diff -w --cached -M >current && | |
475 | test_cmp expected current | |
476 | ' | |
477 | ||
5e568f9e AG |
478 | test_expect_success 'combined diff with autocrlf conversion' ' |
479 | ||
480 | git reset --hard && | |
481 | echo >x hello && | |
482 | git commit -m "one side" x && | |
483 | git checkout HEAD^ && | |
484 | echo >x goodbye && | |
485 | git commit -m "the other side" x && | |
486 | git config core.autocrlf true && | |
487 | test_must_fail git merge master && | |
488 | ||
489 | git diff | sed -e "1,/^@@@/d" >actual && | |
490 | ! grep "^-" actual | |
491 | ||
492 | ' | |
493 | ||
cfd1a984 KB |
494 | # Start testing the colored format for whitespace checks |
495 | ||
496 | test_expect_success 'setup diff colors' ' | |
497 | git config color.diff always && | |
498 | git config color.diff.plain normal && | |
499 | git config color.diff.meta bold && | |
500 | git config color.diff.frag cyan && | |
501 | git config color.diff.func normal && | |
502 | git config color.diff.old red && | |
503 | git config color.diff.new green && | |
504 | git config color.diff.commit yellow && | |
505 | git config color.diff.whitespace "normal red" && | |
506 | ||
507 | git config core.autocrlf false | |
508 | ' | |
509 | cat >expected <<\EOF | |
510 | <BOLD>diff --git a/x b/x<RESET> | |
511 | <BOLD>index 9daeafb..2874b91 100644<RESET> | |
512 | <BOLD>--- a/x<RESET> | |
513 | <BOLD>+++ b/x<RESET> | |
514 | <CYAN>@@ -1 +1,4 @@<RESET> | |
515 | test<RESET> | |
516 | <GREEN>+<RESET><GREEN>{<RESET> | |
517 | <GREEN>+<RESET><BRED> <RESET> | |
518 | <GREEN>+<RESET><GREEN>}<RESET> | |
519 | EOF | |
520 | ||
521 | test_expect_success 'diff that introduces a line with only tabs' ' | |
522 | git config core.whitespace blank-at-eol && | |
523 | git reset --hard && | |
524 | echo "test" > x && | |
525 | git commit -m "initial" x && | |
526 | echo "{NTN}" | tr "NT" "\n\t" >> x && | |
527 | git -c color.diff=always diff | test_decode_color >current && | |
528 | test_cmp expected current | |
529 | ' | |
530 | ||
2344d47f | 531 | test_done |