3 # Copyright (c) 2006 Johannes E. Schindelin
6 test_description
='Test special whitespace in diff engine.
10 .
"$TEST_DIRECTORY"/diff-lib.sh
12 # Ray Lehtiniemi's example
20 git update-index
--add x
32 index adf3937..6edc172 100644
46 test_expect_success
"Ray's example without options" 'test_cmp expect out'
49 test_expect_success
"Ray's example with -w" 'test_cmp expect out'
52 test_expect_success
"Ray's example with -b" 'test_cmp expect out'
54 tr 'Q' '\015' << EOF > x
55 whitespace at beginning
57 whitespace in the middle
66 whitespace at beginning
68 white space in the middle
74 tr 'Q_' '\015 ' << EOF > expect
76 index d99af23..8b32fb5 100644
80 -whitespace at beginning
82 -whitespace in the middle
84 + whitespace at beginning
86 +white space in the middle
93 test_expect_success
'another test, without options' 'test_cmp expect out'
98 test_expect_success
'another test, with -w' 'test_cmp expect 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
108 index d99af23..8b32fb5 100644
112 -whitespace at beginning
113 + whitespace at beginning
115 -whitespace in the middle
116 +white space in the middle
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
128 index d99af23..8b32fb5 100644
132 -whitespace at beginning
134 -whitespace in the middle
135 + whitespace at beginning
137 +white space in the middle
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" &&
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' '
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' '
201 git diff --cached --check
205 test_expect_success
'check staged with trailing whitespace' '
207 echo "foo(); " > 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 &&
218 test_must_fail git diff --cached --check
222 test_expect_success
'check with no whitespace errors (diff-index)' '
226 git diff-index --check HEAD
230 test_expect_success
'check with trailing whitespace (diff-index)' '
232 echo "foo(); " > 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 &&
243 test_must_fail git diff-index --check HEAD
247 test_expect_success
'check staged with no whitespace errors (diff-index)' '
251 git diff-index --cached --check HEAD
255 test_expect_success
'check staged with trailing whitespace (diff-index)' '
257 echo "foo(); " > 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 &&
268 test_must_fail git diff-index --cached --check HEAD
272 test_expect_success
'check with no whitespace errors (diff-tree)' '
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 &&
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 &&
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 &&
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
'ditto, but tabwidth=9' '
349 git config core.whitespace "indent-with-non-tab,tabwidth=9" &&
354 test_expect_success
'check tabs and spaces as indentation (indent-with-non-tab: on)' '
356 git config core.whitespace "indent-with-non-tab" &&
357 echo " foo ();" > x &&
358 test_must_fail git diff --check
362 test_expect_success
'ditto, but tabwidth=10' '
364 git config core.whitespace "indent-with-non-tab,tabwidth=10" &&
365 test_must_fail git diff --check
369 test_expect_success
'ditto, but tabwidth=20' '
371 git config core.whitespace "indent-with-non-tab,tabwidth=20" &&
376 test_expect_success
'check tabs as indentation (tab-in-indent: off)' '
378 git config core.whitespace "-tab-in-indent" &&
379 echo " foo ();" > x &&
384 test_expect_success
'check tabs as indentation (tab-in-indent: on)' '
386 git config core.whitespace "tab-in-indent" &&
387 echo " foo ();" > x &&
388 test_must_fail git diff --check
392 test_expect_success
'check tabs and spaces as indentation (tab-in-indent: on)' '
394 git config core.whitespace "tab-in-indent" &&
395 echo " foo ();" > x &&
396 test_must_fail git diff --check
400 test_expect_success
'ditto, but tabwidth=1 (must be irrelevant)' '
402 git config core.whitespace "tab-in-indent,tabwidth=1" &&
403 test_must_fail git diff --check
407 test_expect_success
'check tab-in-indent and indent-with-non-tab conflict' '
409 git config core.whitespace "tab-in-indent,indent-with-non-tab" &&
410 echo "foo ();" > x &&
411 test_must_fail git diff --check
415 test_expect_success
'check tab-in-indent excluded from wildcard whitespace attribute' '
417 git config --unset core.whitespace &&
418 echo "x whitespace" > .gitattributes &&
419 echo " foo ();" > x &&
425 test_expect_success
'line numbers in --check output are correct' '
428 echo "foo(); " >> x &&
429 git diff --check | grep "x:2:"
433 test_expect_success
'checkdiff detects new trailing blank lines (1)' '
436 git diff --check | grep "new blank line"
439 test_expect_success
'checkdiff detects new trailing blank lines (2)' '
440 { echo a; echo b; echo; echo; } >x &&
442 { echo a; echo; echo; echo; echo; } >x &&
443 git diff --check | grep "new blank line"
446 test_expect_success
'checkdiff allows new blank lines' '
450 echo "/* This is new */" &&
459 test_expect_success
'whitespace-only changes not reported' '
461 echo >x "hello world" &&
463 git commit -m "hello 1" &&
464 echo >x "hello world" &&
465 git diff -b >actual &&
466 test_cmp expect actual
471 similarity index NUM%
474 index 380c32a..a97b785 100644
476 test_expect_success
'whitespace-only changes reported across renames' '
478 for i in 1 2 3 4 5 6 7 8 9; do echo "$i$i$i$i$i$i"; done >x &&
480 git commit -m "base" &&
481 sed -e "5s/^/ /" x >z &&
484 git diff -w -M --cached |
485 sed -e "/^similarity index /s/[0-9][0-9]*/NUM/" >actual &&
486 test_cmp expect actual
490 diff --git a
/empty b
/void
491 similarity index
100%
496 test_expect_success
'rename empty' '
500 git commit -m empty &&
502 git diff -w --cached -M >current &&
503 test_cmp expected current
506 test_expect_success
'combined diff with autocrlf conversion' '
510 git commit -m "one side" x &&
511 git checkout HEAD^ &&
513 git commit -m "the other side" x &&
514 git config core.autocrlf true &&
515 test_must_fail git merge master &&
517 git diff | sed -e "1,/^@@@/d" >actual &&
522 # Start testing the colored format for whitespace checks
524 test_expect_success
'setup diff colors' '
525 git config color.diff always &&
526 git config color.diff.plain normal &&
527 git config color.diff.meta bold &&
528 git config color.diff.frag cyan &&
529 git config color.diff.func normal &&
530 git config color.diff.old red &&
531 git config color.diff.new green &&
532 git config color.diff.commit yellow &&
533 git config color.diff.whitespace "normal red" &&
535 git config core.autocrlf false
538 <BOLD
>diff --git a
/x b
/x
<RESET
>
539 <BOLD
>index
9daeafb.
.2874b91
100644<RESET
>
542 <CYAN
>@@
-1 +1,4 @@
<RESET
>
544 <GREEN
>+<RESET
><GREEN
>{<RESET
>
545 <GREEN
>+<RESET
><BRED
> <RESET
>
546 <GREEN
>+<RESET
><GREEN
>}<RESET
>
549 test_expect_success
'diff that introduces a line with only tabs' '
550 git config core.whitespace blank-at-eol &&
553 git commit -m "initial" x &&
554 echo "{NTN}" | tr "NT" "\n\t" >> x &&
555 git -c color.diff=always diff | test_decode_color >current &&
556 test_cmp expected current