diff --whitespace: fix blank lines at end
[tgit.git] / t / t4015-diff-whitespace.sh
blobe0b481d42b1b5a21cef3a8fae77d7eedcb8aeb2a
1 #!/bin/sh
3 # Copyright (c) 2006 Johannes E. Schindelin
6 test_description='Test special whitespace in diff engine.
9 . ./test-lib.sh
10 . ../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 diff --git a/x b/x
97 index d99af23..8b32fb5 100644
98 EOF
99 git diff -w > out
100 test_expect_success 'another test, with -w' 'test_cmp expect out'
102 tr 'Q' '\015' << EOF > expect
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
112 +white space in the middle
113 whitespace at end
114 unchanged line
115 CR at endQ
117 git diff -b > out
118 test_expect_success 'another test, with -b' 'test_cmp expect out'
120 test_expect_success 'check mixed spaces and tabs in indent' '
122 # This is indented with SP HT SP.
123 echo " foo();" > x &&
124 git diff --check | grep "space before tab in indent"
128 test_expect_success 'check mixed tabs and spaces in indent' '
130 # This is indented with HT SP HT.
131 echo " foo();" > x &&
132 git diff --check | grep "space before tab in indent"
136 test_expect_success 'check with no whitespace errors' '
138 git commit -m "snapshot" &&
139 echo "foo();" > x &&
140 git diff --check
144 test_expect_success 'check with trailing whitespace' '
146 echo "foo(); " > x &&
147 test_must_fail git diff --check
151 test_expect_success 'check with space before tab in indent' '
153 # indent has space followed by hard tab
154 echo " foo();" > x &&
155 test_must_fail git diff --check
159 test_expect_success '--check and --exit-code are not exclusive' '
161 git checkout x &&
162 git diff --check --exit-code
166 test_expect_success '--check and --quiet are not exclusive' '
168 git diff --check --quiet
172 test_expect_success 'check staged with no whitespace errors' '
174 echo "foo();" > x &&
175 git add x &&
176 git diff --cached --check
180 test_expect_success 'check staged with trailing whitespace' '
182 echo "foo(); " > x &&
183 git add x &&
184 test_must_fail git diff --cached --check
188 test_expect_success 'check staged with space before tab in indent' '
190 # indent has space followed by hard tab
191 echo " foo();" > x &&
192 git add x &&
193 test_must_fail git diff --cached --check
197 test_expect_success 'check with no whitespace errors (diff-index)' '
199 echo "foo();" > x &&
200 git add x &&
201 git diff-index --check HEAD
205 test_expect_success 'check with trailing whitespace (diff-index)' '
207 echo "foo(); " > x &&
208 git add x &&
209 test_must_fail git diff-index --check HEAD
213 test_expect_success 'check with space before tab in indent (diff-index)' '
215 # indent has space followed by hard tab
216 echo " foo();" > x &&
217 git add x &&
218 test_must_fail git diff-index --check HEAD
222 test_expect_success 'check staged with no whitespace errors (diff-index)' '
224 echo "foo();" > x &&
225 git add x &&
226 git diff-index --cached --check HEAD
230 test_expect_success 'check staged with trailing whitespace (diff-index)' '
232 echo "foo(); " > x &&
233 git add x &&
234 test_must_fail git diff-index --cached --check HEAD
238 test_expect_success 'check staged 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 --cached --check HEAD
247 test_expect_success 'check with no whitespace errors (diff-tree)' '
249 echo "foo();" > x &&
250 git commit -m "new commit" x &&
251 git diff-tree --check HEAD^ HEAD
255 test_expect_success 'check with trailing whitespace (diff-tree)' '
257 echo "foo(); " > x &&
258 git commit -m "another commit" x &&
259 test_must_fail git diff-tree --check HEAD^ HEAD
263 test_expect_success 'check with space before tab in indent (diff-tree)' '
265 # indent has space followed by hard tab
266 echo " foo();" > x &&
267 git commit -m "yet another" x &&
268 test_must_fail git diff-tree --check HEAD^ HEAD
272 test_expect_success 'check trailing whitespace (trailing-space: off)' '
274 git config core.whitespace "-trailing-space" &&
275 echo "foo (); " > x &&
276 git diff --check
280 test_expect_success 'check trailing whitespace (trailing-space: on)' '
282 git config core.whitespace "trailing-space" &&
283 echo "foo (); " > x &&
284 test_must_fail git diff --check
288 test_expect_success 'check space before tab in indent (space-before-tab: off)' '
290 # indent contains space followed by HT
291 git config core.whitespace "-space-before-tab" &&
292 echo " foo ();" > x &&
293 git diff --check
297 test_expect_success 'check space before tab in indent (space-before-tab: on)' '
299 # indent contains space followed by HT
300 git config core.whitespace "space-before-tab" &&
301 echo " foo (); " > x &&
302 test_must_fail git diff --check
306 test_expect_success 'check spaces as indentation (indent-with-non-tab: off)' '
308 git config core.whitespace "-indent-with-non-tab"
309 echo " foo ();" > x &&
310 git diff --check
314 test_expect_success 'check spaces as indentation (indent-with-non-tab: on)' '
316 git config core.whitespace "indent-with-non-tab" &&
317 echo " foo ();" > x &&
318 test_must_fail git diff --check
322 test_expect_success 'check tabs and spaces as indentation (indent-with-non-tab: on)' '
324 git config core.whitespace "indent-with-non-tab" &&
325 echo " foo ();" > x &&
326 test_must_fail git diff --check
330 test_expect_success 'line numbers in --check output are correct' '
332 echo "" > x &&
333 echo "foo(); " >> x &&
334 git diff --check | grep "x:2:"
338 test_expect_success 'checkdiff detects new trailing blank lines (1)' '
339 echo "foo();" >x &&
340 echo "" >>x &&
341 git diff --check | grep "new blank line"
344 test_expect_success 'checkdiff detects new trailing blank lines (2)' '
345 { echo a; echo b; echo; echo; } >x &&
346 git add x &&
347 { echo a; echo; echo; echo; echo; } >x &&
348 git diff --check | grep "new blank line"
351 test_expect_success 'checkdiff allows new blank lines' '
352 git checkout x &&
353 mv x y &&
355 echo "/* This is new */" &&
356 echo "" &&
357 cat y
358 ) >x &&
359 git diff --check
362 test_expect_success 'combined diff with autocrlf conversion' '
364 git reset --hard &&
365 echo >x hello &&
366 git commit -m "one side" x &&
367 git checkout HEAD^ &&
368 echo >x goodbye &&
369 git commit -m "the other side" x &&
370 git config core.autocrlf true &&
371 test_must_fail git merge master &&
373 git diff | sed -e "1,/^@@@/d" >actual &&
374 ! grep "^-" actual
378 test_done