3 test_description='CRLF conversion all combinations'
8 tr '\015\000' QN <"$1" >"$1".expect &&
9 tr '\015\000' QN <"$2" | tr -d 'Z' >"$2".actual &&
10 test_cmp "$1".expect "$2".actual &&
11 rm "$1".expect "$2".actual
18 tr '\015\000abcdef0123456789' QN00000000000000000 <"$2" >"$exp" &&
19 tr '\015\000abcdef0123456789' QN00000000000000000 <"$3" >"$act" &&
20 test_cmp "$exp" "$act" &&
24 create_gitattributes () {
29 auto) echo '*.txt text=auto' ;;
30 ident) echo '*.txt ident' ;;
31 text) echo '*.txt text' ;;
32 -text) echo '*.txt -text' ;;
33 crlf) echo '*.txt eol=crlf' ;;
34 lf) echo '*.txt eol=lf' ;;
37 echo >&2 invalid attribute: "$1"
47 for crlf in false true input
49 for attr in "" auto text -text
51 for aeol in "" lf crlf
53 pfx=NNO_attr_${attr}_aeol_${aeol}_${crlf}
54 cp CRLF_mix_LF ${pfx}_LF.txt &&
55 cp CRLF_mix_LF ${pfx}_CRLF.txt &&
56 cp CRLF_mix_LF ${pfx}_CRLF_mix_LF.txt &&
57 cp CRLF_mix_LF ${pfx}_LF_mix_CR.txt &&
58 cp CRLF_mix_LF ${pfx}_CRLF_nul.txt
66 LF_CRLF) echo "warning: LF will be replaced by CRLF" >"$2".expect ;;
67 CRLF_LF) echo "warning: CRLF will be replaced by LF" >"$2".expect ;;
69 *) echo >&2 "Illegal 1": "$1" ; return false ;;
71 grep "will be replaced by" "$2" | sed -e "s/\(.*\) in [^ ]*$/\1/" | uniq >"$2".actual
72 test_i18ncmp "$2".expect "$2".actual
75 commit_check_warn () {
83 pfx=crlf_${crlf}_attr_${attr}
84 create_gitattributes "$attr" &&
85 for f in LF CRLF LF_mix_CR CRLF_mix_LF LF_nul CRLF_nul
87 fname=${pfx}_$f.txt &&
89 git -c core.autocrlf=$crlf add $fname 2>"${pfx}_$f.err"
91 git commit -m "core.autocrlf $crlf" &&
92 check_warning "$lfname" ${pfx}_LF.err &&
93 check_warning "$crlfname" ${pfx}_CRLF.err &&
94 check_warning "$lfmixcrlf" ${pfx}_CRLF_mix_LF.err &&
95 check_warning "$lfmixcr" ${pfx}_LF_mix_CR.err &&
96 check_warning "$crlfnul" ${pfx}_CRLF_nul.err
99 commit_chk_wrnNNO () {
108 pfx=NNO_attr_${attr}_aeol_${aeol}_${crlf}
109 #Commit files on top of existing file
110 create_gitattributes "$attr" $aeol &&
111 for f in LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul
113 fname=${pfx}_$f.txt &&
115 printf Z >>"$fname" &&
116 git -c core.autocrlf=$crlf add $fname 2>"${pfx}_$f.err"
119 test_expect_success "commit NNO files crlf=$crlf attr=$attr LF" '
120 check_warning "$lfwarn" ${pfx}_LF.err
122 test_expect_success "commit NNO files attr=$attr aeol=$aeol crlf=$crlf CRLF" '
123 check_warning "$crlfwarn" ${pfx}_CRLF.err
126 test_expect_success "commit NNO files attr=$attr aeol=$aeol crlf=$crlf CRLF_mix_LF" '
127 check_warning "$lfmixcrlf" ${pfx}_CRLF_mix_LF.err
130 test_expect_success "commit NNO files attr=$attr aeol=$aeol crlf=$crlf LF_mix_cr" '
131 check_warning "$lfmixcr" ${pfx}_LF_mix_CR.err
134 test_expect_success "commit NNO files attr=$attr aeol=$aeol crlf=$crlf CRLF_nul" '
135 check_warning "$crlfnul" ${pfx}_CRLF_nul.err
150 LF_mix_CR|CRLF_nul|LF_nul|CRLF_mix_CR)
154 echo error_invalid $1
161 # contruct the attr/ returned by git ls-files --eol
162 # Take none (=empty), one or two args
163 # convert.c: eol=XX overrides text=auto
166 -text,*) echo "-text" ;;
167 text,) echo "text" ;;
168 text,lf) echo "text eol=lf" ;;
169 text,crlf) echo "text eol=crlf" ;;
170 auto,) echo "text=auto" ;;
171 auto,lf) echo "text=auto eol=lf" ;;
172 auto,crlf) echo "text=auto eol=crlf" ;;
173 lf,) echo "text eol=lf" ;;
174 crlf,) echo "text eol=crlf" ;;
176 *) echo invalid_attr "$1,$2" ;;
180 check_files_in_repo () {
188 pfx=crlf_${crlf}_attr_${attr}_ &&
189 compare_files $lfname ${pfx}LF.txt &&
190 compare_files $crlfname ${pfx}CRLF.txt &&
191 compare_files $lfmixcrlf ${pfx}CRLF_mix_LF.txt &&
192 compare_files $lfmixcr ${pfx}LF_mix_CR.txt &&
193 compare_files $crlfnul ${pfx}CRLF_nul.txt
196 check_in_repo_NNO () {
205 pfx=NNO_attr_${attr}_aeol_${aeol}_${crlf}
206 test_expect_success "compare_files $lfname ${pfx}_LF.txt" '
207 compare_files $lfname ${pfx}_LF.txt
209 test_expect_success "compare_files $crlfname ${pfx}_CRLF.txt" '
210 compare_files $crlfname ${pfx}_CRLF.txt
212 test_expect_success "compare_files $lfmixcrlf ${pfx}_CRLF_mix_LF.txt" '
213 compare_files $lfmixcrlf ${pfx}_CRLF_mix_LF.txt
215 test_expect_success "compare_files $lfmixcr ${pfx}_LF_mix_CR.txt" '
216 compare_files $lfmixcr ${pfx}_LF_mix_CR.txt
218 test_expect_success "compare_files $crlfnul ${pfx}_CRLF_nul.txt" '
219 compare_files $crlfnul ${pfx}_CRLF_nul.txt
234 create_gitattributes "$attr" $ident $aeol &&
235 git config core.autocrlf $crlf &&
236 pfx=eol_${ceol}_crlf_${crlf}_attr_${attr}_ &&
237 for f in LF CRLF LF_mix_CR CRLF_mix_LF LF_nul
239 rm crlf_false_attr__$f.txt &&
240 if test -z "$ceol"; then
241 git checkout crlf_false_attr__$f.txt
243 git -c core.eol=$ceol checkout crlf_false_attr__$f.txt
247 test_expect_success "ls-files --eol attr=$attr $ident aeol=$aeol core.autocrlf=$crlf core.eol=$ceol" '
248 test_when_finished "rm expect actual" &&
249 sort <<-EOF >expect &&
250 i/crlf w/$(stats_ascii $crlfname) attr/$(attr_ascii $attr $aeol) crlf_false_attr__CRLF.txt
251 i/mixed w/$(stats_ascii $lfmixcrlf) attr/$(attr_ascii $attr $aeol) crlf_false_attr__CRLF_mix_LF.txt
252 i/lf w/$(stats_ascii $lfname) attr/$(attr_ascii $attr $aeol) crlf_false_attr__LF.txt
253 i/-text w/$(stats_ascii $lfmixcr) attr/$(attr_ascii $attr $aeol) crlf_false_attr__LF_mix_CR.txt
254 i/-text w/$(stats_ascii $crlfnul) attr/$(attr_ascii $attr $aeol) crlf_false_attr__CRLF_nul.txt
255 i/-text w/$(stats_ascii $crlfnul) attr/$(attr_ascii $attr $aeol) crlf_false_attr__LF_nul.txt
257 git ls-files --eol crlf_false_attr__* |
258 sed -e "s/ / /g" -e "s/ */ /g" |
260 test_cmp expect actual
262 test_expect_success "checkout attr=$attr $ident aeol=$aeol core.autocrlf=$crlf core.eol=$ceol file=LF" "
263 compare_ws_file $pfx $lfname crlf_false_attr__LF.txt
265 test_expect_success "checkout attr=$attr $ident aeol=$aeol core.autocrlf=$crlf core.eol=$ceol file=CRLF" "
266 compare_ws_file $pfx $crlfname crlf_false_attr__CRLF.txt
268 test_expect_success "checkout attr=$attr $ident aeol=$aeol core.autocrlf=$crlf core.eol=$ceol file=CRLF_mix_LF" "
269 compare_ws_file $pfx $lfmixcrlf crlf_false_attr__CRLF_mix_LF.txt
271 test_expect_success "checkout attr=$attr $ident aeol=$aeol core.autocrlf=$crlf core.eol=$ceol file=LF_mix_CR" "
272 compare_ws_file $pfx $lfmixcr crlf_false_attr__LF_mix_CR.txt
274 test_expect_success "checkout attr=$attr $ident aeol=$aeol core.autocrlf=$crlf core.eol=$ceol file=LF_nul" "
275 compare_ws_file $pfx $crlfnul crlf_false_attr__LF_nul.txt
279 # Test control characters
281 test_expect_success 'ls-files --eol -o Text/Binary' '
282 test_when_finished "rm expect actual TeBi_*" &&
283 STRT=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA &&
284 STR=$STRT$STRT$STRT$STRT &&
285 printf "${STR}BBB\001" >TeBi_127_S &&
286 printf "${STR}BBBB\001">TeBi_128_S &&
287 printf "${STR}BBB\032" >TeBi_127_E &&
288 printf "\032${STR}BBB" >TeBi_E_127 &&
289 printf "${STR}BBBB\000">TeBi_128_N &&
290 printf "${STR}BBB\012">TeBi_128_L &&
291 printf "${STR}BBB\015">TeBi_127_C &&
292 printf "${STR}BB\015\012" >TeBi_126_CL &&
293 printf "${STR}BB\015\012\015" >TeBi_126_CLC &&
294 sort <<-\EOF >expect &&
295 i/ w/-text TeBi_127_S
298 i/ w/-text TeBi_E_127
299 i/ w/-text TeBi_128_N
301 i/ w/-text TeBi_127_C
302 i/ w/crlf TeBi_126_CL
303 i/ w/-text TeBi_126_CLC
305 git ls-files --eol -o |
306 sed -n -e "/TeBi_/{s!attr/[ ]*!!g
311 test_cmp expect actual
314 test_expect_success 'setup master' '
315 echo >.gitattributes &&
316 git checkout -b master &&
317 git add .gitattributes &&
318 git commit -m "add .gitattributes" "" &&
319 printf "\$Id: 0000000000000000000000000000000000000000 \$\nLINEONE\nLINETWO\nLINETHREE" >LF &&
320 printf "\$Id: 0000000000000000000000000000000000000000 \$\r\nLINEONE\r\nLINETWO\r\nLINETHREE" >CRLF &&
321 printf "\$Id: 0000000000000000000000000000000000000000 \$\nLINEONE\r\nLINETWO\nLINETHREE" >CRLF_mix_LF &&
322 printf "\$Id: 0000000000000000000000000000000000000000 \$\nLINEONE\nLINETWO\rLINETHREE" >LF_mix_CR &&
323 printf "\$Id: 0000000000000000000000000000000000000000 \$\r\nLINEONE\r\nLINETWO\rLINETHREE" >CRLF_mix_CR &&
324 printf "\$Id: 0000000000000000000000000000000000000000 \$\r\nLINEONEQ\r\nLINETWO\r\nLINETHREE" | q_to_nul >CRLF_nul &&
325 printf "\$Id: 0000000000000000000000000000000000000000 \$\nLINEONEQ\nLINETWO\nLINETHREE" | q_to_nul >LF_nul &&
326 create_NNO_files CRLF_mix_LF CRLF_mix_LF CRLF_mix_LF CRLF_mix_LF CRLF_mix_LF &&
327 git -c core.autocrlf=false add NNO_*.txt &&
328 git commit -m "mixed line endings" &&
334 warn_LF_CRLF="LF will be replaced by CRLF"
335 warn_CRLF_LF="CRLF will be replaced by LF"
337 # WILC stands for "Warn if (this OS) converts LF into CRLF".
338 # WICL: Warn if CRLF becomes LF
339 # WAMIX: Mixed line endings: either CRLF->LF or LF->CRLF
340 if test_have_prereq NATIVE_CRLF
351 # attr LF CRLF CRLFmixLF LFmixCR CRLFNUL
352 test_expect_success 'commit files empty attr' '
353 commit_check_warn false "" "" "" "" "" "" &&
354 commit_check_warn true "" "LF_CRLF" "" "LF_CRLF" "" "" &&
355 commit_check_warn input "" "" "CRLF_LF" "CRLF_LF" "" ""
358 test_expect_success 'commit files attr=auto' '
359 commit_check_warn false "auto" "$WILC" "$WICL" "$WAMIX" "" "" &&
360 commit_check_warn true "auto" "LF_CRLF" "" "LF_CRLF" "" "" &&
361 commit_check_warn input "auto" "" "CRLF_LF" "CRLF_LF" "" ""
364 test_expect_success 'commit files attr=text' '
365 commit_check_warn false "text" "$WILC" "$WICL" "$WAMIX" "$WILC" "$WICL" &&
366 commit_check_warn true "text" "LF_CRLF" "" "LF_CRLF" "LF_CRLF" "" &&
367 commit_check_warn input "text" "" "CRLF_LF" "CRLF_LF" "" "CRLF_LF"
370 test_expect_success 'commit files attr=-text' '
371 commit_check_warn false "-text" "" "" "" "" "" &&
372 commit_check_warn true "-text" "" "" "" "" "" &&
373 commit_check_warn input "-text" "" "" "" "" ""
376 test_expect_success 'commit files attr=lf' '
377 commit_check_warn false "lf" "" "CRLF_LF" "CRLF_LF" "" "CRLF_LF" &&
378 commit_check_warn true "lf" "" "CRLF_LF" "CRLF_LF" "" "CRLF_LF" &&
379 commit_check_warn input "lf" "" "CRLF_LF" "CRLF_LF" "" "CRLF_LF"
382 test_expect_success 'commit files attr=crlf' '
383 commit_check_warn false "crlf" "LF_CRLF" "" "LF_CRLF" "LF_CRLF" "" &&
384 commit_check_warn true "crlf" "LF_CRLF" "" "LF_CRLF" "LF_CRLF" "" &&
385 commit_check_warn input "crlf" "LF_CRLF" "" "LF_CRLF" "LF_CRLF" ""
388 # attr LF CRLF CRLFmixLF LF_mix_CR CRLFNUL
389 commit_chk_wrnNNO "" "" false "" "" "" "" ""
390 commit_chk_wrnNNO "" "" true LF_CRLF "" "" "" ""
391 commit_chk_wrnNNO "" "" input "" "" "" "" ""
393 commit_chk_wrnNNO "auto" "" false "$WILC" "" "" "" ""
394 commit_chk_wrnNNO "auto" "" true LF_CRLF "" "" "" ""
395 commit_chk_wrnNNO "auto" "" input "" "" "" "" ""
396 for crlf in true false input
398 commit_chk_wrnNNO -text "" $crlf "" "" "" "" ""
399 commit_chk_wrnNNO -text lf $crlf "" "" "" "" ""
400 commit_chk_wrnNNO -text crlf $crlf "" "" "" "" ""
401 commit_chk_wrnNNO "" lf $crlf "" CRLF_LF CRLF_LF "" CRLF_LF
402 commit_chk_wrnNNO "" crlf $crlf LF_CRLF "" LF_CRLF LF_CRLF ""
403 commit_chk_wrnNNO auto lf $crlf "" "" "" "" ""
404 commit_chk_wrnNNO auto crlf $crlf LF_CRLF "" "" "" ""
405 commit_chk_wrnNNO text lf $crlf "" CRLF_LF CRLF_LF "" CRLF_LF
406 commit_chk_wrnNNO text crlf $crlf LF_CRLF "" LF_CRLF LF_CRLF ""
409 commit_chk_wrnNNO "text" "" false "$WILC" "$WICL" "$WAMIX" "$WILC" "$WICL"
410 commit_chk_wrnNNO "text" "" true LF_CRLF "" LF_CRLF LF_CRLF ""
411 commit_chk_wrnNNO "text" "" input "" CRLF_LF CRLF_LF "" CRLF_LF
413 test_expect_success 'commit NNO and cleanup' '
414 git commit -m "commit files on top of NNO" &&
416 git -c core.autocrlf=false reset --hard
419 test_expect_success 'commit empty gitattribues' '
420 check_files_in_repo false "" LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul &&
421 check_files_in_repo true "" LF LF LF LF_mix_CR CRLF_nul &&
422 check_files_in_repo input "" LF LF LF LF_mix_CR CRLF_nul
425 test_expect_success 'commit text=auto' '
426 check_files_in_repo false "auto" LF LF LF LF_mix_CR CRLF_nul &&
427 check_files_in_repo true "auto" LF LF LF LF_mix_CR CRLF_nul &&
428 check_files_in_repo input "auto" LF LF LF LF_mix_CR CRLF_nul
431 test_expect_success 'commit text' '
432 check_files_in_repo false "text" LF LF LF LF_mix_CR LF_nul &&
433 check_files_in_repo true "text" LF LF LF LF_mix_CR LF_nul &&
434 check_files_in_repo input "text" LF LF LF LF_mix_CR LF_nul
437 test_expect_success 'commit -text' '
438 check_files_in_repo false "-text" LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul &&
439 check_files_in_repo true "-text" LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul &&
440 check_files_in_repo input "-text" LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul
443 for crlf in true false input
445 # attr aeol LF CRLF CRLF_mix_LF LF_mix_CR CRLFNUL
446 check_in_repo_NNO "" "" $crlf LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul
447 check_in_repo_NNO -text "" $crlf LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul
448 check_in_repo_NNO -text lf $crlf LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul
449 check_in_repo_NNO -text crlf $crlf LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul
450 check_in_repo_NNO auto "" $crlf LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul
451 check_in_repo_NNO auto lf $crlf LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul
452 check_in_repo_NNO auto crlf $crlf LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul
453 check_in_repo_NNO text "" $crlf LF LF LF LF_mix_CR LF_nul
454 check_in_repo_NNO text lf $crlf LF LF LF LF_mix_CR LF_nul
455 check_in_repo_NNO text crlf $crlf LF LF LF LF_mix_CR LF_nul
457 ################################################################################
458 # Check how files in the repo are changed when they are checked out
459 # How to read the table below:
460 # - checkout_files will check multiple files with a combination of settings
461 # and attributes (core.autocrlf=input is forbidden with core.eol=crlf)
463 # - parameter $1 : text in .gitattributs "" (empty) | auto | text | -text
464 # - parameter $2 : ident "" | i (i == ident)
465 # - parameter $3 : eol in .gitattributs "" (empty) | lf | crlf
466 # - parameter $4 : core.autocrlf false | true | input
467 # - parameter $5 : core.eol "" | lf | crlf | "native"
468 # - parameter $6 : reference for a file with only LF in the repo
469 # - parameter $7 : reference for a file with only CRLF in the repo
470 # - parameter $8 : reference for a file with mixed LF and CRLF in the repo
471 # - parameter $9 : reference for a file with LF and CR in the repo
472 # - parameter $10 : reference for a file with CRLF and a NUL (should be handled as binary when auto)
474 if test_have_prereq NATIVE_CRLF
477 MIX_LF_CR=CRLF_mix_CR
481 MIX_CRLF_LF=CRLF_mix_LF
486 export CRLF_MIX_LF_CR MIX NL
488 # Same handling with and without ident
491 for ceol in lf crlf native
493 for crlf in true false input
495 # -text overrides core.autocrlf and core.eol
496 # text and eol=crlf or eol=lf override core.autocrlf and core.eol
497 checkout_files -text "$id" "" "$crlf" "$ceol" LF CRLF CRLF_mix_LF LF_mix_CR LF_nul
498 checkout_files -text "$id" "lf" "$crlf" "$ceol" LF CRLF CRLF_mix_LF LF_mix_CR LF_nul
499 checkout_files -text "$id" "crlf" "$crlf" "$ceol" LF CRLF CRLF_mix_LF LF_mix_CR LF_nul
501 checkout_files text "$id" "lf" "$crlf" "$ceol" LF CRLF CRLF_mix_LF LF_mix_CR LF_nul
502 checkout_files text "$id" "crlf" "$crlf" "$ceol" CRLF CRLF CRLF CRLF_mix_CR CRLF_nul
503 # currently the same as text, eol=XXX
504 checkout_files auto "$id" "lf" "$crlf" "$ceol" LF CRLF CRLF_mix_LF LF_mix_CR LF_nul
505 checkout_files auto "$id" "crlf" "$crlf" "$ceol" CRLF CRLF CRLF_mix_LF LF_mix_CR LF_nul
508 # core.autocrlf false, different core.eol
509 checkout_files "" "$id" "" false "$ceol" LF CRLF CRLF_mix_LF LF_mix_CR LF_nul
511 checkout_files "" "$id" "" true "$ceol" CRLF CRLF CRLF_mix_LF LF_mix_CR LF_nul
512 # text: core.autocrlf = true overrides core.eol
513 checkout_files auto "$id" "" true "$ceol" CRLF CRLF CRLF_mix_LF LF_mix_CR LF_nul
514 checkout_files text "$id" "" true "$ceol" CRLF CRLF CRLF CRLF_mix_CR CRLF_nul
515 # text: core.autocrlf = input overrides core.eol
516 checkout_files text "$id" "" input "$ceol" LF CRLF CRLF_mix_LF LF_mix_CR LF_nul
517 checkout_files auto "$id" "" input "$ceol" LF CRLF CRLF_mix_LF LF_mix_CR LF_nul
518 # text=auto + eol=XXX
520 # text: core.autocrlf=false uses core.eol
521 checkout_files text "$id" "" false crlf CRLF CRLF CRLF CRLF_mix_CR CRLF_nul
522 checkout_files text "$id" "" false lf LF CRLF CRLF_mix_LF LF_mix_CR LF_nul
523 # text: core.autocrlf=false and core.eol unset(or native) uses native eol
524 checkout_files text "$id" "" false "" $NL CRLF $MIX_CRLF_LF $MIX_LF_CR $LFNUL
525 checkout_files text "$id" "" false native $NL CRLF $MIX_CRLF_LF $MIX_LF_CR $LFNUL
526 # auto: core.autocrlf=false and core.eol unset(or native) uses native eol
527 checkout_files auto "$id" "" false "" $NL CRLF CRLF_mix_LF LF_mix_CR LF_nul
528 checkout_files auto "$id" "" false native $NL CRLF CRLF_mix_LF LF_mix_CR LF_nul
531 # Should be the last test case: remove some files from the worktree
532 test_expect_success 'ls-files --eol -d -z' '
533 rm crlf_false_attr__CRLF.txt crlf_false_attr__CRLF_mix_LF.txt crlf_false_attr__LF.txt .gitattributes &&
534 cat >expect <<-\EOF &&
535 i/crlf w/ crlf_false_attr__CRLF.txt
536 i/lf w/ .gitattributes
537 i/lf w/ crlf_false_attr__LF.txt
538 i/mixed w/ crlf_false_attr__CRLF_mix_LF.txt
540 git ls-files --eol -d |
541 sed -e "s!attr/[^ ]*!!g" -e "s/ / /g" -e "s/ */ /g" |
543 test_cmp expect actual