Skip to content

Commit 720b176

Browse files
committed
Merge branch 'tb/check-crlf-for-safe-crlf'
The "safe crlf" check incorrectly triggered for contents that does not use CRLF as line endings, which has been corrected. * tb/check-crlf-for-safe-crlf: t0027: Adapt the new MIX tests to Windows convert: tighten the safe autocrlf handling
2 parents 61061ab + 649f1f0 commit 720b176

File tree

2 files changed

+86
-10
lines changed

2 files changed

+86
-10
lines changed

convert.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,18 +220,27 @@ static void check_safe_crlf(const char *path, enum crlf_action crlf_action,
220220
}
221221
}
222222

223-
static int has_cr_in_index(const struct index_state *istate, const char *path)
223+
static int has_crlf_in_index(const struct index_state *istate, const char *path)
224224
{
225225
unsigned long sz;
226226
void *data;
227-
int has_cr;
227+
const char *crp;
228+
int has_crlf = 0;
228229

229230
data = read_blob_data_from_index(istate, path, &sz);
230231
if (!data)
231232
return 0;
232-
has_cr = memchr(data, '\r', sz) != NULL;
233+
234+
crp = memchr(data, '\r', sz);
235+
if (crp) {
236+
unsigned int ret_stats;
237+
ret_stats = gather_convert_stats(data, sz);
238+
if (!(ret_stats & CONVERT_STAT_BITS_BIN) &&
239+
(ret_stats & CONVERT_STAT_BITS_TXT_CRLF))
240+
has_crlf = 1;
241+
}
233242
free(data);
234-
return has_cr;
243+
return has_crlf;
235244
}
236245

