0

The Git documentation says of text=auto (emphasis added):

When text is set to "auto", Git decides by itself whether the file is text or binary. If it is text and the file was not already in Git with CRLF endings, line endings are converted on checkin and checkout as described above. Otherwise, no conversion is done on checkin or checkout.

But I don't see an explanation of how Git makes this decision.

How does Git decide if a file is text or binary? Is the algorithm documented somewhere? Is it substantially different across Git versions?

shadowtalker
  • 12,529
  • 3
  • 53
  • 96
  • 3
    The linked question is certainly not a duplicate as such, but the accepted answer does seem to answer the question I asked here. I'm not sure what the usual policy is in this case. However I think having my question linked to that one will be helpful for people searching in the future so I certainly do not want to delete mine. – shadowtalker Jun 12 '23 at 15:12
  • I don't see a linked question here, but [this answer](https://stackoverflow.com/a/6134127/147356) seems to completely answer your question. – larsks Jun 16 '23 at 13:33
  • @larsks the question was previously marked as a duplicate of that question, but it was just reopened, so they're no longer linked. – shadowtalker Jun 16 '23 at 14:45

1 Answers1

0

As explained in https://stackoverflow.com/a/6134127/2954547:

Git v2.30.0 determines that a file is "binary" if it contains a zero byte (ASCII NUL, often denoted \0 in programming languages) in the first 8000 bytes of the file.

builtin_diff()1 calls diff_filespec_is_binary() which calls buffer_is_binary() which checks for any occurrence of a zero byte (NUL “character”) in the first 8000 bytes (or the entire length if shorter).

1 builtin_diff() has strings like Binary files %s and %s differ that should be familiar.

This is a fairly crude check, but it's also parsimonious and kind of clever. It would certainly be unusual to have a literal zero byte in most text files.

shadowtalker
  • 12,529
  • 3
  • 53
  • 96