Skip to content

Instantly share code, notes, and snippets.

@omalley
Created January 26, 2022 17:58
Show Gist options
  • Save omalley/daf12914027d62752175d071e553de2f to your computer and use it in GitHub Desktop.
Save omalley/daf12914027d62752175d071e553de2f to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
#
import os
import subprocess
import sys
cherry_pick_file = ".git/CHERRY_PICK_HEAD"
if os.path.exists(cherry_pick_file):
commit_msg_file = sys.argv[1]
commit_msg = list(open(commit_msg_file, 'rt').readlines())
cherry_pick = list(map(lambda l: l.strip(), open(cherry_pick_file, 'rt').readlines()))
user = subprocess.run(["git", "config", "user.name"],
capture_output=True, check=True).stdout.strip().decode('utf8')
first_comment=0
for line in commit_msg:
if line.startswith('#'):
break
else:
first_comment += 1
os.rename(commit_msg_file, commit_msg_file + ".bak")
out = open(commit_msg_file, 'wt')
for line in commit_msg[:first_comment]:
out.write(line)
out.write("\n")
out.write(f"Cherry-picked from {cherry_pick[0][:8]} by {user}\n")
for line in commit_msg[first_comment:]:
out.write(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment