Merge branch 'mg/use-default-abbrev-length-in-rev-list' into maint
[git.git] / t / t4015-diff-whitespace.sh
blob90f33423731a84310caf92780482a44f8d2f32d8
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 endQ
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 endQ
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 'check mixed spaces and tabs in indent' '
147 # This is indented with SP HT SP.
148 echo " foo();" > x &&
149 git diff --check | grep "space before tab in indent"
153 test_expect_success 'check mixed tabs and spaces in indent' '
155 # This is indented with HT SP HT.
156 echo " foo();" > x &&
157 git diff --check | grep "space before tab in indent"
161 test_expect_success 'check with no whitespace errors' '
163 git commit -m "snapshot" &&
164 echo "foo();" > x &&
165 git diff --check
169 test_expect_success 'check with trailing whitespace' '
171 echo "foo(); " > x &&
172 test_must_fail git diff --check
176 test_expect_success 'check with space before tab in indent' '
178 # indent has space followed by hard tab
179 echo " foo();" > x &&
180 test_must_fail git diff --check
184 test_expect_success '--check and --exit-code are not exclusive' '
186 git checkout x &&
187 git diff --check --exit-code
191 test_expect_success '--check and --quiet are not exclusive' '
193 git diff --check --quiet
197 test_expect_success 'check staged with no whitespace errors' '
199 echo "foo();" > x &&
200 git add x &&
201 git diff --cached --check
205 test_expect_success 'check staged with trailing whitespace' '
207 echo "foo(); " > x &&
208 git add x &&
209 test_must_fail git diff --cached --check
213 test_expect_success 'check staged with space before tab in indent' '
215 # indent has space followed by hard tab
216 echo " foo();" > x &&
217 git add x &&
218 test_must_fail git diff --cached --check
222 test_expect_success 'check with no whitespace errors (diff-index)' '
224 echo "foo();" > x &&
225 git add x &&
226 git diff-index --check HEAD
230 test_expect_success 'check with trailing whitespace (diff-index)' '
232 echo "foo(); " > x &&
233 git add x &&
234 test_must_fail git diff-index --check HEAD
238 test_expect_success 'check with space before tab in indent (diff-index)' '
240 # indent has space followed by hard tab
241 echo " foo();" > x &&
242 git add x &&
243 test_must_fail git diff-index --check HEAD
247 test_expect_success 'check staged with no whitespace errors (diff-index)' '
249 echo "foo();" > x &&
250 git add x &&
251 git diff-index --cached --check HEAD
255 test_expect_success 'check staged with trailing whitespace (diff-index)' '
257 echo "foo(); " > x &&
258 git add x &&
259 test_must_fail git diff-index --cached --check HEAD
263 test_expect_success 'check staged with space before tab in indent (diff-index)' '
265 # indent has space followed by hard tab
266 echo " foo();" > x &&
267 git add x &&
268 test_must_fail git diff-index --cached --check HEAD
272 test_expect_success 'check with no whitespace errors (diff-tree)' '
274 echo "foo();" > x &&
275 git commit -m "new commit" x &&
276 git diff-tree --check HEAD^ HEAD
280 test_expect_success 'check with trailing whitespace (diff-tree)' '
282 echo "foo(); " > x &&
283 git commit -m "another commit" x &&
284 test_must_fail git diff-tree --check HEAD^ HEAD
288 test_expect_success 'check with space before tab in indent (diff-tree)' '
290 # indent has space followed by hard tab
291 echo " foo();" > x &&
292 git commit -m "yet another" x &&
293 test_must_fail git diff-tree --check HEAD^ HEAD
297 test_expect_success 'check trailing whitespace (trailing-space: off)' '
299 git config core.whitespace "-trailing-space" &&
300 echo "foo (); " > x &&
301 git diff --check
305 test_expect_success 'check trailing whitespace (trailing-space: on)' '
307 git config core.whitespace "trailing-space" &&
308 echo "foo (); " > x &&
309 test_must_fail git diff --check
313 test_expect_success 'check space before tab in indent (space-before-tab: off)' '
315 # indent contains space followed by HT
316 git config core.whitespace "-space-before-tab" &&
317 echo " foo ();" > x &&
318 git diff --check
322 test_expect_success 'check space before tab in indent (space-before-tab: on)' '
324 # indent contains space followed by HT
325 git config core.whitespace "space-before-tab" &&
326 echo " foo (); " > x &&
327 test_must_fail git diff --check
331 test_expect_success 'check spaces as indentation (indent-with-non-tab: off)' '
333 git config core.whitespace "-indent-with-non-tab"
334 echo " foo ();" > x &&
335 git diff --check
339 test_expect_success 'check spaces as indentation (indent-with-non-tab: on)' '
341 git config core.whitespace "indent-with-non-tab" &&
342 echo " foo ();" > x &&
343 test_must_fail git diff --check
347 test_expect_success 'check tabs and spaces as indentation (indent-with-non-tab: on)' '
349 git config core.whitespace "indent-with-non-tab" &&
350 echo " foo ();" > x &&
351 test_must_fail git diff --check
355 test_expect_success 'line numbers in --check output are correct' '
357 echo "" > x &&
358 echo "foo(); " >> x &&
359 git diff --check | grep "x:2:"
363 test_expect_success 'checkdiff detects new trailing blank lines (1)' '
364 echo "foo();" >x &&
365 echo "" >>x &&
366 git diff --check | grep "new blank line"
369 test_expect_success 'checkdiff detects new trailing blank lines (2)' '
370 { echo a; echo b; echo; echo; } >x &&
371 git add x &&
372 { echo a; echo; echo; echo; echo; } >x &&
373 git diff --check | grep "new blank line"
376 test_expect_success 'checkdiff allows new blank lines' '
377 git checkout x &&
378 mv x y &&
380 echo "/* This is new */" &&
381 echo "" &&
382 cat y
383 ) >x &&
384 git diff --check
387 cat <<EOF >expect
389 test_expect_success 'whitespace-only changes not reported' '
390 git reset --hard &&
391 echo >x "hello world" &&
392 git add x &&
393 git commit -m "hello 1" &&
394 echo >x "hello world" &&
395 git diff -b >actual &&
396 test_cmp expect actual
399 test_expect_success 'combined diff with autocrlf conversion' '
401 git reset --hard &&
402 echo >x hello &&
403 git commit -m "one side" x &&
404 git checkout HEAD^ &&
405 echo >x goodbye &&
406 git commit -m "the other side" x &&
407 git config core.autocrlf true &&
408 test_must_fail git merge master &&
410 git diff | sed -e "1,/^@@@/d" >actual &&
411 ! grep "^-" actual
415 test_done