Skip to content

Instantly share code, notes, and snippets.

@ryancdotorg
Created November 24, 2022 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryancdotorg/5af82332124ca5f52e175fcc6a3e68a7 to your computer and use it in GitHub Desktop.
Save ryancdotorg/5af82332124ca5f52e175fcc6a3e68a7 to your computer and use it in GitHub Desktop.
#!/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'
print(a)
result = subprocess.check_output(*a, **kw)
#print(result)
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(f.read())
#print(raw)
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()
#print({'tree':tree,'parent':parent,'new_parent':new_parent})
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
lines.append(line)
print('modified commit')
merged = b'\n'.join(lines)
sha1 = hash_commit_body(merged)
print(merged.decode())
print(apply_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