1#!/bin/sh
   2test_description='word diff colors'
   4. ./test-lib.sh
   6test_expect_success setup '
   8        git config diff.color.old red
  10        git config diff.color.new green
  11'
  13decrypt_color () {
  15        sed \
  16                -e 's/.\[1m/<WHITE>/g' \
  17                -e 's/.\[31m/<RED>/g' \
  18                -e 's/.\[32m/<GREEN>/g' \
  19                -e 's/.\[36m/<BROWN>/g' \
  20                -e 's/.\[m/<RESET>/g'
  21}
  22word_diff () {
  24        test_must_fail git diff --no-index "$@" pre post > output &&
  25        decrypt_color < output > output.decrypted &&
  26        test_cmp expect output.decrypted
  27}
  28cat > pre <<\EOF
  30h(4)
  31a = b + c
  33EOF
  34cat > post <<\EOF
  36h(4),hh[44]
  37a = b + c
  39aa = a
  41aeff = aeff * ( aaa )
  43EOF
  44cat > expect <<\EOF
  46<WHITE>diff --git a/pre b/post<RESET>
  47<WHITE>index 330b04f..5ed8eff 100644<RESET>
  48<WHITE>--- a/pre<RESET>
  49<WHITE>+++ b/post<RESET>
  50<BROWN>@@ -1,3 +1,7 @@<RESET>
  51<RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
  52<RESET>
  53a = b + c<RESET>
  54<GREEN>aa = a<RESET>
  56<GREEN>aeff = aeff * ( aaa )<RESET>
  58EOF
  59test_expect_success 'word diff with runs of whitespace' '
  61        word_diff --color-words
  63'
  65cat > expect <<\EOF
  67<WHITE>diff --git a/pre b/post<RESET>
  68<WHITE>index 330b04f..5ed8eff 100644<RESET>
  69<WHITE>--- a/pre<RESET>
  70<WHITE>+++ b/post<RESET>
  71<BROWN>@@ -1,3 +1,7 @@<RESET>
  72h(4),<GREEN>hh<RESET>[44]
  73<RESET>
  74a = b + c<RESET>
  75<GREEN>aa = a<RESET>
  77<GREEN>aeff = aeff * ( aaa<RESET> )
  79EOF
  80cp expect expect.letter-runs-are-words
  81test_expect_success 'word diff with a regular expression' '
  83        word_diff --color-words="[a-z]+"
  85'
  87test_expect_success 'set a diff driver' '
  89        git config diff.testdriver.wordRegex "[^[:space:]]" &&
  90        cat <<EOF > .gitattributes
  91pre diff=testdriver
  92post diff=testdriver
  93EOF
  94'
  95test_expect_success 'option overrides .gitattributes' '
  97        word_diff --color-words="[a-z]+"
  99'
 101cat > expect <<\EOF
 103<WHITE>diff --git a/pre b/post<RESET>
 104<WHITE>index 330b04f..5ed8eff 100644<RESET>
 105<WHITE>--- a/pre<RESET>
 106<WHITE>+++ b/post<RESET>
 107<BROWN>@@ -1,3 +1,7 @@<RESET>
 108h(4)<GREEN>,hh[44]<RESET>
 109<RESET>
 110a = b + c<RESET>
 111<GREEN>aa = a<RESET>
 113<GREEN>aeff = aeff * ( aaa )<RESET>
 115EOF
 116cp expect expect.non-whitespace-is-word
 117test_expect_success 'use regex supplied by driver' '
 119        word_diff --color-words
 121'
 123test_expect_success 'set diff.wordRegex option' '
 125        git config diff.wordRegex "[[:alnum:]]+"
 126'
 127cp expect.letter-runs-are-words expect
 129test_expect_success 'command-line overrides config' '
 131        word_diff --color-words="[a-z]+"
 132'
 133cp expect.non-whitespace-is-word expect
 135test_expect_success '.gitattributes override config' '
 137        word_diff --color-words
 138'
 139test_expect_success 'remove diff driver regex' '
 141        git config --unset diff.testdriver.wordRegex
 142'
 143cat > expect <<\EOF
 145<WHITE>diff --git a/pre b/post<RESET>
 146<WHITE>index 330b04f..5ed8eff 100644<RESET>
 147<WHITE>--- a/pre<RESET>
 148<WHITE>+++ b/post<RESET>
 149<BROWN>@@ -1,3 +1,7 @@<RESET>
 150h(4),<GREEN>hh[44<RESET>]
 151<RESET>
 152a = b + c<RESET>
 153<GREEN>aa = a<RESET>
 155<GREEN>aeff = aeff * ( aaa<RESET> )
 157EOF
 158test_expect_success 'use configured regex' '
 160        word_diff --color-words
 161'
 162echo 'aaa (aaa)' > pre
 164echo 'aaa (aaa) aaa' > post
 165cat > expect <<\EOF
 167<WHITE>diff --git a/pre b/post<RESET>
 168<WHITE>index c29453b..be22f37 100644<RESET>
 169<WHITE>--- a/pre<RESET>
 170<WHITE>+++ b/post<RESET>
 171<BROWN>@@ -1 +1 @@<RESET>
 172aaa (aaa) <GREEN>aaa<RESET>
 173EOF
 174test_expect_success 'test parsing words for newline' '
 176        word_diff --color-words="a+"
 178'
 181echo '(:' > pre
 183echo '(' > post
 184cat > expect <<\EOF
 186<WHITE>diff --git a/pre b/post<RESET>
 187<WHITE>index 289cb9d..2d06f37 100644<RESET>
 188<WHITE>--- a/pre<RESET>
 189<WHITE>+++ b/post<RESET>
 190<BROWN>@@ -1 +1 @@<RESET>
 191(<RED>:<RESET>
 192EOF
 193test_expect_success 'test when words are only removed at the end' '
 195        word_diff --color-words=.
 197'
 199test_done