1#!/bin/sh
   2test_description='CRLF conversion'
   4. ./test-lib.sh
   6has_cr() {
   8        tr '\015' Q <"$1" | grep Q >/dev/null
   9}
  10test_expect_success setup '
  12        git config core.autocrlf false &&
  14        for w in Hello world how are you; do echo $w; done >one &&
  16        for w in I am very very fine thank you; do echo ${w}Q; done | q_to_cr >two &&
  17        for w in Oh here is a QNUL byte how alarming; do echo ${w}; done | q_to_nul >three &&
  18        git add . &&
  19        git commit -m initial &&
  21        one=`git rev-parse HEAD:one` &&
  23        two=`git rev-parse HEAD:two` &&
  24        three=`git rev-parse HEAD:three` &&
  25        echo happy.
  27'
  28test_expect_success 'default settings cause no changes' '
  30        rm -f .gitattributes tmp one two three &&
  32        git read-tree --reset -u HEAD &&
  33        ! has_cr one &&
  35        has_cr two &&
  36        onediff=`git diff one` &&
  37        twodiff=`git diff two` &&
  38        threediff=`git diff three` &&
  39        test -z "$onediff" -a -z "$twodiff" -a -z "$threediff"
  40'
  41test_expect_success 'crlf=true causes a CRLF file to be normalized' '
  43        # Backwards compatibility check
  45        rm -f .gitattributes tmp one two three &&
  46        echo "two crlf" > .gitattributes &&
  47        git read-tree --reset -u HEAD &&
  48        # Note, "normalized" means that git will normalize it if added
  50        has_cr two &&
  51        twodiff=`git diff two` &&
  52        test -n "$twodiff"
  53'
  54test_expect_success 'text=true causes a CRLF file to be normalized' '
  56        rm -f .gitattributes tmp one two three &&
  58        echo "two text" > .gitattributes &&
  59        git read-tree --reset -u HEAD &&
  60        # Note, "normalized" means that git will normalize it if added
  62        has_cr two &&
  63        twodiff=`git diff two` &&
  64        test -n "$twodiff"
  65'
  66test_expect_success 'eol=crlf gives a normalized file CRLFs with autocrlf=false' '
  68        rm -f .gitattributes tmp one two three &&
  70        git config core.autocrlf false &&
  71        echo "one eol=crlf" > .gitattributes &&
  72        git read-tree --reset -u HEAD &&
  73        has_cr one &&
  75        onediff=`git diff one` &&
  76        test -z "$onediff"
  77'
  78test_expect_success 'eol=crlf gives a normalized file CRLFs with autocrlf=input' '
  80        rm -f .gitattributes tmp one two three &&
  82        git config core.autocrlf input &&
  83        echo "one eol=crlf" > .gitattributes &&
  84        git read-tree --reset -u HEAD &&
  85        has_cr one &&
  87        onediff=`git diff one` &&
  88        test -z "$onediff"
  89'
  90test_expect_success 'eol=lf gives a normalized file LFs with autocrlf=true' '
  92        rm -f .gitattributes tmp one two three &&
  94        git config core.autocrlf true &&
  95        echo "one eol=lf" > .gitattributes &&
  96        git read-tree --reset -u HEAD &&
  97        ! has_cr one &&
  99        onediff=`git diff one` &&
 100        test -z "$onediff"
 101'
 102test_expect_success 'autocrlf=true does not normalize CRLF files' '
 104        rm -f .gitattributes tmp one two three &&
 106        git config core.autocrlf true &&
 107        git read-tree --reset -u HEAD &&
 108        has_cr one &&
 110        has_cr two &&
 111        onediff=`git diff one` &&
 112        twodiff=`git diff two` &&
 113        threediff=`git diff three` &&
 114        test -z "$onediff" -a -z "$twodiff" -a -z "$threediff"
 115'
 116test_expect_success 'text=auto, autocrlf=true _does_ normalize CRLF files' '
 118        rm -f .gitattributes tmp one two three &&
 120        git config core.autocrlf true &&
 121        echo "* text=auto" > .gitattributes &&
 122        git read-tree --reset -u HEAD &&
 123        has_cr one &&
 125        has_cr two &&
 126        onediff=`git diff one` &&
 127        twodiff=`git diff two` &&
 128        threediff=`git diff three` &&
 129        test -z "$onediff" -a -n "$twodiff" -a -z "$threediff"
 130'
 131test_expect_success 'text=auto, autocrlf=true does not normalize binary files' '
 133        rm -f .gitattributes tmp one two three &&
 135        git config core.autocrlf true &&
 136        echo "* text=auto" > .gitattributes &&
 137        git read-tree --reset -u HEAD &&
 138        ! has_cr three &&
 140        threediff=`git diff three` &&
 141        test -z "$threediff"
 142'
 143test_expect_success 'eol=crlf _does_ normalize binary files' '
 145        rm -f .gitattributes tmp one two three &&
 147        echo "three eol=crlf" > .gitattributes &&
 148        git read-tree --reset -u HEAD &&
 149        has_cr three &&
 151        threediff=`git diff three` &&
 152        test -z "$threediff"
 153'
 154test_done