Change sed i\ usage to something Solaris' sed can handle
[git/gitweb.git] / t / t4015-diff-whitespace.sh
blob0126154e081ea356c696ea7cc38d44ed1239ac73
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\\
150 " >x &&
151 git diff --ignore-blank-lines >out &&
152 >expect &&
153 test_cmp out expect
156 test_expect_success 'ignore-blank-lines: only new lines with space' '
157 test_seq 5 >x &&
158 git update-index x &&
159 test_seq 5 | sed "/3/i\\
160 " >x &&
161 git diff -w --ignore-blank-lines >out &&
162 >expect &&
163 test_cmp out expect
166 test_expect_success 'ignore-blank-lines: after change' '
167 cat <<-\EOF >x &&
178 git update-index x &&
179 cat <<-\EOF >x &&
180 change
191 git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp &&
192 cat <<-\EOF >expected &&
193 diff --git a/x b/x
194 --- a/x
195 +++ b/x
196 @@ -1,6 +1,7 @@
197 +change
206 compare_diff_patch expected out.tmp
209 test_expect_success 'ignore-blank-lines: before change' '
210 cat <<-\EOF >x &&
220 git update-index x &&
221 cat <<-\EOF >x &&
231 change
233 git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp &&
234 cat <<-\EOF >expected &&
235 diff --git a/x b/x
236 --- a/x
237 +++ b/x
238 @@ -4,5 +4,7 @@
245 +change
247 compare_diff_patch expected out.tmp
250 test_expect_success 'ignore-blank-lines: between changes' '
251 cat <<-\EOF >x &&
265 git update-index x &&
266 cat <<-\EOF >x &&
267 change
280 change
282 git diff --ignore-blank-lines >out.tmp &&
283 cat <<-\EOF >expected &&
284 diff --git a/x b/x
285 --- a/x
286 +++ b/x
287 @@ -1,5 +1,7 @@
288 +change
295 @@ -8,5 +8,7 @@
302 +change
304 compare_diff_patch expected out.tmp
307 test_expect_success 'ignore-blank-lines: between changes (with interhunkctx)' '
308 test_seq 10 >x &&
309 git update-index x &&
310 cat <<-\EOF >x &&
311 change
325 change
327 git diff --inter-hunk-context=2 --ignore-blank-lines >out.tmp &&
328 cat <<-\EOF >expected &&
329 diff --git a/x b/x
330 --- a/x
331 +++ b/x
332 @@ -1,10 +1,15 @@
333 +change
347 +change
349 compare_diff_patch expected out.tmp
352 test_expect_success 'ignore-blank-lines: scattered spaces' '
353 test_seq 10 >x &&
354 git update-index x &&
355 cat <<-\EOF >x &&
356 change
373 change
375 git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp &&
376 cat <<-\EOF >expected &&
377 diff --git a/x b/x
378 --- a/x
379 +++ b/x
380 @@ -1,3 +1,4 @@
381 +change
385 @@ -8,3 +15,4 @@
389 +change
391 compare_diff_patch expected out.tmp
394 test_expect_success 'ignore-blank-lines: spaces coalesce' '
395 test_seq 6 >x &&
396 git update-index x &&
397 cat <<-\EOF >x &&
398 change
408 change
410 git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp &&
411 cat <<-\EOF >expected &&
412 diff --git a/x b/x
413 --- a/x
414 +++ b/x
415 @@ -1,6 +1,11 @@
416 +change
426 +change
428 compare_diff_patch expected out.tmp
431 test_expect_success 'ignore-blank-lines: mix changes and blank lines' '
432 test_seq 16 >x &&
433 git update-index x &&
434 cat <<-\EOF >x &&
435 change
442 change
450 change
457 change
459 git diff --ignore-blank-lines >out.tmp &&
460 cat <<-\EOF >expected &&
461 diff --git a/x b/x
462 --- a/x
463 +++ b/x
464 @@ -1,8 +1,11 @@
465 +change
472 +change
476 @@ -9,8 +13,11 @@
480 +change
487 +change
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" &&
511 echo "foo();" > x &&
512 git diff --check
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' '
533 git checkout x &&
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' '
546 echo "foo();" > x &&
547 git add x &&
548 git diff --cached --check
552 test_expect_success 'check staged with trailing whitespace' '
554 echo "foo(); " > x &&
555 git add 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 &&
564 git add x &&
565 test_must_fail git diff --cached --check
569 test_expect_success 'check with no whitespace errors (diff-index)' '
571 echo "foo();" > x &&
572 git add x &&
573 git diff-index --check HEAD
577 test_expect_success 'check with trailing whitespace (diff-index)' '
579 echo "foo(); " > x &&
580 git add 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 &&
589 git add x &&
590 test_must_fail git diff-index --check HEAD
594 test_expect_success 'check staged with no whitespace errors (diff-index)' '
596 echo "foo();" > x &&
597 git add x &&
598 git diff-index --cached --check HEAD
602 test_expect_success 'check staged with trailing whitespace (diff-index)' '
604 echo "foo(); " > x &&
605 git add 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 &&
614 git add x &&
615 test_must_fail git diff-index --cached --check HEAD
619 test_expect_success 'check with no whitespace errors (diff-tree)' '
621 echo "foo();" > x &&
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 &&
648 git diff --check
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 &&
665 git diff --check
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 &&
682 git diff --check
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" &&
697 git diff --check
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" &&
719 git diff --check
723 test_expect_success 'check tabs as indentation (tab-in-indent: off)' '
725 git config core.whitespace "-tab-in-indent" &&
726 echo " foo ();" > x &&
727 git diff --check
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 &&
767 git diff --check &&
768 rm -f .gitattributes
772 test_expect_success 'line numbers in --check output are correct' '
774 echo "" > x &&
775 echo "foo(); " >> x &&
776 git diff --check | grep "x:2:"
780 test_expect_success 'checkdiff detects new trailing blank lines (1)' '
781 echo "foo();" >x &&
782 echo "" >>x &&
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 &&
788 git add 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' '
794 git checkout x &&
795 mv x y &&
797 echo "/* This is new */" &&
798 echo "" &&
799 cat y
800 ) >x &&
801 git diff --check
804 cat <<EOF >expect
806 test_expect_success 'whitespace-only changes not reported' '
807 git reset --hard &&
808 echo >x "hello world" &&
809 git add x &&
810 git commit -m "hello 1" &&
811 echo >x "hello world" &&
812 git diff -b >actual &&
813 test_cmp expect actual
816 cat <<EOF >expect
817 diff --git a/x b/z
818 similarity index NUM%
819 rename from x
820 rename to z
821 index 380c32a..a97b785 100644
823 test_expect_success 'whitespace-only changes reported across renames' '
824 git reset --hard &&
825 for i in 1 2 3 4 5 6 7 8 9; do echo "$i$i$i$i$i$i"; done >x &&
826 git add x &&
827 git commit -m "base" &&
828 sed -e "5s/^/ /" x >z &&
829 git rm x &&
830 git add z &&
831 git diff -w -M --cached |
832 sed -e "/^similarity index /s/[0-9][0-9]*/NUM/" >actual &&
833 test_cmp expect actual
836 cat >expected <<\EOF
837 diff --git a/empty b/void
838 similarity index 100%
839 rename from empty
840 rename to void
843 test_expect_success 'rename empty' '
844 git reset --hard &&
845 >empty &&
846 git add empty &&
847 git commit -m empty &&
848 git mv empty void &&
849 git diff -w --cached -M >current &&
850 test_cmp expected current
853 test_expect_success 'combined diff with autocrlf conversion' '
855 git reset --hard &&
856 echo >x hello &&
857 git commit -m "one side" x &&
858 git checkout HEAD^ &&
859 echo >x goodbye &&
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 &&
865 ! grep "^-" 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
884 cat >expected <<\EOF
885 <BOLD>diff --git a/x b/x<RESET>
886 <BOLD>index 9daeafb..2874b91 100644<RESET>
887 <BOLD>--- a/x<RESET>
888 <BOLD>+++ b/x<RESET>
889 <CYAN>@@ -1 +1,4 @@<RESET>
890 test<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 &&
898 git reset --hard &&
899 echo "test" > x &&
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
906 test_done