Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 47e9a6c

Browse files
author
Edward Thomson
committed
crlf: use statistics to control to workdir filter
Use statistics (like core git) to control the behavior of the to workdir CRLF filter.
1 parent 71686dd commit 47e9a6c

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/crlf.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,16 +224,14 @@ static const char *line_ending(struct crlf_attrs *ca)
224224
static int crlf_apply_to_workdir(
225225
struct crlf_attrs *ca, git_buf *to, const git_buf *from)
226226
{
227+
git_buf_text_stats stats;
227228
const char *workdir_ending = NULL;
229+
bool is_binary;
228230

229231
/* Empty file? Nothing to do. */
230232
if (git_buf_len(from) == 0)
231233
return 0;
232234

233-
/* Don't filter binary files */
234-
if (git_buf_text_is_binary(from))
235-
return GIT_PASSTHROUGH;
236-
237235
/* Determine proper line ending */
238236
workdir_ending = line_ending(ca);
239237
if (!workdir_ending)
@@ -243,6 +241,29 @@ static int crlf_apply_to_workdir(
243241
if (strcmp(workdir_ending, "\r\n") != 0)
244242
return GIT_PASSTHROUGH;
245243

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+
246267
return git_buf_text_lf_to_crlf(to, from);
247268
}
248269

0 commit comments

Comments
 (0)