810934b9b797c04144e8a42d0bc42921301e86d8
   1#!/bin/sh
   2
   3test_description='CRLF conversion all combinations'
   4
   5. ./test-lib.sh
   6
   7if ! test_have_prereq EXPENSIVE
   8then
   9        skip_all="EXPENSIVE not set"
  10        test_done
  11fi
  12
  13compare_files () {
  14        tr '\015\000' QN <"$1" >"$1".expect &&
  15        tr '\015\000' QN <"$2" >"$2".actual &&
  16        test_cmp "$1".expect "$2".actual &&
  17        rm "$1".expect "$2".actual
  18}
  19
  20compare_ws_file () {
  21        pfx=$1
  22        exp=$2.expect
  23        act=$pfx.actual.$3
  24        tr '\015\000' QN <"$2" >"$exp" &&
  25        tr '\015\000' QN <"$3" >"$act" &&
  26        test_cmp $exp $act &&
  27        rm $exp $act
  28}
  29
  30create_gitattributes () {
  31        attr=$1
  32        case "$attr" in
  33                auto)
  34                echo "*.txt text=auto" >.gitattributes
  35                ;;
  36                text)
  37                echo "*.txt text" >.gitattributes
  38                ;;
  39                -text)
  40                echo "*.txt -text" >.gitattributes
  41                ;;
  42                crlf)
  43                echo "*.txt eol=crlf" >.gitattributes
  44                ;;
  45                lf)
  46                echo "*.txt eol=lf" >.gitattributes
  47                ;;
  48                "")
  49                echo >.gitattributes
  50                ;;
  51                *)
  52                echo >&2 invalid attribute: $attr
  53                exit 1
  54                ;;
  55        esac
  56}
  57
  58check_warning () {
  59        case "$1" in
  60        LF_CRLF) echo "warning: LF will be replaced by CRLF" >"$2".expect ;;
  61        CRLF_LF) echo "warning: CRLF will be replaced by LF" >"$2".expect ;;
  62        '')                                                      >"$2".expect ;;
  63        *) echo >&2 "Illegal 1": "$1" ; return false ;;
  64        esac
  65        grep "will be replaced by" "$2" | sed -e "s/\(.*\) in [^ ]*$/\1/" >"$2".actual
  66        test_cmp "$2".expect "$2".actual
  67}
  68
  69commit_check_warn () {
  70        crlf=$1
  71        attr=$2
  72        lfname=$3
  73        crlfname=$4
  74        lfmixcrlf=$5
  75        lfmixcr=$6
  76        crlfnul=$7
  77        create_gitattributes "$attr" &&
  78        pfx=crlf_${crlf}_attr_${attr}
  79        for f in LF CRLF LF_mix_CR CRLF_mix_LF CRLF_nul
  80        do
  81                fname=${pfx}_$f.txt &&
  82                cp $f $fname &&
  83                git -c core.autocrlf=$crlf add $fname 2>"${pfx}_$f.err"
  84        done &&
  85        git commit -m "core.autocrlf $crlf" &&
  86        check_warning "$lfname" ${pfx}_LF.err &&
  87        check_warning "$crlfname" ${pfx}_CRLF.err &&
  88        check_warning "$lfmixcrlf" ${pfx}_CRLF_mix_LF.err &&
  89        check_warning "$lfmixcr" ${pfx}_LF_mix_CR.err &&
  90        check_warning "$crlfnul" ${pfx}_CRLF_nul.err
  91}
  92
  93check_files_in_repo () {
  94        crlf=$1
  95        attr=$2
  96        lfname=$3
  97        crlfname=$4
  98        lfmixcrlf=$5
  99        lfmixcr=$6
 100        crlfnul=$7
 101        pfx=crlf_${crlf}_attr_${attr}_ &&
 102        compare_files $lfname ${pfx}LF.txt &&
 103        compare_files $crlfname ${pfx}CRLF.txt &&
 104        compare_files $lfmixcrlf ${pfx}CRLF_mix_LF.txt &&
 105        compare_files $lfmixcr ${pfx}LF_mix_CR.txt &&
 106        compare_files $crlfnul ${pfx}CRLF_nul.txt
 107}
 108
 109
 110checkout_files () {
 111        eol=$1
 112        crlf=$2
 113        attr=$3
 114        lfname=$4
 115        crlfname=$5
 116        lfmixcrlf=$6
 117        lfmixcr=$7
 118        crlfnul=$8
 119        create_gitattributes $attr &&
 120        git config core.autocrlf $crlf &&
 121        pfx=eol_${eol}_crlf_${crlf}_attr_${attr}_ &&
 122        src=crlf_false_attr__ &&
 123        for f in LF CRLF LF_mix_CR CRLF_mix_LF CRLF_nul
 124        do
 125                rm $src$f.txt &&
 126                if test -z "$eol"; then
 127                        git checkout $src$f.txt
 128                else
 129                        git -c core.eol=$eol checkout $src$f.txt
 130                fi
 131        done
 132
 133        test_expect_success "checkout core.eol=$eol core.autocrlf=$crlf gitattributes=$attr file=LF" "
 134                compare_ws_file $pfx $lfname    ${src}LF.txt
 135        "
 136        test_expect_success "checkout core.eol=$eol core.autocrlf=$crlf gitattributes=$attr file=CRLF" "
 137                compare_ws_file $pfx $crlfname  ${src}CRLF.txt
 138        "
 139        test_expect_success "checkout core.eol=$eol core.autocrlf=$crlf gitattributes=$attr file=CRLF_mix_LF" "
 140                compare_ws_file $pfx $lfmixcrlf ${src}CRLF_mix_LF.txt
 141        "
 142        test_expect_success "checkout core.eol=$eol core.autocrlf=$crlf gitattributes=$attr file=LF_mix_CR" "
 143                compare_ws_file $pfx $lfmixcr   ${src}LF_mix_CR.txt
 144        "
 145        test_expect_success "checkout core.eol=$eol core.autocrlf=$crlf gitattributes=$attr file=CRLF_nul" "
 146                compare_ws_file $pfx $crlfnul   ${src}CRLF_nul.txt
 147        "
 148}
 149
 150#######
 151test_expect_success 'setup master' '
 152        echo >.gitattributes &&
 153        git checkout -b master &&
 154        git add .gitattributes &&
 155        git commit -m "add .gitattributes" "" &&
 156        printf "line1\nline2\nline3"     >LF &&
 157        printf "line1\r\nline2\r\nline3" >CRLF &&
 158        printf "line1\r\nline2\nline3"   >CRLF_mix_LF &&
 159        printf "line1\nline2\rline3"     >LF_mix_CR &&
 160        printf "line1\r\nline2\rline3"   >CRLF_mix_CR &&
 161        printf "line1Q\r\nline2\r\nline3" | q_to_nul >CRLF_nul &&
 162        printf "line1Q\nline2\nline3" | q_to_nul >LF_nul
 163'
 164
 165
 166
 167warn_LF_CRLF="LF will be replaced by CRLF"
 168warn_CRLF_LF="CRLF will be replaced by LF"
 169
 170# WILC stands for "Warn if (this OS) converts LF into CRLF".
 171# WICL: Warn if CRLF becomes LF
 172# WAMIX: Mixed line endings: either CRLF->LF or LF->CRLF
 173if test_have_prereq NATIVE_CRLF
 174then
 175        WILC=LF_CRLF
 176        WICL=
 177        WAMIX=LF_CRLF
 178else
 179        WILC=
 180        WICL=CRLF_LF
 181        WAMIX=CRLF_LF
 182fi
 183
 184test_expect_success 'commit files empty attr' '
 185        commit_check_warn false ""     ""        ""        ""        ""        "" &&
 186        commit_check_warn true  ""     "LF_CRLF" ""        "LF_CRLF" ""        "" &&
 187        commit_check_warn input ""     ""        "CRLF_LF" "CRLF_LF" ""        ""
 188'
 189
 190test_expect_success 'commit files attr=auto' '
 191        commit_check_warn false "auto" "$WILC"  "$WICL"    "$WAMIX"  ""        "" &&
 192        commit_check_warn true  "auto" "LF_CRLF" ""        "LF_CRLF" ""        "" &&
 193        commit_check_warn input "auto" ""        "CRLF_LF" "CRLF_LF" ""        ""
 194'
 195
 196test_expect_success 'commit files attr=text' '
 197        commit_check_warn false "text" "$WILC"  "$WICL"    "$WAMIX"  "$WILC"  "$WICL" &&
 198        commit_check_warn true  "text" "LF_CRLF" ""        "LF_CRLF" "LF_CRLF" ""        &&
 199        commit_check_warn input "text" ""        "CRLF_LF" "CRLF_LF" ""        "CRLF_LF"
 200'
 201
 202test_expect_success 'commit files attr=-text' '
 203        commit_check_warn false "-text" ""       ""        ""        ""        "" &&
 204        commit_check_warn true  "-text" ""       ""        ""        ""        "" &&
 205        commit_check_warn input "-text" ""       ""        ""        ""        ""
 206'
 207
 208test_expect_success 'commit files attr=lf' '
 209        commit_check_warn false "lf"    ""       "CRLF_LF" "CRLF_LF"  ""       "CRLF_LF" &&
 210        commit_check_warn true  "lf"    ""       "CRLF_LF" "CRLF_LF"  ""       "CRLF_LF" &&
 211        commit_check_warn input "lf"    ""       "CRLF_LF" "CRLF_LF"  ""       "CRLF_LF"
 212'
 213
 214test_expect_success 'commit files attr=crlf' '
 215        commit_check_warn false "crlf" "LF_CRLF" ""        "LF_CRLF" "LF_CRLF" "" &&
 216        commit_check_warn true  "crlf" "LF_CRLF" ""        "LF_CRLF" "LF_CRLF" "" &&
 217        commit_check_warn input "crlf" "LF_CRLF" ""        "LF_CRLF" "LF_CRLF" ""
 218'
 219
 220test_expect_success 'create files cleanup' '
 221        rm -f *.txt &&
 222        git reset --hard
 223'
 224
 225test_expect_success 'commit empty gitattribues' '
 226        check_files_in_repo false ""      LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul &&
 227        check_files_in_repo true  ""      LF LF   LF          LF_mix_CR CRLF_nul &&
 228        check_files_in_repo input ""      LF LF   LF          LF_mix_CR CRLF_nul
 229'
 230
 231test_expect_success 'commit text=auto' '
 232        check_files_in_repo false "auto"  LF LF   LF          LF_mix_CR CRLF_nul &&
 233        check_files_in_repo true  "auto"  LF LF   LF          LF_mix_CR CRLF_nul &&
 234        check_files_in_repo input "auto"  LF LF   LF          LF_mix_CR CRLF_nul
 235'
 236
 237test_expect_success 'commit text' '
 238        check_files_in_repo false "text"  LF LF   LF          LF_mix_CR LF_nul &&
 239        check_files_in_repo true  "text"  LF LF   LF          LF_mix_CR LF_nul &&
 240        check_files_in_repo input "text"  LF LF   LF          LF_mix_CR LF_nul
 241'
 242
 243test_expect_success 'commit -text' '
 244        check_files_in_repo false "-text" LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul &&
 245        check_files_in_repo true  "-text" LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul &&
 246        check_files_in_repo input "-text" LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul
 247'
 248
 249################################################################################
 250# Check how files in the repo are changed when they are checked out
 251# How to read the table below:
 252# - checkout_files will check multiple files with a combination of settings
 253#   and attributes (core.autocrlf=input is forbidden with core.eol=crlf)
 254# - parameter $1 : core.eol               lf | crlf
 255# - parameter $2 : core.autocrlf          false | true | input
 256# - parameter $3 : text in .gitattributs  "" (empty) | auto | text | -text
 257# - parameter $4 : reference for a file with only LF in the repo
 258# - parameter $5 : reference for a file with only CRLF in the repo
 259# - parameter $6 : reference for a file with mixed LF and CRLF in the repo
 260# - parameter $7 : reference for a file with LF and CR in the repo (does somebody uses this ?)
 261# - parameter $8 : reference for a file with CRLF and a NUL (should be handled as binary when auto)
 262
 263#                                            What we have in the repo:
 264#                                            ----------------- EOL in repo ----------------
 265#                                            LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 266#                   settings with checkout:
 267#                   core.   core.   .gitattr
 268#                    eol     acrlf
 269#                                            ----------------------------------------------
 270#                                            What we want to have in the working tree:
 271if test_have_prereq MINGW
 272then
 273MIX_CRLF_LF=CRLF
 274MIX_LF_CR=CRLF_mix_CR
 275NL=CRLF
 276else
 277MIX_CRLF_LF=CRLF_mix_LF
 278MIX_LF_CR=LF_mix_CR
 279NL=LF
 280fi
 281export CRLF_MIX_LF_CR MIX NL
 282
 283checkout_files    lf      false  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 284checkout_files    lf      true   ""       CRLF  CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 285checkout_files    lf      input  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 286checkout_files    lf      false "auto"    LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 287checkout_files    lf      true  "auto"    CRLF  CRLF  CRLF         LF_mix_CR    CRLF_nul
 288checkout_files    lf      input "auto"    LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 289checkout_files    lf      false "text"    LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 290checkout_files    lf      true  "text"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 291checkout_files    lf      input "text"    LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 292checkout_files    lf      false "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 293checkout_files    lf      true  "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 294checkout_files    lf      input "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 295checkout_files    lf      false "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 296checkout_files    lf      true  "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 297checkout_files    lf      input "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 298checkout_files    lf      false "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 299checkout_files    lf      true  "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 300checkout_files    lf      input "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 301
 302checkout_files    crlf    false  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 303checkout_files    crlf    true   ""       CRLF  CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 304checkout_files    crlf    false "auto"    CRLF  CRLF  CRLF         LF_mix_CR    CRLF_nul
 305checkout_files    crlf    true  "auto"    CRLF  CRLF  CRLF         LF_mix_CR    CRLF_nul
 306checkout_files    crlf    false "text"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 307checkout_files    crlf    true  "text"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 308checkout_files    crlf    false "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 309checkout_files    crlf    true  "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 310checkout_files    crlf    false "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 311checkout_files    crlf    true  "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 312checkout_files    crlf    false "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 313checkout_files    crlf    true  "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 314
 315checkout_files    ""      false  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 316checkout_files    ""      true   ""       CRLF  CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 317checkout_files    ""      input  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 318checkout_files    ""      false "auto"    $NL   CRLF  $MIX_CRLF_LF LF_mix_CR    CRLF_nul
 319checkout_files    ""      true  "auto"    CRLF  CRLF  CRLF         LF_mix_CR    CRLF_nul
 320checkout_files    ""      input "auto"    LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 321checkout_files    ""      false "text"    $NL   CRLF  $MIX_CRLF_LF $MIX_LF_CR   CRLF_nul
 322checkout_files    ""      true  "text"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 323checkout_files    ""      input "text"    LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 324checkout_files    ""      false "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 325checkout_files    ""      true  "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 326checkout_files    ""      input "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 327checkout_files    ""      false "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 328checkout_files    ""      true  "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 329checkout_files    ""      input "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 330checkout_files    ""      false "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 331checkout_files    ""      true  "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 332checkout_files    ""      input "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 333
 334checkout_files    native  false  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 335checkout_files    native  true   ""       CRLF  CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 336checkout_files    native  false "auto"    $NL   CRLF  $MIX_CRLF_LF LF_mix_CR    CRLF_nul
 337checkout_files    native  true  "auto"    CRLF  CRLF  CRLF         LF_mix_CR    CRLF_nul
 338checkout_files    native  false "text"    $NL   CRLF  $MIX_CRLF_LF $MIX_LF_CR   CRLF_nul
 339checkout_files    native  true  "text"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 340checkout_files    native  false "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 341checkout_files    native  true  "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 342checkout_files    native  false "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 343checkout_files    native  true  "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 344checkout_files    native  false "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 345checkout_files    native  true  "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 346
 347test_done