t / t0027-auto-crlf.shon commit worktree: add: make -b/-B default to HEAD when <branch> is omitted (0f4af3b)
   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
  58create_file_in_repo () {
  59        crlf=$1
  60        attr=$2
  61        create_gitattributes "$attr" &&
  62        for f in LF CRLF LF_mix_CR CRLF_mix_LF CRLF_nul
  63        do
  64                pfx=crlf_${crlf}_attr_${attr}_$f.txt &&
  65                cp $f $pfx && git -c core.autocrlf=$crlf add $pfx
  66        done &&
  67        git commit -m "core.autocrlf $crlf"
  68}
  69
  70check_files_in_repo () {
  71        crlf=$1
  72        attr=$2
  73        lfname=$3
  74        crlfname=$4
  75        lfmixcrlf=$5
  76        lfmixcr=$6
  77        crlfnul=$7
  78        pfx=crlf_${crlf}_attr_${attr}_ &&
  79        compare_files $lfname ${pfx}LF.txt &&
  80        compare_files $crlfname ${pfx}CRLF.txt &&
  81        compare_files $lfmixcrlf ${pfx}CRLF_mix_LF.txt &&
  82        compare_files $lfmixcr ${pfx}LF_mix_CR.txt &&
  83        compare_files $crlfnul ${pfx}CRLF_nul.txt
  84}
  85
  86
  87check_files_in_ws () {
  88        eol=$1
  89        crlf=$2
  90        attr=$3
  91        lfname=$4
  92        crlfname=$5
  93        lfmixcrlf=$6
  94        lfmixcr=$7
  95        crlfnul=$8
  96        create_gitattributes $attr &&
  97        git config core.autocrlf $crlf &&
  98        pfx=eol_${eol}_crlf_${crlf}_attr_${attr}_ &&
  99        src=crlf_false_attr__ &&
 100        for f in LF CRLF LF_mix_CR CRLF_mix_LF CRLF_nul
 101        do
 102                rm $src$f.txt &&
 103                if test -z "$eol"; then
 104                        git checkout $src$f.txt
 105                else
 106                        git -c core.eol=$eol checkout $src$f.txt
 107                fi
 108        done
 109
 110        test_expect_success "checkout core.eol=$eol core.autocrlf=$crlf gitattributes=$attr file=LF" "
 111                compare_ws_file $pfx $lfname    ${src}LF.txt
 112        "
 113        test_expect_success "checkout core.eol=$eol core.autocrlf=$crlf gitattributes=$attr file=CRLF" "
 114                compare_ws_file $pfx $crlfname  ${src}CRLF.txt
 115        "
 116        test_expect_success "checkout core.eol=$eol core.autocrlf=$crlf gitattributes=$attr file=CRLF_mix_LF" "
 117                compare_ws_file $pfx $lfmixcrlf ${src}CRLF_mix_LF.txt
 118        "
 119        test_expect_success "checkout core.eol=$eol core.autocrlf=$crlf gitattributes=$attr file=LF_mix_CR" "
 120                compare_ws_file $pfx $lfmixcr   ${src}LF_mix_CR.txt
 121        "
 122        test_expect_success "checkout core.eol=$eol core.autocrlf=$crlf gitattributes=$attr file=CRLF_nul" "
 123                compare_ws_file $pfx $crlfnul   ${src}CRLF_nul.txt
 124        "
 125}
 126
 127#######
 128test_expect_success 'setup master' '
 129        echo >.gitattributes &&
 130        git checkout -b master &&
 131        git add .gitattributes &&
 132        git commit -m "add .gitattributes" "" &&
 133        printf "line1\nline2\nline3"     >LF &&
 134        printf "line1\r\nline2\r\nline3" >CRLF &&
 135        printf "line1\r\nline2\nline3"   >CRLF_mix_LF &&
 136        printf "line1\nline2\rline3"     >LF_mix_CR &&
 137        printf "line1\r\nline2\rline3"   >CRLF_mix_CR &&
 138        printf "line1Q\r\nline2\r\nline3" | q_to_nul >CRLF_nul &&
 139        printf "line1Q\nline2\nline3" | q_to_nul >LF_nul
 140'
 141
 142
 143test_expect_success 'create files' '
 144        create_file_in_repo false "" &&
 145        create_file_in_repo true  "" &&
 146        create_file_in_repo input "" &&
 147
 148        create_file_in_repo false "auto" &&
 149        create_file_in_repo true  "auto" &&
 150        create_file_in_repo input "auto" &&
 151
 152        create_file_in_repo false "text" &&
 153        create_file_in_repo true  "text" &&
 154        create_file_in_repo input "text" &&
 155
 156        create_file_in_repo false "-text" &&
 157        create_file_in_repo true  "-text" &&
 158        create_file_in_repo input "-text" &&
 159        rm -f *.txt &&
 160        git reset --hard
 161'
 162
 163test_expect_success 'commit empty gitattribues' '
 164        check_files_in_repo false ""      LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul &&
 165        check_files_in_repo true  ""      LF LF   LF          LF_mix_CR CRLF_nul &&
 166        check_files_in_repo input ""      LF LF   LF          LF_mix_CR CRLF_nul
 167'
 168
 169test_expect_success 'commit text=auto' '
 170        check_files_in_repo false "auto"  LF LF   LF          LF_mix_CR CRLF_nul &&
 171        check_files_in_repo true  "auto"  LF LF   LF          LF_mix_CR CRLF_nul &&
 172        check_files_in_repo input "auto"  LF LF   LF          LF_mix_CR CRLF_nul
 173'
 174
 175test_expect_success 'commit text' '
 176        check_files_in_repo false "text"  LF LF   LF          LF_mix_CR LF_nul &&
 177        check_files_in_repo true  "text"  LF LF   LF          LF_mix_CR LF_nul &&
 178        check_files_in_repo input "text"  LF LF   LF          LF_mix_CR LF_nul
 179'
 180
 181test_expect_success 'commit -text' '
 182        check_files_in_repo false "-text" LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul &&
 183        check_files_in_repo true  "-text" LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul &&
 184        check_files_in_repo input "-text" LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul
 185'
 186
 187################################################################################
 188# Check how files in the repo are changed when they are checked out
 189# How to read the table below:
 190# - check_files_in_ws will check multiple files with a combination of settings
 191#   and attributes (core.autocrlf=input is forbidden with core.eol=crlf)
 192# - parameter $1 : core.eol               lf | crlf
 193# - parameter $2 : core.autocrlf          false | true | input
 194# - parameter $3 : text in .gitattributs  "" (empty) | auto | text | -text
 195# - parameter $4 : reference for a file with only LF in the repo
 196# - parameter $5 : reference for a file with only CRLF in the repo
 197# - parameter $6 : reference for a file with mixed LF and CRLF in the repo
 198# - parameter $7 : reference for a file with LF and CR in the repo (does somebody uses this ?)
 199# - parameter $8 : reference for a file with CRLF and a NUL (should be handled as binary when auto)
 200
 201#                                            What we have in the repo:
 202#                                                                                                                                ----------------- EOL in repo ----------------
 203#                                                                                                                                LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 204#                   settings with checkout:
 205#                   core.   core.   .gitattr
 206#                    eol     acrlf
 207#                                            ----------------------------------------------
 208#                                            What we want to have in the working tree:
 209if test_have_prereq MINGW
 210then
 211MIX_CRLF_LF=CRLF
 212MIX_LF_CR=CRLF_mix_CR
 213NL=CRLF
 214else
 215MIX_CRLF_LF=CRLF_mix_LF
 216MIX_LF_CR=LF_mix_CR
 217NL=LF
 218fi
 219export CRLF_MIX_LF_CR MIX NL
 220
 221check_files_in_ws    lf      false  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 222check_files_in_ws    lf      true   ""       CRLF  CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 223check_files_in_ws    lf      input  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 224check_files_in_ws    lf      false "auto"    LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 225check_files_in_ws    lf      true  "auto"    CRLF  CRLF  CRLF         LF_mix_CR    CRLF_nul
 226check_files_in_ws    lf      input "auto"    LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 227check_files_in_ws    lf      false "text"    LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 228check_files_in_ws    lf      true  "text"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 229check_files_in_ws    lf      input "text"    LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 230check_files_in_ws    lf      false "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 231check_files_in_ws    lf      true  "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 232check_files_in_ws    lf      input "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 233check_files_in_ws    lf      false "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 234check_files_in_ws    lf      true  "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 235check_files_in_ws    lf      input "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 236check_files_in_ws    lf      false "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 237check_files_in_ws    lf      true  "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 238check_files_in_ws    lf      input "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 239
 240check_files_in_ws    crlf    false  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 241check_files_in_ws    crlf    true   ""       CRLF  CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 242check_files_in_ws    crlf    false "auto"    CRLF  CRLF  CRLF         LF_mix_CR    CRLF_nul
 243check_files_in_ws    crlf    true  "auto"    CRLF  CRLF  CRLF         LF_mix_CR    CRLF_nul
 244check_files_in_ws    crlf    false "text"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 245check_files_in_ws    crlf    true  "text"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 246check_files_in_ws    crlf    false "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 247check_files_in_ws    crlf    true  "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 248check_files_in_ws    crlf    false "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 249check_files_in_ws    crlf    true  "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 250check_files_in_ws    crlf    false "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 251check_files_in_ws    crlf    true  "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 252
 253check_files_in_ws    ""      false  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 254check_files_in_ws    ""      true   ""       CRLF  CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 255check_files_in_ws    ""      input  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 256check_files_in_ws    ""      false "auto"    $NL   CRLF  $MIX_CRLF_LF LF_mix_CR    CRLF_nul
 257check_files_in_ws    ""      true  "auto"    CRLF  CRLF  CRLF         LF_mix_CR    CRLF_nul
 258check_files_in_ws    ""      input "auto"    LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 259check_files_in_ws    ""      false "text"    $NL   CRLF  $MIX_CRLF_LF $MIX_LF_CR   CRLF_nul
 260check_files_in_ws    ""      true  "text"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 261check_files_in_ws    ""      input "text"    LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 262check_files_in_ws    ""      false "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 263check_files_in_ws    ""      true  "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 264check_files_in_ws    ""      input "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 265check_files_in_ws    ""      false "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 266check_files_in_ws    ""      true  "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 267check_files_in_ws    ""      input "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 268check_files_in_ws    ""      false "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 269check_files_in_ws    ""      true  "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 270check_files_in_ws    ""      input "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 271
 272check_files_in_ws    native  false  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 273check_files_in_ws    native  true   ""       CRLF  CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 274check_files_in_ws    native  false "auto"    $NL   CRLF  $MIX_CRLF_LF LF_mix_CR    CRLF_nul
 275check_files_in_ws    native  true  "auto"    CRLF  CRLF  CRLF         LF_mix_CR    CRLF_nul
 276check_files_in_ws    native  false "text"    $NL   CRLF  $MIX_CRLF_LF $MIX_LF_CR   CRLF_nul
 277check_files_in_ws    native  true  "text"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 278check_files_in_ws    native  false "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 279check_files_in_ws    native  true  "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 280check_files_in_ws    native  false "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 281check_files_in_ws    native  true  "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
 282check_files_in_ws    native  false "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 283check_files_in_ws    native  true  "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
 284
 285test_done