Merge branch 'as/pretty-truncate' into maint
[git.git] / t / t4015-diff-whitespace.sh
blob604a838c1ab4d173f9910158935d74398fabf788
1 #!/bin/sh
3 # Copyright (c) 2006 Johannes E. Schindelin
6 test_description='Test special whitespace in diff engine.
9 . ./test-lib.sh
10 . "$TEST_DIRECTORY"/diff-lib.sh
12 # Ray Lehtiniemi's example
14 cat << EOF > x
15 do {
16 nothing;
17 } while (0);
18 EOF
20 git update-index --add x
22 cat << EOF > x
25 nothing;
27 while (0);
28 EOF
30 cat << EOF > expect
31 diff --git a/x b/x
32 index adf3937..6edc172 100644
33 --- a/x
34 +++ b/x
35 @@ -1,3 +1,5 @@
36 -do {
37 +do
39 nothing;
40 -} while (0);
42 +while (0);
43 EOF
45 git diff > out
46 test_expect_success "Ray's example without options" 'test_cmp expect out'
48 git diff -w > out
49 test_expect_success "Ray's example with -w" 'test_cmp expect out'
51 git diff -b > out
52 test_expect_success "Ray's example with -b" 'test_cmp expect out'
54 tr 'Q' '\015' << EOF > x
55 whitespace at beginning
56 whitespace change
57 whitespace in the middle
58 whitespace at end
59 unchanged line
60 CR at endQ
61 EOF
63 git update-index x
65 tr '_' ' ' << EOF > x
66 whitespace at beginning
67 whitespace change
68 white space in the middle
69 whitespace at end__
70 unchanged line
71 CR at end
72 EOF
74 tr 'Q_' '\015 ' << EOF > expect
75 diff --git a/x b/x
76 index d99af23..8b32fb5 100644
77 --- a/x
78 +++ b/x
79 @@ -1,6 +1,6 @@
80 -whitespace at beginning
81 -whitespace change
82 -whitespace in the middle
83 -whitespace at end
84 + whitespace at beginning
85 +whitespace change
86 +white space in the middle
87 +whitespace at end__
88 unchanged line
89 -CR at endQ
90 +CR at end
91 EOF
92 git diff > out
93 test_expect_success 'another test, without options' 'test_cmp expect out'
95 cat << EOF > expect
96 EOF
97 git diff -w > out
98 test_expect_success 'another test, with -w' 'test_cmp expect out'
99 git diff -w -b > 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
107 diff --git a/x b/x
108 index d99af23..8b32fb5 100644
109 --- a/x
110 +++ b/x
111 @@ -1,6 +1,6 @@
112 -whitespace at beginning
113 + whitespace at beginning
114 whitespace change
115 -whitespace in the middle
116 +white space in the middle
117 whitespace at end__
118 unchanged line
119 CR at end
121 git diff -b > out
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
127 diff --git a/x b/x
128 index d99af23..8b32fb5 100644
129 --- a/x
130 +++ b/x
131 @@ -1,6 +1,6 @@
132 -whitespace at beginning
133 -whitespace change
134 -whitespace in the middle
135 + whitespace at beginning
136 +whitespace change
137 +white space in the middle
138 whitespace at end__
139 unchanged line
140 CR at end
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' '
146 test_seq 5 >x &&
147 git update-index x &&
148 test_seq 5 | sed "/3/i\\
149 " >x &&
150 git diff --ignore-blank-lines >out &&
151 >expect &&
152 test_cmp out expect
155 test_expect_success 'ignore-blank-lines: only new lines with space' '
156 test_seq 5 >x &&
157 git update-index x &&
158 test_seq 5 | sed "/3/i\\
159 " >x &&
160 git diff -w --ignore-blank-lines >out &&
161 >expect &&
162 test_cmp out expect
165 test_expect_success 'ignore-blank-lines: after change' '
166 cat <<-\EOF >x &&
177 git update-index x &&
178 cat <<-\EOF >x &&
179 change
190 git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp &&
191 cat <<-\EOF >expected &&
192 diff --git a/x b/x
193 --- a/x
194 +++ b/x
195 @@ -1,6 +1,7 @@
196 +change
205 compare_diff_patch expected out.tmp
208 test_expect_success 'ignore-blank-lines: before change' '
209 cat <<-\EOF >x &&
219 git update-index x &&
220 cat <<-\EOF >x &&
230 change
232 git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp &&
233 cat <<-\EOF >expected &&
234 diff --git a/x b/x
235 --- a/x
236 +++ b/x
237 @@ -4,5 +4,7 @@
244 +change
246 compare_diff_patch expected out.tmp
249 test_expect_success 'ignore-blank-lines: between changes' '
250 cat <<-\EOF >x &&
264 git update-index x &&
265 cat <<-\EOF >x &&
266 change
279 change
281 git diff --ignore-blank-lines >out.tmp &&
282 cat <<-\EOF >expected &&
283 diff --git a/x b/x
284 --- a/x
285 +++ b/x
286 @@ -1,5 +1,7 @@
287 +change
294 @@ -8,5 +8,7 @@
301 +change
303 compare_diff_patch expected out.tmp
306 test_expect_success 'ignore-blank-lines: between changes (with interhunkctx)' '
307 test_seq 10 >x &&
308 git update-index x &&
309 cat <<-\EOF >x &&
310 change
324 change
326 git diff --inter-hunk-context=2 --ignore-blank-lines >out.tmp &&
327 cat <<-\EOF >expected &&
328 diff --git a/x b/x
329 --- a/x
330 +++ b/x
331 @@ -1,10 +1,15 @@
332 +change
346 +change
348 compare_diff_patch expected out.tmp
351 test_expect_success 'ignore-blank-lines: scattered spaces' '
352 test_seq 10 >x &&
353 git update-index x &&
354 cat <<-\EOF >x &&
355 change
372 change
374 git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp &&
375 cat <<-\EOF >expected &&
376 diff --git a/x b/x
377 --- a/x
378 +++ b/x
379 @@ -1,3 +1,4 @@
380 +change
384 @@ -8,3 +15,4 @@
388 +change
390 compare_diff_patch expected out.tmp
393 test_expect_success 'ignore-blank-lines: spaces coalesce' '
394 test_seq 6 >x &&
395 git update-index x &&
396 cat <<-\EOF >x &&
397 change
407 change
409 git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp &&
410 cat <<-\EOF >expected &&
411 diff --git a/x b/x
412 --- a/x
413 +++ b/x
414 @@ -1,6 +1,11 @@
415 +change
425 +change
427 compare_diff_patch expected out.tmp
430 test_expect_success 'ignore-blank-lines: mix changes and blank lines' '
431 test_seq 16 >x &&
432 git update-index x &&
433 cat <<-\EOF >x &&
434 change
441 change
449 change
456 change
458 git diff --ignore-blank-lines >out.tmp &&
459 cat <<-\EOF >expected &&
460 diff --git a/x b/x
461 --- a/x
462 +++ b/x
463 @@ -1,8 +1,11 @@
464 +change
471 +change
475 @@ -9,8 +13,11 @@
479 +change
486 +change
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" &&
510 echo "foo();" > x &&
511 git diff --check
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' '
532 git checkout x &&
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' '
545 echo "foo();" > x &&
546 git add x &&
547 git diff --cached --check
551 test_expect_success 'check staged with trailing whitespace' '
553 echo "foo(); " > x &&
554 git add 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 &&
563 git add x &&
564 test_must_fail git diff --cached --check
568 test_expect_success 'check with no whitespace errors (diff-index)' '
570 echo "foo();" > x &&
571 git add x &&
572 git diff-index --check HEAD
576 test_expect_success 'check with trailing whitespace (diff-index)' '
578 echo "foo(); " > x &&
579 git add 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 &&
588 git add x &&
589 test_must_fail git diff-index --check HEAD
593 test_expect_success 'check staged with no whitespace errors (diff-index)' '
595 echo "foo();" > x &&
596 git add x &&
597 git diff-index --cached --check HEAD
601 test_expect_success 'check staged with trailing whitespace (diff-index)' '
603 echo "foo(); " > x &&
604 git add 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 &&
613 git add x &&
614 test_must_fail git diff-index --cached --check HEAD
618 test_expect_success 'check with no whitespace errors (diff-tree)' '
620 echo "foo();" > x &&
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 &&
647 git diff --check
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 &&
664 git diff --check
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 &&
681 git diff --check
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" &&
696 git diff --check
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" &&
718 git diff --check
722 test_expect_success 'check tabs as indentation (tab-in-indent: off)' '
724 git config core.whitespace "-tab-in-indent" &&
725 echo " foo ();" > x &&
726 git diff --check
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 &&
766 git diff --check &&
767 rm -f .gitattributes
771 test_expect_success 'line numbers in --check output are correct' '
773 echo "" > x &&
774 echo "foo(); " >> x &&
775 git diff --check | grep "x:2:"
779 test_expect_success 'checkdiff detects new trailing blank lines (1)' '
780 echo "foo();" >x &&
781 echo "" >>x &&
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 &&
787 git add 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' '
793 git checkout x &&
794 mv x y &&
796 echo "/* This is new */" &&
797 echo "" &&
798 cat y
799 ) >x &&
800 git diff --check
803 cat <<EOF >expect
805 test_expect_success 'whitespace-only changes not reported' '
806 git reset --hard &&
807 echo >x "hello world" &&
808 git add x &&
809 git commit -m "hello 1" &&
810 echo >x "hello world" &&
811 git diff -b >actual &&
812 test_cmp expect actual
815 cat <<EOF >expect
816 diff --git a/x b/z
817 similarity index NUM%
818 rename from x
819 rename to z
820 index 380c32a..a97b785 100644
822 test_expect_success 'whitespace-only changes reported across renames' '
823 git reset --hard &&
824 for i in 1 2 3 4 5 6 7 8 9; do echo "$i$i$i$i$i$i"; done >x &&
825 git add x &&
826 git commit -m "base" &&
827 sed -e "5s/^/ /" x >z &&
828 git rm x &&
829 git add z &&
830 git diff -w -M --cached |
831 sed -e "/^similarity index /s/[0-9][0-9]*/NUM/" >actual &&
832 test_cmp expect actual
835 cat >expected <<\EOF
836 diff --git a/empty b/void
837 similarity index 100%
838 rename from empty
839 rename to void
842 test_expect_success 'rename empty' '
843 git reset --hard &&
844 >empty &&
845 git add empty &&
846 git commit -m empty &&
847 git mv empty void &&
848 git diff -w --cached -M >current &&
849 test_cmp expected current
852 test_expect_success 'combined diff with autocrlf conversion' '
854 git reset --hard &&
855 echo >x hello &&
856 git commit -m "one side" x &&
857 git checkout HEAD^ &&
858 echo >x goodbye &&
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 &&
864 ! grep "^-" 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
883 cat >expected <<\EOF
884 <BOLD>diff --git a/x b/x<RESET>
885 <BOLD>index 9daeafb..2874b91 100644<RESET>
886 <BOLD>--- a/x<RESET>
887 <BOLD>+++ b/x<RESET>
888 <CYAN>@@ -1 +1,4 @@<RESET>
889 test<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 &&
897 git reset --hard &&
898 echo "test" > x &&
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
905 test_done