3 # Copyright (c) 2006 Johannes E. Schindelin
6 test_description='Test special whitespace in diff engine.
10 . "$TEST_DIRECTORY"/diff-lib.sh
12 test_expect_success "Ray Lehtiniemi's example" '
18 git update-index --add x &&
28 cat <<-\EOF >expect &&
30 index adf3937..6edc172 100644
44 test_cmp expect out &&
47 test_cmp expect out &&
53 test_expect_success 'another test, without options' '
54 tr Q "\015" <<-\EOF >x &&
55 whitespace at beginning
57 whitespace in the middle
65 tr "_" " " <<-\EOF >x &&
66 _ whitespace at beginning
68 white space in the middle
74 tr "Q_" "\015 " <<-\EOF >expect &&
76 index d99af23..22d9f73 100644
80 -whitespace at beginning
82 -whitespace in the middle
84 + whitespace at beginning
86 +white space in the middle
94 test_cmp expect out &&
98 test_cmp expect out &&
100 git diff -w -b >out &&
101 test_cmp expect out &&
103 git diff -w --ignore-space-at-eol >out &&
104 test_cmp expect out &&
106 git diff -w -b --ignore-space-at-eol >out &&
107 test_cmp expect out &&
109 git diff -w --ignore-cr-at-eol >out &&
110 test_cmp expect out &&
112 tr "Q_" "\015 " <<-\EOF >expect &&
114 index d99af23..22d9f73 100644
118 -whitespace at beginning
119 +_ whitespace at beginning
121 -whitespace in the middle
122 +white space in the middle
128 test_cmp expect out &&
130 git diff -b --ignore-space-at-eol >out &&
131 test_cmp expect out &&
133 git diff -b --ignore-cr-at-eol >out &&
134 test_cmp expect out &&
136 tr "Q_" "\015 " <<-\EOF >expect &&
138 index d99af23..22d9f73 100644
142 -whitespace at beginning
144 -whitespace in the middle
145 +_ whitespace at beginning
147 +white space in the middle
152 git diff --ignore-space-at-eol >out &&
153 test_cmp expect out &&
155 git diff --ignore-space-at-eol --ignore-cr-at-eol >out &&
156 test_cmp expect out &&
158 tr "Q_" "\015 " <<-\EOF >expect &&
160 index_d99af23..22d9f73 100644
164 -whitespace at beginning
166 -whitespace in the middle
168 +_ whitespace at beginning
170 +white space in the middle
175 git diff --ignore-cr-at-eol >out &&
179 test_expect_success 'ignore-blank-lines: only new lines' '
181 git update-index x &&
182 test_seq 5 | sed "/3/i\\
184 git diff --ignore-blank-lines >out &&
189 test_expect_success 'ignore-blank-lines: only new lines with space' '
191 git update-index x &&
192 test_seq 5 | sed "/3/i\\
194 git diff -w --ignore-blank-lines >out &&
199 test_expect_success 'ignore-blank-lines: after change' '
211 git update-index x &&
224 git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp &&
225 cat <<-\EOF >expected &&
239 compare_diff_patch expected out.tmp
242 test_expect_success 'ignore-blank-lines: before change' '
253 git update-index x &&
266 git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp &&
267 cat <<-\EOF >expected &&
280 compare_diff_patch expected out.tmp
283 test_expect_success 'ignore-blank-lines: between changes' '
298 git update-index x &&
315 git diff --ignore-blank-lines >out.tmp &&
316 cat <<-\EOF >expected &&
337 compare_diff_patch expected out.tmp
340 test_expect_success 'ignore-blank-lines: between changes (with interhunkctx)' '
342 git update-index x &&
360 git diff --inter-hunk-context=2 --ignore-blank-lines >out.tmp &&
361 cat <<-\EOF >expected &&
382 compare_diff_patch expected out.tmp
385 test_expect_success 'ignore-blank-lines: scattered spaces' '
387 git update-index x &&
408 git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp &&
409 cat <<-\EOF >expected &&
424 compare_diff_patch expected out.tmp
427 test_expect_success 'ignore-blank-lines: spaces coalesce' '
429 git update-index x &&
443 git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp &&
444 cat <<-\EOF >expected &&
461 compare_diff_patch expected out.tmp
464 test_expect_success 'ignore-blank-lines: mix changes and blank lines' '
466 git update-index x &&
492 git diff --ignore-blank-lines >out.tmp &&
493 cat <<-\EOF >expected &&
522 compare_diff_patch expected out.tmp
525 test_expect_success 'check mixed spaces and tabs in indent' '
526 # This is indented with SP HT SP.
528 git diff --check | grep "space before tab in indent"
531 test_expect_success 'check mixed tabs and spaces in indent' '
532 # This is indented with HT SP HT.
534 git diff --check | grep "space before tab in indent"
537 test_expect_success 'check with no whitespace errors' '
538 git commit -m "snapshot" &&
543 test_expect_success 'check with trailing whitespace' '
545 test_must_fail git diff --check
548 test_expect_success 'check with space before tab in indent' '
549 # indent has space followed by hard tab
551 test_must_fail git diff --check
554 test_expect_success '--check and --exit-code are not exclusive' '
556 git diff --check --exit-code
559 test_expect_success '--check and --quiet are not exclusive' '
560 git diff --check --quiet
563 test_expect_success 'check staged with no whitespace errors' '
566 git diff --cached --check
569 test_expect_success 'check staged with trailing whitespace' '
572 test_must_fail git diff --cached --check
575 test_expect_success 'check staged with space before tab in indent' '
576 # indent has space followed by hard tab
579 test_must_fail git diff --cached --check
582 test_expect_success 'check with no whitespace errors (diff-index)' '
585 git diff-index --check HEAD
588 test_expect_success 'check with trailing whitespace (diff-index)' '
591 test_must_fail git diff-index --check HEAD
594 test_expect_success 'check with space before tab in indent (diff-index)' '
595 # indent has space followed by hard tab
598 test_must_fail git diff-index --check HEAD
601 test_expect_success 'check staged with no whitespace errors (diff-index)' '
604 git diff-index --cached --check HEAD
607 test_expect_success 'check staged with trailing whitespace (diff-index)' '
610 test_must_fail git diff-index --cached --check HEAD
613 test_expect_success 'check staged with space before tab in indent (diff-index)' '
614 # indent has space followed by hard tab
617 test_must_fail git diff-index --cached --check HEAD
620 test_expect_success 'check with no whitespace errors (diff-tree)' '
622 git commit -m "new commit" x &&
623 git diff-tree --check HEAD^ HEAD
626 test_expect_success 'check with trailing whitespace (diff-tree)' '
628 git commit -m "another commit" x &&
629 test_must_fail git diff-tree --check HEAD^ HEAD
632 test_expect_success 'check with space before tab in indent (diff-tree)' '
633 # indent has space followed by hard tab
635 git commit -m "yet another" x &&
636 test_must_fail git diff-tree --check HEAD^ HEAD
639 test_expect_success 'check trailing whitespace (trailing-space: off)' '
640 git config core.whitespace "-trailing-space" &&
641 echo "foo (); " >x &&
645 test_expect_success 'check trailing whitespace (trailing-space: on)' '
646 git config core.whitespace "trailing-space" &&
647 echo "foo (); " >x &&
648 test_must_fail git diff --check
651 test_expect_success 'check space before tab in indent (space-before-tab: off)' '
652 # indent contains space followed by HT
653 git config core.whitespace "-space-before-tab" &&
654 echo " foo ();" >x &&
658 test_expect_success 'check space before tab in indent (space-before-tab: on)' '
659 # indent contains space followed by HT
660 git config core.whitespace "space-before-tab" &&
661 echo " foo (); " >x &&
662 test_must_fail git diff --check
665 test_expect_success 'check spaces as indentation (indent-with-non-tab: off)' '
666 git config core.whitespace "-indent-with-non-tab" &&
667 echo " foo ();" >x &&
671 test_expect_success 'check spaces as indentation (indent-with-non-tab: on)' '
672 git config core.whitespace "indent-with-non-tab" &&
673 echo " foo ();" >x &&
674 test_must_fail git diff --check
677 test_expect_success 'ditto, but tabwidth=9' '
678 git config core.whitespace "indent-with-non-tab,tabwidth=9" &&
682 test_expect_success 'check tabs and spaces as indentation (indent-with-non-tab: on)' '
683 git config core.whitespace "indent-with-non-tab" &&
684 echo " foo ();" >x &&
685 test_must_fail git diff --check
688 test_expect_success 'ditto, but tabwidth=10' '
689 git config core.whitespace "indent-with-non-tab,tabwidth=10" &&
690 test_must_fail git diff --check
693 test_expect_success 'ditto, but tabwidth=20' '
694 git config core.whitespace "indent-with-non-tab,tabwidth=20" &&
698 test_expect_success 'check tabs as indentation (tab-in-indent: off)' '
699 git config core.whitespace "-tab-in-indent" &&
700 echo " foo ();" >x &&
704 test_expect_success 'check tabs as indentation (tab-in-indent: on)' '
705 git config core.whitespace "tab-in-indent" &&
706 echo " foo ();" >x &&
707 test_must_fail git diff --check
710 test_expect_success 'check tabs and spaces as indentation (tab-in-indent: on)' '
711 git config core.whitespace "tab-in-indent" &&
712 echo " foo ();" >x &&
713 test_must_fail git diff --check
716 test_expect_success 'ditto, but tabwidth=1 (must be irrelevant)' '
717 git config core.whitespace "tab-in-indent,tabwidth=1" &&
718 test_must_fail git diff --check
721 test_expect_success 'check tab-in-indent and indent-with-non-tab conflict' '
722 git config core.whitespace "tab-in-indent,indent-with-non-tab" &&
724 test_must_fail git diff --check
727 test_expect_success 'check tab-in-indent excluded from wildcard whitespace attribute' '
728 git config --unset core.whitespace &&
729 echo "x whitespace" >.gitattributes &&
730 echo " foo ();" >x &&
735 test_expect_success 'line numbers in --check output are correct' '
737 echo "foo(); " >>x &&
738 git diff --check | grep "x:2:"
741 test_expect_success 'checkdiff detects new trailing blank lines (1)' '
744 git diff --check | grep "new blank line"
747 test_expect_success 'checkdiff detects new trailing blank lines (2)' '
748 { echo a; echo b; echo; echo; } >x &&
750 { echo a; echo; echo; echo; echo; } >x &&
751 git diff --check | grep "new blank line"
754 test_expect_success 'checkdiff allows new blank lines' '
758 echo "/* This is new */" &&
767 test_expect_success 'whitespace-only changes not reported' '
769 echo >x "hello world" &&
771 git commit -m "hello 1" &&
772 echo >x "hello world" &&
773 git diff -b >actual &&
774 test_cmp expect actual
779 similarity index NUM%
782 index 380c32a..a97b785 100644
784 test_expect_success 'whitespace-only changes reported across renames' '
786 for i in 1 2 3 4 5 6 7 8 9; do echo "$i$i$i$i$i$i"; done >x &&
788 git commit -m "base" &&
789 sed -e "5s/^/ /" x >z &&
792 git diff -w -M --cached |
793 sed -e "/^similarity index /s/[0-9][0-9]*/NUM/" >actual &&
794 test_cmp expect actual
798 diff --git a/empty b/void
799 similarity index 100%
804 test_expect_success 'rename empty' '
808 git commit -m empty &&
810 git diff -w --cached -M >current &&
811 test_cmp expected current
814 test_expect_success 'combined diff with autocrlf conversion' '
818 git commit -m "one side" x &&
819 git checkout HEAD^ &&
821 git commit -m "the other side" x &&
822 git config core.autocrlf true &&
823 test_must_fail git merge master &&
825 git diff | sed -e "1,/^@@@/d" >actual &&
830 # Start testing the colored format for whitespace checks
832 test_expect_success 'setup diff colors' '
833 git config color.diff always &&
834 git config color.diff.plain normal &&
835 git config color.diff.meta bold &&
836 git config color.diff.frag cyan &&
837 git config color.diff.func normal &&
838 git config color.diff.old red &&
839 git config color.diff.new green &&
840 git config color.diff.commit yellow &&
841 git config color.diff.whitespace blue &&
843 git config core.autocrlf false
846 test_expect_success 'diff that introduces a line with only tabs' '
847 git config core.whitespace blank-at-eol &&
850 git commit -m "initial" x &&
851 echo "{NTN}" | tr "NT" "\n\t" >>x &&
852 git -c color.diff=always diff | test_decode_color >current &&
854 cat >expected <<-\EOF &&
855 <BOLD>diff --git a/x b/x<RESET>
856 <BOLD>index 9daeafb..2874b91 100644<RESET>
859 <CYAN>@@ -1 +1,4 @@<RESET>
861 <GREEN>+<RESET><GREEN>{<RESET>
862 <GREEN>+<RESET><BLUE> <RESET>
863 <GREEN>+<RESET><GREEN>}<RESET>
866 test_cmp expected current
869 test_expect_success 'diff that introduces and removes ws breakages' '
872 echo "0. blank-at-eol " &&
873 echo "1. blank-at-eol "
875 git commit -a --allow-empty -m preimage &&
877 echo "0. blank-at-eol " &&
878 echo "1. still-blank-at-eol " &&
879 echo "2. and a new line "
882 git -c color.diff=always diff |
883 test_decode_color >current &&
885 cat >expected <<-\EOF &&
886 <BOLD>diff --git a/x b/x<RESET>
887 <BOLD>index d0233a2..700886e 100644<RESET>
890 <CYAN>@@ -1,2 +1,3 @@<RESET>
891 0. blank-at-eol <RESET>
892 <RED>-1. blank-at-eol <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>
897 test_cmp expected current
900 test_expect_success 'ws-error-highlight test setup' '
904 echo "0. blank-at-eol " &&
905 echo "1. blank-at-eol "
907 git commit -a --allow-empty -m preimage &&
909 echo "0. blank-at-eol " &&
910 echo "1. still-blank-at-eol " &&
911 echo "2. and a new line "
914 cat >expect.default-old <<-\EOF &&
915 <BOLD>diff --git a/x b/x<RESET>
916 <BOLD>index d0233a2..700886e 100644<RESET>
919 <CYAN>@@ -1,2 +1,3 @@<RESET>
920 0. blank-at-eol <RESET>
921 <RED>-<RESET><RED>1. blank-at-eol<RESET><BLUE> <RESET>
922 <GREEN>+<RESET><GREEN>1. still-blank-at-eol<RESET><BLUE> <RESET>
923 <GREEN>+<RESET><GREEN>2. and a new line<RESET><BLUE> <RESET>
926 cat >expect.all <<-\EOF &&
927 <BOLD>diff --git a/x b/x<RESET>
928 <BOLD>index d0233a2..700886e 100644<RESET>
931 <CYAN>@@ -1,2 +1,3 @@<RESET>
932 <RESET>0. blank-at-eol<RESET><BLUE> <RESET>
933 <RED>-<RESET><RED>1. blank-at-eol<RESET><BLUE> <RESET>
934 <GREEN>+<RESET><GREEN>1. still-blank-at-eol<RESET><BLUE> <RESET>
935 <GREEN>+<RESET><GREEN>2. and a new line<RESET><BLUE> <RESET>
938 cat >expect.none <<-\EOF
939 <BOLD>diff --git a/x b/x<RESET>
940 <BOLD>index d0233a2..700886e 100644<RESET>
943 <CYAN>@@ -1,2 +1,3 @@<RESET>
944 0. blank-at-eol <RESET>
945 <RED>-1. blank-at-eol <RESET>
946 <GREEN>+1. still-blank-at-eol <RESET>
947 <GREEN>+2. and a new line <RESET>
952 test_expect_success 'test --ws-error-highlight option' '
954 git -c color.diff=always diff --ws-error-highlight=default,old |
955 test_decode_color >current &&
956 test_cmp expect.default-old current &&
958 git -c color.diff=always diff --ws-error-highlight=all |
959 test_decode_color >current &&
960 test_cmp expect.all current &&
962 git -c color.diff=always diff --ws-error-highlight=none |
963 test_decode_color >current &&
964 test_cmp expect.none current
968 test_expect_success 'test diff.wsErrorHighlight config' '
970 git -c color.diff=always -c diff.wsErrorHighlight=default,old diff |
971 test_decode_color >current &&
972 test_cmp expect.default-old current &&
974 git -c color.diff=always -c diff.wsErrorHighlight=all diff |
975 test_decode_color >current &&
976 test_cmp expect.all current &&
978 git -c color.diff=always -c diff.wsErrorHighlight=none diff |
979 test_decode_color >current &&
980 test_cmp expect.none current
984 test_expect_success 'option overrides diff.wsErrorHighlight' '
986 git -c color.diff=always -c diff.wsErrorHighlight=none \
987 diff --ws-error-highlight=default,old |
988 test_decode_color >current &&
989 test_cmp expect.default-old current &&
991 git -c color.diff=always -c diff.wsErrorHighlight=default \
992 diff --ws-error-highlight=all |
993 test_decode_color >current &&
994 test_cmp expect.all current &&
996 git -c color.diff=always -c diff.wsErrorHighlight=all \
997 diff --ws-error-highlight=none |
998 test_decode_color >current &&
999 test_cmp expect.none current