3 # Copyright (c) 2006 Johannes E. Schindelin
6 test_description
='Test special whitespace in diff engine.
10 .
"$TEST_DIRECTORY"/diff-lib.sh
12 # Ray Lehtiniemi's example
20 git update-index
--add x
32 index adf3937..6edc172 100644
46 test_expect_success
"Ray's example without options" 'test_cmp expect out'
49 test_expect_success
"Ray's example with -w" 'test_cmp expect out'
52 test_expect_success
"Ray's example with -b" 'test_cmp expect out'
54 tr 'Q' '\015' << EOF > x
55 whitespace at beginning
57 whitespace in the middle
66 whitespace at beginning
68 white space in the middle
74 tr 'Q_' '\015 ' << EOF > expect
76 index d99af23..8b32fb5 100644
80 -whitespace at beginning
82 -whitespace in the middle
84 + whitespace at beginning
86 +white space in the middle
93 test_expect_success
'another test, without options' 'test_cmp expect out'
98 test_expect_success
'another test, with -w' 'test_cmp expect out'
100 test_expect_success
'another test, with -w -b' 'test_cmp expect out'
101 git
diff -w --ignore-space-at-eol > out
102 test_expect_success
'another test, with -w --ignore-space-at-eol' 'test_cmp expect out'
103 git
diff -w -b --ignore-space-at-eol > out
104 test_expect_success
'another test, with -w -b --ignore-space-at-eol' 'test_cmp expect out'
106 tr 'Q_' '\015 ' << EOF > expect
108 index d99af23..8b32fb5 100644
112 -whitespace at beginning
113 + whitespace at beginning
115 -whitespace in the middle
116 +white space in the middle
122 test_expect_success
'another test, with -b' 'test_cmp expect out'
123 git
diff -b --ignore-space-at-eol > out
124 test_expect_success
'another test, with -b --ignore-space-at-eol' 'test_cmp expect out'
126 tr 'Q_' '\015 ' << EOF > expect
128 index d99af23..8b32fb5 100644
132 -whitespace at beginning
134 -whitespace in the middle
135 + whitespace at beginning
137 +white space in the middle
142 git
diff --ignore-space-at-eol > out
143 test_expect_success
'another test, with --ignore-space-at-eol' 'test_cmp expect out'
145 test_expect_success
'ignore-blank-lines: only new lines' '
147 git update-index x &&
148 test_seq 5 | sed "/3/i\\
151 git diff --ignore-blank-lines >out &&
156 test_expect_success
'ignore-blank-lines: only new lines with space' '
158 git update-index x &&
159 test_seq 5 | sed "/3/i\\
161 git diff -w --ignore-blank-lines >out &&
166 test_expect_success
'ignore-blank-lines: after change' '
178 git update-index x &&
191 git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp &&
192 cat <<-\EOF >expected &&
206 compare_diff_patch expected out.tmp
209 test_expect_success
'ignore-blank-lines: before change' '
220 git update-index x &&
233 git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp &&
234 cat <<-\EOF >expected &&
247 compare_diff_patch expected out.tmp
250 test_expect_success
'ignore-blank-lines: between changes' '
265 git update-index x &&
282 git diff --ignore-blank-lines >out.tmp &&
283 cat <<-\EOF >expected &&
304 compare_diff_patch expected out.tmp
307 test_expect_success
'ignore-blank-lines: between changes (with interhunkctx)' '
309 git update-index x &&
327 git diff --inter-hunk-context=2 --ignore-blank-lines >out.tmp &&
328 cat <<-\EOF >expected &&
349 compare_diff_patch expected out.tmp
352 test_expect_success
'ignore-blank-lines: scattered spaces' '
354 git update-index x &&
375 git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp &&
376 cat <<-\EOF >expected &&
391 compare_diff_patch expected out.tmp
394 test_expect_success
'ignore-blank-lines: spaces coalesce' '
396 git update-index x &&
410 git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp &&
411 cat <<-\EOF >expected &&
428 compare_diff_patch expected out.tmp
431 test_expect_success
'ignore-blank-lines: mix changes and blank lines' '
433 git update-index x &&
459 git diff --ignore-blank-lines >out.tmp &&
460 cat <<-\EOF >expected &&
489 compare_diff_patch expected out.tmp
492 test_expect_success
'check mixed spaces and tabs in indent' '
494 # This is indented with SP HT SP.
495 echo " foo();" > x &&
496 git diff --check | grep "space before tab in indent"
500 test_expect_success
'check mixed tabs and spaces in indent' '
502 # This is indented with HT SP HT.
503 echo " foo();" > x &&
504 git diff --check | grep "space before tab in indent"
508 test_expect_success
'check with no whitespace errors' '
510 git commit -m "snapshot" &&
516 test_expect_success
'check with trailing whitespace' '
518 echo "foo(); " > x &&
519 test_must_fail git diff --check
523 test_expect_success
'check with space before tab in indent' '
525 # indent has space followed by hard tab
526 echo " foo();" > x &&
527 test_must_fail git diff --check
531 test_expect_success
'--check and --exit-code are not exclusive' '
534 git diff --check --exit-code
538 test_expect_success
'--check and --quiet are not exclusive' '
540 git diff --check --quiet
544 test_expect_success
'check staged with no whitespace errors' '
548 git diff --cached --check
552 test_expect_success
'check staged with trailing whitespace' '
554 echo "foo(); " > x &&
556 test_must_fail git diff --cached --check
560 test_expect_success
'check staged with space before tab in indent' '
562 # indent has space followed by hard tab
563 echo " foo();" > x &&
565 test_must_fail git diff --cached --check
569 test_expect_success
'check with no whitespace errors (diff-index)' '
573 git diff-index --check HEAD
577 test_expect_success
'check with trailing whitespace (diff-index)' '
579 echo "foo(); " > x &&
581 test_must_fail git diff-index --check HEAD
585 test_expect_success
'check with space before tab in indent (diff-index)' '
587 # indent has space followed by hard tab
588 echo " foo();" > x &&
590 test_must_fail git diff-index --check HEAD
594 test_expect_success
'check staged with no whitespace errors (diff-index)' '
598 git diff-index --cached --check HEAD
602 test_expect_success
'check staged with trailing whitespace (diff-index)' '
604 echo "foo(); " > x &&
606 test_must_fail git diff-index --cached --check HEAD
610 test_expect_success
'check staged with space before tab in indent (diff-index)' '
612 # indent has space followed by hard tab
613 echo " foo();" > x &&
615 test_must_fail git diff-index --cached --check HEAD
619 test_expect_success
'check with no whitespace errors (diff-tree)' '
622 git commit -m "new commit" x &&
623 git diff-tree --check HEAD^ HEAD
627 test_expect_success
'check with trailing whitespace (diff-tree)' '
629 echo "foo(); " > x &&
630 git commit -m "another commit" x &&
631 test_must_fail git diff-tree --check HEAD^ HEAD
635 test_expect_success
'check with space before tab in indent (diff-tree)' '
637 # indent has space followed by hard tab
638 echo " foo();" > x &&
639 git commit -m "yet another" x &&
640 test_must_fail git diff-tree --check HEAD^ HEAD
644 test_expect_success
'check trailing whitespace (trailing-space: off)' '
646 git config core.whitespace "-trailing-space" &&
647 echo "foo (); " > x &&
652 test_expect_success
'check trailing whitespace (trailing-space: on)' '
654 git config core.whitespace "trailing-space" &&
655 echo "foo (); " > x &&
656 test_must_fail git diff --check
660 test_expect_success
'check space before tab in indent (space-before-tab: off)' '
662 # indent contains space followed by HT
663 git config core.whitespace "-space-before-tab" &&
664 echo " foo ();" > x &&
669 test_expect_success
'check space before tab in indent (space-before-tab: on)' '
671 # indent contains space followed by HT
672 git config core.whitespace "space-before-tab" &&
673 echo " foo (); " > x &&
674 test_must_fail git diff --check
678 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 &&
686 test_expect_success
'check spaces as indentation (indent-with-non-tab: on)' '
688 git config core.whitespace "indent-with-non-tab" &&
689 echo " foo ();" > x &&
690 test_must_fail git diff --check
694 test_expect_success
'ditto, but tabwidth=9' '
696 git config core.whitespace "indent-with-non-tab,tabwidth=9" &&
701 test_expect_success
'check tabs and spaces as indentation (indent-with-non-tab: on)' '
703 git config core.whitespace "indent-with-non-tab" &&
704 echo " foo ();" > x &&
705 test_must_fail git diff --check
709 test_expect_success
'ditto, but tabwidth=10' '
711 git config core.whitespace "indent-with-non-tab,tabwidth=10" &&
712 test_must_fail git diff --check
716 test_expect_success
'ditto, but tabwidth=20' '
718 git config core.whitespace "indent-with-non-tab,tabwidth=20" &&
723 test_expect_success
'check tabs as indentation (tab-in-indent: off)' '
725 git config core.whitespace "-tab-in-indent" &&
726 echo " foo ();" > x &&
731 test_expect_success
'check tabs as indentation (tab-in-indent: on)' '
733 git config core.whitespace "tab-in-indent" &&
734 echo " foo ();" > x &&
735 test_must_fail git diff --check
739 test_expect_success
'check tabs and spaces as indentation (tab-in-indent: on)' '
741 git config core.whitespace "tab-in-indent" &&
742 echo " foo ();" > x &&
743 test_must_fail git diff --check
747 test_expect_success
'ditto, but tabwidth=1 (must be irrelevant)' '
749 git config core.whitespace "tab-in-indent,tabwidth=1" &&
750 test_must_fail git diff --check
754 test_expect_success
'check tab-in-indent and indent-with-non-tab conflict' '
756 git config core.whitespace "tab-in-indent,indent-with-non-tab" &&
757 echo "foo ();" > x &&
758 test_must_fail git diff --check
762 test_expect_success
'check tab-in-indent excluded from wildcard whitespace attribute' '
764 git config --unset core.whitespace &&
765 echo "x whitespace" > .gitattributes &&
766 echo " foo ();" > x &&
772 test_expect_success
'line numbers in --check output are correct' '
775 echo "foo(); " >> x &&
776 git diff --check | grep "x:2:"
780 test_expect_success
'checkdiff detects new trailing blank lines (1)' '
783 git diff --check | grep "new blank line"
786 test_expect_success
'checkdiff detects new trailing blank lines (2)' '
787 { echo a; echo b; echo; echo; } >x &&
789 { echo a; echo; echo; echo; echo; } >x &&
790 git diff --check | grep "new blank line"
793 test_expect_success
'checkdiff allows new blank lines' '
797 echo "/* This is new */" &&
806 test_expect_success
'whitespace-only changes not reported' '
808 echo >x "hello world" &&
810 git commit -m "hello 1" &&
811 echo >x "hello world" &&
812 git diff -b >actual &&
813 test_cmp expect actual
818 similarity index NUM%
821 index 380c32a..a97b785 100644
823 test_expect_success
'whitespace-only changes reported across renames' '
825 for i in 1 2 3 4 5 6 7 8 9; do echo "$i$i$i$i$i$i"; done >x &&
827 git commit -m "base" &&
828 sed -e "5s/^/ /" x >z &&
831 git diff -w -M --cached |
832 sed -e "/^similarity index /s/[0-9][0-9]*/NUM/" >actual &&
833 test_cmp expect actual
837 diff --git a
/empty b
/void
838 similarity index
100%
843 test_expect_success
'rename empty' '
847 git commit -m empty &&
849 git diff -w --cached -M >current &&
850 test_cmp expected current
853 test_expect_success
'combined diff with autocrlf conversion' '
857 git commit -m "one side" x &&
858 git checkout HEAD^ &&
860 git commit -m "the other side" x &&
861 git config core.autocrlf true &&
862 test_must_fail git merge master &&
864 git diff | sed -e "1,/^@@@/d" >actual &&
869 # Start testing the colored format for whitespace checks
871 test_expect_success
'setup diff colors' '
872 git config color.diff always &&
873 git config color.diff.plain normal &&
874 git config color.diff.meta bold &&
875 git config color.diff.frag cyan &&
876 git config color.diff.func normal &&
877 git config color.diff.old red &&
878 git config color.diff.new green &&
879 git config color.diff.commit yellow &&
880 git config color.diff.whitespace "normal red" &&
882 git config core.autocrlf false
885 <BOLD
>diff --git a
/x b
/x
<RESET
>
886 <BOLD
>index
9daeafb.
.2874b91
100644<RESET
>
889 <CYAN
>@@
-1 +1,4 @@
<RESET
>
891 <GREEN
>+<RESET
><GREEN
>{<RESET
>
892 <GREEN
>+<RESET
><BRED
> <RESET
>
893 <GREEN
>+<RESET
><GREEN
>}<RESET
>
896 test_expect_success
'diff that introduces a line with only tabs' '
897 git config core.whitespace blank-at-eol &&
900 git commit -m "initial" x &&
901 echo "{NTN}" | tr "NT" "\n\t" >> x &&
902 git -c color.diff=always diff | test_decode_color >current &&
903 test_cmp expected current