3 test_description
='diff whitespace error detection'
5 TEST_PASSES_SANITIZE_LEAK
=true
8 test_expect_success setup
'
10 git config diff.color.whitespace "blue reverse" &&
13 echo " Eight SP indent" >>F &&
14 echo " HT and SP indent" >>F &&
15 echo "With trailing SP " >>F &&
16 echo "Carriage ReturnQ" | tr Q "\015" >>F &&
17 echo "No problem" >>F &&
22 blue_grep
='7;34m' ;# ESC [ 7 ; 3 4 m
24 printf "\033[%s" "$blue_grep" >check-grep
25 if (grep "$blue_grep" <check-grep |
grep "$blue_grep") >/dev
/null
2>&1
28 elif (grep -a "$blue_grep" <check-grep |
grep -a "$blue_grep") >/dev
/null
2>&1
32 grep_a
=grep ;# expected to fail...
37 git
diff --color >output
38 $grep_a "$blue_grep" output
>error
39 $grep_a -v "$blue_grep" output
>normal
43 test_expect_success default
'
47 grep Eight normal >/dev/null &&
48 grep HT error >/dev/null &&
49 grep With error >/dev/null &&
50 grep Return error >/dev/null &&
51 grep No normal >/dev/null
55 test_expect_success
'default (attribute)' '
57 test_might_fail git config --unset core.whitespace &&
58 echo "F whitespace" >.gitattributes &&
61 grep Eight error >/dev/null &&
62 grep HT error >/dev/null &&
63 grep With error >/dev/null &&
64 grep Return error >/dev/null &&
65 grep No normal >/dev/null
69 test_expect_success
'default, tabwidth=10 (attribute)' '
71 git config core.whitespace "tabwidth=10" &&
72 echo "F whitespace" >.gitattributes &&
75 grep Eight normal >/dev/null &&
76 grep HT error >/dev/null &&
77 grep With error >/dev/null &&
78 grep Return error >/dev/null &&
79 grep No normal >/dev/null
83 test_expect_success
'no check (attribute)' '
85 test_might_fail git config --unset core.whitespace &&
86 echo "F -whitespace" >.gitattributes &&
89 grep Eight normal >/dev/null &&
90 grep HT normal >/dev/null &&
91 grep With normal >/dev/null &&
92 grep Return normal >/dev/null &&
93 grep No normal >/dev/null
97 test_expect_success
'no check, tabwidth=10 (attribute), must be irrelevant' '
99 git config core.whitespace "tabwidth=10" &&
100 echo "F -whitespace" >.gitattributes &&
103 grep Eight normal >/dev/null &&
104 grep HT normal >/dev/null &&
105 grep With normal >/dev/null &&
106 grep Return normal >/dev/null &&
107 grep No normal >/dev/null
111 test_expect_success
'without -trail' '
113 rm -f .gitattributes &&
114 git config core.whitespace -trail &&
117 grep Eight normal >/dev/null &&
118 grep HT error >/dev/null &&
119 grep With normal >/dev/null &&
120 grep Return normal >/dev/null &&
121 grep No normal >/dev/null
125 test_expect_success
'without -trail (attribute)' '
127 test_might_fail git config --unset core.whitespace &&
128 echo "F whitespace=-trail" >.gitattributes &&
131 grep Eight normal >/dev/null &&
132 grep HT error >/dev/null &&
133 grep With normal >/dev/null &&
134 grep Return normal >/dev/null &&
135 grep No normal >/dev/null
139 test_expect_success
'without -space' '
141 rm -f .gitattributes &&
142 git config core.whitespace -space &&
145 grep Eight normal >/dev/null &&
146 grep HT normal >/dev/null &&
147 grep With error >/dev/null &&
148 grep Return error >/dev/null &&
149 grep No normal >/dev/null
153 test_expect_success
'without -space (attribute)' '
155 test_might_fail git config --unset core.whitespace &&
156 echo "F whitespace=-space" >.gitattributes &&
159 grep Eight normal >/dev/null &&
160 grep HT normal >/dev/null &&
161 grep With error >/dev/null &&
162 grep Return error >/dev/null &&
163 grep No normal >/dev/null
167 test_expect_success
'with indent-non-tab only' '
169 rm -f .gitattributes &&
170 git config core.whitespace indent,-trailing,-space &&
173 grep Eight error >/dev/null &&
174 grep HT normal >/dev/null &&
175 grep With normal >/dev/null &&
176 grep Return normal >/dev/null &&
177 grep No normal >/dev/null
181 test_expect_success
'with indent-non-tab only (attribute)' '
183 test_might_fail git config --unset core.whitespace &&
184 echo "F whitespace=indent,-trailing,-space" >.gitattributes &&
187 grep Eight error >/dev/null &&
188 grep HT normal >/dev/null &&
189 grep With normal >/dev/null &&
190 grep Return normal >/dev/null &&
191 grep No normal >/dev/null
195 test_expect_success
'with indent-non-tab only, tabwidth=10' '
197 rm -f .gitattributes &&
198 git config core.whitespace indent,tabwidth=10,-trailing,-space &&
201 grep Eight normal >/dev/null &&
202 grep HT normal >/dev/null &&
203 grep With normal >/dev/null &&
204 grep Return normal >/dev/null &&
205 grep No normal >/dev/null
209 test_expect_success
'with indent-non-tab only, tabwidth=10 (attribute)' '
211 test_might_fail git config --unset core.whitespace &&
212 echo "F whitespace=indent,-trailing,-space,tabwidth=10" >.gitattributes &&
215 grep Eight normal >/dev/null &&
216 grep HT normal >/dev/null &&
217 grep With normal >/dev/null &&
218 grep Return normal >/dev/null &&
219 grep No normal >/dev/null
223 test_expect_success
'with cr-at-eol' '
225 rm -f .gitattributes &&
226 git config core.whitespace cr-at-eol &&
229 grep Eight normal >/dev/null &&
230 grep HT error >/dev/null &&
231 grep With error >/dev/null &&
232 grep Return normal >/dev/null &&
233 grep No normal >/dev/null
237 test_expect_success
'with cr-at-eol (attribute)' '
239 test_might_fail git config --unset core.whitespace &&
240 echo "F whitespace=trailing,cr-at-eol" >.gitattributes &&
243 grep Eight normal >/dev/null &&
244 grep HT error >/dev/null &&
245 grep With error >/dev/null &&
246 grep Return normal >/dev/null &&
247 grep No normal >/dev/null
251 test_expect_success
'trailing empty lines (1)' '
253 rm -f .gitattributes &&
254 test_must_fail git diff --check >output &&
255 grep "new blank line at" output &&
256 grep "trailing whitespace" output
260 test_expect_success
'trailing empty lines (2)' '
262 echo "F -whitespace" >.gitattributes &&
263 git diff --check >output &&
264 test_must_be_empty output
268 test_expect_success
'checkdiff shows correct line number for trailing blank lines' '
270 printf "a\nb\n" > G &&
272 printf "x\nx\nx\na\nb\nc\n\n" > G &&
273 [ "$(git diff --check -- G)" = "G:7: new blank line at EOF." ]
277 test_expect_success
'do not color trailing cr in context' '
278 test_might_fail git config --unset core.whitespace &&
279 rm -f .gitattributes &&
280 echo AAAQ | tr Q "\015" >G &&
282 echo BBBQ | tr Q "\015" >>G &&
283 git diff --color G | tr "\015" Q >output &&
284 grep "BBB.*${blue_grep}Q" output &&
285 grep "AAA.*\[mQ" output
289 test_expect_success
'color new trailing blank lines' '
290 test_write_lines a b "" "" >x &&
292 test_write_lines a "" "" "" c "" "" "" "" >x &&
293 git diff --color x >output &&
294 cnt=$($grep_a "${blue_grep}" output | wc -l) &&