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. ../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'
 101tr 'Q' '\015' << EOF > expect
 103diff --git a/x b/x
 104index 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
 116EOF
 117git diff -b > out
 118test_expect_success 'another test, with -b' 'test_cmp expect out'
 119test_expect_success 'check mixed spaces and tabs in indent' '
 121        # This is indented with SP HT SP.
 123        echo "   foo();" > x &&
 124        git diff --check | grep "space before tab in indent"
 125'
 127test_expect_success 'check mixed tabs and spaces in indent' '
 129        # This is indented with HT SP HT.
 131        echo "          foo();" > x &&
 132        git diff --check | grep "space before tab in indent"
 133'
 135test_expect_success 'check with no whitespace errors' '
 137        git commit -m "snapshot" &&
 139        echo "foo();" > x &&
 140        git diff --check
 141'
 143test_expect_success 'check with trailing whitespace' '
 145        echo "foo(); " > x &&
 147        test_must_fail git diff --check
 148'
 150test_expect_success 'check with space before tab in indent' '
 152        # indent has space followed by hard tab
 154        echo "  foo();" > x &&
 155        test_must_fail git diff --check
 156'
 158test_expect_success '--check and --exit-code are not exclusive' '
 160        git checkout x &&
 162        git diff --check --exit-code
 163'
 165test_expect_success '--check and --quiet are not exclusive' '
 167        git diff --check --quiet
 169'
 171test_expect_success 'check staged with no whitespace errors' '
 173        echo "foo();" > x &&
 175        git add x &&
 176        git diff --cached --check
 177'
 179test_expect_success 'check staged with trailing whitespace' '
 181        echo "foo(); " > x &&
 183        git add x &&
 184        test_must_fail git diff --cached --check
 185'
 187test_expect_success 'check staged with space before tab in indent' '
 189        # indent has space followed by hard tab
 191        echo "  foo();" > x &&
 192        git add x &&
 193        test_must_fail git diff --cached --check
 194'
 196test_expect_success 'check with no whitespace errors (diff-index)' '
 198        echo "foo();" > x &&
 200        git add x &&
 201        git diff-index --check HEAD
 202'
 204test_expect_success 'check with trailing whitespace (diff-index)' '
 206        echo "foo(); " > x &&
 208        git add x &&
 209        test_must_fail git diff-index --check HEAD
 210'
 212test_expect_success 'check with space before tab in indent (diff-index)' '
 214        # indent has space followed by hard tab
 216        echo "  foo();" > x &&
 217        git add x &&
 218        test_must_fail git diff-index --check HEAD
 219'
 221test_expect_success 'check staged with no whitespace errors (diff-index)' '
 223        echo "foo();" > x &&
 225        git add x &&
 226        git diff-index --cached --check HEAD
 227'
 229test_expect_success 'check staged with trailing whitespace (diff-index)' '
 231        echo "foo(); " > x &&
 233        git add x &&
 234        test_must_fail git diff-index --cached --check HEAD
 235'
 237test_expect_success 'check staged 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 --cached --check HEAD
 244'
 246test_expect_success 'check with no whitespace errors (diff-tree)' '
 248        echo "foo();" > x &&
 250        git commit -m "new commit" x &&
 251        git diff-tree --check HEAD^ HEAD
 252'
 254test_expect_success 'check with trailing whitespace (diff-tree)' '
 256        echo "foo(); " > x &&
 258        git commit -m "another commit" x &&
 259        test_must_fail git diff-tree --check HEAD^ HEAD
 260'
 262test_expect_success 'check with space before tab in indent (diff-tree)' '
 264        # 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
 269'
 271test_expect_success 'check trailing whitespace (trailing-space: off)' '
 273        git config core.whitespace "-trailing-space" &&
 275        echo "foo ();   " > x &&
 276        git diff --check
 277'
 279test_expect_success 'check trailing whitespace (trailing-space: on)' '
 281        git config core.whitespace "trailing-space" &&
 283        echo "foo ();   " > x &&
 284        test_must_fail git diff --check
 285'
 287test_expect_success 'check space before tab in indent (space-before-tab: off)' '
 289        # indent contains space followed by HT
 291        git config core.whitespace "-space-before-tab" &&
 292        echo "  foo ();" > x &&
 293        git diff --check
 294'
 296test_expect_success 'check space before tab in indent (space-before-tab: on)' '
 298        # 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
 303'
 305test_expect_success 'check spaces as indentation (indent-with-non-tab: off)' '
 307        git config core.whitespace "-indent-with-non-tab"
 309        echo "        foo ();" > x &&
 310        git diff --check
 311'
 313test_expect_success 'check spaces as indentation (indent-with-non-tab: on)' '
 315        git config core.whitespace "indent-with-non-tab" &&
 317        echo "        foo ();" > x &&
 318        test_must_fail git diff --check
 319'
 321test_expect_success 'check tabs and spaces as indentation (indent-with-non-tab: on)' '
 323        git config core.whitespace "indent-with-non-tab" &&
 325        echo "                  foo ();" > x &&
 326        test_must_fail git diff --check
 327'
 329test_expect_success 'line numbers in --check output are correct' '
 331        echo "" > x &&
 333        echo "foo(); " >> x &&
 334        git diff --check | grep "x:2:"
 335'
 337test_expect_success 'checkdiff detects trailing blank lines' '
 339        echo "foo();" >x &&
 340        echo "" >>x &&
 341        git diff --check | grep "ends with blank"
 342'
 343test_done