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\\
160 git diff -w --ignore-blank-lines >out &&
165 test_expect_success 'ignore-blank-lines: after change' '
177 git update-index x &&
190 git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp &&
191 cat <<-\EOF >expected &&
205 compare_diff_patch expected out.tmp
208 test_expect_success 'ignore-blank-lines: before change' '
219 git update-index x &&
232 git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp &&
233 cat <<-\EOF >expected &&
246 compare_diff_patch expected out.tmp
249 test_expect_success 'ignore-blank-lines: between changes' '
264 git update-index x &&
281 git diff --ignore-blank-lines >out.tmp &&
282 cat <<-\EOF >expected &&
303 compare_diff_patch expected out.tmp
306 test_expect_success 'ignore-blank-lines: between changes (with interhunkctx)' '
308 git update-index x &&
326 git diff --inter-hunk-context=2 --ignore-blank-lines >out.tmp &&
327 cat <<-\EOF >expected &&
348 compare_diff_patch expected out.tmp
351 test_expect_success 'ignore-blank-lines: scattered spaces' '
353 git update-index x &&
374 git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp &&
375 cat <<-\EOF >expected &&
390 compare_diff_patch expected out.tmp
393 test_expect_success 'ignore-blank-lines: spaces coalesce' '
395 git update-index x &&
409 git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp &&
410 cat <<-\EOF >expected &&
427 compare_diff_patch expected out.tmp
430 test_expect_success 'ignore-blank-lines: mix changes and blank lines' '
432 git update-index x &&
458 git diff --ignore-blank-lines >out.tmp &&
459 cat <<-\EOF >expected &&
488 compare_diff_patch expected out.tmp
491 test_expect_success 'check mixed spaces and tabs in indent' '
493 # This is indented with SP HT SP.
494 echo " foo();" > x &&
495 git diff --check | grep "space before tab in indent"
499 test_expect_success 'check mixed tabs and spaces in indent' '
501 # This is indented with HT SP HT.
502 echo " foo();" > x &&
503 git diff --check | grep "space before tab in indent"
507 test_expect_success 'check with no whitespace errors' '
509 git commit -m "snapshot" &&
515 test_expect_success 'check with trailing whitespace' '
517 echo "foo(); " > x &&
518 test_must_fail git diff --check
522 test_expect_success 'check with space before tab in indent' '
524 # indent has space followed by hard tab
525 echo " foo();" > x &&
526 test_must_fail git diff --check
530 test_expect_success '--check and --exit-code are not exclusive' '
533 git diff --check --exit-code
537 test_expect_success '--check and --quiet are not exclusive' '
539 git diff --check --quiet
543 test_expect_success 'check staged with no whitespace errors' '
547 git diff --cached --check
551 test_expect_success 'check staged with trailing whitespace' '
553 echo "foo(); " > x &&
555 test_must_fail git diff --cached --check
559 test_expect_success 'check staged with space before tab in indent' '
561 # indent has space followed by hard tab
562 echo " foo();" > x &&
564 test_must_fail git diff --cached --check
568 test_expect_success 'check with no whitespace errors (diff-index)' '
572 git diff-index --check HEAD
576 test_expect_success 'check with trailing whitespace (diff-index)' '
578 echo "foo(); " > x &&
580 test_must_fail git diff-index --check HEAD
584 test_expect_success 'check with space before tab in indent (diff-index)' '
586 # indent has space followed by hard tab
587 echo " foo();" > x &&
589 test_must_fail git diff-index --check HEAD
593 test_expect_success 'check staged with no whitespace errors (diff-index)' '
597 git diff-index --cached --check HEAD
601 test_expect_success 'check staged with trailing whitespace (diff-index)' '
603 echo "foo(); " > x &&
605 test_must_fail git diff-index --cached --check HEAD
609 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 &&
614 test_must_fail git diff-index --cached --check HEAD
618 test_expect_success 'check with no whitespace errors (diff-tree)' '
621 git commit -m "new commit" x &&
622 git diff-tree --check HEAD^ HEAD
626 test_expect_success 'check with trailing whitespace (diff-tree)' '
628 echo "foo(); " > x &&
629 git commit -m "another commit" x &&
630 test_must_fail git diff-tree --check HEAD^ HEAD
634 test_expect_success 'check with space before tab in indent (diff-tree)' '
636 # indent has space followed by hard tab
637 echo " foo();" > x &&
638 git commit -m "yet another" x &&
639 test_must_fail git diff-tree --check HEAD^ HEAD
643 test_expect_success 'check trailing whitespace (trailing-space: off)' '
645 git config core.whitespace "-trailing-space" &&
646 echo "foo (); " > x &&
651 test_expect_success 'check trailing whitespace (trailing-space: on)' '
653 git config core.whitespace "trailing-space" &&
654 echo "foo (); " > x &&
655 test_must_fail git diff --check
659 test_expect_success 'check space before tab in indent (space-before-tab: off)' '
661 # indent contains space followed by HT
662 git config core.whitespace "-space-before-tab" &&
663 echo " foo ();" > x &&
668 test_expect_success 'check space before tab in indent (space-before-tab: on)' '
670 # indent contains space followed by HT
671 git config core.whitespace "space-before-tab" &&
672 echo " foo (); " > x &&
673 test_must_fail git diff --check
677 test_expect_success 'check spaces as indentation (indent-with-non-tab: off)' '
679 git config core.whitespace "-indent-with-non-tab" &&
680 echo " foo ();" > x &&
685 test_expect_success 'check spaces as indentation (indent-with-non-tab: on)' '
687 git config core.whitespace "indent-with-non-tab" &&
688 echo " foo ();" > x &&
689 test_must_fail git diff --check
693 test_expect_success 'ditto, but tabwidth=9' '
695 git config core.whitespace "indent-with-non-tab,tabwidth=9" &&
700 test_expect_success 'check tabs and spaces as indentation (indent-with-non-tab: on)' '
702 git config core.whitespace "indent-with-non-tab" &&
703 echo " foo ();" > x &&
704 test_must_fail git diff --check
708 test_expect_success 'ditto, but tabwidth=10' '
710 git config core.whitespace "indent-with-non-tab,tabwidth=10" &&
711 test_must_fail git diff --check
715 test_expect_success 'ditto, but tabwidth=20' '
717 git config core.whitespace "indent-with-non-tab,tabwidth=20" &&
722 test_expect_success 'check tabs as indentation (tab-in-indent: off)' '
724 git config core.whitespace "-tab-in-indent" &&
725 echo " foo ();" > x &&
730 test_expect_success 'check tabs as indentation (tab-in-indent: on)' '
732 git config core.whitespace "tab-in-indent" &&
733 echo " foo ();" > x &&
734 test_must_fail git diff --check
738 test_expect_success 'check tabs and spaces as indentation (tab-in-indent: on)' '
740 git config core.whitespace "tab-in-indent" &&
741 echo " foo ();" > x &&
742 test_must_fail git diff --check
746 test_expect_success 'ditto, but tabwidth=1 (must be irrelevant)' '
748 git config core.whitespace "tab-in-indent,tabwidth=1" &&
749 test_must_fail git diff --check
753 test_expect_success 'check tab-in-indent and indent-with-non-tab conflict' '
755 git config core.whitespace "tab-in-indent,indent-with-non-tab" &&
756 echo "foo ();" > x &&
757 test_must_fail git diff --check
761 test_expect_success 'check tab-in-indent excluded from wildcard whitespace attribute' '
763 git config --unset core.whitespace &&
764 echo "x whitespace" > .gitattributes &&
765 echo " foo ();" > x &&
771 test_expect_success 'line numbers in --check output are correct' '
774 echo "foo(); " >> x &&
775 git diff --check | grep "x:2:"
779 test_expect_success 'checkdiff detects new trailing blank lines (1)' '
782 git diff --check | grep "new blank line"
785 test_expect_success 'checkdiff detects new trailing blank lines (2)' '
786 { echo a; echo b; echo; echo; } >x &&
788 { echo a; echo; echo; echo; echo; } >x &&
789 git diff --check | grep "new blank line"
792 test_expect_success 'checkdiff allows new blank lines' '
796 echo "/* This is new */" &&
805 test_expect_success 'whitespace-only changes not reported' '
807 echo >x "hello world" &&
809 git commit -m "hello 1" &&
810 echo >x "hello world" &&
811 git diff -b >actual &&
812 test_cmp expect actual
817 similarity index NUM%
820 index 380c32a..a97b785 100644
822 test_expect_success 'whitespace-only changes reported across renames' '
824 for i in 1 2 3 4 5 6 7 8 9; do echo "$i$i$i$i$i$i"; done >x &&
826 git commit -m "base" &&
827 sed -e "5s/^/ /" x >z &&
830 git diff -w -M --cached |
831 sed -e "/^similarity index /s/[0-9][0-9]*/NUM/" >actual &&
832 test_cmp expect actual
836 diff --git a/empty b/void
837 similarity index 100%
842 test_expect_success 'rename empty' '
846 git commit -m empty &&
848 git diff -w --cached -M >current &&
849 test_cmp expected current
852 test_expect_success 'combined diff with autocrlf conversion' '
856 git commit -m "one side" x &&
857 git checkout HEAD^ &&
859 git commit -m "the other side" x &&
860 git config core.autocrlf true &&
861 test_must_fail git merge master &&
863 git diff | sed -e "1,/^@@@/d" >actual &&
868 # Start testing the colored format for whitespace checks
870 test_expect_success 'setup diff colors' '
871 git config color.diff always &&
872 git config color.diff.plain normal &&
873 git config color.diff.meta bold &&
874 git config color.diff.frag cyan &&
875 git config color.diff.func normal &&
876 git config color.diff.old red &&
877 git config color.diff.new green &&
878 git config color.diff.commit yellow &&
879 git config color.diff.whitespace "normal red" &&
881 git config core.autocrlf false
884 <BOLD>diff --git a/x b/x<RESET>
885 <BOLD>index 9daeafb..2874b91 100644<RESET>
888 <CYAN>@@ -1 +1,4 @@<RESET>
890 <GREEN>+<RESET><GREEN>{<RESET>
891 <GREEN>+<RESET><BRED> <RESET>
892 <GREEN>+<RESET><GREEN>}<RESET>
895 test_expect_success 'diff that introduces a line with only tabs' '
896 git config core.whitespace blank-at-eol &&
899 git commit -m "initial" x &&
900 echo "{NTN}" | tr "NT" "\n\t" >> x &&
901 git -c color.diff=always diff | test_decode_color >current &&
902 test_cmp expected current