t / t4020-diff-external.shon commit built-in rebase --autostash: leave the current branch alone if possible (176f5d9)
   1#!/bin/sh
   2
   3test_description='external diff interface test'
   4
   5. ./test-lib.sh
   6
   7test_expect_success setup '
   8
   9        test_tick &&
  10        echo initial >file &&
  11        git add file &&
  12        git commit -m initial &&
  13
  14        test_tick &&
  15        echo second >file &&
  16        before=$(git hash-object file) &&
  17        before=$(git rev-parse --short $before) &&
  18        git add file &&
  19        git commit -m second &&
  20
  21        test_tick &&
  22        echo third >file
  23'
  24
  25test_expect_success 'GIT_EXTERNAL_DIFF environment' '
  26
  27        GIT_EXTERNAL_DIFF=echo git diff | {
  28                read path oldfile oldhex oldmode newfile newhex newmode &&
  29                test "z$path" = zfile &&
  30                test "z$oldmode" = z100644 &&
  31                test "z$newhex" = "z$ZERO_OID" &&
  32                test "z$newmode" = z100644 &&
  33                oh=$(git rev-parse --verify HEAD:file) &&
  34                test "z$oh" = "z$oldhex"
  35        }
  36
  37'
  38
  39test_expect_success 'GIT_EXTERNAL_DIFF environment should apply only to diff' '
  40
  41        GIT_EXTERNAL_DIFF=echo git log -p -1 HEAD |
  42        grep "^diff --git a/file b/file"
  43
  44'
  45
  46test_expect_success 'GIT_EXTERNAL_DIFF environment and --no-ext-diff' '
  47
  48        GIT_EXTERNAL_DIFF=echo git diff --no-ext-diff |
  49        grep "^diff --git a/file b/file"
  50
  51'
  52
  53test_expect_success SYMLINKS 'typechange diff' '
  54        rm -f file &&
  55        ln -s elif file &&
  56        GIT_EXTERNAL_DIFF=echo git diff  | {
  57                read path oldfile oldhex oldmode newfile newhex newmode &&
  58                test "z$path" = zfile &&
  59                test "z$oldmode" = z100644 &&
  60                test "z$newhex" = "z$ZERO_OID" &&
  61                test "z$newmode" = z120000 &&
  62                oh=$(git rev-parse --verify HEAD:file) &&
  63                test "z$oh" = "z$oldhex"
  64        } &&
  65        GIT_EXTERNAL_DIFF=echo git diff --no-ext-diff >actual &&
  66        git diff >expect &&
  67        test_cmp expect actual
  68'
  69
  70test_expect_success 'diff.external' '
  71        git reset --hard &&
  72        echo third >file &&
  73        test_config diff.external echo &&
  74        git diff | {
  75                read path oldfile oldhex oldmode newfile newhex newmode &&
  76                test "z$path" = zfile &&
  77                test "z$oldmode" = z100644 &&
  78                test "z$newhex" = "z$ZERO_OID" &&
  79                test "z$newmode" = z100644 &&
  80                oh=$(git rev-parse --verify HEAD:file) &&
  81                test "z$oh" = "z$oldhex"
  82        }
  83'
  84
  85test_expect_success 'diff.external should apply only to diff' '
  86        test_config diff.external echo &&
  87        git log -p -1 HEAD |
  88        grep "^diff --git a/file b/file"
  89'
  90
  91test_expect_success 'diff.external and --no-ext-diff' '
  92        test_config diff.external echo &&
  93        git diff --no-ext-diff |
  94        grep "^diff --git a/file b/file"
  95'
  96
  97test_expect_success 'diff attribute' '
  98        git reset --hard &&
  99        echo third >file &&
 100
 101        git config diff.parrot.command echo &&
 102
 103        echo >.gitattributes "file diff=parrot" &&
 104
 105        git diff | {
 106                read path oldfile oldhex oldmode newfile newhex newmode &&
 107                test "z$path" = zfile &&
 108                test "z$oldmode" = z100644 &&
 109                test "z$newhex" = "z$ZERO_OID" &&
 110                test "z$newmode" = z100644 &&
 111                oh=$(git rev-parse --verify HEAD:file) &&
 112                test "z$oh" = "z$oldhex"
 113        }
 114
 115'
 116
 117test_expect_success 'diff attribute should apply only to diff' '
 118
 119        git log -p -1 HEAD |
 120        grep "^diff --git a/file b/file"
 121
 122'
 123
 124test_expect_success 'diff attribute and --no-ext-diff' '
 125
 126        git diff --no-ext-diff |
 127        grep "^diff --git a/file b/file"
 128
 129'
 130
 131test_expect_success 'diff attribute' '
 132
 133        git config --unset diff.parrot.command &&
 134        git config diff.color.command echo &&
 135
 136        echo >.gitattributes "file diff=color" &&
 137
 138        git diff | {
 139                read path oldfile oldhex oldmode newfile newhex newmode &&
 140                test "z$path" = zfile &&
 141                test "z$oldmode" = z100644 &&
 142                test "z$newhex" = "z$ZERO_OID" &&
 143                test "z$newmode" = z100644 &&
 144                oh=$(git rev-parse --verify HEAD:file) &&
 145                test "z$oh" = "z$oldhex"
 146        }
 147
 148'
 149
 150test_expect_success 'diff attribute should apply only to diff' '
 151
 152        git log -p -1 HEAD |
 153        grep "^diff --git a/file b/file"
 154
 155'
 156
 157test_expect_success 'diff attribute and --no-ext-diff' '
 158
 159        git diff --no-ext-diff |
 160        grep "^diff --git a/file b/file"
 161
 162'
 163
 164test_expect_success 'GIT_EXTERNAL_DIFF trumps diff.external' '
 165        >.gitattributes &&
 166        test_config diff.external "echo ext-global" &&
 167        GIT_EXTERNAL_DIFF="echo ext-env" git diff | grep ext-env
 168'
 169
 170test_expect_success 'attributes trump GIT_EXTERNAL_DIFF and diff.external' '
 171        test_config diff.foo.command "echo ext-attribute" &&
 172        test_config diff.external "echo ext-global" &&
 173        echo "file diff=foo" >.gitattributes &&
 174        GIT_EXTERNAL_DIFF="echo ext-env" git diff | grep ext-attribute
 175'
 176
 177test_expect_success 'no diff with -diff' '
 178        echo >.gitattributes "file -diff" &&
 179        git diff | grep Binary
 180'
 181
 182echo NULZbetweenZwords | perl -pe 'y/Z/\000/' > file
 183
 184test_expect_success 'force diff with "diff"' '
 185        after=$(git hash-object file) &&
 186        after=$(git rev-parse --short $after) &&
 187        echo >.gitattributes "file diff" &&
 188        git diff >actual &&
 189        sed -e "s/^index .*/index $before..$after 100644/" \
 190                "$TEST_DIRECTORY"/t4020/diff.NUL >expected-diff &&
 191        test_cmp expected-diff actual
 192'
 193
 194test_expect_success 'GIT_EXTERNAL_DIFF with more than one changed files' '
 195        echo anotherfile > file2 &&
 196        git add file2 &&
 197        git commit -m "added 2nd file" &&
 198        echo modified >file2 &&
 199        GIT_EXTERNAL_DIFF=echo git diff
 200'
 201
 202test_expect_success 'GIT_EXTERNAL_DIFF path counter/total' '
 203        write_script external-diff.sh <<-\EOF &&
 204        echo $GIT_DIFF_PATH_COUNTER of $GIT_DIFF_PATH_TOTAL >>counter.txt
 205        EOF
 206        >counter.txt &&
 207        cat >expect <<-\EOF &&
 208        1 of 2
 209        2 of 2
 210        EOF
 211        GIT_EXTERNAL_DIFF=./external-diff.sh git diff &&
 212        test_cmp expect counter.txt
 213'
 214
 215test_expect_success 'GIT_EXTERNAL_DIFF generates pretty paths' '
 216        touch file.ext &&
 217        git add file.ext &&
 218        echo with extension > file.ext &&
 219        GIT_EXTERNAL_DIFF=echo git diff file.ext | grep ......_file\.ext &&
 220        git update-index --force-remove file.ext &&
 221        rm file.ext
 222'
 223
 224echo "#!$SHELL_PATH" >fake-diff.sh
 225cat >> fake-diff.sh <<\EOF
 226cat $2 >> crlfed.txt
 227EOF
 228chmod a+x fake-diff.sh
 229
 230keep_only_cr () {
 231        tr -dc '\015'
 232}
 233
 234test_expect_success 'external diff with autocrlf = true' '
 235        test_config core.autocrlf true &&
 236        GIT_EXTERNAL_DIFF=./fake-diff.sh git diff &&
 237        test $(wc -l < crlfed.txt) = $(cat crlfed.txt | keep_only_cr | wc -c)
 238'
 239
 240test_expect_success 'diff --cached' '
 241        test_config core.autocrlf true &&
 242        git add file &&
 243        git update-index --assume-unchanged file &&
 244        echo second >file &&
 245        git diff --cached >actual &&
 246        test_cmp expected-diff actual
 247'
 248
 249test_expect_success 'clean up crlf leftovers' '
 250        git update-index --no-assume-unchanged file &&
 251        rm -f file* &&
 252        git reset --hard
 253'
 254
 255test_expect_success 'submodule diff' '
 256        git init sub &&
 257        ( cd sub && test_commit sub1 ) &&
 258        git add sub &&
 259        test_tick &&
 260        git commit -m "add submodule" &&
 261        ( cd sub && test_commit sub2 ) &&
 262        write_script gather_pre_post.sh <<-\EOF &&
 263        echo "$1 $4" # path, mode
 264        cat "$2" # old file
 265        cat "$5" # new file
 266        EOF
 267        GIT_EXTERNAL_DIFF=./gather_pre_post.sh git diff >actual &&
 268        cat >expected <<-EOF &&
 269        sub 160000
 270        Subproject commit $(git rev-parse HEAD:sub)
 271        Subproject commit $(cd sub && git rev-parse HEAD)
 272        EOF
 273        test_cmp expected actual
 274'
 275
 276test_done