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