Select Git revision
pre-commit.py
Brady James Garvin authored
pre-commit.py 1.49 KiB
#! /usr/bin/env python3
import sys
from subprocess import run
from datetime import datetime
def check():
result = run((
'pycodestyle',
'--ignore=E121,E123,E126,E226,E24,E704,W504',
'--max-line-length=120',
'--show-pep8',
'.'
))
if result.returncode != 0:
print()
print('Fix code style problems above and then try committing again.', file=sys.stderr)
return False
result = run(('python3', './setup.py', 'test'))
if result.returncode != 0:
print()
print('Fix test failures above and then try committing again.', file=sys.stderr)
return False
return True
if __name__ == '__main__':
result = run(('git', 'stash', 'push', '--quiet', '--keep-index', '--include-untracked'))
if result.returncode != 0:
print()
print('Failed to stash before pre-commit hook.', file=sys.stderr)
sys.exit(1)
result = run(('git', 'clean', '--quiet', '--force', '-d'))
if result.returncode != 0:
print()
print('Failed post-stash cleanup before pre-commit hook. (You should investigate the situation and then run '
'`git stash pop` to restore your repository status.)', file=sys.stderr)
sys.exit(1)
success = check()
result = run(('git', 'stash', 'pop', '--quiet'))
if result.returncode != 0:
print()
print('Failed to unstash after pre-commit hook.', file=sys.stderr)
sys.exit(1)
if not success:
sys.exit(1)