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 &&
110 tr "Q_" "\015 " <<-\EOF >expect &&
112 index d99af23..22d9f73 100644
116 -whitespace at beginning
117 +_ whitespace at beginning
119 -whitespace in the middle
120 +white space in the middle
126 test_cmp expect out &&
128 git diff -b --ignore-space-at-eol >out &&
129 test_cmp expect out &&
131 tr "Q_" "\015 " <<-\EOF >expect &&
133 index d99af23..22d9f73 100644
137 -whitespace at beginning
139 -whitespace in the middle
140 +_ whitespace at beginning
142 +white space in the middle
147 git diff --ignore-space-at-eol >out &&
151 test_expect_success
'ignore-blank-lines: only new lines' '
153 git update-index x &&
154 test_seq 5 | sed "/3/i\\
156 git diff --ignore-blank-lines >out &&
161 test_expect_success
'ignore-blank-lines: only new lines with space' '
163 git update-index x &&
164 test_seq 5 | sed "/3/i\\
166 git diff -w --ignore-blank-lines >out &&
171 test_expect_success
'ignore-blank-lines: after change' '
183 git update-index x &&
196 git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp &&
197 cat <<-\EOF >expected &&
211 compare_diff_patch expected out.tmp
214 test_expect_success
'ignore-blank-lines: before change' '
225 git update-index x &&
238 git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp &&
239 cat <<-\EOF >expected &&
252 compare_diff_patch expected out.tmp
255 test_expect_success
'ignore-blank-lines: between changes' '
270 git update-index x &&
287 git diff --ignore-blank-lines >out.tmp &&
288 cat <<-\EOF >expected &&
309 compare_diff_patch expected out.tmp
312 test_expect_success
'ignore-blank-lines: between changes (with interhunkctx)' '
314 git update-index x &&
332 git diff --inter-hunk-context=2 --ignore-blank-lines >out.tmp &&
333 cat <<-\EOF >expected &&
354 compare_diff_patch expected out.tmp
357 test_expect_success
'ignore-blank-lines: scattered spaces' '
359 git update-index x &&
380 git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp &&
381 cat <<-\EOF >expected &&
396 compare_diff_patch expected out.tmp
399 test_expect_success
'ignore-blank-lines: spaces coalesce' '
401 git update-index x &&
415 git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp &&
416 cat <<-\EOF >expected &&
433 compare_diff_patch expected out.tmp
436 test_expect_success
'ignore-blank-lines: mix changes and blank lines' '
438 git update-index x &&
464 git diff --ignore-blank-lines >out.tmp &&
465 cat <<-\EOF >expected &&
494 compare_diff_patch expected out.tmp
497 test_expect_success
'check mixed spaces and tabs in indent' '
498 # This is indented with SP HT SP.
500 git diff --check | grep "space before tab in indent"
503 test_expect_success
'check mixed tabs and spaces in indent' '
504 # This is indented with HT SP HT.
506 git diff --check | grep "space before tab in indent"
509 test_expect_success
'check with no whitespace errors' '
510 git commit -m "snapshot" &&
515 test_expect_success
'check with trailing whitespace' '
517 test_must_fail git diff --check
520 test_expect_success
'check with space before tab in indent' '
521 # indent has space followed by hard tab
523 test_must_fail git diff --check
526 test_expect_success
'--check and --exit-code are not exclusive' '
528 git diff --check --exit-code
531 test_expect_success
'--check and --quiet are not exclusive' '
532 git diff --check --quiet
535 test_expect_success
'check staged with no whitespace errors' '
538 git diff --cached --check
541 test_expect_success
'check staged with trailing whitespace' '
544 test_must_fail git diff --cached --check
547 test_expect_success
'check staged with space before tab in indent' '
548 # indent has space followed by hard tab
551 test_must_fail git diff --cached --check
554 test_expect_success
'check with no whitespace errors (diff-index)' '
557 git diff-index --check HEAD
560 test_expect_success
'check with trailing whitespace (diff-index)' '
563 test_must_fail git diff-index --check HEAD
566 test_expect_success
'check with space before tab in indent (diff-index)' '
567 # indent has space followed by hard tab
570 test_must_fail git diff-index --check HEAD
573 test_expect_success
'check staged with no whitespace errors (diff-index)' '
576 git diff-index --cached --check HEAD
579 test_expect_success
'check staged with trailing whitespace (diff-index)' '
582 test_must_fail git diff-index --cached --check HEAD
585 test_expect_success
'check staged with space before tab in indent (diff-index)' '
586 # indent has space followed by hard tab
589 test_must_fail git diff-index --cached --check HEAD
592 test_expect_success
'check with no whitespace errors (diff-tree)' '
594 git commit -m "new commit" x &&
595 git diff-tree --check HEAD^ HEAD
598 test_expect_success
'check with trailing whitespace (diff-tree)' '
600 git commit -m "another commit" x &&
601 test_must_fail git diff-tree --check HEAD^ HEAD
604 test_expect_success
'check with space before tab in indent (diff-tree)' '
605 # indent has space followed by hard tab
607 git commit -m "yet another" x &&
608 test_must_fail git diff-tree --check HEAD^ HEAD
611 test_expect_success
'check with ignored trailing whitespace attr (diff-tree)' '
612 test_when_finished "git reset --hard HEAD^" &&
614 # create a whitespace error that should be ignored
615 echo "* -whitespace" >.gitattributes &&
616 git add .gitattributes &&
619 git commit -m "add trailing space" &&
621 # with a worktree diff-tree ignores the whitespace error
622 git diff-tree --root --check HEAD &&
624 # without a worktree diff-tree still ignores the whitespace error
625 git -C .git diff-tree --root --check HEAD
628 test_expect_success
'check trailing whitespace (trailing-space: off)' '
629 git config core.whitespace "-trailing-space" &&
630 echo "foo (); " >x &&
634 test_expect_success
'check trailing whitespace (trailing-space: on)' '
635 git config core.whitespace "trailing-space" &&
636 echo "foo (); " >x &&
637 test_must_fail git diff --check
640 test_expect_success
'check space before tab in indent (space-before-tab: off)' '
641 # indent contains space followed by HT
642 git config core.whitespace "-space-before-tab" &&
643 echo " foo ();" >x &&
647 test_expect_success
'check space before tab in indent (space-before-tab: on)' '
648 # indent contains space followed by HT
649 git config core.whitespace "space-before-tab" &&
650 echo " foo (); " >x &&
651 test_must_fail git diff --check
654 test_expect_success
'check spaces as indentation (indent-with-non-tab: off)' '
655 git config core.whitespace "-indent-with-non-tab" &&
656 echo " foo ();" >x &&
660 test_expect_success
'check spaces as indentation (indent-with-non-tab: on)' '
661 git config core.whitespace "indent-with-non-tab" &&
662 echo " foo ();" >x &&
663 test_must_fail git diff --check
666 test_expect_success
'ditto, but tabwidth=9' '
667 git config core.whitespace "indent-with-non-tab,tabwidth=9" &&
671 test_expect_success
'check tabs and 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=10' '
678 git config core.whitespace "indent-with-non-tab,tabwidth=10" &&
679 test_must_fail git diff --check
682 test_expect_success
'ditto, but tabwidth=20' '
683 git config core.whitespace "indent-with-non-tab,tabwidth=20" &&
687 test_expect_success
'check tabs as indentation (tab-in-indent: off)' '
688 git config core.whitespace "-tab-in-indent" &&
689 echo " foo ();" >x &&
693 test_expect_success
'check tabs as indentation (tab-in-indent: on)' '
694 git config core.whitespace "tab-in-indent" &&
695 echo " foo ();" >x &&
696 test_must_fail git diff --check
699 test_expect_success
'check tabs and spaces as indentation (tab-in-indent: on)' '
700 git config core.whitespace "tab-in-indent" &&
701 echo " foo ();" >x &&
702 test_must_fail git diff --check
705 test_expect_success
'ditto, but tabwidth=1 (must be irrelevant)' '
706 git config core.whitespace "tab-in-indent,tabwidth=1" &&
707 test_must_fail git diff --check
710 test_expect_success
'check tab-in-indent and indent-with-non-tab conflict' '
711 git config core.whitespace "tab-in-indent,indent-with-non-tab" &&
713 test_must_fail git diff --check
716 test_expect_success
'check tab-in-indent excluded from wildcard whitespace attribute' '
717 git config --unset core.whitespace &&
718 echo "x whitespace" >.gitattributes &&
719 echo " foo ();" >x &&
724 test_expect_success
'line numbers in --check output are correct' '
726 echo "foo(); " >>x &&
727 git diff --check | grep "x:2:"
730 test_expect_success
'checkdiff detects new trailing blank lines (1)' '
733 git diff --check | grep "new blank line"
736 test_expect_success
'checkdiff detects new trailing blank lines (2)' '
737 { echo a; echo b; echo; echo; } >x &&
739 { echo a; echo; echo; echo; echo; } >x &&
740 git diff --check | grep "new blank line"
743 test_expect_success
'checkdiff allows new blank lines' '
747 echo "/* This is new */" &&
756 test_expect_success
'whitespace-only changes not reported' '
758 echo >x "hello world" &&
760 git commit -m "hello 1" &&
761 echo >x "hello world" &&
762 git diff -b >actual &&
763 test_cmp expect actual
768 similarity index NUM%
771 index 380c32a..a97b785 100644
773 test_expect_success
'whitespace-only changes reported across renames' '
775 for i in 1 2 3 4 5 6 7 8 9; do echo "$i$i$i$i$i$i"; done >x &&
777 git commit -m "base" &&
778 sed -e "5s/^/ /" x >z &&
781 git diff -w -M --cached |
782 sed -e "/^similarity index /s/[0-9][0-9]*/NUM/" >actual &&
783 test_cmp expect actual
787 diff --git a
/empty b
/void
788 similarity index
100%
793 test_expect_success
'rename empty' '
797 git commit -m empty &&
799 git diff -w --cached -M >current &&
800 test_cmp expected current
803 test_expect_success
'combined diff with autocrlf conversion' '
807 git commit -m "one side" x &&
808 git checkout HEAD^ &&
810 git commit -m "the other side" x &&
811 git config core.autocrlf true &&
812 test_must_fail git merge master &&
814 git diff | sed -e "1,/^@@@/d" >actual &&
819 # Start testing the colored format for whitespace checks
821 test_expect_success
'setup diff colors' '
822 git config color.diff always &&
823 git config color.diff.plain normal &&
824 git config color.diff.meta bold &&
825 git config color.diff.frag cyan &&
826 git config color.diff.func normal &&
827 git config color.diff.old red &&
828 git config color.diff.new green &&
829 git config color.diff.commit yellow &&
830 git config color.diff.whitespace blue &&
832 git config core.autocrlf false
835 test_expect_success
'diff that introduces a line with only tabs' '
836 git config core.whitespace blank-at-eol &&
839 git commit -m "initial" x &&
840 echo "{NTN}" | tr "NT" "\n\t" >>x &&
841 git -c color.diff=always diff | test_decode_color >current &&
843 cat >expected <<-\EOF &&
844 <BOLD>diff --git a/x b/x<RESET>
845 <BOLD>index 9daeafb..2874b91 100644<RESET>
848 <CYAN>@@ -1 +1,4 @@<RESET>
850 <GREEN>+<RESET><GREEN>{<RESET>
851 <GREEN>+<RESET><BLUE> <RESET>
852 <GREEN>+<RESET><GREEN>}<RESET>
855 test_cmp expected current
858 test_expect_success
'diff that introduces and removes ws breakages' '
861 echo "0. blank-at-eol " &&
862 echo "1. blank-at-eol "
864 git commit -a --allow-empty -m preimage &&
866 echo "0. blank-at-eol " &&
867 echo "1. still-blank-at-eol " &&
868 echo "2. and a new line "
871 git -c color.diff=always diff |
872 test_decode_color >current &&
874 cat >expected <<-\EOF &&
875 <BOLD>diff --git a/x b/x<RESET>
876 <BOLD>index d0233a2..700886e 100644<RESET>
879 <CYAN>@@ -1,2 +1,3 @@<RESET>
880 0. blank-at-eol <RESET>
881 <RED>-1. blank-at-eol <RESET>
882 <GREEN>+<RESET><GREEN>1. still-blank-at-eol<RESET><BLUE> <RESET>
883 <GREEN>+<RESET><GREEN>2. and a new line<RESET><BLUE> <RESET>
886 test_cmp expected current
889 test_expect_success
'ws-error-highlight test setup' '
893 echo "0. blank-at-eol " &&
894 echo "1. blank-at-eol "
896 git commit -a --allow-empty -m preimage &&
898 echo "0. blank-at-eol " &&
899 echo "1. still-blank-at-eol " &&
900 echo "2. and a new line "
903 cat >expect.default-old <<-\EOF &&
904 <BOLD>diff --git a/x b/x<RESET>
905 <BOLD>index d0233a2..700886e 100644<RESET>
908 <CYAN>@@ -1,2 +1,3 @@<RESET>
909 0. blank-at-eol <RESET>
910 <RED>-<RESET><RED>1. blank-at-eol<RESET><BLUE> <RESET>
911 <GREEN>+<RESET><GREEN>1. still-blank-at-eol<RESET><BLUE> <RESET>
912 <GREEN>+<RESET><GREEN>2. and a new line<RESET><BLUE> <RESET>
915 cat >expect.all <<-\EOF &&
916 <BOLD>diff --git a/x b/x<RESET>
917 <BOLD>index d0233a2..700886e 100644<RESET>
920 <CYAN>@@ -1,2 +1,3 @@<RESET>
921 <RESET>0. blank-at-eol<RESET><BLUE> <RESET>
922 <RED>-<RESET><RED>1. blank-at-eol<RESET><BLUE> <RESET>
923 <GREEN>+<RESET><GREEN>1. still-blank-at-eol<RESET><BLUE> <RESET>
924 <GREEN>+<RESET><GREEN>2. and a new line<RESET><BLUE> <RESET>
927 cat >expect.none <<-\EOF
928 <BOLD>diff --git a/x b/x<RESET>
929 <BOLD>index d0233a2..700886e 100644<RESET>
932 <CYAN>@@ -1,2 +1,3 @@<RESET>
933 0. blank-at-eol <RESET>
934 <RED>-1. blank-at-eol <RESET>
935 <GREEN>+1. still-blank-at-eol <RESET>
936 <GREEN>+2. and a new line <RESET>
941 test_expect_success
'test --ws-error-highlight option' '
943 git -c color.diff=always diff --ws-error-highlight=default,old |
944 test_decode_color >current &&
945 test_cmp expect.default-old current &&
947 git -c color.diff=always diff --ws-error-highlight=all |
948 test_decode_color >current &&
949 test_cmp expect.all current &&
951 git -c color.diff=always diff --ws-error-highlight=none |
952 test_decode_color >current &&
953 test_cmp expect.none current
957 test_expect_success
'test diff.wsErrorHighlight config' '
959 git -c color.diff=always -c diff.wsErrorHighlight=default,old diff |
960 test_decode_color >current &&
961 test_cmp expect.default-old current &&
963 git -c color.diff=always -c diff.wsErrorHighlight=all diff |
964 test_decode_color >current &&
965 test_cmp expect.all current &&
967 git -c color.diff=always -c diff.wsErrorHighlight=none diff |
968 test_decode_color >current &&
969 test_cmp expect.none current
973 test_expect_success
'option overrides diff.wsErrorHighlight' '
975 git -c color.diff=always -c diff.wsErrorHighlight=none \
976 diff --ws-error-highlight=default,old |
977 test_decode_color >current &&
978 test_cmp expect.default-old current &&
980 git -c color.diff=always -c diff.wsErrorHighlight=default \
981 diff --ws-error-highlight=all |
982 test_decode_color >current &&
983 test_cmp expect.all current &&
985 git -c color.diff=always -c diff.wsErrorHighlight=all \
986 diff --ws-error-highlight=none |
987 test_decode_color >current &&
988 test_cmp expect.none current