Why is the git index file binary?

醉酒当歌 提交于 2019-12-07 07:29:07

问题


Most of the files in my Git directory are plain text files (except for the compressed loose objects and the packfiles). So I can just cat and edit files like .git/HEAD or .git/refs/heads/master and inspect the repository if it gets corrupted.

But the .git/index is a binary file. Wouldn't a plain text file be more useful because it can easily be modified by hand?

Scott Chacon shows in his presentation the following image (Slide 278):

In my opinion, this can easily be put to a plain text file.

So why is it a binary file rather than a plain text file?


回答1:


The index, as presented in "What does the git index contain EXACTLY?" contains metadata and, as noted below by Jazimov, references:

  • index entries: references to entries, with metadata (time, mode, size, SHA1, ...)
  • cached trees, that references to trees ("pre-computed hashes for trees that can be derived from the index"), which helps speed up tree object generation from index for a new commit.

The concatenation of those data makes it a binary file, although the actual reason is pure speculation. Not being able to modify it by hand could by one.




回答2:


None of the reasons given by the answer adequately addresses the question posed, which is "Why is the Git index file binary?". The accepted answer is just not correct. The index doesn't "contain" any plain-text files--it contains references to plain-text files. Furthermore, to say that the Git index contains "index entries" says really nothing useful at all, especially to a fellow developer seeking Truth... Finally, trees are not cached by the index--references to the trees are cached.

The index isn't binary because it's "indexed" (as the poster concluded in a comment above)--and it isn't binary for "performance reasons", per se. Everything in the index could be expressed using a pure text file--even the flags and bits expressed within the binary index file could be expressed as ASCII. It's binary because binary file formats that contain bit-wise flags are able to use disk space more efficiently. And, knowing Linus, it probably is binary so as to dissuade tampering by newbies with easy-access to text editors.

* New information * Version 4 of the index implements path compression, saving up to roughly 50% on the size of the index for large repos. (Source: https://git-scm.com/docs/git-update-index) This compression would lend itself to a binary-format index file.



来源:https://stackoverflow.com/questions/27246290/why-is-the-git-index-file-binary

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