@@ -31,7 +31,7 @@ enum crlf_action {
31
31
32
32
struct text_stat {
33
33
/* NUL, CR, LF and CRLF counts */
34
- unsigned nul , cr , lf , crlf ;
34
+ unsigned nul , lonecr , lonelf , crlf ;
35
35
36
36
/* These are just approximations! */
37
37
unsigned printable , nonprintable ;
@@ -46,13 +46,15 @@ static void gather_stats(const char *buf, unsigned long size, struct text_stat *
46
46
for (i = 0 ; i < size ; i ++ ) {
47
47
unsigned char c = buf [i ];
48
48
if (c == '\r' ) {
49
- stats -> cr ++ ;
50
- if (i + 1 < size && buf [i + 1 ] == '\n' )
49
+ if (i + 1 < size && buf [i + 1 ] == '\n' ) {
51
50
stats -> crlf ++ ;
51
+ i ++ ;
52
+ } else
53
+ stats -> lonecr ++ ;
52
54
continue ;
53
55
}
54
56
if (c == '\n' ) {
55
- stats -> lf ++ ;
57
+ stats -> lonelf ++ ;
56
58
continue ;
57
59
}
58
60
if (c == 127 )
@@ -86,7 +88,7 @@ static void gather_stats(const char *buf, unsigned long size, struct text_stat *
86
88
*/
87
89
static int convert_is_binary (unsigned long size , const struct text_stat * stats )
88
90
{
89
- if (stats -> cr != stats -> crlf )
91
+ if (stats -> lonecr )
90
92
return 1 ;
91
93
if (stats -> nul )
92
94
return 1 ;
@@ -98,19 +100,18 @@ static int convert_is_binary(unsigned long size, const struct text_stat *stats)
98
100
static unsigned int gather_convert_stats (const char * data , unsigned long size )
99
101
{
100
102
struct text_stat stats ;
103
+ int ret = 0 ;
101
104
if (!data || !size )
102
105
return 0 ;
103
106
gather_stats (data , size , & stats );
104
107
if (convert_is_binary (size , & stats ))
105
- return CONVERT_STAT_BITS_BIN ;
106
- else if (stats .crlf && stats .crlf == stats .lf )
107
- return CONVERT_STAT_BITS_TXT_CRLF ;
108
- else if (stats .crlf && stats .lf )
109
- return CONVERT_STAT_BITS_TXT_CRLF | CONVERT_STAT_BITS_TXT_LF ;
110
- else if (stats .lf )
111
- return CONVERT_STAT_BITS_TXT_LF ;
112
- else
113
- return 0 ;
108
+ ret |= CONVERT_STAT_BITS_BIN ;
109
+ if (stats .crlf )
110
+ ret |= CONVERT_STAT_BITS_TXT_CRLF ;
111
+ if (stats .lonelf )
112
+ ret |= CONVERT_STAT_BITS_TXT_LF ;
113
+
114
+ return ret ;
114
115
}
115
116
116
117
static const char * gather_convert_stats_ascii (const char * data , unsigned long size )
@@ -207,7 +208,7 @@ static void check_safe_crlf(const char *path, enum crlf_action crlf_action,
207
208
* CRLFs would be added by checkout:
208
209
* check if we have "naked" LFs
209
210
*/
210
- if (stats -> lf != stats -> crlf ) {
211
+ if (stats -> lonelf ) {
211
212
if (checksafe == SAFE_CRLF_WARN )
212
213
warning ("LF will be replaced by CRLF in %s.\nThe file will have its original line endings in your working directory." , path );
213
214
else /* i.e. SAFE_CRLF_FAIL */
@@ -266,8 +267,8 @@ static int crlf_to_git(const char *path, const char *src, size_t len,
266
267
267
268
check_safe_crlf (path , crlf_action , & stats , checksafe );
268
269
269
- /* Optimization: No CR ? Nothing to convert, regardless. */
270
- if (!stats .cr )
270
+ /* Optimization: No CRLF ? Nothing to convert, regardless. */
271
+ if (!stats .crlf )
271
272
return 0 ;
272
273
273
274
/*
@@ -314,19 +315,15 @@ static int crlf_to_worktree(const char *path, const char *src, size_t len,
314
315
315
316
gather_stats (src , len , & stats );
316
317
317
- /* No LF? Nothing to convert, regardless. */
318
- if (!stats .lf )
319
- return 0 ;
320
-
321
- /* Was it already in CRLF format? */
322
- if (stats .lf == stats .crlf )
318
+ /* No "naked" LF? Nothing to convert, regardless. */
319
+ if (!stats .lonelf )
323
320
return 0 ;
324
321
325
322
if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF ) {
326
323
if (crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF ) {
327
324
/* If we have any CR or CRLF line endings, we do not touch it */
328
325
/* This is the new safer autocrlf-handling */
329
- if (stats .cr > 0 || stats .crlf > 0 )
326
+ if (stats .lonecr || stats .crlf )
330
327
return 0 ;
331
328
}
332
329
@@ -338,7 +335,7 @@ static int crlf_to_worktree(const char *path, const char *src, size_t len,
338
335
if (src == buf -> buf )
339
336
to_free = strbuf_detach (buf , NULL );
340
337
341
- strbuf_grow (buf , len + stats .lf - stats . crlf );
338
+ strbuf_grow (buf , len + stats .lonelf );
342
339
for (;;) {
343
340
const char * nl = memchr (src , '\n' , len );
344
341
if (!nl )
0 commit comments