t / t0020-crlf.shon commit post-checkout hook, tests, and docs (1abbe47)
   1#!/bin/sh
   2
   3test_description='CRLF conversion'
   4
   5. ./test-lib.sh
   6
   7q_to_nul () {
   8        tr Q '\0'
   9}
  10
  11append_cr () {
  12        sed -e 's/$/Q/' | tr Q '\015'
  13}
  14
  15remove_cr () {
  16        tr '\015' Q <"$1" | grep Q >/dev/null &&
  17        tr '\015' Q <"$1" | sed -ne 's/Q$//p'
  18}
  19
  20test_expect_success setup '
  21
  22        git repo-config core.autocrlf false &&
  23
  24        for w in Hello world how are you; do echo $w; done >one &&
  25        mkdir dir &&
  26        for w in I am very very fine thank you; do echo $w; done >dir/two &&
  27        for w in Oh here is NULQin text here; do echo $w; done | q_to_nul >three &&
  28        git add . &&
  29
  30        git commit -m initial &&
  31
  32        one=`git rev-parse HEAD:one` &&
  33        dir=`git rev-parse HEAD:dir` &&
  34        two=`git rev-parse HEAD:dir/two` &&
  35        three=`git rev-parse HEAD:three` &&
  36
  37        for w in Some extra lines here; do echo $w; done >>one &&
  38        git diff >patch.file &&
  39        patched=`git hash-object --stdin <one` &&
  40        git read-tree --reset -u HEAD &&
  41
  42        echo happy.
  43'
  44
  45test_expect_success 'update with autocrlf=input' '
  46
  47        rm -f tmp one dir/two three &&
  48        git read-tree --reset -u HEAD &&
  49        git repo-config core.autocrlf input &&
  50
  51        for f in one dir/two
  52        do
  53                append_cr <$f >tmp && mv -f tmp $f &&
  54                git update-index -- $f || {
  55                        echo Oops
  56                        false
  57                        break
  58                }
  59        done &&
  60
  61        differs=`git diff-index --cached HEAD` &&
  62        test -z "$differs" || {
  63                echo Oops "$differs"
  64                false
  65        }
  66
  67'
  68
  69test_expect_success 'update with autocrlf=true' '
  70
  71        rm -f tmp one dir/two three &&
  72        git read-tree --reset -u HEAD &&
  73        git repo-config core.autocrlf true &&
  74
  75        for f in one dir/two
  76        do
  77                append_cr <$f >tmp && mv -f tmp $f &&
  78                git update-index -- $f || {
  79                        echo "Oops $f"
  80                        false
  81                        break
  82                }
  83        done &&
  84
  85        differs=`git diff-index --cached HEAD` &&
  86        test -z "$differs" || {
  87                echo Oops "$differs"
  88                false
  89        }
  90
  91'
  92
  93test_expect_success 'checkout with autocrlf=true' '
  94
  95        rm -f tmp one dir/two three &&
  96        git repo-config core.autocrlf true &&
  97        git read-tree --reset -u HEAD &&
  98
  99        for f in one dir/two
 100        do
 101                remove_cr "$f" >tmp && mv -f tmp $f &&
 102                git update-index -- $f || {
 103                        echo "Eh? $f"
 104                        false
 105                        break
 106                }
 107        done &&
 108        test "$one" = `git hash-object --stdin <one` &&
 109        test "$two" = `git hash-object --stdin <dir/two` &&
 110        differs=`git diff-index --cached HEAD` &&
 111        test -z "$differs" || {
 112                echo Oops "$differs"
 113                false
 114        }
 115'
 116
 117test_expect_success 'checkout with autocrlf=input' '
 118
 119        rm -f tmp one dir/two three &&
 120        git repo-config core.autocrlf input &&
 121        git read-tree --reset -u HEAD &&
 122
 123        for f in one dir/two
 124        do
 125                if remove_cr "$f" >/dev/null
 126                then
 127                        echo "Eh? $f"
 128                        false
 129                        break
 130                else
 131                        git update-index -- $f
 132                fi
 133        done &&
 134        test "$one" = `git hash-object --stdin <one` &&
 135        test "$two" = `git hash-object --stdin <dir/two` &&
 136        differs=`git diff-index --cached HEAD` &&
 137        test -z "$differs" || {
 138                echo Oops "$differs"
 139                false
 140        }
 141'
 142
 143test_expect_success 'apply patch (autocrlf=input)' '
 144
 145        rm -f tmp one dir/two three &&
 146        git repo-config core.autocrlf input &&
 147        git read-tree --reset -u HEAD &&
 148
 149        git apply patch.file &&
 150        test "$patched" = "`git hash-object --stdin <one`" || {
 151                echo "Eh?  apply without index"
 152                false
 153        }
 154'
 155
 156test_expect_success 'apply patch --cached (autocrlf=input)' '
 157
 158        rm -f tmp one dir/two three &&
 159        git repo-config core.autocrlf input &&
 160        git read-tree --reset -u HEAD &&
 161
 162        git apply --cached patch.file &&
 163        test "$patched" = `git rev-parse :one` || {
 164                echo "Eh?  apply with --cached"
 165                false
 166        }
 167'
 168
 169test_expect_success 'apply patch --index (autocrlf=input)' '
 170
 171        rm -f tmp one dir/two three &&
 172        git repo-config core.autocrlf input &&
 173        git read-tree --reset -u HEAD &&
 174
 175        git apply --index patch.file &&
 176        test "$patched" = `git rev-parse :one` &&
 177        test "$patched" = `git hash-object --stdin <one` || {
 178                echo "Eh?  apply with --index"
 179                false
 180        }
 181'
 182
 183test_expect_success 'apply patch (autocrlf=true)' '
 184
 185        rm -f tmp one dir/two three &&
 186        git repo-config core.autocrlf true &&
 187        git read-tree --reset -u HEAD &&
 188
 189        git apply patch.file &&
 190        test "$patched" = "`remove_cr one | git hash-object --stdin`" || {
 191                echo "Eh?  apply without index"
 192                false
 193        }
 194'
 195
 196test_expect_success 'apply patch --cached (autocrlf=true)' '
 197
 198        rm -f tmp one dir/two three &&
 199        git repo-config core.autocrlf true &&
 200        git read-tree --reset -u HEAD &&
 201
 202        git apply --cached patch.file &&
 203        test "$patched" = `git rev-parse :one` || {
 204                echo "Eh?  apply without index"
 205                false
 206        }
 207'
 208
 209test_expect_success 'apply patch --index (autocrlf=true)' '
 210
 211        rm -f tmp one dir/two three &&
 212        git repo-config core.autocrlf true &&
 213        git read-tree --reset -u HEAD &&
 214
 215        git apply --index patch.file &&
 216        test "$patched" = `git rev-parse :one` &&
 217        test "$patched" = "`remove_cr one | git hash-object --stdin`" || {
 218                echo "Eh?  apply with --index"
 219                false
 220        }
 221'
 222
 223test_expect_success '.gitattributes says two is binary' '
 224
 225        rm -f tmp one dir/two three &&
 226        echo "two -crlf" >.gitattributes &&
 227        git repo-config core.autocrlf true &&
 228        git read-tree --reset -u HEAD &&
 229
 230        if remove_cr dir/two >/dev/null
 231        then
 232                echo "Huh?"
 233                false
 234        else
 235                : happy
 236        fi &&
 237
 238        if remove_cr one >/dev/null
 239        then
 240                : happy
 241        else
 242                echo "Huh?"
 243                false
 244        fi &&
 245
 246        if remove_cr three >/dev/null
 247        then
 248                echo "Huh?"
 249                false
 250        else
 251                : happy
 252        fi
 253'
 254
 255test_expect_success '.gitattributes says two is input' '
 256
 257        rm -f tmp one dir/two three &&
 258        echo "two crlf=input" >.gitattributes &&
 259        git read-tree --reset -u HEAD &&
 260
 261        if remove_cr dir/two >/dev/null
 262        then
 263                echo "Huh?"
 264                false
 265        else
 266                : happy
 267        fi
 268'
 269
 270test_expect_success '.gitattributes says two and three are text' '
 271
 272        rm -f tmp one dir/two three &&
 273        echo "t* crlf" >.gitattributes &&
 274        git read-tree --reset -u HEAD &&
 275
 276        if remove_cr dir/two >/dev/null
 277        then
 278                : happy
 279        else
 280                echo "Huh?"
 281                false
 282        fi &&
 283
 284        if remove_cr three >/dev/null
 285        then
 286                : happy
 287        else
 288                echo "Huh?"
 289                false
 290        fi
 291'
 292
 293test_expect_success 'in-tree .gitattributes (1)' '
 294
 295        echo "one -crlf" >>.gitattributes &&
 296        git add .gitattributes &&
 297        git commit -m "Add .gitattributes" &&
 298
 299        rm -rf tmp one dir .gitattributes patch.file three &&
 300        git read-tree --reset -u HEAD &&
 301
 302        if remove_cr one >/dev/null
 303        then
 304                echo "Eh? one should not have CRLF"
 305                false
 306        else
 307                : happy
 308        fi &&
 309        remove_cr three >/dev/null || {
 310                echo "Eh? three should still have CRLF"
 311                false
 312        }
 313'
 314
 315test_expect_success 'in-tree .gitattributes (2)' '
 316
 317        rm -rf tmp one dir .gitattributes patch.file three &&
 318        git read-tree --reset HEAD &&
 319        git checkout-index -f -q -u -a &&
 320
 321        if remove_cr one >/dev/null
 322        then
 323                echo "Eh? one should not have CRLF"
 324                false
 325        else
 326                : happy
 327        fi &&
 328        remove_cr three >/dev/null || {
 329                echo "Eh? three should still have CRLF"
 330                false
 331        }
 332'
 333
 334test_expect_success 'in-tree .gitattributes (3)' '
 335
 336        rm -rf tmp one dir .gitattributes patch.file three &&
 337        git read-tree --reset HEAD &&
 338        git checkout-index -u .gitattributes &&
 339        git checkout-index -u one dir/two three &&
 340
 341        if remove_cr one >/dev/null
 342        then
 343                echo "Eh? one should not have CRLF"
 344                false
 345        else
 346                : happy
 347        fi &&
 348        remove_cr three >/dev/null || {
 349                echo "Eh? three should still have CRLF"
 350                false
 351        }
 352'
 353
 354test_expect_success 'in-tree .gitattributes (4)' '
 355
 356        rm -rf tmp one dir .gitattributes patch.file three &&
 357        git read-tree --reset HEAD &&
 358        git checkout-index -u one dir/two three &&
 359        git checkout-index -u .gitattributes &&
 360
 361        if remove_cr one >/dev/null
 362        then
 363                echo "Eh? one should not have CRLF"
 364                false
 365        else
 366                : happy
 367        fi &&
 368        remove_cr three >/dev/null || {
 369                echo "Eh? three should still have CRLF"
 370                false
 371        }
 372'
 373
 374test_done