t / t0020-crlf.shon commit Merge branch 'maint' (784b11c)
   1#!/bin/sh
   2
   3test_description='CRLF conversion'
   4
   5. ./test-lib.sh
   6
   7append_cr () {
   8        sed -e 's/$/Q/' | tr Q '\015'
   9}
  10
  11remove_cr () {
  12        tr '\015' Q <"$1" | grep Q >/dev/null &&
  13        tr '\015' Q <"$1" | sed -ne 's/Q$//p'
  14}
  15
  16test_expect_success setup '
  17
  18        git repo-config core.autocrlf false &&
  19
  20        for w in Hello world how are you; do echo $w; done >one &&
  21        mkdir dir &&
  22        for w in I am very very fine thank you; do echo $w; done >dir/two &&
  23        git add . &&
  24
  25        git commit -m initial &&
  26
  27        one=`git rev-parse HEAD:one` &&
  28        dir=`git rev-parse HEAD:dir` &&
  29        two=`git rev-parse HEAD:dir/two` &&
  30
  31        for w in Some extra lines here; do echo $w; done >>one &&
  32        git diff >patch.file &&
  33        patched=`git hash-object --stdin <one` &&
  34        git read-tree --reset -u HEAD &&
  35
  36        echo happy.
  37'
  38
  39test_expect_success 'update with autocrlf=input' '
  40
  41        rm -f tmp one dir/two &&
  42        git read-tree --reset -u HEAD &&
  43        git repo-config core.autocrlf input &&
  44
  45        for f in one dir/two
  46        do
  47                append_cr <$f >tmp && mv -f tmp $f &&
  48                git update-index -- $f || {
  49                        echo Oops
  50                        false
  51                        break
  52                }
  53        done &&
  54
  55        differs=`git diff-index --cached HEAD` &&
  56        test -z "$differs" || {
  57                echo Oops "$differs"
  58                false
  59        }
  60
  61'
  62
  63test_expect_success 'update with autocrlf=true' '
  64
  65        rm -f tmp one dir/two &&
  66        git read-tree --reset -u HEAD &&
  67        git repo-config core.autocrlf true &&
  68
  69        for f in one dir/two
  70        do
  71                append_cr <$f >tmp && mv -f tmp $f &&
  72                git update-index -- $f || {
  73                        echo "Oops $f"
  74                        false
  75                        break
  76                }
  77        done &&
  78
  79        differs=`git diff-index --cached HEAD` &&
  80        test -z "$differs" || {
  81                echo Oops "$differs"
  82                false
  83        }
  84
  85'
  86
  87test_expect_success 'checkout with autocrlf=true' '
  88
  89        rm -f tmp one dir/two &&
  90        git repo-config core.autocrlf true &&
  91        git read-tree --reset -u HEAD &&
  92
  93        for f in one dir/two
  94        do
  95                remove_cr "$f" >tmp && mv -f tmp $f &&
  96                git update-index -- $f || {
  97                        echo "Eh? $f"
  98                        false
  99                        break
 100                }
 101        done &&
 102        test "$one" = `git hash-object --stdin <one` &&
 103        test "$two" = `git hash-object --stdin <dir/two` &&
 104        differs=`git diff-index --cached HEAD` &&
 105        test -z "$differs" || {
 106                echo Oops "$differs"
 107                false
 108        }
 109'
 110
 111test_expect_success 'checkout with autocrlf=input' '
 112
 113        rm -f tmp one dir/two &&
 114        git repo-config core.autocrlf input &&
 115        git read-tree --reset -u HEAD &&
 116
 117        for f in one dir/two
 118        do
 119                if remove_cr "$f" >/dev/null
 120                then
 121                        echo "Eh? $f"
 122                        false
 123                        break
 124                else
 125                        git update-index -- $f
 126                fi
 127        done &&
 128        test "$one" = `git hash-object --stdin <one` &&
 129        test "$two" = `git hash-object --stdin <dir/two` &&
 130        differs=`git diff-index --cached HEAD` &&
 131        test -z "$differs" || {
 132                echo Oops "$differs"
 133                false
 134        }
 135'
 136
 137test_expect_success 'apply patch (autocrlf=input)' '
 138
 139        rm -f tmp one dir/two &&
 140        git repo-config core.autocrlf input &&
 141        git read-tree --reset -u HEAD &&
 142
 143        git apply patch.file &&
 144        test "$patched" = "`git hash-object --stdin <one`" || {
 145                echo "Eh?  apply without index"
 146                false
 147        }
 148'
 149
 150test_expect_success 'apply patch --cached (autocrlf=input)' '
 151
 152        rm -f tmp one dir/two &&
 153        git repo-config core.autocrlf input &&
 154        git read-tree --reset -u HEAD &&
 155
 156        git apply --cached patch.file &&
 157        test "$patched" = `git rev-parse :one` || {
 158                echo "Eh?  apply with --cached"
 159                false
 160        }
 161'
 162
 163test_expect_success 'apply patch --index (autocrlf=input)' '
 164
 165        rm -f tmp one dir/two &&
 166        git repo-config core.autocrlf input &&
 167        git read-tree --reset -u HEAD &&
 168
 169        git apply --index patch.file &&
 170        test "$patched" = `git rev-parse :one` &&
 171        test "$patched" = `git hash-object --stdin <one` || {
 172                echo "Eh?  apply with --index"
 173                false
 174        }
 175'
 176
 177test_expect_success 'apply patch (autocrlf=true)' '
 178
 179        rm -f tmp one dir/two &&
 180        git repo-config core.autocrlf true &&
 181        git read-tree --reset -u HEAD &&
 182
 183        git apply patch.file &&
 184        test "$patched" = "`remove_cr one | git hash-object --stdin`" || {
 185                echo "Eh?  apply without index"
 186                false
 187        }
 188'
 189
 190test_expect_success 'apply patch --cached (autocrlf=true)' '
 191
 192        rm -f tmp one dir/two &&
 193        git repo-config core.autocrlf true &&
 194        git read-tree --reset -u HEAD &&
 195
 196        git apply --cached patch.file &&
 197        test "$patched" = `git rev-parse :one` || {
 198                echo "Eh?  apply without index"
 199                false
 200        }
 201'
 202
 203test_expect_success 'apply patch --index (autocrlf=true)' '
 204
 205        rm -f tmp one dir/two &&
 206        git repo-config core.autocrlf true &&
 207        git read-tree --reset -u HEAD &&
 208
 209        git apply --index patch.file &&
 210        test "$patched" = `git rev-parse :one` &&
 211        test "$patched" = "`remove_cr one | git hash-object --stdin`" || {
 212                echo "Eh?  apply with --index"
 213                false
 214        }
 215'
 216
 217test_done