@@ -224,16 +224,14 @@ static const char *line_ending(struct crlf_attrs *ca)
224
224
static int crlf_apply_to_workdir (
225
225
struct crlf_attrs * ca , git_buf * to , const git_buf * from )
226
226
{
227
+ git_buf_text_stats stats ;
227
228
const char * workdir_ending = NULL ;
229
+ bool is_binary ;
228
230
229
231
/* Empty file? Nothing to do. */
230
232
if (git_buf_len (from ) == 0 )
231
233
return 0 ;
232
234
233
- /* Don't filter binary files */
234
- if (git_buf_text_is_binary (from ))
235
- return GIT_PASSTHROUGH ;
236
-
237
235
/* Determine proper line ending */
238
236
workdir_ending = line_ending (ca );
239
237
if (!workdir_ending )
@@ -243,6 +241,29 @@ static int crlf_apply_to_workdir(
243
241
if (strcmp (workdir_ending , "\r\n" ) != 0 )
244
242
return GIT_PASSTHROUGH ;
245
243
244
+ /* If there are no LFs, or all LFs are part of a CRLF, nothing to do */
245
+ is_binary = git_buf_text_gather_stats (& stats , from , false);
246
+
247
+ if (stats .lf == 0 || stats .lf == stats .crlf )
248
+ return GIT_PASSTHROUGH ;
249
+
250
+ if (ca -> crlf_action == GIT_CRLF_AUTO ||
251
+ ca -> crlf_action == GIT_CRLF_GUESS ) {
252
+
253
+ /* If we have any existing CR or CRLF line endings, do nothing */
254
+ if (ca -> crlf_action == GIT_CRLF_GUESS &&
255
+ stats .cr > 0 && stats .crlf > 0 )
256
+ return GIT_PASSTHROUGH ;
257
+
258
+ /* If we have bare CR characters, do nothing */
259
+ if (stats .cr != stats .crlf )
260
+ return GIT_PASSTHROUGH ;
261
+
262
+ /* Don't filter binary files */
263
+ if (is_binary )
264
+ return GIT_PASSTHROUGH ;
265
+ }
266
+
246
267
return git_buf_text_lf_to_crlf (to , from );
247
268
}
248
269
0 commit comments