Last active
April 6, 2023 12:20
-
-
Save lordjabez/7f24c848a1424365617300b0d9bc2d04 to your computer and use it in GitHub Desktop.
Python script to decode a git object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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