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