Let's build git-cat with Python

Python is better than I expected

What is git-cat

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.

Overview of git objects

File tree

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)

I will read it first

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.

I want to know more

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

Let's build git-cat with Python
Let's run Excel with Python
Build python3 environment with ubuntu 16.04
Build python environment with direnv
Let's write python with cinema4d.
Build python 1.0
Build python virtual environment with virtualenv
Let's make a GUI with python.
Build Mysql + Python environment with docker
Let's play with Excel with Python [Beginner]
Build mlpy with python3.3 (64bit) (windows 64bit)
Let's do image scraping with Python
Automatically build Python documentation with Sphinx
Build Vim with MinGW. (+ lua, + python)
Let's analyze voice with Python # 1 FFT
Let's build a belief propagation method (Python)
FizzBuzz with Python3
Scraping with Python
Let's make a shiritori game with Python
Build Jupyter Lab (Python) environment with Docker
Statistics with python
Build a blockchain with Python ① Create a class
Scraping with Python
Python with Go
Let's create a free group with Python
Twilio with Python
Integrate with Python
Play with 2016-Python
[Introduction to Python] Let's use foreach with Python
AES256 with python
Let's read the RINEX file with Python ①
Tested with Python
Let's make a voice slowly with Python
python starts with ()
Build Python environment with Anaconda on Mac
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Let's do MySQL data manipulation with Python
Build a python virtual environment with pyenv
Let's make a web framework with Python! (1)
Let's make a Twitter Bot with Python!
Build a modern Python environment with Neovim
Let's develop an investment algorithm with Python 1
Let's get along with Python # 0 (Environment construction)
Build AI / machine learning environment with Python
Let's make a web framework with Python! (2)
Excel with Python
Microcomputer with Python
[Blender x Python] Let's get started with Blender Python !!
Cast with python
Let's solve simultaneous linear equations with Python sympy!
Build Python development environment with Visual Studio Code
Build a python environment with ansible on centos6
Let's write FizzBuzz with an error: Python Version
[Python] Build a Django development environment with Docker
Create a python3 build environment with Sublime Text3
[AWS] Let's build an ECS Cluster with CDK
Let's control EV3 motors and sensors with Python
Build a Python environment with OSX El capitan
Quickly build a Python Django environment with IntelliJ