Skip to content

Instantly share code, notes, and snippets.

@dmnd
Last active January 11, 2020 22:53
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 dmnd/9497250 to your computer and use it in GitHub Desktop.
Save dmnd/9497250 to your computer and use it in GitHub Desktop.
Show authors of changed lines in commit message template
#!/usr/bin/env python
"""
Makes it easy to pick a reviewer by showing who authored lines you changed
in a commit. You better have `git findreviewers` installed.
This is a Python script because every single time I try to write a shell
script, I regret it and have to rewrite.
"""
import re
import subprocess
import sys
debug = False
def log(s):
if debug:
print s
log('prepare-commit-msg debug')
if subprocess.call(['git', 'symbolic-ref', '-q', 'HEAD']):
# exit code 1 (error) means no HEAD
print "detached head, probably a rebase"
sys.exit(0)
if len(sys.argv) > 3:
log("looks like an amend: '{}'".format(sys.argv[3]))
sys.exit(0)
log('$3 is empty, so this is probably a regular commit!')
with open(sys.argv[1], 'r') as f:
message = f.read()
if "Differential Revision:" in message:
log('looks like phabricator')
sys.exit(0)
if re.search(r'(Auditor|Reviewer)s?:\s*\w', message, re.IGNORECASE):
log('reviewer already specified')
sys.exit(0)
with open(sys.argv[1], 'a') as f:
f.write("# Reviewers:\n")
authors = subprocess.check_output(['git', 'findreviewers'])
for a in (a for a in authors.split('\n') if a):
f.write("# ")
f.write(a)
f.write("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment