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