1#!/bin/sh
   2#
   3# Copyright (c) 2006 Johannes E. Schindelin
   4#
   5test_description='Test special whitespace in diff engine.
   7'
   9. ./test-lib.sh
  10. "$TEST_DIRECTORY"/diff-lib.sh
  11# Ray Lehtiniemi's example
  13cat << EOF > x
  15do {
  16   nothing;
  17} while (0);
  18EOF
  19git update-index --add x
  21cat << EOF > x
  23do
  24{
  25   nothing;
  26}
  27while (0);
  28EOF
  29cat << 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
  44git diff > out
  46test_expect_success "Ray's example without options" 'test_cmp expect out'
  47git diff -w > out
  49test_expect_success "Ray's example with -w" 'test_cmp expect out'
  50git diff -b > out
  52test_expect_success "Ray's example with -b" 'test_cmp expect out'
  53tr 'Q' '\015' << EOF > x
  55whitespace at beginning
  56whitespace change
  57whitespace in the middle
  58whitespace at end
  59unchanged line
  60CR at endQ
  61EOF
  62git update-index x
  64tr '_' ' ' << EOF > x
  66        whitespace at beginning
  67whitespace       change
  68white space in the middle
  69whitespace at end__
  70unchanged line
  71CR at end
  72EOF
  73tr 'Q_' '\015 ' << EOF > expect
  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
  87+whitespace at end__
  88 unchanged line
  89-CR at endQ
  90+CR at end
  91EOF
  92git diff > out
  93test_expect_success 'another test, without options' 'test_cmp expect out'
  94cat << EOF > expect
  96EOF
  97git diff -w > out
  98test_expect_success 'another test, with -w' 'test_cmp expect out'
  99git diff -w -b > out
 100test_expect_success 'another test, with -w -b' 'test_cmp expect out'
 101git diff -w --ignore-space-at-eol > out
 102test_expect_success 'another test, with -w --ignore-space-at-eol' 'test_cmp expect out'
 103git diff -w -b --ignore-space-at-eol > out
 104test_expect_success 'another test, with -w -b --ignore-space-at-eol' 'test_cmp expect out'
 105tr 'Q_' '\015 ' << EOF > expect
 107diff --git a/x b/x
 108index 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 end
 120EOF
 121git diff -b > out
 122test_expect_success 'another test, with -b' 'test_cmp expect out'
 123git diff -b --ignore-space-at-eol > out
 124test_expect_success 'another test, with -b --ignore-space-at-eol' 'test_cmp expect out'
 125tr 'Q_' '\015 ' << EOF > expect
 127diff --git a/x b/x
 128index 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 end
 141EOF
 142git diff --ignore-space-at-eol > out
 143test_expect_success 'another test, with --ignore-space-at-eol' 'test_cmp expect out'
 144test_expect_success 'check mixed spaces and tabs in indent' '
 146        # This is indented with SP HT SP.
 148        echo "   foo();" > x &&
 149        git diff --check | grep "space before tab in indent"
 150'
 152test_expect_success 'check mixed tabs and spaces in indent' '
 154        # This is indented with HT SP HT.
 156        echo "          foo();" > x &&
 157        git diff --check | grep "space before tab in indent"
 158'
 160test_expect_success 'check with no whitespace errors' '
 162        git commit -m "snapshot" &&
 164        echo "foo();" > x &&
 165        git diff --check
 166'
 168test_expect_success 'check with trailing whitespace' '
 170        echo "foo(); " > x &&
 172        test_must_fail git diff --check
 173'
 175test_expect_success 'check with space before tab in indent' '
 177        # indent has space followed by hard tab
 179        echo "  foo();" > x &&
 180        test_must_fail git diff --check
 181'
 183test_expect_success '--check and --exit-code are not exclusive' '
 185        git checkout x &&
 187        git diff --check --exit-code
 188'
 190test_expect_success '--check and --quiet are not exclusive' '
 192        git diff --check --quiet
 194'
 196test_expect_success 'check staged with no whitespace errors' '
 198        echo "foo();" > x &&
 200        git add x &&
 201        git diff --cached --check
 202'
 204test_expect_success 'check staged with trailing whitespace' '
 206        echo "foo(); " > x &&
 208        git add x &&
 209        test_must_fail git diff --cached --check
 210'
 212test_expect_success 'check staged with space before tab in indent' '
 214        # indent has space followed by hard tab
 216        echo "  foo();" > x &&
 217        git add x &&
 218        test_must_fail git diff --cached --check
 219'
 221test_expect_success 'check with no whitespace errors (diff-index)' '
 223        echo "foo();" > x &&
 225        git add x &&
 226        git diff-index --check HEAD
 227'
 229test_expect_success 'check with trailing whitespace (diff-index)' '
 231        echo "foo(); " > x &&
 233        git add x &&
 234        test_must_fail git diff-index --check HEAD
 235'
 237test_expect_success 'check with space before tab in indent (diff-index)' '
 239        # indent has space followed by hard tab
 241        echo "  foo();" > x &&
 242        git add x &&
 243        test_must_fail git diff-index --check HEAD
 244'
 246test_expect_success 'check staged with no whitespace errors (diff-index)' '
 248        echo "foo();" > x &&
 250        git add x &&
 251        git diff-index --cached --check HEAD
 252'
 254test_expect_success 'check staged with trailing whitespace (diff-index)' '
 256        echo "foo(); " > x &&
 258        git add x &&
 259        test_must_fail git diff-index --cached --check HEAD
 260'
 262test_expect_success 'check staged with space before tab in indent (diff-index)' '
 264        # 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
 269'
 271test_expect_success 'check with no whitespace errors (diff-tree)' '
 273        echo "foo();" > x &&
 275        git commit -m "new commit" x &&
 276        git diff-tree --check HEAD^ HEAD
 277'
 279test_expect_success 'check with trailing whitespace (diff-tree)' '
 281        echo "foo(); " > x &&
 283        git commit -m "another commit" x &&
 284        test_must_fail git diff-tree --check HEAD^ HEAD
 285'
 287test_expect_success 'check with space before tab in indent (diff-tree)' '
 289        # 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
 294'
 296test_expect_success 'check trailing whitespace (trailing-space: off)' '
 298        git config core.whitespace "-trailing-space" &&
 300        echo "foo ();   " > x &&
 301        git diff --check
 302'
 304test_expect_success 'check trailing whitespace (trailing-space: on)' '
 306        git config core.whitespace "trailing-space" &&
 308        echo "foo ();   " > x &&
 309        test_must_fail git diff --check
 310'
 312test_expect_success 'check space before tab in indent (space-before-tab: off)' '
 314        # indent contains space followed by HT
 316        git config core.whitespace "-space-before-tab" &&
 317        echo "  foo ();" > x &&
 318        git diff --check
 319'
 321test_expect_success 'check space before tab in indent (space-before-tab: on)' '
 323        # 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
 328'
 330test_expect_success 'check spaces as indentation (indent-with-non-tab: off)' '
 332        git config core.whitespace "-indent-with-non-tab" &&
 334        echo "        foo ();" > x &&
 335        git diff --check
 336'
 338test_expect_success 'check spaces as indentation (indent-with-non-tab: on)' '
 340        git config core.whitespace "indent-with-non-tab" &&
 342        echo "        foo ();" > x &&
 343        test_must_fail git diff --check
 344'
 346test_expect_success 'ditto, but tabwidth=9' '
 348        git config core.whitespace "indent-with-non-tab,tabwidth=9" &&
 350        git diff --check
 351'
 353test_expect_success 'check tabs and spaces as indentation (indent-with-non-tab: on)' '
 355        git config core.whitespace "indent-with-non-tab" &&
 357        echo "                  foo ();" > x &&
 358        test_must_fail git diff --check
 359'
 361test_expect_success 'ditto, but tabwidth=10' '
 363        git config core.whitespace "indent-with-non-tab,tabwidth=10" &&
 365        test_must_fail git diff --check
 366'
 368test_expect_success 'ditto, but tabwidth=20' '
 370        git config core.whitespace "indent-with-non-tab,tabwidth=20" &&
 372        git diff --check
 373'
 375test_expect_success 'check tabs as indentation (tab-in-indent: off)' '
 377        git config core.whitespace "-tab-in-indent" &&
 379        echo "  foo ();" > x &&
 380        git diff --check
 381'
 383test_expect_success 'check tabs as indentation (tab-in-indent: on)' '
 385        git config core.whitespace "tab-in-indent" &&
 387        echo "  foo ();" > x &&
 388        test_must_fail git diff --check
 389'
 391test_expect_success 'check tabs and spaces as indentation (tab-in-indent: on)' '
 393        git config core.whitespace "tab-in-indent" &&
 395        echo "                  foo ();" > x &&
 396        test_must_fail git diff --check
 397'
 399test_expect_success 'ditto, but tabwidth=1 (must be irrelevant)' '
 401        git config core.whitespace "tab-in-indent,tabwidth=1" &&
 403        test_must_fail git diff --check
 404'
 406test_expect_success 'check tab-in-indent and indent-with-non-tab conflict' '
 408        git config core.whitespace "tab-in-indent,indent-with-non-tab" &&
 410        echo "foo ();" > x &&
 411        test_must_fail git diff --check
 412'
 414test_expect_success 'check tab-in-indent excluded from wildcard whitespace attribute' '
 416        git config --unset core.whitespace &&
 418        echo "x whitespace" > .gitattributes &&
 419        echo "    foo ();" > x &&
 420        git diff --check &&
 421        rm -f .gitattributes
 422'
 424test_expect_success 'line numbers in --check output are correct' '
 426        echo "" > x &&
 428        echo "foo(); " >> x &&
 429        git diff --check | grep "x:2:"
 430'
 432test_expect_success 'checkdiff detects new trailing blank lines (1)' '
 434        echo "foo();" >x &&
 435        echo "" >>x &&
 436        git diff --check | grep "new blank line"
 437'
 438test_expect_success 'checkdiff detects new trailing blank lines (2)' '
 440        { echo a; echo b; echo; echo; } >x &&
 441        git add x &&
 442        { echo a; echo; echo; echo; echo; } >x &&
 443        git diff --check | grep "new blank line"
 444'
 445test_expect_success 'checkdiff allows new blank lines' '
 447        git checkout x &&
 448        mv x y &&
 449        (
 450                echo "/* This is new */" &&
 451                echo "" &&
 452                cat y
 453        ) >x &&
 454        git diff --check
 455'
 456cat <<EOF >expect
 458EOF
 459test_expect_success 'whitespace-only changes not reported' '
 460        git reset --hard &&
 461        echo >x "hello world" &&
 462        git add x &&
 463        git commit -m "hello 1" &&
 464        echo >x "hello  world" &&
 465        git diff -b >actual &&
 466        test_cmp expect actual
 467'
 468cat <<EOF >expect
 470diff --git a/x b/z
 471similarity index NUM%
 472rename from x
 473rename to z
 474index 380c32a..a97b785 100644
 475EOF
 476test_expect_success 'whitespace-only changes reported across renames' '
 477        git reset --hard &&
 478        for i in 1 2 3 4 5 6 7 8 9; do echo "$i$i$i$i$i$i"; done >x &&
 479        git add x &&
 480        git commit -m "base" &&
 481        sed -e "5s/^/ /" x >z &&
 482        git rm x &&
 483        git add z &&
 484        git diff -w -M --cached |
 485        sed -e "/^similarity index /s/[0-9][0-9]*/NUM/" >actual &&
 486        test_cmp expect actual
 487'
 488cat >expected <<\EOF
 490diff --git a/empty b/void
 491similarity index 100%
 492rename from empty
 493rename to void
 494EOF
 495test_expect_success 'rename empty' '
 497        git reset --hard &&
 498        >empty &&
 499        git add empty &&
 500        git commit -m empty &&
 501        git mv empty void &&
 502        git diff -w --cached -M >current &&
 503        test_cmp expected current
 504'
 505test_expect_success 'combined diff with autocrlf conversion' '
 507        git reset --hard &&
 509        echo >x hello &&
 510        git commit -m "one side" x &&
 511        git checkout HEAD^ &&
 512        echo >x goodbye &&
 513        git commit -m "the other side" x &&
 514        git config core.autocrlf true &&
 515        test_must_fail git merge master &&
 516        git diff | sed -e "1,/^@@@/d" >actual &&
 518        ! grep "^-" actual
 519'
 521# Start testing the colored format for whitespace checks
 523test_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" &&
 534        git config core.autocrlf false
 536'
 537cat >expected <<\EOF
 538<BOLD>diff --git a/x b/x<RESET>
 539<BOLD>index 9daeafb..2874b91 100644<RESET>
 540<BOLD>--- a/x<RESET>
 541<BOLD>+++ b/x<RESET>
 542<CYAN>@@ -1 +1,4 @@<RESET>
 543 test<RESET>
 544<GREEN>+<RESET><GREEN>{<RESET>
 545<GREEN>+<RESET><BRED>   <RESET>
 546<GREEN>+<RESET><GREEN>}<RESET>
 547EOF
 548test_expect_success 'diff that introduces a line with only tabs' '
 550        git config core.whitespace blank-at-eol &&
 551        git reset --hard &&
 552        echo "test" > x &&
 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
 557'
 558test_done