Python is better than I expected
git-cat is a single program of cat-file, which is one of the git commands that displays git objects (the ones under .git / objects /) in a way that is easy for people to understand. I thought it could be used when building a Git viewer with PHP, so I decided to build it with Python, which I've been curious about recently.
Due to the nature of the git version control system, it has a slightly special structure. I'll leave the details to other more detailed people, but to make git-cat, I need some of the following.
If you open the above file, you will see a 40-digit hexadecimal number. This is the latest commit hash for that branch. (The reason for using hashes is that the possibility of collision is extremely low.) Below, maybe I thought it was awkward to have a large number of files composed of 40-digit hashes, decomposed into the first 2 digits + the remaining 38 digits, so that the first 2 digits are used as the directory name to some extent. It has become. (Still, I'm not the only one who thinks it's dirty)
Even if you open the file as it is, only a list of characters that you do not understand will appear. The reason is that they are all strings compressed by zlib. So let's unzip it first.
#!/usr/bin/python
import zlib
import sys
if __name__ = "__main__":
fname = sys.argv[1]
fd = open(fname, "rb");
line = fd.read()
print zlib.decompress(line)
Save this in a suitable location, make sure zlib is ready for Python, and try ./git-cat xxx / .git / objects / 00/112233445566778899aabbccddeeff00112233
, it makes sense. I think it was displayed as a certain character string.
As you try various files, you will find that there is always an identifier (?) And a number such as tree or commit at the beginning, and each content is entered after \ 0 is inserted. I wondered if there was any explanation for this or something, and when I looked it up, I was lucky to find a page called http://linquize.blogspot.hk/2011/10/supplemental-information-for-git.html. it is complete. I'll omit the code because all I have to do is process the strings, but it was easier than I expected. If you make it in this way, you can make git's python / PHP wrapper relatively easily.
Recommended Posts