@matheusbsbs / matheus-bagliano / commits / 28624193b2
Fork 0

treat any file with NUL as binary

There are two heuristics in Git to detect whether a file is binary or text. One in xdiff-interface.c (which is taken from GNU diff) relies on existence of the NUL byte at the beginning. However, convert.c used a different heuristic, which relied on the percent of non-printable symbols (less than 1% for text files). Due to differences in detection whether a file is binary or not, it was possible that a file that diff treats as binary could be treated as text by CRLF conversion. This is very confusing for a user who sees that 'git diff' shows the file as binary expects it to be added as binary. This patch makes is_binary to consider any file that contains at least one NUL character as binary, to ensure that the heuristics used for CRLF conversion is tighter than what is used by diff. Signed-off-by: Dmitry Potapov <dpotapov@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Dmitry Potapov committed Jan 16, 2008 at 04:59 UTC 28624193b2ce622144136d373e136455b6b13967
1 file changed +7 -2
convert.c
+7 -2
@@ -17,8 +17,8 @@
17 #define CRLF_INPUT 2
18
19 struct text_stat {
20 - /* CR, LF and CRLF counts */
21 - unsigned cr, lf, crlf;
20 + /* NUL, CR, LF and CRLF counts */
21 + unsigned nul, cr, lf, crlf;
22
23 /* These are just approximations! */
24 unsigned printable, nonprintable;
@@ -51,6 +51,9 @@ static void gather_stats(const char *buf, unsigned long size, struct text_stat *
51 case '\b': case '\t': case '\033': case '\014':
52 stats->printable++;
53 break;
54 + case 0:
55 + stats->nul++;
56 + /* fall through */
57 default:
58 stats->nonprintable++;
59 }
@@ -66,6 +69,8 @@ static void gather_stats(const char *buf, unsigned long size, struct text_stat *
69 static int is_binary(unsigned long size, struct text_stat *stats)
70 {
71
72 + if (stats->nul)
73 + return 1;
74 if ((stats->printable >> 7) < stats->nonprintable)
75 return 1;
76 /*