]> git.ipfire.org Git - thirdparty/git.git/blame - t/t4015-diff-whitespace.sh
t4015: check "negative" case for "-w --color-moved"
[thirdparty/git.git] / t / t4015-diff-whitespace.sh
CommitLineData
2344d47f
JS
1#!/bin/sh
2#
3# Copyright (c) 2006 Johannes E. Schindelin
4#
5
6test_description='Test special whitespace in diff engine.
7
8'
9. ./test-lib.sh
bfdbee98 10. "$TEST_DIRECTORY"/diff-lib.sh
2344d47f 11
d55ef3e0
JH
12test_expect_success "Ray Lehtiniemi's example" '
13 cat <<-\EOF >x &&
14 do {
15 nothing;
16 } while (0);
17 EOF
18 git update-index --add x &&
2344d47f 19
d55ef3e0
JH
20 cat <<-\EOF >x &&
21 do
22 {
23 nothing;
24 }
25 while (0);
26 EOF
2344d47f 27
d55ef3e0
JH
28 cat <<-\EOF >expect &&
29 diff --git a/x b/x
30 index adf3937..6edc172 100644
31 --- a/x
32 +++ b/x
33 @@ -1,3 +1,5 @@
34 -do {
35 +do
36 +{
37 nothing;
38 -} while (0);
39 +}
40 +while (0);
41 EOF
2344d47f 42
d55ef3e0
JH
43 git diff >out &&
44 test_cmp expect out &&
2344d47f 45
d55ef3e0
JH
46 git diff -w >out &&
47 test_cmp expect out &&
2344d47f 48
d55ef3e0
JH
49 git diff -b >out &&
50 test_cmp expect out
51'
2344d47f 52
d55ef3e0
JH
53test_expect_success 'another test, without options' '
54 tr Q "\015" <<-\EOF >x &&
55 whitespace at beginning
56 whitespace change
57 whitespace in the middle
58 whitespace at end
59 unchanged line
60 CR at endQ
61 EOF
2344d47f 62
d55ef3e0 63 git update-index x &&
2344d47f 64
d55ef3e0
JH
65 tr "_" " " <<-\EOF >x &&
66 _ whitespace at beginning
67 whitespace change
68 white space in the middle
69 whitespace at end__
70 unchanged line
71 CR at end
72 EOF
2344d47f 73
d55ef3e0
JH
74 tr "Q_" "\015 " <<-\EOF >expect &&
75 diff --git a/x b/x
76 index d99af23..22d9f73 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
87 +whitespace at end__
88 unchanged line
89 -CR at endQ
90 +CR at end
91 EOF
2344d47f 92
d55ef3e0
JH
93 git diff >out &&
94 test_cmp expect out &&
2344d47f 95
d55ef3e0
JH
96 >expect &&
97 git diff -w >out &&
98 test_cmp expect out &&
99
100 git diff -w -b >out &&
101 test_cmp expect out &&
102
103 git diff -w --ignore-space-at-eol >out &&
104 test_cmp expect out &&
105
106 git diff -w -b --ignore-space-at-eol >out &&
107 test_cmp expect out &&
2344d47f 108
d55ef3e0
JH
109
110 tr "Q_" "\015 " <<-\EOF >expect &&
111 diff --git a/x b/x
112 index d99af23..22d9f73 100644
113 --- a/x
114 +++ b/x
115 @@ -1,6 +1,6 @@
116 -whitespace at beginning
117 +_ whitespace at beginning
118 whitespace change
119 -whitespace in the middle
120 +white space in the middle
121 whitespace at end__
122 unchanged line
123 CR at end
124 EOF
125 git diff -b >out &&
126 test_cmp expect out &&
127
128 git diff -b --ignore-space-at-eol >out &&
129 test_cmp expect out &&
130
131 tr "Q_" "\015 " <<-\EOF >expect &&
132 diff --git a/x b/x
133 index d99af23..22d9f73 100644
134 --- a/x
135 +++ b/x
136 @@ -1,6 +1,6 @@
137 -whitespace at beginning
138 -whitespace change
139 -whitespace in the middle
140 +_ whitespace at beginning
141 +whitespace change
142 +white space in the middle
143 whitespace at end__
144 unchanged line
145 CR at end
146 EOF
147 git diff --ignore-space-at-eol >out &&
148 test_cmp expect out
149'
2344d47f 150
36617af7
AP
151test_expect_success 'ignore-blank-lines: only new lines' '
152 test_seq 5 >x &&
153 git update-index x &&
90a95301 154 test_seq 5 | sed "/3/i\\
36617af7
AP
155" >x &&
156 git diff --ignore-blank-lines >out &&
157 >expect &&
9c5b2fab 158 test_cmp expect out
36617af7
AP
159'
160
161test_expect_success 'ignore-blank-lines: only new lines with space' '
162 test_seq 5 >x &&
163 git update-index x &&
90a95301
BW
164 test_seq 5 | sed "/3/i\\
165 " >x &&
36617af7
AP
166 git diff -w --ignore-blank-lines >out &&
167 >expect &&
9c5b2fab 168 test_cmp expect out
36617af7
AP
169'
170
171test_expect_success 'ignore-blank-lines: after change' '
172 cat <<-\EOF >x &&
173 1
174 2
175
176 3
177 4
178 5
179
180 6
181 7
182 EOF
183 git update-index x &&
184 cat <<-\EOF >x &&
185 change
186
187 1
188 2
189 3
190 4
191 5
192 6
193
194 7
195 EOF
196 git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp &&
197 cat <<-\EOF >expected &&
198 diff --git a/x b/x
199 --- a/x
200 +++ b/x
201 @@ -1,6 +1,7 @@
202 +change
203 +
204 1
205 2
206 -
207 3
208 4
209 5
210 EOF
211 compare_diff_patch expected out.tmp
212'
213
214test_expect_success 'ignore-blank-lines: before change' '
215 cat <<-\EOF >x &&
216 1
217 2
218
219 3
220 4
221 5
222 6
223 7
224 EOF
225 git update-index x &&
226 cat <<-\EOF >x &&
227
228 1
229 2
230 3
231 4
232 5
233
234 6
235 7
236 change
237 EOF
238 git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp &&
239 cat <<-\EOF >expected &&
240 diff --git a/x b/x
241 --- a/x
242 +++ b/x
243 @@ -4,5 +4,7 @@
244 3
245 4
246 5
247 +
248 6
249 7
250 +change
251 EOF
252 compare_diff_patch expected out.tmp
253'
254
255test_expect_success 'ignore-blank-lines: between changes' '
256 cat <<-\EOF >x &&
257 1
258 2
259 3
260 4
261 5
262
263
264 6
265 7
266 8
267 9
268 10
269 EOF
270 git update-index x &&
271 cat <<-\EOF >x &&
272 change
273 1
274 2
275
276 3
277 4
278 5
279 6
280 7
281 8
282
283 9
284 10
285 change
286 EOF
287 git diff --ignore-blank-lines >out.tmp &&
288 cat <<-\EOF >expected &&
289 diff --git a/x b/x
290 --- a/x
291 +++ b/x
292 @@ -1,5 +1,7 @@
293 +change
294 1
295 2
296 +
297 3
298 4
299 5
300 @@ -8,5 +8,7 @@
301 6
302 7
303 8
304 +
305 9
306 10
307 +change
308 EOF
309 compare_diff_patch expected out.tmp
310'
311
312test_expect_success 'ignore-blank-lines: between changes (with interhunkctx)' '
313 test_seq 10 >x &&
314 git update-index x &&
315 cat <<-\EOF >x &&
316 change
317 1
318 2
319
320 3
321 4
322 5
323
324 6
325 7
326 8
327 9
328
329 10
330 change
331 EOF
332 git diff --inter-hunk-context=2 --ignore-blank-lines >out.tmp &&
333 cat <<-\EOF >expected &&
334 diff --git a/x b/x
335 --- a/x
336 +++ b/x
337 @@ -1,10 +1,15 @@
338 +change
339 1
340 2
341 +
342 3
343 4
344 5
345 +
346 6
347 7
348 8
349 9
350 +
351 10
352 +change
353 EOF
354 compare_diff_patch expected out.tmp
355'
356
357test_expect_success 'ignore-blank-lines: scattered spaces' '
358 test_seq 10 >x &&
359 git update-index x &&
360 cat <<-\EOF >x &&
361 change
362 1
363 2
364 3
365
366 4
367
368 5
369
370 6
371
372
373 7
374
375 8
376 9
377 10
378 change
379 EOF
380 git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp &&
381 cat <<-\EOF >expected &&
382 diff --git a/x b/x
383 --- a/x
384 +++ b/x
385 @@ -1,3 +1,4 @@
386 +change
387 1
388 2
389 3
390 @@ -8,3 +15,4 @@
391 8
392 9
393 10
394 +change
395 EOF
396 compare_diff_patch expected out.tmp
397'
398
399test_expect_success 'ignore-blank-lines: spaces coalesce' '
400 test_seq 6 >x &&
401 git update-index x &&
402 cat <<-\EOF >x &&
403 change
404 1
405 2
406 3
407
408 4
409
410 5
411
412 6
413 change
414 EOF
415 git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp &&
416 cat <<-\EOF >expected &&
417 diff --git a/x b/x
418 --- a/x
419 +++ b/x
420 @@ -1,6 +1,11 @@
421 +change
422 1
423 2
424 3
425 +
426 4
427 +
428 5
429 +
430 6
431 +change
432 EOF
433 compare_diff_patch expected out.tmp
434'
435
436test_expect_success 'ignore-blank-lines: mix changes and blank lines' '
437 test_seq 16 >x &&
438 git update-index x &&
439 cat <<-\EOF >x &&
440 change
441 1
442 2
443
444 3
445 4
446 5
447 change
448 6
449 7
450 8
451
452 9
453 10
454 11
455 change
456 12
457 13
458 14
459
460 15
461 16
462 change
463 EOF
464 git diff --ignore-blank-lines >out.tmp &&
465 cat <<-\EOF >expected &&
466 diff --git a/x b/x
467 --- a/x
468 +++ b/x
469 @@ -1,8 +1,11 @@
470 +change
471 1
472 2
473 +
474 3
475 4
476 5
477 +change
478 6
479 7
480 8
481 @@ -9,8 +13,11 @@
482 9
483 10
484 11
485 +change
486 12
487 13
488 14
489 +
490 15
491 16
492 +change
493 EOF
494 compare_diff_patch expected out.tmp
495'
496
86f8c236 497test_expect_success 'check mixed spaces and tabs in indent' '
86f8c236 498 # This is indented with SP HT SP.
d55ef3e0 499 echo " foo();" >x &&
420f4f04 500 git diff --check | grep "space before tab in indent"
86f8c236
WC
501'
502
9afa2d4a 503test_expect_success 'check mixed tabs and spaces in indent' '
9afa2d4a 504 # This is indented with HT SP HT.
d55ef3e0 505 echo " foo();" >x &&
9afa2d4a 506 git diff --check | grep "space before tab in indent"
9afa2d4a
BF
507'
508
62c64895 509test_expect_success 'check with no whitespace errors' '
62c64895 510 git commit -m "snapshot" &&
d55ef3e0 511 echo "foo();" >x &&
62c64895 512 git diff --check
62c64895
WC
513'
514
f8175466 515test_expect_success 'check with trailing whitespace' '
d55ef3e0 516 echo "foo(); " >x &&
d492b31c 517 test_must_fail git diff --check
62c64895
WC
518'
519
f8175466 520test_expect_success 'check with space before tab in indent' '
62c64895 521 # indent has space followed by hard tab
d55ef3e0 522 echo " foo();" >x &&
d492b31c 523 test_must_fail git diff --check
62c64895
WC
524'
525
da31b358 526test_expect_success '--check and --exit-code are not exclusive' '
62c64895
WC
527 git checkout x &&
528 git diff --check --exit-code
62c64895
WC
529'
530
da31b358 531test_expect_success '--check and --quiet are not exclusive' '
62c64895 532 git diff --check --quiet
62c64895
WC
533'
534
535test_expect_success 'check staged with no whitespace errors' '
d55ef3e0 536 echo "foo();" >x &&
62c64895
WC
537 git add x &&
538 git diff --cached --check
62c64895
WC
539'
540
f8175466 541test_expect_success 'check staged with trailing whitespace' '
d55ef3e0 542 echo "foo(); " >x &&
62c64895 543 git add x &&
d492b31c 544 test_must_fail git diff --cached --check
62c64895
WC
545'
546
f8175466 547test_expect_success 'check staged with space before tab in indent' '
62c64895 548 # indent has space followed by hard tab
d55ef3e0 549 echo " foo();" >x &&
62c64895 550 git add x &&
d492b31c 551 test_must_fail git diff --cached --check
62c64895
WC
552'
553
554test_expect_success 'check with no whitespace errors (diff-index)' '
d55ef3e0 555 echo "foo();" >x &&
62c64895
WC
556 git add x &&
557 git diff-index --check HEAD
62c64895
WC
558'
559
f8175466 560test_expect_success 'check with trailing whitespace (diff-index)' '
d55ef3e0 561 echo "foo(); " >x &&
62c64895 562 git add x &&
d492b31c 563 test_must_fail git diff-index --check HEAD
62c64895
WC
564'
565
f8175466 566test_expect_success 'check with space before tab in indent (diff-index)' '
62c64895 567 # indent has space followed by hard tab
d55ef3e0 568 echo " foo();" >x &&
62c64895 569 git add x &&
d492b31c 570 test_must_fail git diff-index --check HEAD
62c64895
WC
571'
572
573test_expect_success 'check staged with no whitespace errors (diff-index)' '
d55ef3e0 574 echo "foo();" >x &&
62c64895
WC
575 git add x &&
576 git diff-index --cached --check HEAD
62c64895
WC
577'
578
f8175466 579test_expect_success 'check staged with trailing whitespace (diff-index)' '
d55ef3e0 580 echo "foo(); " >x &&
62c64895 581 git add x &&
d492b31c 582 test_must_fail git diff-index --cached --check HEAD
62c64895
WC
583'
584
f8175466 585test_expect_success 'check staged with space before tab in indent (diff-index)' '
62c64895 586 # indent has space followed by hard tab
d55ef3e0 587 echo " foo();" >x &&
62c64895 588 git add x &&
d492b31c 589 test_must_fail git diff-index --cached --check HEAD
62c64895
WC
590'
591
592test_expect_success 'check with no whitespace errors (diff-tree)' '
d55ef3e0 593 echo "foo();" >x &&
62c64895
WC
594 git commit -m "new commit" x &&
595 git diff-tree --check HEAD^ HEAD
62c64895
WC
596'
597
f8175466 598test_expect_success 'check with trailing whitespace (diff-tree)' '
d55ef3e0 599 echo "foo(); " >x &&
62c64895 600 git commit -m "another commit" x &&
d492b31c 601 test_must_fail git diff-tree --check HEAD^ HEAD
62c64895
WC
602'
603
f8175466 604test_expect_success 'check with space before tab in indent (diff-tree)' '
62c64895 605 # indent has space followed by hard tab
d55ef3e0 606 echo " foo();" >x &&
62c64895 607 git commit -m "yet another" x &&
d492b31c 608 test_must_fail git diff-tree --check HEAD^ HEAD
f8175466
JH
609'
610
611test_expect_success 'check trailing whitespace (trailing-space: off)' '
f8175466 612 git config core.whitespace "-trailing-space" &&
d55ef3e0 613 echo "foo (); " >x &&
f8175466 614 git diff --check
f8175466
JH
615'
616
617test_expect_success 'check trailing whitespace (trailing-space: on)' '
f8175466 618 git config core.whitespace "trailing-space" &&
d55ef3e0 619 echo "foo (); " >x &&
d492b31c 620 test_must_fail git diff --check
f8175466
JH
621'
622
623test_expect_success 'check space before tab in indent (space-before-tab: off)' '
f8175466
JH
624 # indent contains space followed by HT
625 git config core.whitespace "-space-before-tab" &&
d55ef3e0 626 echo " foo ();" >x &&
f8175466 627 git diff --check
f8175466
JH
628'
629
630test_expect_success 'check space before tab in indent (space-before-tab: on)' '
f8175466
JH
631 # indent contains space followed by HT
632 git config core.whitespace "space-before-tab" &&
d55ef3e0 633 echo " foo (); " >x &&
d492b31c 634 test_must_fail git diff --check
f8175466
JH
635'
636
637test_expect_success 'check spaces as indentation (indent-with-non-tab: off)' '
a48fcd83 638 git config core.whitespace "-indent-with-non-tab" &&
d55ef3e0 639 echo " foo ();" >x &&
f8175466 640 git diff --check
f8175466
JH
641'
642
643test_expect_success 'check spaces as indentation (indent-with-non-tab: on)' '
f8175466 644 git config core.whitespace "indent-with-non-tab" &&
d55ef3e0 645 echo " foo ();" >x &&
d492b31c 646 test_must_fail git diff --check
62c64895
WC
647'
648
f4b05a49 649test_expect_success 'ditto, but tabwidth=9' '
f4b05a49
JS
650 git config core.whitespace "indent-with-non-tab,tabwidth=9" &&
651 git diff --check
f4b05a49
JS
652'
653
9afa2d4a 654test_expect_success 'check tabs and spaces as indentation (indent-with-non-tab: on)' '
9afa2d4a 655 git config core.whitespace "indent-with-non-tab" &&
d55ef3e0 656 echo " foo ();" >x &&
d492b31c 657 test_must_fail git diff --check
9afa2d4a 658'
0ef617f4 659
f4b05a49 660test_expect_success 'ditto, but tabwidth=10' '
f4b05a49
JS
661 git config core.whitespace "indent-with-non-tab,tabwidth=10" &&
662 test_must_fail git diff --check
f4b05a49
JS
663'
664
665test_expect_success 'ditto, but tabwidth=20' '
f4b05a49
JS
666 git config core.whitespace "indent-with-non-tab,tabwidth=20" &&
667 git diff --check
f4b05a49
JS
668'
669
b27eb499 670test_expect_success 'check tabs as indentation (tab-in-indent: off)' '
b27eb499 671 git config core.whitespace "-tab-in-indent" &&
d55ef3e0 672 echo " foo ();" >x &&
b27eb499 673 git diff --check
b27eb499
CW
674'
675
676test_expect_success 'check tabs as indentation (tab-in-indent: on)' '
b27eb499 677 git config core.whitespace "tab-in-indent" &&
d55ef3e0 678 echo " foo ();" >x &&
b27eb499 679 test_must_fail git diff --check
b27eb499
CW
680'
681
682test_expect_success 'check tabs and spaces as indentation (tab-in-indent: on)' '
b27eb499 683 git config core.whitespace "tab-in-indent" &&
d55ef3e0 684 echo " foo ();" >x &&
b27eb499 685 test_must_fail git diff --check
b27eb499
CW
686'
687
f4b05a49 688test_expect_success 'ditto, but tabwidth=1 (must be irrelevant)' '
f4b05a49
JS
689 git config core.whitespace "tab-in-indent,tabwidth=1" &&
690 test_must_fail git diff --check
f4b05a49
JS
691'
692
b27eb499 693test_expect_success 'check tab-in-indent and indent-with-non-tab conflict' '
b27eb499 694 git config core.whitespace "tab-in-indent,indent-with-non-tab" &&
d55ef3e0 695 echo "foo ();" >x &&
b27eb499 696 test_must_fail git diff --check
b27eb499
CW
697'
698
699test_expect_success 'check tab-in-indent excluded from wildcard whitespace attribute' '
b27eb499 700 git config --unset core.whitespace &&
d55ef3e0
JH
701 echo "x whitespace" >.gitattributes &&
702 echo " foo ();" >x &&
b27eb499
CW
703 git diff --check &&
704 rm -f .gitattributes
b27eb499
CW
705'
706
0ef617f4 707test_expect_success 'line numbers in --check output are correct' '
d55ef3e0
JH
708 echo "" >x &&
709 echo "foo(); " >>x &&
0ef617f4 710 git diff --check | grep "x:2:"
0ef617f4
JH
711'
712
5b5061ef 713test_expect_success 'checkdiff detects new trailing blank lines (1)' '
877f23cc
JH
714 echo "foo();" >x &&
715 echo "" >>x &&
5b5061ef 716 git diff --check | grep "new blank line"
877f23cc
JH
717'
718
467babf8
JH
719test_expect_success 'checkdiff detects new trailing blank lines (2)' '
720 { echo a; echo b; echo; echo; } >x &&
721 git add x &&
722 { echo a; echo; echo; echo; echo; } >x &&
723 git diff --check | grep "new blank line"
877f23cc
JH
724'
725
c35539eb
JH
726test_expect_success 'checkdiff allows new blank lines' '
727 git checkout x &&
728 mv x y &&
729 (
730 echo "/* This is new */" &&
731 echo "" &&
732 cat y
733 ) >x &&
734 git diff --check
735'
736
3e97c7c6
GB
737cat <<EOF >expect
738EOF
739test_expect_success 'whitespace-only changes not reported' '
740 git reset --hard &&
741 echo >x "hello world" &&
742 git add x &&
743 git commit -m "hello 1" &&
744 echo >x "hello world" &&
745 git diff -b >actual &&
746 test_cmp expect actual
747'
748
296c6bb2
CC
749cat <<EOF >expect
750diff --git a/x b/z
751similarity index NUM%
752rename from x
753rename to z
754index 380c32a..a97b785 100644
755EOF
756test_expect_success 'whitespace-only changes reported across renames' '
757 git reset --hard &&
758 for i in 1 2 3 4 5 6 7 8 9; do echo "$i$i$i$i$i$i"; done >x &&
759 git add x &&
760 git commit -m "base" &&
761 sed -e "5s/^/ /" x >z &&
762 git rm x &&
763 git add z &&
764 git diff -w -M --cached |
765 sed -e "/^similarity index /s/[0-9][0-9]*/NUM/" >actual &&
766 test_cmp expect actual
767'
768
769cat >expected <<\EOF
770diff --git a/empty b/void
771similarity index 100%
772rename from empty
773rename to void
774EOF
775
776test_expect_success 'rename empty' '
777 git reset --hard &&
778 >empty &&
779 git add empty &&
780 git commit -m empty &&
781 git mv empty void &&
782 git diff -w --cached -M >current &&
783 test_cmp expected current
784'
785
5e568f9e
AG
786test_expect_success 'combined diff with autocrlf conversion' '
787
788 git reset --hard &&
789 echo >x hello &&
790 git commit -m "one side" x &&
791 git checkout HEAD^ &&
792 echo >x goodbye &&
793 git commit -m "the other side" x &&
794 git config core.autocrlf true &&
795 test_must_fail git merge master &&
796
797 git diff | sed -e "1,/^@@@/d" >actual &&
798 ! grep "^-" actual
799
800'
801
cfd1a984
KB
802# Start testing the colored format for whitespace checks
803
804test_expect_success 'setup diff colors' '
cfd1a984
KB
805 git config color.diff.plain normal &&
806 git config color.diff.meta bold &&
807 git config color.diff.frag cyan &&
808 git config color.diff.func normal &&
809 git config color.diff.old red &&
810 git config color.diff.new green &&
811 git config color.diff.commit yellow &&
0ad782f2 812 git config color.diff.whitespace blue &&
cfd1a984 813
0ad782f2
JH
814 git config core.autocrlf false
815'
816
817test_expect_success 'diff that introduces a line with only tabs' '
818 git config core.whitespace blank-at-eol &&
819 git reset --hard &&
820 echo "test" >x &&
821 git commit -m "initial" x &&
822 echo "{NTN}" | tr "NT" "\n\t" >>x &&
a655a595 823 git diff --color | test_decode_color >current &&
d55ef3e0 824
0ad782f2 825 cat >expected <<-\EOF &&
d55ef3e0
JH
826 <BOLD>diff --git a/x b/x<RESET>
827 <BOLD>index 9daeafb..2874b91 100644<RESET>
828 <BOLD>--- a/x<RESET>
829 <BOLD>+++ b/x<RESET>
830 <CYAN>@@ -1 +1,4 @@<RESET>
831 test<RESET>
832 <GREEN>+<RESET><GREEN>{<RESET>
0ad782f2 833 <GREEN>+<RESET><BLUE> <RESET>
d55ef3e0
JH
834 <GREEN>+<RESET><GREEN>}<RESET>
835 EOF
cfd1a984 836
cfd1a984
KB
837 test_cmp expected current
838'
839
b8767f79
JH
840test_expect_success 'diff that introduces and removes ws breakages' '
841 git reset --hard &&
842 {
843 echo "0. blank-at-eol " &&
844 echo "1. blank-at-eol "
845 } >x &&
846 git commit -a --allow-empty -m preimage &&
847 {
848 echo "0. blank-at-eol " &&
849 echo "1. still-blank-at-eol " &&
850 echo "2. and a new line "
851 } >x &&
852
a655a595 853 git diff --color |
b8767f79
JH
854 test_decode_color >current &&
855
856 cat >expected <<-\EOF &&
857 <BOLD>diff --git a/x b/x<RESET>
858 <BOLD>index d0233a2..700886e 100644<RESET>
859 <BOLD>--- a/x<RESET>
860 <BOLD>+++ b/x<RESET>
861 <CYAN>@@ -1,2 +1,3 @@<RESET>
862 0. blank-at-eol <RESET>
863 <RED>-1. blank-at-eol <RESET>
864 <GREEN>+<RESET><GREEN>1. still-blank-at-eol<RESET><BLUE> <RESET>
865 <GREEN>+<RESET><GREEN>2. and a new line<RESET><BLUE> <RESET>
866 EOF
867
868 test_cmp expected current
869'
870
f3f5c7f5
JH
871test_expect_success 'ws-error-highlight test setup' '
872
b8767f79
JH
873 git reset --hard &&
874 {
875 echo "0. blank-at-eol " &&
876 echo "1. blank-at-eol "
877 } >x &&
878 git commit -a --allow-empty -m preimage &&
879 {
880 echo "0. blank-at-eol " &&
881 echo "1. still-blank-at-eol " &&
882 echo "2. and a new line "
883 } >x &&
884
f3f5c7f5 885 cat >expect.default-old <<-\EOF &&
b8767f79
JH
886 <BOLD>diff --git a/x b/x<RESET>
887 <BOLD>index d0233a2..700886e 100644<RESET>
888 <BOLD>--- a/x<RESET>
889 <BOLD>+++ b/x<RESET>
890 <CYAN>@@ -1,2 +1,3 @@<RESET>
891 0. blank-at-eol <RESET>
892 <RED>-<RESET><RED>1. blank-at-eol<RESET><BLUE> <RESET>
893 <GREEN>+<RESET><GREEN>1. still-blank-at-eol<RESET><BLUE> <RESET>
894 <GREEN>+<RESET><GREEN>2. and a new line<RESET><BLUE> <RESET>
895 EOF
896
f3f5c7f5 897 cat >expect.all <<-\EOF &&
b8767f79
JH
898 <BOLD>diff --git a/x b/x<RESET>
899 <BOLD>index d0233a2..700886e 100644<RESET>
900 <BOLD>--- a/x<RESET>
901 <BOLD>+++ b/x<RESET>
902 <CYAN>@@ -1,2 +1,3 @@<RESET>
903 <RESET>0. blank-at-eol<RESET><BLUE> <RESET>
904 <RED>-<RESET><RED>1. blank-at-eol<RESET><BLUE> <RESET>
905 <GREEN>+<RESET><GREEN>1. still-blank-at-eol<RESET><BLUE> <RESET>
906 <GREEN>+<RESET><GREEN>2. and a new line<RESET><BLUE> <RESET>
907 EOF
908
f3f5c7f5 909 cat >expect.none <<-\EOF
b8767f79
JH
910 <BOLD>diff --git a/x b/x<RESET>
911 <BOLD>index d0233a2..700886e 100644<RESET>
912 <BOLD>--- a/x<RESET>
913 <BOLD>+++ b/x<RESET>
914 <CYAN>@@ -1,2 +1,3 @@<RESET>
915 0. blank-at-eol <RESET>
916 <RED>-1. blank-at-eol <RESET>
917 <GREEN>+1. still-blank-at-eol <RESET>
918 <GREEN>+2. and a new line <RESET>
919 EOF
920
f3f5c7f5
JH
921'
922
923test_expect_success 'test --ws-error-highlight option' '
924
a655a595 925 git diff --color --ws-error-highlight=default,old |
f3f5c7f5
JH
926 test_decode_color >current &&
927 test_cmp expect.default-old current &&
928
a655a595 929 git diff --color --ws-error-highlight=all |
f3f5c7f5
JH
930 test_decode_color >current &&
931 test_cmp expect.all current &&
932
a655a595 933 git diff --color --ws-error-highlight=none |
f3f5c7f5
JH
934 test_decode_color >current &&
935 test_cmp expect.none current
936
b8767f79
JH
937'
938
a17505f2
JH
939test_expect_success 'test diff.wsErrorHighlight config' '
940
a655a595 941 git -c diff.wsErrorHighlight=default,old diff --color |
a17505f2
JH
942 test_decode_color >current &&
943 test_cmp expect.default-old current &&
944
a655a595 945 git -c diff.wsErrorHighlight=all diff --color |
a17505f2
JH
946 test_decode_color >current &&
947 test_cmp expect.all current &&
948
a655a595 949 git -c diff.wsErrorHighlight=none diff --color |
a17505f2
JH
950 test_decode_color >current &&
951 test_cmp expect.none current
952
953'
954
955test_expect_success 'option overrides diff.wsErrorHighlight' '
956
a655a595
JK
957 git -c diff.wsErrorHighlight=none \
958 diff --color --ws-error-highlight=default,old |
a17505f2
JH
959 test_decode_color >current &&
960 test_cmp expect.default-old current &&
961
a655a595
JK
962 git -c diff.wsErrorHighlight=default \
963 diff --color --ws-error-highlight=all |
a17505f2
JH
964 test_decode_color >current &&
965 test_cmp expect.all current &&
966
a655a595
JK
967 git -c diff.wsErrorHighlight=all \
968 diff --color --ws-error-highlight=none |
a17505f2
JH
969 test_decode_color >current &&
970 test_cmp expect.none current
971
972'
973
2e2d5ac1
SB
974test_expect_success 'detect moved code, complete file' '
975 git reset --hard &&
976 cat <<-\EOF >test.c &&
977 #include<stdio.h>
978 main()
979 {
980 printf("Hello World");
981 }
982 EOF
983 git add test.c &&
984 git commit -m "add main function" &&
985 git mv test.c main.c &&
986 test_config color.diff.oldMoved "normal red" &&
987 test_config color.diff.newMoved "normal green" &&
269c73e8 988 git diff HEAD --color-moved=zebra --color --no-renames | test_decode_color >actual &&
2e2d5ac1
SB
989 cat >expected <<-\EOF &&
990 <BOLD>diff --git a/main.c b/main.c<RESET>
991 <BOLD>new file mode 100644<RESET>
992 <BOLD>index 0000000..a986c57<RESET>
993 <BOLD>--- /dev/null<RESET>
994 <BOLD>+++ b/main.c<RESET>
995 <CYAN>@@ -0,0 +1,5 @@<RESET>
996 <BGREEN>+<RESET><BGREEN>#include<stdio.h><RESET>
997 <BGREEN>+<RESET><BGREEN>main()<RESET>
998 <BGREEN>+<RESET><BGREEN>{<RESET>
999 <BGREEN>+<RESET><BGREEN>printf("Hello World");<RESET>
1000 <BGREEN>+<RESET><BGREEN>}<RESET>
1001 <BOLD>diff --git a/test.c b/test.c<RESET>
1002 <BOLD>deleted file mode 100644<RESET>
1003 <BOLD>index a986c57..0000000<RESET>
1004 <BOLD>--- a/test.c<RESET>
1005 <BOLD>+++ /dev/null<RESET>
1006 <CYAN>@@ -1,5 +0,0 @@<RESET>
1007 <BRED>-#include<stdio.h><RESET>
1008 <BRED>-main()<RESET>
1009 <BRED>-{<RESET>
1010 <BRED>-printf("Hello World");<RESET>
1011 <BRED>-}<RESET>
1012 EOF
1013
1014 test_cmp expected actual
1015'
1016
1017test_expect_success 'detect malicious moved code, inside file' '
1018 test_config color.diff.oldMoved "normal red" &&
1019 test_config color.diff.newMoved "normal green" &&
1020 test_config color.diff.oldMovedAlternative "blue" &&
1021 test_config color.diff.newMovedAlternative "yellow" &&
1022 git reset --hard &&
1023 cat <<-\EOF >main.c &&
1024 #include<stdio.h>
1025 int stuff()
1026 {
1027 printf("Hello ");
1028 printf("World\n");
1029 }
1030
1031 int secure_foo(struct user *u)
1032 {
1033 if (!u->is_allowed_foo)
1034 return;
1035 foo(u);
1036 }
1037
1038 int main()
1039 {
1040 foo();
1041 }
1042 EOF
1043 cat <<-\EOF >test.c &&
1044 #include<stdio.h>
1045 int bar()
1046 {
1047 printf("Hello World, but different\n");
1048 }
1049
1050 int another_function()
1051 {
1052 bar();
1053 }
1054 EOF
1055 git add main.c test.c &&
1056 git commit -m "add main and test file" &&
1057 cat <<-\EOF >main.c &&
1058 #include<stdio.h>
1059 int stuff()
1060 {
1061 printf("Hello ");
1062 printf("World\n");
1063 }
1064
1065 int main()
1066 {
1067 foo();
1068 }
1069 EOF
1070 cat <<-\EOF >test.c &&
1071 #include<stdio.h>
1072 int bar()
1073 {
1074 printf("Hello World, but different\n");
1075 }
1076
1077 int secure_foo(struct user *u)
1078 {
1079 foo(u);
1080 if (!u->is_allowed_foo)
1081 return;
1082 }
1083
1084 int another_function()
1085 {
1086 bar();
1087 }
1088 EOF
269c73e8 1089 git diff HEAD --no-renames --color-moved=zebra --color | test_decode_color >actual &&
2e2d5ac1
SB
1090 cat <<-\EOF >expected &&
1091 <BOLD>diff --git a/main.c b/main.c<RESET>
1092 <BOLD>index 27a619c..7cf9336 100644<RESET>
1093 <BOLD>--- a/main.c<RESET>
1094 <BOLD>+++ b/main.c<RESET>
1095 <CYAN>@@ -5,13 +5,6 @@<RESET> <RESET>printf("Hello ");<RESET>
1096 printf("World\n");<RESET>
1097 }<RESET>
1098 <RESET>
1099 <BRED>-int secure_foo(struct user *u)<RESET>
1100 <BRED>-{<RESET>
1101 <BLUE>-if (!u->is_allowed_foo)<RESET>
1102 <BLUE>-return;<RESET>
f0b8fb6e
JT
1103 <RED>-foo(u);<RESET>
1104 <RED>-}<RESET>
1105 <RED>-<RESET>
2e2d5ac1
SB
1106 int main()<RESET>
1107 {<RESET>
1108 foo();<RESET>
1109 <BOLD>diff --git a/test.c b/test.c<RESET>
1110 <BOLD>index 1dc1d85..2bedec9 100644<RESET>
1111 <BOLD>--- a/test.c<RESET>
1112 <BOLD>+++ b/test.c<RESET>
1113 <CYAN>@@ -4,6 +4,13 @@<RESET> <RESET>int bar()<RESET>
1114 printf("Hello World, but different\n");<RESET>
1115 }<RESET>
1116 <RESET>
1117 <BGREEN>+<RESET><BGREEN>int secure_foo(struct user *u)<RESET>
1118 <BGREEN>+<RESET><BGREEN>{<RESET>
f0b8fb6e 1119 <GREEN>+<RESET><GREEN>foo(u);<RESET>
2e2d5ac1
SB
1120 <BGREEN>+<RESET><BGREEN>if (!u->is_allowed_foo)<RESET>
1121 <BGREEN>+<RESET><BGREEN>return;<RESET>
f0b8fb6e
JT
1122 <GREEN>+<RESET><GREEN>}<RESET>
1123 <GREEN>+<RESET>
2e2d5ac1
SB
1124 int another_function()<RESET>
1125 {<RESET>
1126 bar();<RESET>
1127 EOF
1128
1129 test_cmp expected actual
1130'
1131
176841f0
SB
1132test_expect_success 'plain moved code, inside file' '
1133 test_config color.diff.oldMoved "normal red" &&
1134 test_config color.diff.newMoved "normal green" &&
1135 test_config color.diff.oldMovedAlternative "blue" &&
1136 test_config color.diff.newMovedAlternative "yellow" &&
1137 # needs previous test as setup
269c73e8 1138 git diff HEAD --no-renames --color-moved=plain --color | test_decode_color >actual &&
176841f0
SB
1139 cat <<-\EOF >expected &&
1140 <BOLD>diff --git a/main.c b/main.c<RESET>
1141 <BOLD>index 27a619c..7cf9336 100644<RESET>
1142 <BOLD>--- a/main.c<RESET>
1143 <BOLD>+++ b/main.c<RESET>
1144 <CYAN>@@ -5,13 +5,6 @@<RESET> <RESET>printf("Hello ");<RESET>
1145 printf("World\n");<RESET>
1146 }<RESET>
1147 <RESET>
1148 <BRED>-int secure_foo(struct user *u)<RESET>
1149 <BRED>-{<RESET>
1150 <BRED>-if (!u->is_allowed_foo)<RESET>
1151 <BRED>-return;<RESET>
1152 <BRED>-foo(u);<RESET>
1153 <BRED>-}<RESET>
1154 <BRED>-<RESET>
1155 int main()<RESET>
1156 {<RESET>
1157 foo();<RESET>
1158 <BOLD>diff --git a/test.c b/test.c<RESET>
1159 <BOLD>index 1dc1d85..2bedec9 100644<RESET>
1160 <BOLD>--- a/test.c<RESET>
1161 <BOLD>+++ b/test.c<RESET>
1162 <CYAN>@@ -4,6 +4,13 @@<RESET> <RESET>int bar()<RESET>
1163 printf("Hello World, but different\n");<RESET>
1164 }<RESET>
1165 <RESET>
1166 <BGREEN>+<RESET><BGREEN>int secure_foo(struct user *u)<RESET>
1167 <BGREEN>+<RESET><BGREEN>{<RESET>
1168 <BGREEN>+<RESET><BGREEN>foo(u);<RESET>
1169 <BGREEN>+<RESET><BGREEN>if (!u->is_allowed_foo)<RESET>
1170 <BGREEN>+<RESET><BGREEN>return;<RESET>
1171 <BGREEN>+<RESET><BGREEN>}<RESET>
1172 <BGREEN>+<RESET>
1173 int another_function()<RESET>
1174 {<RESET>
1175 bar();<RESET>
1176 EOF
1177
1178 test_cmp expected actual
1179'
1180
86b452e2
SB
1181test_expect_success 'detect permutations inside moved code -- dimmed_zebra' '
1182 git reset --hard &&
1183 cat <<-\EOF >lines.txt &&
f0b8fb6e
JT
1184 long line 1
1185 long line 2
1186 long line 3
86b452e2
SB
1187 line 4
1188 line 5
1189 line 6
1190 line 7
1191 line 8
1192 line 9
1193 line 10
1194 line 11
1195 line 12
1196 line 13
f0b8fb6e
JT
1197 long line 14
1198 long line 15
1199 long line 16
86b452e2
SB
1200 EOF
1201 git add lines.txt &&
1202 git commit -m "add poetry" &&
1203 cat <<-\EOF >lines.txt &&
1204 line 4
1205 line 5
1206 line 6
1207 line 7
1208 line 8
1209 line 9
f0b8fb6e
JT
1210 long line 1
1211 long line 2
1212 long line 3
1213 long line 14
1214 long line 15
1215 long line 16
86b452e2
SB
1216 line 10
1217 line 11
1218 line 12
1219 line 13
1220 EOF
1221 test_config color.diff.oldMoved "magenta" &&
1222 test_config color.diff.newMoved "cyan" &&
1223 test_config color.diff.oldMovedAlternative "blue" &&
1224 test_config color.diff.newMovedAlternative "yellow" &&
1225 test_config color.diff.oldMovedDimmed "normal magenta" &&
1226 test_config color.diff.newMovedDimmed "normal cyan" &&
1227 test_config color.diff.oldMovedAlternativeDimmed "normal blue" &&
1228 test_config color.diff.newMovedAlternativeDimmed "normal yellow" &&
269c73e8 1229 git diff HEAD --no-renames --color-moved=dimmed_zebra --color |
f0b8fb6e
JT
1230 grep -v "index" |
1231 test_decode_color >actual &&
86b452e2
SB
1232 cat <<-\EOF >expected &&
1233 <BOLD>diff --git a/lines.txt b/lines.txt<RESET>
86b452e2
SB
1234 <BOLD>--- a/lines.txt<RESET>
1235 <BOLD>+++ b/lines.txt<RESET>
1236 <CYAN>@@ -1,16 +1,16 @@<RESET>
f0b8fb6e
JT
1237 <BMAGENTA>-long line 1<RESET>
1238 <BMAGENTA>-long line 2<RESET>
1239 <BMAGENTA>-long line 3<RESET>
86b452e2
SB
1240 line 4<RESET>
1241 line 5<RESET>
1242 line 6<RESET>
1243 line 7<RESET>
1244 line 8<RESET>
1245 line 9<RESET>
f0b8fb6e
JT
1246 <BCYAN>+<RESET><BCYAN>long line 1<RESET>
1247 <BCYAN>+<RESET><BCYAN>long line 2<RESET>
1248 <CYAN>+<RESET><CYAN>long line 3<RESET>
1249 <YELLOW>+<RESET><YELLOW>long line 14<RESET>
1250 <BYELLOW>+<RESET><BYELLOW>long line 15<RESET>
1251 <BYELLOW>+<RESET><BYELLOW>long line 16<RESET>
86b452e2
SB
1252 line 10<RESET>
1253 line 11<RESET>
1254 line 12<RESET>
1255 line 13<RESET>
f0b8fb6e
JT
1256 <BMAGENTA>-long line 14<RESET>
1257 <BMAGENTA>-long line 15<RESET>
1258 <BMAGENTA>-long line 16<RESET>
86b452e2
SB
1259 EOF
1260 test_cmp expected actual
1261'
1262
1263test_expect_success 'cmd option assumes configured colored-moved' '
1264 test_config color.diff.oldMoved "magenta" &&
1265 test_config color.diff.newMoved "cyan" &&
1266 test_config color.diff.oldMovedAlternative "blue" &&
1267 test_config color.diff.newMovedAlternative "yellow" &&
1268 test_config color.diff.oldMovedDimmed "normal magenta" &&
1269 test_config color.diff.newMovedDimmed "normal cyan" &&
1270 test_config color.diff.oldMovedAlternativeDimmed "normal blue" &&
1271 test_config color.diff.newMovedAlternativeDimmed "normal yellow" &&
1272 test_config diff.colorMoved zebra &&
269c73e8 1273 git diff HEAD --no-renames --color-moved --color |
f0b8fb6e
JT
1274 grep -v "index" |
1275 test_decode_color >actual &&
86b452e2
SB
1276 cat <<-\EOF >expected &&
1277 <BOLD>diff --git a/lines.txt b/lines.txt<RESET>
86b452e2
SB
1278 <BOLD>--- a/lines.txt<RESET>
1279 <BOLD>+++ b/lines.txt<RESET>
1280 <CYAN>@@ -1,16 +1,16 @@<RESET>
f0b8fb6e
JT
1281 <MAGENTA>-long line 1<RESET>
1282 <MAGENTA>-long line 2<RESET>
1283 <MAGENTA>-long line 3<RESET>
86b452e2
SB
1284 line 4<RESET>
1285 line 5<RESET>
1286 line 6<RESET>
1287 line 7<RESET>
1288 line 8<RESET>
1289 line 9<RESET>
f0b8fb6e
JT
1290 <CYAN>+<RESET><CYAN>long line 1<RESET>
1291 <CYAN>+<RESET><CYAN>long line 2<RESET>
1292 <CYAN>+<RESET><CYAN>long line 3<RESET>
1293 <YELLOW>+<RESET><YELLOW>long line 14<RESET>
1294 <YELLOW>+<RESET><YELLOW>long line 15<RESET>
1295 <YELLOW>+<RESET><YELLOW>long line 16<RESET>
86b452e2
SB
1296 line 10<RESET>
1297 line 11<RESET>
1298 line 12<RESET>
1299 line 13<RESET>
f0b8fb6e
JT
1300 <MAGENTA>-long line 14<RESET>
1301 <MAGENTA>-long line 15<RESET>
1302 <MAGENTA>-long line 16<RESET>
86b452e2
SB
1303 EOF
1304 test_cmp expected actual
1305'
1306
2e2d5ac1
SB
1307test_expect_success 'no effect from --color-moved with --word-diff' '
1308 cat <<-\EOF >text.txt &&
1309 Lorem Ipsum is simply dummy text of the printing and typesetting industry.
1310 EOF
1311 git add text.txt &&
1312 git commit -a -m "clean state" &&
1313 cat <<-\EOF >text.txt &&
1314 simply Lorem Ipsum dummy is text of the typesetting and printing industry.
1315 EOF
1316 git diff --color-moved --word-diff >actual &&
1317 git diff --word-diff >expect &&
1318 test_cmp expect actual
1319'
1320
ecd51258 1321test_expect_success 'set up whitespace tests' '
2e2d5ac1 1322 git reset --hard &&
ecd51258
JK
1323 # Note that these lines have no leading or trailing whitespace.
1324 cat <<-\EOF >lines.txt &&
1325 line 1
1326 line 2
1327 line 3
1328 line 4
83de23cf 1329 line 5
f0b8fb6e
JT
1330 long line 6
1331 long line 7
83de23cf
JK
1332 long line 8
1333 long line 9
ecd51258
JK
1334 EOF
1335 git add lines.txt &&
1336 git commit -m "add poetry" &&
1337 git config color.diff.oldMoved "magenta" &&
1338 git config color.diff.newMoved "cyan"
1339'
1340
1341test_expect_success 'move detection ignoring whitespace ' '
1342 q_to_tab <<-\EOF >lines.txt &&
ecd51258
JK
1343 Qlong line 6
1344 Qlong line 7
83de23cf
JK
1345 Qlong line 8
1346 Qchanged long line 9
ecd51258
JK
1347 line 1
1348 line 2
1349 line 3
1350 line 4
83de23cf 1351 line 5
ecd51258 1352 EOF
269c73e8 1353 git diff HEAD --no-renames --color-moved --color |
f0b8fb6e
JT
1354 grep -v "index" |
1355 test_decode_color >actual &&
2e2d5ac1
SB
1356 cat <<-\EOF >expected &&
1357 <BOLD>diff --git a/lines.txt b/lines.txt<RESET>
2e2d5ac1
SB
1358 <BOLD>--- a/lines.txt<RESET>
1359 <BOLD>+++ b/lines.txt<RESET>
83de23cf 1360 <CYAN>@@ -1,9 +1,9 @@<RESET>
f0b8fb6e
JT
1361 <GREEN>+<RESET> <GREEN>long line 6<RESET>
1362 <GREEN>+<RESET> <GREEN>long line 7<RESET>
83de23cf
JK
1363 <GREEN>+<RESET> <GREEN>long line 8<RESET>
1364 <GREEN>+<RESET> <GREEN>changed long line 9<RESET>
2e2d5ac1
SB
1365 line 1<RESET>
1366 line 2<RESET>
1367 line 3<RESET>
1368 line 4<RESET>
83de23cf 1369 line 5<RESET>
f0b8fb6e
JT
1370 <RED>-long line 6<RESET>
1371 <RED>-long line 7<RESET>
83de23cf
JK
1372 <RED>-long line 8<RESET>
1373 <RED>-long line 9<RESET>
2e2d5ac1
SB
1374 EOF
1375 test_cmp expected actual &&
1376
269c73e8 1377 git diff HEAD --no-renames -w --color-moved --color |
f0b8fb6e
JT
1378 grep -v "index" |
1379 test_decode_color >actual &&
2e2d5ac1
SB
1380 cat <<-\EOF >expected &&
1381 <BOLD>diff --git a/lines.txt b/lines.txt<RESET>
2e2d5ac1
SB
1382 <BOLD>--- a/lines.txt<RESET>
1383 <BOLD>+++ b/lines.txt<RESET>
83de23cf 1384 <CYAN>@@ -1,9 +1,9 @@<RESET>
f0b8fb6e
JT
1385 <CYAN>+<RESET> <CYAN>long line 6<RESET>
1386 <CYAN>+<RESET> <CYAN>long line 7<RESET>
83de23cf
JK
1387 <CYAN>+<RESET> <CYAN>long line 8<RESET>
1388 <GREEN>+<RESET> <GREEN>changed long line 9<RESET>
2e2d5ac1
SB
1389 line 1<RESET>
1390 line 2<RESET>
1391 line 3<RESET>
1392 line 4<RESET>
83de23cf 1393 line 5<RESET>
f0b8fb6e
JT
1394 <MAGENTA>-long line 6<RESET>
1395 <MAGENTA>-long line 7<RESET>
83de23cf
JK
1396 <MAGENTA>-long line 8<RESET>
1397 <RED>-long line 9<RESET>
2e2d5ac1
SB
1398 EOF
1399 test_cmp expected actual
1400'
1401
ecd51258
JK
1402test_expect_success 'clean up whitespace-test colors' '
1403 git config --unset color.diff.oldMoved &&
1404 git config --unset color.diff.newMoved
1405'
1406
f0b8fb6e 1407test_expect_success '--color-moved block at end of diff output respects MIN_ALNUM_COUNT' '
09153277
JT
1408 git reset --hard &&
1409 >bar &&
1410 cat <<-\EOF >foo &&
1411 irrelevant_line
1412 line1
1413 EOF
1414 git add foo bar &&
1415 git commit -m x &&
1416
1417 cat <<-\EOF >bar &&
1418 line1
1419 EOF
1420 cat <<-\EOF >foo &&
1421 irrelevant_line
1422 EOF
1423
269c73e8 1424 git diff HEAD --color-moved=zebra --color --no-renames |
09153277
JT
1425 grep -v "index" |
1426 test_decode_color >actual &&
1427 cat >expected <<-\EOF &&
1428 <BOLD>diff --git a/bar b/bar<RESET>
1429 <BOLD>--- a/bar<RESET>
1430 <BOLD>+++ b/bar<RESET>
1431 <CYAN>@@ -0,0 +1 @@<RESET>
1432 <GREEN>+<RESET><GREEN>line1<RESET>
1433 <BOLD>diff --git a/foo b/foo<RESET>
1434 <BOLD>--- a/foo<RESET>
1435 <BOLD>+++ b/foo<RESET>
1436 <CYAN>@@ -1,2 +1 @@<RESET>
1437 irrelevant_line<RESET>
1438 <RED>-line1<RESET>
1439 EOF
1440
1441 test_cmp expected actual
1442'
1443
f0b8fb6e
JT
1444test_expect_success '--color-moved respects MIN_ALNUM_COUNT' '
1445 git reset --hard &&
1446 cat <<-\EOF >foo &&
1447 nineteen chars 456789
1448 irrelevant_line
1449 twenty chars 234567890
1450 EOF
1451 >bar &&
1452 git add foo bar &&
1453 git commit -m x &&
1454
1455 cat <<-\EOF >foo &&
1456 irrelevant_line
1457 EOF
1458 cat <<-\EOF >bar &&
1459 twenty chars 234567890
1460 nineteen chars 456789
1461 EOF
1462
269c73e8 1463 git diff HEAD --color-moved=zebra --color --no-renames |
f0b8fb6e
JT
1464 grep -v "index" |
1465 test_decode_color >actual &&
1466 cat >expected <<-\EOF &&
1467 <BOLD>diff --git a/bar b/bar<RESET>
1468 <BOLD>--- a/bar<RESET>
1469 <BOLD>+++ b/bar<RESET>
1470 <CYAN>@@ -0,0 +1,2 @@<RESET>
1471 <BOLD;CYAN>+<RESET><BOLD;CYAN>twenty chars 234567890<RESET>
1472 <GREEN>+<RESET><GREEN>nineteen chars 456789<RESET>
1473 <BOLD>diff --git a/foo b/foo<RESET>
1474 <BOLD>--- a/foo<RESET>
1475 <BOLD>+++ b/foo<RESET>
1476 <CYAN>@@ -1,3 +1 @@<RESET>
1477 <RED>-nineteen chars 456789<RESET>
1478 irrelevant_line<RESET>
1479 <BOLD;MAGENTA>-twenty chars 234567890<RESET>
1480 EOF
1481
1482 test_cmp expected actual
1483'
1484
1485test_expect_success '--color-moved treats adjacent blocks as separate for MIN_ALNUM_COUNT' '
1486 git reset --hard &&
1487 cat <<-\EOF >foo &&
1488 7charsA
1489 irrelevant_line
1490 7charsB
1491 7charsC
1492 EOF
1493 >bar &&
1494 git add foo bar &&
1495 git commit -m x &&
1496
1497 cat <<-\EOF >foo &&
1498 irrelevant_line
1499 EOF
1500 cat <<-\EOF >bar &&
1501 7charsB
1502 7charsC
1503 7charsA
1504 EOF
1505
269c73e8 1506 git diff HEAD --color-moved=zebra --color --no-renames | grep -v "index" | test_decode_color >actual &&
f0b8fb6e
JT
1507 cat >expected <<-\EOF &&
1508 <BOLD>diff --git a/bar b/bar<RESET>
1509 <BOLD>--- a/bar<RESET>
1510 <BOLD>+++ b/bar<RESET>
1511 <CYAN>@@ -0,0 +1,3 @@<RESET>
1512 <GREEN>+<RESET><GREEN>7charsB<RESET>
1513 <GREEN>+<RESET><GREEN>7charsC<RESET>
1514 <GREEN>+<RESET><GREEN>7charsA<RESET>
1515 <BOLD>diff --git a/foo b/foo<RESET>
1516 <BOLD>--- a/foo<RESET>
1517 <BOLD>+++ b/foo<RESET>
1518 <CYAN>@@ -1,4 +1 @@<RESET>
1519 <RED>-7charsA<RESET>
1520 irrelevant_line<RESET>
1521 <RED>-7charsB<RESET>
1522 <RED>-7charsC<RESET>
1523 EOF
1524
1525 test_cmp expected actual
1526'
1527
2e2d5ac1
SB
1528test_expect_success 'move detection with submodules' '
1529 test_create_repo bananas &&
1530 echo ripe >bananas/recipe &&
1531 git -C bananas add recipe &&
1532 test_commit fruit &&
1533 test_commit -C bananas recipe &&
1534 git submodule add ./bananas &&
1535 git add bananas &&
1536 git commit -a -m "bananas are like a heavy library?" &&
1537 echo foul >bananas/recipe &&
1538 echo ripe >fruit.t &&
1539
269c73e8 1540 git diff --submodule=diff --color-moved --color >actual &&
2e2d5ac1
SB
1541
1542 # no move detection as the moved line is across repository boundaries.
1543 test_decode_color <actual >decoded_actual &&
1544 ! grep BGREEN decoded_actual &&
1545 ! grep BRED decoded_actual &&
1546
1547 # nor did we mess with it another way
269c73e8 1548 git diff --submodule=diff --color | test_decode_color >expect &&
2e2d5ac1
SB
1549 test_cmp expect decoded_actual
1550'
1551
fa5ba2c1
JK
1552test_expect_success 'move detection with whitespace changes' '
1553 test_when_finished "git reset --hard" &&
1554 test_seq 10 >test &&
1555 git add test &&
1556 sed s/3/42/ <test >test.tmp &&
1557 mv test.tmp test &&
1558 git -c diff.colormoved diff --ignore-space-change -- test
1559'
1560
2344d47f 1561test_done