I know that Git somehow automatically detects if a file is binary or text and that .gitattributes can be used to set this manually if needed. But is there also a way to ask Git how it treats a file?
So let’s say I have a Git repository with two files in it: An ascii.dat file containing plain-text and a binary.dat file containing random binary stuff. Git handles the first .dat file as text and the secondary file as binary. Now I want to write a Git web front end which has a viewer for text files and a special viewer for binary files (displaying a hex dump for example). Sure, I could implement my own text/binary check but it would be more useful if the viewer relies on the information how Git handles these files.
So how can I ask Git if it treats a file as text or binary?
builtin_diff()1 callsdiff_filespec_is_binary()which callsbuffer_is_binary()which checks for any occurrence of a zero byte (NUL “character”) in the first 8000 bytes (or the entire length if shorter).I do not see that this “is it binary?” test is explicitly exposed in any command though.
git merge-filedirectly usesbuffer_is_binary(), so you may be able to make use of it:It seems to produce the error message like
error: Cannot merge binary files: file-to-testand yields an exit status of 255 when given a binary file. I am not sure I would want to rely on this behavior though.Maybe
git diff --numstatwould be more reliable:For binary files, the
--numstatoutput should start with-TAB-TAB, so we just test for that.1
builtin_diff()has strings likeBinary files %s and %s differthat should be familiar.