Skip to content

Instantly share code, notes, and snippets.

@lordjabez
Last active April 6, 2023 12:20
Show Gist options
  • Save lordjabez/7f24c848a1424365617300b0d9bc2d04 to your computer and use it in GitHub Desktop.
Save lordjabez/7f24c848a1424365617300b0d9bc2d04 to your computer and use it in GitHub Desktop.
Python script to decode a git object
#!/usr/bin/env python3
import sys
import zlib
# Installation:
# curl -s https://gist.githubusercontent.com/lordjabez/7f24c848a1424365617300b0d9bc2d04/raw/67797f4fc6e19c48acff1188a47848f314f9e9e0/decode-git-object.py > /usr/local/bin/decode-git-object
# chmod +x /usr/local/bin/decode-git-object
# Usage:
# decode-git-object path/to/.git/objects/00/00000000000000000000000000000000000000
object_filename = sys.argv[1]
with open(object_filename, 'rb') as object_file:
raw_object_content = zlib.decompress(object_file.read())
header, content = raw_object_content.split(b'\x00', maxsplit=1)
object_type, content_size = header.decode().split()
try:
content = content.decode()
except UnicodeDecodeError:
pass
print(f'{object_type=}')
print(f'{content_size=}')
print()
print(content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment