Merge branch 'jc/topo-author-date-sort'
[git/jrn.git] / t / t4015-diff-whitespace.sh
blob3fb4b976a277e69cdbf73172caf335748be609a3
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 \ " >x &&
159 git diff -w --ignore-blank-lines >out &&
160 >expect &&
161 test_cmp out expect
164 test_expect_success 'ignore-blank-lines: after change' '
165 cat <<-\EOF >x &&
176 git update-index x &&
177 cat <<-\EOF >x &&
178 change
189 git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp &&
190 cat <<-\EOF >expected &&
191 diff --git a/x b/x
192 --- a/x
193 +++ b/x
194 @@ -1,6 +1,7 @@
195 +change
204 compare_diff_patch expected out.tmp
207 test_expect_success 'ignore-blank-lines: before change' '
208 cat <<-\EOF >x &&
218 git update-index x &&
219 cat <<-\EOF >x &&
229 change
231 git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp &&
232 cat <<-\EOF >expected &&
233 diff --git a/x b/x
234 --- a/x
235 +++ b/x
236 @@ -4,5 +4,7 @@
243 +change
245 compare_diff_patch expected out.tmp
248 test_expect_success 'ignore-blank-lines: between changes' '
249 cat <<-\EOF >x &&
263 git update-index x &&
264 cat <<-\EOF >x &&
265 change
278 change
280 git diff --ignore-blank-lines >out.tmp &&
281 cat <<-\EOF >expected &&
282 diff --git a/x b/x
283 --- a/x
284 +++ b/x
285 @@ -1,5 +1,7 @@
286 +change
293 @@ -8,5 +8,7 @@
300 +change
302 compare_diff_patch expected out.tmp
305 test_expect_success 'ignore-blank-lines: between changes (with interhunkctx)' '
306 test_seq 10 >x &&
307 git update-index x &&
308 cat <<-\EOF >x &&
309 change
323 change
325 git diff --inter-hunk-context=2 --ignore-blank-lines >out.tmp &&
326 cat <<-\EOF >expected &&
327 diff --git a/x b/x
328 --- a/x
329 +++ b/x
330 @@ -1,10 +1,15 @@
331 +change
345 +change
347 compare_diff_patch expected out.tmp
350 test_expect_success 'ignore-blank-lines: scattered spaces' '
351 test_seq 10 >x &&
352 git update-index x &&
353 cat <<-\EOF >x &&
354 change
371 change
373 git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp &&
374 cat <<-\EOF >expected &&
375 diff --git a/x b/x
376 --- a/x
377 +++ b/x
378 @@ -1,3 +1,4 @@
379 +change
383 @@ -8,3 +15,4 @@
387 +change
389 compare_diff_patch expected out.tmp
392 test_expect_success 'ignore-blank-lines: spaces coalesce' '
393 test_seq 6 >x &&
394 git update-index x &&
395 cat <<-\EOF >x &&
396 change
406 change
408 git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp &&
409 cat <<-\EOF >expected &&
410 diff --git a/x b/x
411 --- a/x
412 +++ b/x
413 @@ -1,6 +1,11 @@
414 +change
424 +change
426 compare_diff_patch expected out.tmp
429 test_expect_success 'ignore-blank-lines: mix changes and blank lines' '
430 test_seq 16 >x &&
431 git update-index x &&
432 cat <<-\EOF >x &&
433 change
440 change
448 change
455 change
457 git diff --ignore-blank-lines >out.tmp &&
458 cat <<-\EOF >expected &&
459 diff --git a/x b/x
460 --- a/x
461 +++ b/x
462 @@ -1,8 +1,11 @@
463 +change
470 +change
474 @@ -9,8 +13,11 @@
478 +change
485 +change
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" &&
509 echo "foo();" > x &&
510 git diff --check
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' '
531 git checkout x &&
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' '
544 echo "foo();" > x &&
545 git add x &&
546 git diff --cached --check
550 test_expect_success 'check staged with trailing whitespace' '
552 echo "foo(); " > x &&
553 git add 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 &&
562 git add x &&
563 test_must_fail git diff --cached --check
567 test_expect_success 'check with no whitespace errors (diff-index)' '
569 echo "foo();" > x &&
570 git add x &&
571 git diff-index --check HEAD
575 test_expect_success 'check with trailing whitespace (diff-index)' '
577 echo "foo(); " > x &&
578 git add 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 &&
587 git add x &&
588 test_must_fail git diff-index --check HEAD
592 test_expect_success 'check staged with no whitespace errors (diff-index)' '
594 echo "foo();" > x &&
595 git add x &&
596 git diff-index --cached --check HEAD
600 test_expect_success 'check staged with trailing whitespace (diff-index)' '
602 echo "foo(); " > x &&
603 git add 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 &&
612 git add x &&
613 test_must_fail git diff-index --cached --check HEAD
617 test_expect_success 'check with no whitespace errors (diff-tree)' '
619 echo "foo();" > x &&
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 &&
646 git diff --check
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 &&
663 git diff --check
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 &&
680 git diff --check
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" &&
695 git diff --check
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" &&
717 git diff --check
721 test_expect_success 'check tabs as indentation (tab-in-indent: off)' '
723 git config core.whitespace "-tab-in-indent" &&
724 echo " foo ();" > x &&
725 git diff --check
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 &&
765 git diff --check &&
766 rm -f .gitattributes
770 test_expect_success 'line numbers in --check output are correct' '
772 echo "" > x &&
773 echo "foo(); " >> x &&
774 git diff --check | grep "x:2:"
778 test_expect_success 'checkdiff detects new trailing blank lines (1)' '
779 echo "foo();" >x &&
780 echo "" >>x &&
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 &&
786 git add 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' '
792 git checkout x &&
793 mv x y &&
795 echo "/* This is new */" &&
796 echo "" &&
797 cat y
798 ) >x &&
799 git diff --check
802 cat <<EOF >expect
804 test_expect_success 'whitespace-only changes not reported' '
805 git reset --hard &&
806 echo >x "hello world" &&
807 git add x &&
808 git commit -m "hello 1" &&
809 echo >x "hello world" &&
810 git diff -b >actual &&
811 test_cmp expect actual
814 cat <<EOF >expect
815 diff --git a/x b/z
816 similarity index NUM%
817 rename from x
818 rename to z
819 index 380c32a..a97b785 100644
821 test_expect_success 'whitespace-only changes reported across renames' '
822 git reset --hard &&
823 for i in 1 2 3 4 5 6 7 8 9; do echo "$i$i$i$i$i$i"; done >x &&
824 git add x &&
825 git commit -m "base" &&
826 sed -e "5s/^/ /" x >z &&
827 git rm x &&
828 git add z &&
829 git diff -w -M --cached |
830 sed -e "/^similarity index /s/[0-9][0-9]*/NUM/" >actual &&
831 test_cmp expect actual
834 cat >expected <<\EOF
835 diff --git a/empty b/void
836 similarity index 100%
837 rename from empty
838 rename to void
841 test_expect_success 'rename empty' '
842 git reset --hard &&
843 >empty &&
844 git add empty &&
845 git commit -m empty &&
846 git mv empty void &&
847 git diff -w --cached -M >current &&
848 test_cmp expected current
851 test_expect_success 'combined diff with autocrlf conversion' '
853 git reset --hard &&
854 echo >x hello &&
855 git commit -m "one side" x &&
856 git checkout HEAD^ &&
857 echo >x goodbye &&
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 &&
863 ! grep "^-" 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
882 cat >expected <<\EOF
883 <BOLD>diff --git a/x b/x<RESET>
884 <BOLD>index 9daeafb..2874b91 100644<RESET>
885 <BOLD>--- a/x<RESET>
886 <BOLD>+++ b/x<RESET>
887 <CYAN>@@ -1 +1,4 @@<RESET>
888 test<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 &&
896 git reset --hard &&
897 echo "test" > x &&
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
904 test_done