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
  96diff --git a/x b/x
  97index d99af23..8b32fb5 100644
  98EOF
  99git diff -w > out
 100test_expect_success 'another test, with -w' 'test_cmp expect out'
 101git diff -w -b > out
 102test_expect_success 'another test, with -w -b' 'test_cmp expect out'
 103git diff -w --ignore-space-at-eol > out
 104test_expect_success 'another test, with -w --ignore-space-at-eol' 'test_cmp expect out'
 105git diff -w -b --ignore-space-at-eol > out
 106test_expect_success 'another test, with -w -b --ignore-space-at-eol' 'test_cmp expect out'
 107tr 'Q' '\015' << EOF > expect
 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
 118+white space in the middle
 119 whitespace at end
 120 unchanged line
 121 CR at endQ
 122EOF
 123git diff -b > out
 124test_expect_success 'another test, with -b' 'test_cmp expect out'
 125git diff -b --ignore-space-at-eol > out
 126test_expect_success 'another test, with -b --ignore-space-at-eol' 'test_cmp expect out'
 127tr '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'
 146test_expect_success 'check mixed spaces and tabs in indent' '
 148        # This is indented with SP HT SP.
 150        echo "   foo();" > x &&
 151        git diff --check | grep "space before tab in indent"
 152'
 154test_expect_success 'check mixed tabs and spaces in indent' '
 156        # This is indented with HT SP HT.
 158        echo "          foo();" > x &&
 159        git diff --check | grep "space before tab in indent"
 160'
 162test_expect_success 'check with no whitespace errors' '
 164        git commit -m "snapshot" &&
 166        echo "foo();" > x &&
 167        git diff --check
 168'
 170test_expect_success 'check with trailing whitespace' '
 172        echo "foo(); " > x &&
 174        test_must_fail git diff --check
 175'
 177test_expect_success 'check with space before tab in indent' '
 179        # indent has space followed by hard tab
 181        echo "  foo();" > x &&
 182        test_must_fail git diff --check
 183'
 185test_expect_success '--check and --exit-code are not exclusive' '
 187        git checkout x &&
 189        git diff --check --exit-code
 190'
 192test_expect_success '--check and --quiet are not exclusive' '
 194        git diff --check --quiet
 196'
 198test_expect_success 'check staged with no whitespace errors' '
 200        echo "foo();" > x &&
 202        git add x &&
 203        git diff --cached --check
 204'
 206test_expect_success 'check staged with trailing whitespace' '
 208        echo "foo(); " > x &&
 210        git add x &&
 211        test_must_fail git diff --cached --check
 212'
 214test_expect_success 'check staged with space before tab in indent' '
 216        # indent has space followed by hard tab
 218        echo "  foo();" > x &&
 219        git add x &&
 220        test_must_fail git diff --cached --check
 221'
 223test_expect_success 'check with no whitespace errors (diff-index)' '
 225        echo "foo();" > x &&
 227        git add x &&
 228        git diff-index --check HEAD
 229'
 231test_expect_success 'check with trailing whitespace (diff-index)' '
 233        echo "foo(); " > x &&
 235        git add x &&
 236        test_must_fail git diff-index --check HEAD
 237'
 239test_expect_success 'check with space before tab in indent (diff-index)' '
 241        # indent has space followed by hard tab
 243        echo "  foo();" > x &&
 244        git add x &&
 245        test_must_fail git diff-index --check HEAD
 246'
 248test_expect_success 'check staged with no whitespace errors (diff-index)' '
 250        echo "foo();" > x &&
 252        git add x &&
 253        git diff-index --cached --check HEAD
 254'
 256test_expect_success 'check staged with trailing whitespace (diff-index)' '
 258        echo "foo(); " > x &&
 260        git add x &&
 261        test_must_fail git diff-index --cached --check HEAD
 262'
 264test_expect_success 'check staged with space before tab in indent (diff-index)' '
 266        # indent has space followed by hard tab
 268        echo "  foo();" > x &&
 269        git add x &&
 270        test_must_fail git diff-index --cached --check HEAD
 271'
 273test_expect_success 'check with no whitespace errors (diff-tree)' '
 275        echo "foo();" > x &&
 277        git commit -m "new commit" x &&
 278        git diff-tree --check HEAD^ HEAD
 279'
 281test_expect_success 'check with trailing whitespace (diff-tree)' '
 283        echo "foo(); " > x &&
 285        git commit -m "another commit" x &&
 286        test_must_fail git diff-tree --check HEAD^ HEAD
 287'
 289test_expect_success 'check with space before tab in indent (diff-tree)' '
 291        # indent has space followed by hard tab
 293        echo "  foo();" > x &&
 294        git commit -m "yet another" x &&
 295        test_must_fail git diff-tree --check HEAD^ HEAD
 296'
 298test_expect_success 'check trailing whitespace (trailing-space: off)' '
 300        git config core.whitespace "-trailing-space" &&
 302        echo "foo ();   " > x &&
 303        git diff --check
 304'
 306test_expect_success 'check trailing whitespace (trailing-space: on)' '
 308        git config core.whitespace "trailing-space" &&
 310        echo "foo ();   " > x &&
 311        test_must_fail git diff --check
 312'
 314test_expect_success 'check space before tab in indent (space-before-tab: off)' '
 316        # indent contains space followed by HT
 318        git config core.whitespace "-space-before-tab" &&
 319        echo "  foo ();" > x &&
 320        git diff --check
 321'
 323test_expect_success 'check space before tab in indent (space-before-tab: on)' '
 325        # indent contains space followed by HT
 327        git config core.whitespace "space-before-tab" &&
 328        echo "  foo ();   " > x &&
 329        test_must_fail git diff --check
 330'
 332test_expect_success 'check spaces as indentation (indent-with-non-tab: off)' '
 334        git config core.whitespace "-indent-with-non-tab"
 336        echo "        foo ();" > x &&
 337        git diff --check
 338'
 340test_expect_success 'check spaces as indentation (indent-with-non-tab: on)' '
 342        git config core.whitespace "indent-with-non-tab" &&
 344        echo "        foo ();" > x &&
 345        test_must_fail git diff --check
 346'
 348test_expect_success 'check tabs and spaces as indentation (indent-with-non-tab: on)' '
 350        git config core.whitespace "indent-with-non-tab" &&
 352        echo "                  foo ();" > x &&
 353        test_must_fail git diff --check
 354'
 356test_expect_success 'line numbers in --check output are correct' '
 358        echo "" > x &&
 360        echo "foo(); " >> x &&
 361        git diff --check | grep "x:2:"
 362'
 364test_expect_success 'checkdiff detects trailing blank lines' '
 366        echo "foo();" >x &&
 367        echo "" >>x &&
 368        git diff --check | grep "ends with blank"
 369'
 370test_expect_success 'checkdiff allows new blank lines' '
 372        git checkout x &&
 373        mv x y &&
 374        (
 375                echo "/* This is new */" &&
 376                echo "" &&
 377                cat y
 378        ) >x &&
 379        git diff --check
 380'
 381test_expect_success 'combined diff with autocrlf conversion' '
 383        git reset --hard &&
 385        echo >x hello &&
 386        git commit -m "one side" x &&
 387        git checkout HEAD^ &&
 388        echo >x goodbye &&
 389        git commit -m "the other side" x &&
 390        git config core.autocrlf true &&
 391        test_must_fail git merge master &&
 392        git diff | sed -e "1,/^@@@/d" >actual &&
 394        ! grep "^-" actual
 395'
 397test_done