Do I really need to specify all binary files in .gitattributes

喜夏-厌秋 提交于 2021-01-21 09:22:07

问题


I've read Git documentation that shows that I can explicitly set certain files to be treated as text, so their line endings are automatically changed or as binary to ensure that they are untouched.

However, I have also read that Git is pretty good at detecting binary files, which makes me thing this is not needed. So my question is do I really need to specify these explicit settings for every single file extension in my repository? I've seen some recommend to do so for all image file extensions.

# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.c text
*.h text

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary

回答1:


Git will check the first 8,000 bytes of a file to see if it contains a NUL character. If it does, the file is assumed to be binary.

From git's source code:

#define FIRST_FEW_BYTES 8000
int buffer_is_binary(const char *ptr, unsigned long size)
{
    if (FIRST_FEW_BYTES < size)
        size = FIRST_FEW_BYTES;
    return !!memchr(ptr, 0, size);
}

For text files, unless you intentionally insert a NUL character for some reason, they'll be correctly guessed. For binaries, it's more than likely that the first 8,000 bytes will contain at least a single instance.

For the most part, you shouldn't need to declare a file's type explicitly (I don't think I ever have). Realistically, just declare a specific file if you run into an issue.




回答2:


Git is, in general, good about detecting whether a file is text or binary, and so you may not explicitly need to set anything. Setting a default of * text=auto is a good idea regardless, as you point out.

However, if you or anyone working on the project is working with files in UTF-16, it's a very good idea to explicitly set the text attribute on those files, as well as the working-tree-encoding attribute, since Git will notice the NUL bytes in them and think of them as binary.

You should also specify any file type as binary that you think might be misdetected as text. For example, if you have some image format or file that consists only of printable ASCII bytes, Git might misdetect that as text. You'd want to specify those files explicitly to avoid confusion. Only you would know which files in your repository are likely to hit that issue.



来源:https://stackoverflow.com/questions/57030698/do-i-really-need-to-specify-all-binary-files-in-gitattributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!