]> git.ipfire.org Git - thirdparty/git.git/commitdiff
treat any file with NUL as binary
authorDmitry Potapov <dpotapov@gmail.com>
Wed, 16 Jan 2008 01:59:12 +0000 (04:59 +0300)
committerJunio C Hamano <gitster@pobox.com>
Wed, 16 Jan 2008 17:10:34 +0000 (09:10 -0800)
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>
convert.c

index 4df75595b1f3e689a9685039c372ffb4c8688291..80f114b2e2d169eef2a046d112d7e8729f2c1880 100644 (file)
--- a/convert.c
+++ b/convert.c
@@ -17,8 +17,8 @@
 #define CRLF_INPUT     2
 
 struct text_stat {
-       /* CR, LF and CRLF counts */
-       unsigned cr, lf, crlf;
+       /* NUL, CR, LF and CRLF counts */
+       unsigned nul, cr, lf, crlf;
 
        /* These are just approximations! */
        unsigned printable, nonprintable;
@@ -51,6 +51,9 @@ static void gather_stats(const char *buf, unsigned long size, struct text_stat *
                        case '\b': case '\t': case '\033': case '\014':
                                stats->printable++;
                                break;
+                       case 0:
+                               stats->nul++;
+                               /* fall through */
                        default:
                                stats->nonprintable++;
                        }
@@ -66,6 +69,8 @@ static void gather_stats(const char *buf, unsigned long size, struct text_stat *
 static int is_binary(unsigned long size, struct text_stat *stats)
 {
 
+       if (stats->nul)
+               return 1;
        if ((stats->printable >> 7) < stats->nonprintable)
                return 1;
        /*