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 \\
150 git diff --ignore-blank-lines >out &&
155 test_expect_success
'ignore-blank-lines: only new lines with space' '
157 git update-index x &&
158 test_seq 5 | sed "/3/i \ " >x &&
159 git diff -w --ignore-blank-lines >out &&
164 test_expect_success
'ignore-blank-lines: after change' '
176 git update-index x &&
189 git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp &&
190 cat <<-\EOF >expected &&
204 compare_diff_patch expected out.tmp
207 test_expect_success
'ignore-blank-lines: before change' '
218 git update-index x &&
231 git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp &&
232 cat <<-\EOF >expected &&
245 compare_diff_patch expected out.tmp
248 test_expect_success
'ignore-blank-lines: between changes' '
263 git update-index x &&
280 git diff --ignore-blank-lines >out.tmp &&
281 cat <<-\EOF >expected &&
302 compare_diff_patch expected out.tmp
305 test_expect_success
'ignore-blank-lines: between changes (with interhunkctx)' '
307 git update-index x &&
325 git diff --inter-hunk-context=2 --ignore-blank-lines >out.tmp &&
326 cat <<-\EOF >expected &&
347 compare_diff_patch expected out.tmp
350 test_expect_success
'ignore-blank-lines: scattered spaces' '
352 git update-index x &&
373 git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp &&
374 cat <<-\EOF >expected &&
389 compare_diff_patch expected out.tmp
392 test_expect_success
'ignore-blank-lines: spaces coalesce' '
394 git update-index x &&
408 git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp &&
409 cat <<-\EOF >expected &&
426 compare_diff_patch expected out.tmp
429 test_expect_success
'ignore-blank-lines: mix changes and blank lines' '
431 git update-index x &&
457 git diff --ignore-blank-lines >out.tmp &&
458 cat <<-\EOF >expected &&
487 compare_diff_patch expected out.tmp
490 test_expect_success
'check mixed spaces and tabs in indent' '
492 # This is indented with SP HT SP.
493 echo " foo();" > x &&
494 git diff --check | grep "space before tab in indent"
498 test_expect_success
'check mixed tabs and spaces in indent' '
500 # This is indented with HT SP HT.
501 echo " foo();" > x &&
502 git diff --check | grep "space before tab in indent"
506 test_expect_success
'check with no whitespace errors' '
508 git commit -m "snapshot" &&
514 test_expect_success
'check with trailing whitespace' '
516 echo "foo(); " > x &&
517 test_must_fail git diff --check
521 test_expect_success
'check with space before tab in indent' '
523 # indent has space followed by hard tab
524 echo " foo();" > x &&
525 test_must_fail git diff --check
529 test_expect_success
'--check and --exit-code are not exclusive' '
532 git diff --check --exit-code
536 test_expect_success
'--check and --quiet are not exclusive' '
538 git diff --check --quiet
542 test_expect_success
'check staged with no whitespace errors' '
546 git diff --cached --check
550 test_expect_success
'check staged with trailing whitespace' '
552 echo "foo(); " > x &&
554 test_must_fail git diff --cached --check
558 test_expect_success
'check staged with space before tab in indent' '
560 # indent has space followed by hard tab
561 echo " foo();" > x &&
563 test_must_fail git diff --cached --check
567 test_expect_success
'check with no whitespace errors (diff-index)' '
571 git diff-index --check HEAD
575 test_expect_success
'check with trailing whitespace (diff-index)' '
577 echo "foo(); " > x &&
579 test_must_fail git diff-index --check HEAD
583 test_expect_success
'check with space before tab in indent (diff-index)' '
585 # indent has space followed by hard tab
586 echo " foo();" > x &&
588 test_must_fail git diff-index --check HEAD
592 test_expect_success
'check staged with no whitespace errors (diff-index)' '
596 git diff-index --cached --check HEAD
600 test_expect_success
'check staged with trailing whitespace (diff-index)' '
602 echo "foo(); " > x &&
604 test_must_fail git diff-index --cached --check HEAD
608 test_expect_success
'check staged with space before tab in indent (diff-index)' '
610 # indent has space followed by hard tab
611 echo " foo();" > x &&
613 test_must_fail git diff-index --cached --check HEAD
617 test_expect_success
'check with no whitespace errors (diff-tree)' '
620 git commit -m "new commit" x &&
621 git diff-tree --check HEAD^ HEAD
625 test_expect_success
'check with trailing whitespace (diff-tree)' '
627 echo "foo(); " > x &&
628 git commit -m "another commit" x &&
629 test_must_fail git diff-tree --check HEAD^ HEAD
633 test_expect_success
'check with space before tab in indent (diff-tree)' '
635 # indent has space followed by hard tab
636 echo " foo();" > x &&
637 git commit -m "yet another" x &&
638 test_must_fail git diff-tree --check HEAD^ HEAD
642 test_expect_success
'check trailing whitespace (trailing-space: off)' '
644 git config core.whitespace "-trailing-space" &&
645 echo "foo (); " > x &&
650 test_expect_success
'check trailing whitespace (trailing-space: on)' '
652 git config core.whitespace "trailing-space" &&
653 echo "foo (); " > x &&
654 test_must_fail git diff --check
658 test_expect_success
'check space before tab in indent (space-before-tab: off)' '
660 # indent contains space followed by HT
661 git config core.whitespace "-space-before-tab" &&
662 echo " foo ();" > x &&
667 test_expect_success
'check space before tab in indent (space-before-tab: on)' '
669 # indent contains space followed by HT
670 git config core.whitespace "space-before-tab" &&
671 echo " foo (); " > x &&
672 test_must_fail git diff --check
676 test_expect_success
'check spaces as indentation (indent-with-non-tab: off)' '
678 git config core.whitespace "-indent-with-non-tab" &&
679 echo " foo ();" > x &&
684 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
692 test_expect_success
'ditto, but tabwidth=9' '
694 git config core.whitespace "indent-with-non-tab,tabwidth=9" &&
699 test_expect_success
'check tabs and spaces as indentation (indent-with-non-tab: on)' '
701 git config core.whitespace "indent-with-non-tab" &&
702 echo " foo ();" > x &&
703 test_must_fail git diff --check
707 test_expect_success
'ditto, but tabwidth=10' '
709 git config core.whitespace "indent-with-non-tab,tabwidth=10" &&
710 test_must_fail git diff --check
714 test_expect_success
'ditto, but tabwidth=20' '
716 git config core.whitespace "indent-with-non-tab,tabwidth=20" &&
721 test_expect_success
'check tabs as indentation (tab-in-indent: off)' '
723 git config core.whitespace "-tab-in-indent" &&
724 echo " foo ();" > x &&
729 test_expect_success
'check tabs as indentation (tab-in-indent: on)' '
731 git config core.whitespace "tab-in-indent" &&
732 echo " foo ();" > x &&
733 test_must_fail git diff --check
737 test_expect_success
'check tabs and spaces as indentation (tab-in-indent: on)' '
739 git config core.whitespace "tab-in-indent" &&
740 echo " foo ();" > x &&
741 test_must_fail git diff --check
745 test_expect_success
'ditto, but tabwidth=1 (must be irrelevant)' '
747 git config core.whitespace "tab-in-indent,tabwidth=1" &&
748 test_must_fail git diff --check
752 test_expect_success
'check tab-in-indent and indent-with-non-tab conflict' '
754 git config core.whitespace "tab-in-indent,indent-with-non-tab" &&
755 echo "foo ();" > x &&
756 test_must_fail git diff --check
760 test_expect_success
'check tab-in-indent excluded from wildcard whitespace attribute' '
762 git config --unset core.whitespace &&
763 echo "x whitespace" > .gitattributes &&
764 echo " foo ();" > x &&
770 test_expect_success
'line numbers in --check output are correct' '
773 echo "foo(); " >> x &&
774 git diff --check | grep "x:2:"
778 test_expect_success
'checkdiff detects new trailing blank lines (1)' '
781 git diff --check | grep "new blank line"
784 test_expect_success
'checkdiff detects new trailing blank lines (2)' '
785 { echo a; echo b; echo; echo; } >x &&
787 { echo a; echo; echo; echo; echo; } >x &&
788 git diff --check | grep "new blank line"
791 test_expect_success
'checkdiff allows new blank lines' '
795 echo "/* This is new */" &&
804 test_expect_success
'whitespace-only changes not reported' '
806 echo >x "hello world" &&
808 git commit -m "hello 1" &&
809 echo >x "hello world" &&
810 git diff -b >actual &&
811 test_cmp expect actual
816 similarity index NUM%
819 index 380c32a..a97b785 100644
821 test_expect_success
'whitespace-only changes reported across renames' '
823 for i in 1 2 3 4 5 6 7 8 9; do echo "$i$i$i$i$i$i"; done >x &&
825 git commit -m "base" &&
826 sed -e "5s/^/ /" x >z &&
829 git diff -w -M --cached |
830 sed -e "/^similarity index /s/[0-9][0-9]*/NUM/" >actual &&
831 test_cmp expect actual
835 diff --git a
/empty b
/void
836 similarity index
100%
841 test_expect_success
'rename empty' '
845 git commit -m empty &&
847 git diff -w --cached -M >current &&
848 test_cmp expected current
851 test_expect_success
'combined diff with autocrlf conversion' '
855 git commit -m "one side" x &&
856 git checkout HEAD^ &&
858 git commit -m "the other side" x &&
859 git config core.autocrlf true &&
860 test_must_fail git merge master &&
862 git diff | sed -e "1,/^@@@/d" >actual &&
867 # Start testing the colored format for whitespace checks
869 test_expect_success
'setup diff colors' '
870 git config color.diff always &&
871 git config color.diff.plain normal &&
872 git config color.diff.meta bold &&
873 git config color.diff.frag cyan &&
874 git config color.diff.func normal &&
875 git config color.diff.old red &&
876 git config color.diff.new green &&
877 git config color.diff.commit yellow &&
878 git config color.diff.whitespace "normal red" &&
880 git config core.autocrlf false
883 <BOLD
>diff --git a
/x b
/x
<RESET
>
884 <BOLD
>index
9daeafb.
.2874b91
100644<RESET
>
887 <CYAN
>@@
-1 +1,4 @@
<RESET
>
889 <GREEN
>+<RESET
><GREEN
>{<RESET
>
890 <GREEN
>+<RESET
><BRED
> <RESET
>
891 <GREEN
>+<RESET
><GREEN
>}<RESET
>
894 test_expect_success
'diff that introduces a line with only tabs' '
895 git config core.whitespace blank-at-eol &&
898 git commit -m "initial" x &&
899 echo "{NTN}" | tr "NT" "\n\t" >> x &&
900 git -c color.diff=always diff | test_decode_color >current &&
901 test_cmp expected current