Skip to content

Instantly share code, notes, and snippets.

@leonidessaguisagjr
Last active July 30, 2023 06:46
Show Gist options
  • Save leonidessaguisagjr/594cd8fbbc9b18a1dde5084d981b8028 to your computer and use it in GitHub Desktop.
Save leonidessaguisagjr/594cd8fbbc9b18a1dde5084d981b8028 to your computer and use it in GitHub Desktop.
Studying git internals: implement `git cat-file` in Python
#!/usr/bin/env python3
"""Implementation of git cat-file in Python.
This Gist is published under the MIT License:
MIT License
Copyright (c) 2019 Leonides T. Saguisag, Jr.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
My notes:
This is done in the spirit of Glen Jarvis' presentation, "Exploring Git
Internals using Python" found here:
https://github.com/glenjarvis/explore_git_internals
This is primarily for educational purposes i.e. the point is not to re-implement
Git in pure Python but rather to better understand what is going on "under the
hood" of Git.
Usage for this script should be reasonably similar to 'git cat-file' i.e.
Display the object type: py-git-cat-file.py -t <sha1>
Display the object size: py-git-cat-file.py -s <sha1>
Pretty-print the object: py-git-cat-file.py -p <sha1>
TODO:
* Add support for Git "tag" objects (currently this script only supports
"blob", "commit" and "tree" objects.)
Reference:
* https://git-scm.com/book/en/v2/Git-Internals-Git-Objects
Some quick notes on format of git objects:
* Git objects are zlib compressed on disk.
* The general format of a Git object:
<object type>[SPACE]<object size>[NULL]<object content>
* For a tree object, the content format is:
<filemode>[SPACE]<filename>[NULL]<SHA1>
"""
import argparse
import binascii
import os
import sys
import zlib
class GitObject(object):
"""Class that wraps around a Git object."""
def __init__(self, git_root, object_hash):
"""Initialize a new GitObject.
:param git_root: string - Path that contains the .git Directory
:param object_hash: string - SHA1 hash of the git object to open.
"""
self.git_root = git_root
self.object_hash = object_hash
self._raw_data = None
self._data = None
self._type = None
self._size = None
self._contents = None
if not os.path.isdir(self.git_root):
raise ValueError(
"Directory {0} does not exist!".format(
self.git_root))
# Given the SHA1 hash of the git object, the location on disk will be
# .git/objects/<1st 2 letters of SHA1>/<remaining 38 letters of SHA1>
object_dir = self.object_hash[:2]
object_filename = self.object_hash[2:]
self.filename = os.path.join(self.git_root,
".git",
"objects",
object_dir,
object_filename)
if not os.path.isfile(self.filename):
raise ValueError(
"Unable to find git object: {0}".format(
self.object_hash))
with open(self.filename, "rb") as file_obj:
self._raw_data = file_obj.read()
self._data = zlib.decompress(self._raw_data)
@property
def type(self):
"""Returns a string indicating the type of the object."""
if self._type is None:
self._type = self._data.split(b' ', maxsplit=1)[0].decode()
return self._type
@property
def size(self):
""""Returns an integer indicating the size of the object."""
if self._size is None:
self._size = self._data.split(b'\x00', maxsplit=1)[0]
self._size = self._size.split(b' ', maxsplit=1)[1]
self._size = int(self._size)
return self._size
@property
def contents(self):
"""Returns the contents of the Git object.
In the case of a 'blob' or 'commit' object, a string is returned.
In the case of a 'tree' object, a list of tuples is returned where
each tuple contains the following: (filemode, filename, sha1)
"""
if self._contents is None:
contents = self._data.split(b'\x00', maxsplit=1)[1]
if self.type in ["blob", "commit"]:
self._contents = contents.decode()
elif self.type == "tree":
self._contents = list()
while contents != b'':
filemode, contents = contents.split(b' ', maxsplit=1)
filename, contents = contents.split(b'\x00', maxsplit=1)
sha1, contents = contents[:20], contents[20:]
filemode = filemode.decode()
filename = filename.decode()
sha1 = binascii.hexlify(sha1).decode()
self._contents.append((filemode, filename, sha1))
else:
self._contents = contents
return self._contents
class GitTreeContent(object):
"""Class for wrapping around the contents of a Git 'tree' object."""
def __init__(self, contents):
"""Initializes a new GitTreeContent object.
:param contents: list - List of tuples representing the contents of a
Git 'tree' object.
"""
self.contents = contents
def __str__(self):
"""Returns a pretty-printed version of a 'tree' object's contents.
Format is like 'git cat-file -p': <mode> <type> <sha1> <filename>
"""
result = list()
for tree_item in self.contents:
obj_type = "None"
if tree_item[0] == "100644":
obj_type = "blob"
elif tree_item[0] == "40000":
obj_type = "tree"
result.append(
"{filemode:0>6} {obj_type} {sha1} {filename}".format(
filemode=tree_item[0],
obj_type=obj_type,
sha1=tree_item[2],
filename=tree_item[1]))
return "\n".join(result)
def find_git_root(starting_path=None):
"""Finds the directory that contains the .git directory.
:param starting_path: string - Directory to start searching for Git root.
If no starting_path is provided, it starts at the current working
directory and recursively goes up the parent dirs searching for the .git
directory. If it gets all the way to the root and still fails to find
a .git directory, it returns 'None'.
"""
if starting_path is None:
starting_path = os.path.abspath(os.getcwd())
if os.path.isdir(os.path.join(starting_path, ".git")):
return starting_path
elif os.path.dirname(starting_path) == starting_path:
# We're at root already, can't go up any further...
return None
else:
parent_dir = os.path.abspath(
os.path.join(
starting_path,
os.path.pardir))
return find_git_root(parent_dir)
def main():
"""Main entry point of script."""
git_root = find_git_root()
if git_root is None:
print("ERROR: Not a git repository!")
sys.exit(1)
parser = argparse.ArgumentParser(description="View git objects.")
group = parser.add_mutually_exclusive_group()
group.add_argument(
"-t",
"--type",
action="store_true",
help="Show object type")
group.add_argument(
"-s",
"--size",
action="store_true",
help="Show object size")
group.add_argument(
"-p",
"--pretty-print",
action="store_true",
help="Prett-print object's content")
parser.add_argument("object_hash", help="SHA1 hash of git object to view.")
args = parser.parse_args()
git_object = GitObject(git_root, args.object_hash)
if args.type:
print(git_object.type)
elif args.size:
print(git_object.size)
elif args.pretty_print:
if git_object.type == "tree":
print(GitTreeContent(git_object.contents))
else:
print(git_object.contents)
else:
parser.print_help()
if __name__ == "__main__":
main()
@yeger00
Copy link

yeger00 commented Jul 10, 2019

Hi,
I saw your snippet and it will help me with a project I'm working on (graph-diff).
Is it possible to use it? What is the license?

Thanks,
Yeger

@leonidessaguisagjr
Copy link
Author

Hello, I have updated the contents of the gist to indicate that I am publishing it under the MIT license. I hope that helps!

@vimkim
Copy link

vimkim commented Jul 30, 2023

Line number 113, there are 4 double quotes, just fyi!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment