Skip to content

Instantly share code, notes, and snippets.

Created November 24, 2022 21:32
What would you like to do?
#!/usr/bin/env python3
import os
import re
import sys
import zlib
import base64
import subprocess
from hashlib import sha1
from pathlib import Path
from itertools import chain
def check_output(*a, **kw):
if 'encoding' not in kw:
kw['encoding'] = 'utf-8'
result = subprocess.check_output(*a, **kw)
return result
def current_commit():
return check_output(['git', 'log', '-1', '--pretty=format:%H']).strip()
def parent_commit():
return check_output(['git', 'log', '-1', '--pretty=format:%P']).strip()
def raw_commit(hash_):
path = Path('.git', 'objects', hash_[0:2], hash_[2:])
if path.is_file():
with open(path, 'rb') as f:
raw = zlib.decompress(
return raw[raw.index(b'\0')+1:]
def write_tree():
return check_output(['git', 'write-tree']).strip()
def rev_parse_head():
return check_output(['git', 'rev-parse', 'HEAD']).strip()
def hash_commit_body(body):
output = check_output(['git', 'hash-object', '-t', 'commit', '--stdin'], input=body, encoding=None).decode()
return output.strip()
def apply_commit_body(body):
return check_output(['git', 'hash-object', '-t', 'commit', '--stdin', '-w'], input=body, encoding=None).decode()
if __name__ == '__main__':
parent = parent_commit()
current = current_commit()
body = raw_commit(current)
#check_output(['git', 'reset', '--hard', parent])
tree = write_tree()
new_parent = rev_parse_head()
lines = []
headers = True
for line in body.split(b'\n'):
if headers:
#if line.startswith(b'tree '):
# line = b'tree %s' % tree.encode()
if line.startswith(b'parent '):
line = b'parent %s' % new_parent.encode()
elif line == b'':
lines.append(b'magic_%s placeholder' % new_parent.encode())
headers = False
print('modified commit')
merged = b'\n'.join(lines)
sha1 = hash_commit_body(merged)
check_output(['git', 'reset', '--hard', sha1])
check_output(['git', 'log', '--pretty=format:%ae'], encoding=None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment