Skip to content

Instantly share code, notes, and snippets.

@tbelaire
Last active August 29, 2015 13:56
Show Gist options
  • Save tbelaire/9280384 to your computer and use it in GitHub Desktop.
Save tbelaire/9280384 to your computer and use it in GitHub Desktop.
Replace ~/.git_template/commit_template and webapp/.git/hooks/prepare-commit-msg
Test Plan:
# Commands to run, or process to follow to test the changes
#
# Previous commit:
# {PREV_COMMIT_MSG}
# Review: {Dxxxx}
# can be used instead of 'Test Plan:' to continue that commit's Differential
#!/usr/bin/env python
import argparse
import subprocess
import re
def get_prev_commit_msg():
return subprocess.check_output(['git', 'log', '-1', '--pretty=%B'])
def update_template(template):
prev_msg = get_prev_commit_msg()
summery = prev_msg.splitlines()[0]
differential = re.findall("D\d\d\d\d+", prev_msg)
if differential:
revision = differential[0]
else:
revision = "Dxxxx"
return template.format(PREV_COMMIT_MSG=summery,
Dxxxx=revision)
def main(commit_file):
with open(commit_file, 'r') as f:
template = f.read()
template = update_template(template)
with open(commit_file, 'w') as f:
f.write(template)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
# It takes one to three parameters. The first is the name of the
# file that contains the commit log message. The second is the
# source of the commit message, and can be: message (if a -m or
# -F option was given); template (if a -t option was given or the
# configuration option commit.template is set); merge (if the
# commit is a merge or a .git/MERGE_MSG file exists); squash (if
# a .git/SQUASH_MSG file exists); or commit, followed by a commit
# SHA1 (if a -c, -C or --amend option was given).
parser.add_argument('commit_msg_file',
help="The file to edit the commit msg of")
parser.add_argument('kind',
help="I have no idea")
parser.add_argument('commit', nargs='?',
help="SHA of amend or --commit type commits")
args = parser.parse_args()
if args.kind == 'template':
main(args.commit_msg_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment