3 test_description='diff whitespace error detection'
7 test_expect_success setup '
9 git config diff.color.whitespace "blue reverse" &&
12 echo " Eight SP indent" >>F &&
13 echo " HT and SP indent" >>F &&
14 echo "With trailing SP " >>F &&
15 echo "Carriage ReturnQ" | tr Q "\015" >>F &&
16 echo "No problem" >>F &&
21 blue_grep='7;34m' ;# ESC [ 7 ; 3 4 m
23 printf "\033[%s" "$blue_grep" >check-grep
24 if (grep "$blue_grep" <check-grep | grep "$blue_grep") >/dev/null 2>&1
27 elif (grep -a "$blue_grep" <check-grep | grep -a "$blue_grep") >/dev/null 2>&1
31 grep_a=grep ;# expected to fail...
36 git diff --color >output
37 $grep_a "$blue_grep" output >error
38 $grep_a -v "$blue_grep" output >normal
42 test_expect_success default '
46 grep Eight normal >/dev/null &&
47 grep HT error >/dev/null &&
48 grep With error >/dev/null &&
49 grep Return error >/dev/null &&
50 grep No normal >/dev/null
54 test_expect_success 'without -trail' '
56 git config core.whitespace -trail &&
59 grep Eight normal >/dev/null &&
60 grep HT error >/dev/null &&
61 grep With normal >/dev/null &&
62 grep Return normal >/dev/null &&
63 grep No normal >/dev/null
67 test_expect_success 'without -trail (attribute)' '
69 test_might_fail git config --unset core.whitespace &&
70 echo "F whitespace=-trail" >.gitattributes &&
73 grep Eight normal >/dev/null &&
74 grep HT error >/dev/null &&
75 grep With normal >/dev/null &&
76 grep Return normal >/dev/null &&
77 grep No normal >/dev/null
81 test_expect_success 'without -space' '
83 rm -f .gitattributes &&
84 git config core.whitespace -space &&
87 grep Eight normal >/dev/null &&
88 grep HT normal >/dev/null &&
89 grep With error >/dev/null &&
90 grep Return error >/dev/null &&
91 grep No normal >/dev/null
95 test_expect_success 'without -space (attribute)' '
97 test_might_fail git config --unset core.whitespace &&
98 echo "F whitespace=-space" >.gitattributes &&
101 grep Eight normal >/dev/null &&
102 grep HT normal >/dev/null &&
103 grep With error >/dev/null &&
104 grep Return error >/dev/null &&
105 grep No normal >/dev/null
109 test_expect_success 'with indent-non-tab only' '
111 rm -f .gitattributes &&
112 git config core.whitespace indent,-trailing,-space &&
115 grep Eight error >/dev/null &&
116 grep HT normal >/dev/null &&
117 grep With normal >/dev/null &&
118 grep Return normal >/dev/null &&
119 grep No normal >/dev/null
123 test_expect_success 'with indent-non-tab only (attribute)' '
125 test_might_fail git config --unset core.whitespace &&
126 echo "F whitespace=indent,-trailing,-space" >.gitattributes &&
129 grep Eight error >/dev/null &&
130 grep HT normal >/dev/null &&
131 grep With normal >/dev/null &&
132 grep Return normal >/dev/null &&
133 grep No normal >/dev/null
137 test_expect_success 'with cr-at-eol' '
139 rm -f .gitattributes &&
140 git config core.whitespace cr-at-eol &&
143 grep Eight normal >/dev/null &&
144 grep HT error >/dev/null &&
145 grep With error >/dev/null &&
146 grep Return normal >/dev/null &&
147 grep No normal >/dev/null
151 test_expect_success 'with cr-at-eol (attribute)' '
153 test_might_fail git config --unset core.whitespace &&
154 echo "F whitespace=trailing,cr-at-eol" >.gitattributes &&
157 grep Eight normal >/dev/null &&
158 grep HT error >/dev/null &&
159 grep With error >/dev/null &&
160 grep Return normal >/dev/null &&
161 grep No normal >/dev/null
165 test_expect_success 'trailing empty lines (1)' '
167 rm -f .gitattributes &&
168 test_must_fail git diff --check >output &&
169 grep "new blank line at" output &&
170 grep "trailing whitespace" output
174 test_expect_success 'trailing empty lines (2)' '
176 echo "F -whitespace" >.gitattributes &&
177 git diff --check >output &&
182 test_expect_success 'checkdiff shows correct line number for trailing blank lines' '
184 printf "a\nb\n" > G &&
186 printf "x\nx\nx\na\nb\nc\n\n" > G &&
187 [ "$(git diff --check -- G)" = "G:7: new blank line at EOF." ]
191 test_expect_success 'do not color trailing cr in context' '
192 test_might_fail git config --unset core.whitespace &&
193 rm -f .gitattributes &&
194 echo AAAQ | tr Q "\015" >G &&
196 echo BBBQ | tr Q "\015" >>G &&
197 git diff --color G | tr "\015" Q >output &&
198 grep "BBB.*${blue_grep}Q" output &&
199 grep "AAA.*\[mQ" output
203 test_expect_success 'color new trailing blank lines' '
204 { echo a; echo b; echo; echo; } >x &&
206 { echo a; echo; echo; echo; echo c; echo; echo; echo; echo; } >x &&
207 git diff --color x >output &&
208 cnt=$($grep_a "${blue_grep}" output | wc -l) &&