237246
static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats,
@@ -290,7 +299,7 @@ static int crlf_to_git(const struct index_state *istate,
290299
* cherry-pick.
291300
*/
292301
if ((checksafe != SAFE_CRLF_RENORMALIZE) &&
293-
has_cr_in_index(istate, path))
302+
has_crlf_in_index(istate, path))
294303
convert_crlf_into_lf = 0;
295304
}
296305
if ((checksafe == SAFE_CRLF_WARN ||

t/t0027-auto-crlf.sh

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,31 @@ create_gitattributes () {
4343
} >.gitattributes
4444
}
4545

46-
create_NNO_files () {
46+
# Create 2 sets of files:
47+
# The NNO files are "Not NOrmalized in the repo. We use CRLF_mix_LF and store
48+
# it under different names for the different test cases, see ${pfx}
49+
# Depending on .gitattributes they are normalized at the next commit (or not)
50+
# The MIX files have different contents in the repo.
51+
# Depending on its contents, the "new safer autocrlf" may kick in.
52+
create_NNO_MIX_files () {
4753
for crlf in false true input
4854
do
4955
for attr in "" auto text -text
5056
do
5157
for aeol in "" lf crlf
5258
do
53-
pfx=NNO_attr_${attr}_aeol_${aeol}_${crlf}
59+
pfx=NNO_attr_${attr}_aeol_${aeol}_${crlf} &&
5460
cp CRLF_mix_LF ${pfx}_LF.txt &&
5561
cp CRLF_mix_LF ${pfx}_CRLF.txt &&
5662
cp CRLF_mix_LF ${pfx}_CRLF_mix_LF.txt &&
5763
cp CRLF_mix_LF ${pfx}_LF_mix_CR.txt &&
58-
cp CRLF_mix_LF ${pfx}_CRLF_nul.txt
64+
cp CRLF_mix_LF ${pfx}_CRLF_nul.txt &&
65+
pfx=MIX_attr_${attr}_aeol_${aeol}_${crlf} &&
66+
cp LF ${pfx}_LF.txt &&
67+
cp CRLF ${pfx}_CRLF.txt &&
68+
cp CRLF_mix_LF ${pfx}_CRLF_mix_LF.txt &&
69+
cp LF_mix_CR ${pfx}_LF_mix_CR.txt &&
70+
cp CRLF_nul ${pfx}_CRLF_nul.txt
5971
done
6072
done
6173
done
@@ -136,6 +148,49 @@ commit_chk_wrnNNO () {
136148
'
137149
}
138150

151+
# Commit a file with mixed line endings on top of different files
152+
# in the index. Check for warnings
153+
commit_MIX_chkwrn () {
154+
attr=$1 ; shift
155+
aeol=$1 ; shift
156+
crlf=$1 ; shift
157+
lfwarn=$1 ; shift
158+
crlfwarn=$1 ; shift
159+
lfmixcrlf=$1 ; shift
160+
lfmixcr=$1 ; shift
161+
crlfnul=$1 ; shift
162+
pfx=MIX_attr_${attr}_aeol_${aeol}_${crlf}
163+
#Commit file with CLRF_mix_LF on top of existing file
164+
create_gitattributes "$attr" $aeol &&
165+
for f in LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul
166+
do
167+
fname=${pfx}_$f.txt &&
168+
cp CRLF_mix_LF $fname &&
169+
printf Z >>"$fname" &&
170+
git -c core.autocrlf=$crlf add $fname 2>"${pfx}_$f.err"
171+
done
172+
173+
test_expect_success "commit file with mixed EOL onto LF crlf=$crlf attr=$attr" '
174+
check_warning "$lfwarn" ${pfx}_LF.err
175+
'
176+
test_expect_success "commit file with mixed EOL onto CLRF attr=$attr aeol=$aeol crlf=$crlf" '
177+
check_warning "$crlfwarn" ${pfx}_CRLF.err
178+
'
179+
180+
test_expect_success "commit file with mixed EOL onto CRLF_mix_LF attr=$attr aeol=$aeol crlf=$crlf" '
181+
check_warning "$lfmixcrlf" ${pfx}_CRLF_mix_LF.err
182+
'
183+
184+
test_expect_success "commit file with mixed EOL onto LF_mix_cr attr=$attr aeol=$aeol crlf=$crlf " '
185+
check_warning "$lfmixcr" ${pfx}_LF_mix_CR.err
186+
'
187+
188+
test_expect_success "commit file with mixed EOL onto CRLF_nul attr=$attr aeol=$aeol crlf=$crlf" '
189+
check_warning "$crlfnul" ${pfx}_CRLF_nul.err
190+
'
191+
}
192+
193+
139194
stats_ascii () {
140195
case "$1" in
141196
LF)
@@ -323,8 +378,8 @@ test_expect_success 'setup master' '
323378
printf "\$Id: 0000000000000000000000000000000000000000 \$\r\nLINEONE\r\nLINETWO\rLINETHREE" >CRLF_mix_CR &&
324379
printf "\$Id: 0000000000000000000000000000000000000000 \$\r\nLINEONEQ\r\nLINETWO\r\nLINETHREE" | q_to_nul >CRLF_nul &&
325380
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 &&
381+
create_NNO_MIX_files &&
382+
git -c core.autocrlf=false add NNO_*.txt MIX_*.txt &&
328383
git commit -m "mixed line endings" &&
329384
test_tick
330385
'
@@ -385,6 +440,18 @@ test_expect_success 'commit files attr=crlf' '
385440
commit_check_warn input "crlf" "LF_CRLF" "" "LF_CRLF" "LF_CRLF" ""
386441
'
387442

443+
# Commit "CRLFmixLF" on top of these files already in the repo:
444+
# mixed mixed mixed mixed mixed
445+
# onto onto onto onto onto
446+
# attr LF CRLF CRLFmixLF LF_mix_CR CRLFNUL
447+
commit_MIX_chkwrn "" "" false "" "" "" "" ""
448+
commit_MIX_chkwrn "" "" true "LF_CRLF" "" "" "LF_CRLF" "LF_CRLF"
449+
commit_MIX_chkwrn "" "" input "CRLF_LF" "" "" "CRLF_LF" "CRLF_LF"
450+
451+
commit_MIX_chkwrn "auto" "" false "$WAMIX" "" "" "$WAMIX" "$WAMIX"
452+
commit_MIX_chkwrn "auto" "" true "LF_CRLF" "" "" "LF_CRLF" "LF_CRLF"
453+
commit_MIX_chkwrn "auto" "" input "CRLF_LF" "" "" "CRLF_LF" "CRLF_LF"
454+
388455
# attr LF CRLF CRLFmixLF LF_mix_CR CRLFNUL
389456
commit_chk_wrnNNO "" "" false "" "" "" "" ""
390457
commit_chk_wrnNNO "" "" true LF_CRLF "" "" "" ""

0 commit comments

Comments
 (0)