]>
Commit | Line | Data |
---|---|---|
2344d47f JS |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2006 Johannes E. Schindelin | |
4 | # | |
5 | ||
6 | test_description='Test special whitespace in diff engine. | |
7 | ||
8 | ' | |
9 | . ./test-lib.sh | |
10 | . ../diff-lib.sh | |
11 | ||
12 | # Ray Lehtiniemi's example | |
13 | ||
14 | cat << EOF > x | |
15 | do { | |
16 | nothing; | |
17 | } while (0); | |
18 | EOF | |
19 | ||
5be60078 | 20 | git update-index --add x |
2344d47f JS |
21 | |
22 | cat << EOF > x | |
23 | do | |
24 | { | |
25 | nothing; | |
26 | } | |
27 | while (0); | |
28 | EOF | |
29 | ||
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 | |
38 | +{ | |
39 | nothing; | |
40 | -} while (0); | |
41 | +} | |
42 | +while (0); | |
43 | EOF | |
44 | ||
5be60078 | 45 | git diff > out |
3af82863 | 46 | test_expect_success "Ray's example without options" 'test_cmp expect out' |
2344d47f | 47 | |
5be60078 | 48 | git diff -w > out |
3af82863 | 49 | test_expect_success "Ray's example with -w" 'test_cmp expect out' |
2344d47f | 50 | |
5be60078 | 51 | git diff -b > out |
3af82863 | 52 | test_expect_success "Ray's example with -b" 'test_cmp expect out' |
2344d47f | 53 | |
4035b46e | 54 | tr 'Q' '\015' << EOF > x |
2344d47f JS |
55 | whitespace at beginning |
56 | whitespace change | |
57 | whitespace in the middle | |
58 | whitespace at end | |
59 | unchanged line | |
4035b46e | 60 | CR at endQ |
2344d47f JS |
61 | EOF |
62 | ||
5be60078 | 63 | git update-index x |
2344d47f | 64 | |
74f16b0c | 65 | tr '_' ' ' << EOF > x |
2344d47f JS |
66 | whitespace at beginning |
67 | whitespace change | |
68 | white space in the middle | |
74f16b0c | 69 | whitespace at end__ |
2344d47f JS |
70 | unchanged line |
71 | CR at end | |
72 | EOF | |
73 | ||
74f16b0c | 74 | tr 'Q_' '\015 ' << EOF > expect |
2344d47f JS |
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 | |
74f16b0c | 87 | +whitespace at end__ |
2344d47f | 88 | unchanged line |
4035b46e | 89 | -CR at endQ |
2344d47f JS |
90 | +CR at end |
91 | EOF | |
5be60078 | 92 | git diff > out |
3af82863 | 93 | test_expect_success 'another test, without options' 'test_cmp expect out' |
2344d47f JS |
94 | |
95 | cat << EOF > expect | |
96 | diff --git a/x b/x | |
97 | index d99af23..8b32fb5 100644 | |
98 | EOF | |
5be60078 | 99 | git diff -w > out |
3af82863 | 100 | test_expect_success 'another test, with -w' 'test_cmp expect out' |
2344d47f | 101 | |
4035b46e | 102 | tr 'Q' '\015' << EOF > expect |
2344d47f JS |
103 | diff --git a/x b/x |
104 | index d99af23..8b32fb5 100644 | |
105 | --- a/x | |
106 | +++ b/x | |
107 | @@ -1,6 +1,6 @@ | |
108 | -whitespace at beginning | |
109 | + whitespace at beginning | |
110 | whitespace change | |
111 | -whitespace in the middle | |
2344d47f | 112 | +white space in the middle |
c7c24889 | 113 | whitespace at end |
2344d47f | 114 | unchanged line |
c7c24889 | 115 | CR at endQ |
2344d47f | 116 | EOF |
5be60078 | 117 | git diff -b > out |
3af82863 | 118 | test_expect_success 'another test, with -b' 'test_cmp expect out' |
2344d47f | 119 | |
86f8c236 WC |
120 | test_expect_success 'check mixed spaces and tabs in indent' ' |
121 | ||
122 | # This is indented with SP HT SP. | |
123 | echo " foo();" > x && | |
420f4f04 | 124 | git diff --check | grep "space before tab in indent" |
86f8c236 WC |
125 | |
126 | ' | |
127 | ||
9afa2d4a BF |
128 | test_expect_success 'check mixed tabs and spaces in indent' ' |
129 | ||
130 | # This is indented with HT SP HT. | |
131 | echo " foo();" > x && | |
132 | git diff --check | grep "space before tab in indent" | |
133 | ||
134 | ' | |
135 | ||
62c64895 WC |
136 | test_expect_success 'check with no whitespace errors' ' |
137 | ||
138 | git commit -m "snapshot" && | |
139 | echo "foo();" > x && | |
140 | git diff --check | |
141 | ||
142 | ' | |
143 | ||
f8175466 | 144 | test_expect_success 'check with trailing whitespace' ' |
62c64895 WC |
145 | |
146 | echo "foo(); " > x && | |
d492b31c | 147 | test_must_fail git diff --check |
62c64895 WC |
148 | |
149 | ' | |
150 | ||
f8175466 | 151 | test_expect_success 'check with space before tab in indent' ' |
62c64895 WC |
152 | |
153 | # indent has space followed by hard tab | |
154 | echo " foo();" > x && | |
d492b31c | 155 | test_must_fail git diff --check |
62c64895 WC |
156 | |
157 | ' | |
158 | ||
da31b358 | 159 | test_expect_success '--check and --exit-code are not exclusive' ' |
62c64895 WC |
160 | |
161 | git checkout x && | |
162 | git diff --check --exit-code | |
163 | ||
164 | ' | |
165 | ||
da31b358 | 166 | test_expect_success '--check and --quiet are not exclusive' ' |
62c64895 WC |
167 | |
168 | git diff --check --quiet | |
169 | ||
170 | ' | |
171 | ||
172 | test_expect_success 'check staged with no whitespace errors' ' | |
173 | ||
174 | echo "foo();" > x && | |
175 | git add x && | |
176 | git diff --cached --check | |
177 | ||
178 | ' | |
179 | ||
f8175466 | 180 | test_expect_success 'check staged with trailing whitespace' ' |
62c64895 WC |
181 | |
182 | echo "foo(); " > x && | |
183 | git add x && | |
d492b31c | 184 | test_must_fail git diff --cached --check |
62c64895 WC |
185 | |
186 | ' | |
187 | ||
f8175466 | 188 | test_expect_success 'check staged with space before tab in indent' ' |
62c64895 WC |
189 | |
190 | # indent has space followed by hard tab | |
191 | echo " foo();" > x && | |
192 | git add x && | |
d492b31c | 193 | test_must_fail git diff --cached --check |
62c64895 WC |
194 | |
195 | ' | |
196 | ||
197 | test_expect_success 'check with no whitespace errors (diff-index)' ' | |
198 | ||
199 | echo "foo();" > x && | |
200 | git add x && | |
201 | git diff-index --check HEAD | |
202 | ||
203 | ' | |
204 | ||
f8175466 | 205 | test_expect_success 'check with trailing whitespace (diff-index)' ' |
62c64895 WC |
206 | |
207 | echo "foo(); " > x && | |
208 | git add x && | |
d492b31c | 209 | test_must_fail git diff-index --check HEAD |
62c64895 WC |
210 | |
211 | ' | |
212 | ||
f8175466 | 213 | test_expect_success 'check with space before tab in indent (diff-index)' ' |
62c64895 WC |
214 | |
215 | # indent has space followed by hard tab | |
216 | echo " foo();" > x && | |
217 | git add x && | |
d492b31c | 218 | test_must_fail git diff-index --check HEAD |
62c64895 WC |
219 | |
220 | ' | |
221 | ||
222 | test_expect_success 'check staged with no whitespace errors (diff-index)' ' | |
223 | ||
224 | echo "foo();" > x && | |
225 | git add x && | |
226 | git diff-index --cached --check HEAD | |
227 | ||
228 | ' | |
229 | ||
f8175466 | 230 | test_expect_success 'check staged with trailing whitespace (diff-index)' ' |
62c64895 WC |
231 | |
232 | echo "foo(); " > x && | |
233 | git add x && | |
d492b31c | 234 | test_must_fail git diff-index --cached --check HEAD |
62c64895 WC |
235 | |
236 | ' | |
237 | ||
f8175466 | 238 | test_expect_success 'check staged with space before tab in indent (diff-index)' ' |
62c64895 WC |
239 | |
240 | # indent has space followed by hard tab | |
241 | echo " foo();" > x && | |
242 | git add x && | |
d492b31c | 243 | test_must_fail git diff-index --cached --check HEAD |
62c64895 WC |
244 | |
245 | ' | |
246 | ||
247 | test_expect_success 'check with no whitespace errors (diff-tree)' ' | |
248 | ||
249 | echo "foo();" > x && | |
250 | git commit -m "new commit" x && | |
251 | git diff-tree --check HEAD^ HEAD | |
252 | ||
253 | ' | |
254 | ||
f8175466 | 255 | test_expect_success 'check with trailing whitespace (diff-tree)' ' |
62c64895 WC |
256 | |
257 | echo "foo(); " > x && | |
258 | git commit -m "another commit" x && | |
d492b31c | 259 | test_must_fail git diff-tree --check HEAD^ HEAD |
62c64895 WC |
260 | |
261 | ' | |
262 | ||
f8175466 | 263 | test_expect_success 'check with space before tab in indent (diff-tree)' ' |
62c64895 WC |
264 | |
265 | # indent has space followed by hard tab | |
266 | echo " foo();" > x && | |
267 | git commit -m "yet another" x && | |
d492b31c | 268 | test_must_fail git diff-tree --check HEAD^ HEAD |
f8175466 JH |
269 | |
270 | ' | |
271 | ||
272 | test_expect_success 'check trailing whitespace (trailing-space: off)' ' | |
273 | ||
274 | git config core.whitespace "-trailing-space" && | |
275 | echo "foo (); " > x && | |
276 | git diff --check | |
277 | ||
278 | ' | |
279 | ||
280 | test_expect_success 'check trailing whitespace (trailing-space: on)' ' | |
281 | ||
282 | git config core.whitespace "trailing-space" && | |
283 | echo "foo (); " > x && | |
d492b31c | 284 | test_must_fail git diff --check |
f8175466 JH |
285 | |
286 | ' | |
287 | ||
288 | test_expect_success 'check space before tab in indent (space-before-tab: off)' ' | |
289 | ||
290 | # indent contains space followed by HT | |
291 | git config core.whitespace "-space-before-tab" && | |
292 | echo " foo ();" > x && | |
293 | git diff --check | |
294 | ||
295 | ' | |
296 | ||
297 | test_expect_success 'check space before tab in indent (space-before-tab: on)' ' | |
298 | ||
299 | # indent contains space followed by HT | |
300 | git config core.whitespace "space-before-tab" && | |
301 | echo " foo (); " > x && | |
d492b31c | 302 | test_must_fail git diff --check |
f8175466 JH |
303 | |
304 | ' | |
305 | ||
306 | test_expect_success 'check spaces as indentation (indent-with-non-tab: off)' ' | |
307 | ||
308 | git config core.whitespace "-indent-with-non-tab" | |
4d9697c7 | 309 | echo " foo ();" > x && |
f8175466 JH |
310 | git diff --check |
311 | ||
312 | ' | |
313 | ||
314 | test_expect_success 'check spaces as indentation (indent-with-non-tab: on)' ' | |
315 | ||
316 | git config core.whitespace "indent-with-non-tab" && | |
4d9697c7 | 317 | echo " foo ();" > x && |
d492b31c | 318 | test_must_fail git diff --check |
62c64895 WC |
319 | |
320 | ' | |
321 | ||
9afa2d4a BF |
322 | test_expect_success 'check tabs and spaces as indentation (indent-with-non-tab: on)' ' |
323 | ||
324 | git config core.whitespace "indent-with-non-tab" && | |
325 | echo " foo ();" > x && | |
d492b31c | 326 | test_must_fail git diff --check |
9afa2d4a BF |
327 | |
328 | ' | |
0ef617f4 JH |
329 | |
330 | test_expect_success 'line numbers in --check output are correct' ' | |
331 | ||
332 | echo "" > x && | |
333 | echo "foo(); " >> x && | |
334 | git diff --check | grep "x:2:" | |
335 | ||
336 | ' | |
337 | ||
877f23cc JH |
338 | test_expect_success 'checkdiff detects trailing blank lines' ' |
339 | echo "foo();" >x && | |
340 | echo "" >>x && | |
341 | git diff --check | grep "ends with blank" | |
342 | ' | |
343 | ||
c35539eb JH |
344 | test_expect_success 'checkdiff allows new blank lines' ' |
345 | git checkout x && | |
346 | mv x y && | |
347 | ( | |
348 | echo "/* This is new */" && | |
349 | echo "" && | |
350 | cat y | |
351 | ) >x && | |
352 | git diff --check | |
353 | ' | |
354 | ||
5e568f9e AG |
355 | test_expect_success 'combined diff with autocrlf conversion' ' |
356 | ||
357 | git reset --hard && | |
358 | echo >x hello && | |
359 | git commit -m "one side" x && | |
360 | git checkout HEAD^ && | |
361 | echo >x goodbye && | |
362 | git commit -m "the other side" x && | |
363 | git config core.autocrlf true && | |
364 | test_must_fail git merge master && | |
365 | ||
366 | git diff | sed -e "1,/^@@@/d" >actual && | |
367 | ! grep "^-" actual | |
368 | ||
369 | ' | |
370 | ||
2344d47f | 371 | test_done |