Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds pre-commit hook support #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions git-hooks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh

echo "Configuring pre-commit hook..."

# make a symbolic link with the pre-commit hook
if [ ! -f ./git/hooks/check_flake8.py ]; then
ln hooks/check_flake8.py .git/hooks/check_flake8.py
fi
if [ ! -f ./git/hooks/check_ast.py ]; then
ln hooks/check_ast.py .git/hooks/check_ast.py
fi
if [ ! -f ./git/hooks/check_merge_conflict.py ]; then
ln hooks/check_merge_conflict.py .git/hooks/check_merge_conflict.py
fi
if [ ! -f ./git/hooks/util.py ]; then
ln hooks/util.py .git/hooks/util.py
fi

if [ ! -f ./git/hooks/pre-commit ]; then
ln hooks/pre-commit.py .git/hooks/pre-commit
echo "Done"
else
cat <<EOF
A pre-commit hook exists already.
EOF
fi
1 change: 1 addition & 0 deletions hooks/check_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def check_ast(argv=None):
try:
ast.parse(open(filename, 'rb').read(), filename=filename)
except SyntaxError:
print("***** ast check failed *****")
print('{}: failed parsing with {} {}:'.format(
filename,
platform.python_implementation(),
Expand Down
1 change: 0 additions & 1 deletion hooks/check_flake8.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def check_flake8(argv=None):
sys.exit(1)

files = added_files()

retval = 0
for filename in files:
try:
Expand Down
2 changes: 2 additions & 0 deletions hooks/check_merge_conflict.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def detect_merge_conflict(argv=None):
for i, line in enumerate(inputfile):
for pattern in CONFLICT_PATTERNS:
if line.startswith(pattern):
if retcode == 0:
print("***** merge conflict check failed *****")
print(WARNING_MSG.format(
pattern.decode(), filename, i + 1,
))
Expand Down
33 changes: 33 additions & 0 deletions hooks/pre-commit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python

import sys

from check_ast import check_ast
from check_flake8 import check_flake8
from check_merge_conflict import detect_merge_conflict

def main(argv=None):
rc = 0
try:
check_flake8()
except:
rc = 1

try:
check_ast()
except:
rc = 1

try:
detect_merge_conflict()
except:
rc = 1

return rc


if __name__ == '__main__':
exit(main())



26 changes: 20 additions & 6 deletions hooks/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@
from __future__ import unicode_literals

import subprocess
import os

devnull = open(os.devnull, 'w')


class CalledProcessError(RuntimeError):
pass


def exists(cmd):
out, err, rc = cmd_output('which', '%s' % cmd)
return rc == 0


def added_files():
out, err, rc = cmd_output('git', 'diff', '--staged', '--name-only')
return set(out.splitlines())
return set([f for f in out.splitlines() if f.endswith('py')])


def cmd_output(*cmd, **kwargs):
Expand All @@ -35,3 +33,19 @@ def cmd_output(*cmd, **kwargs):
if retcode is not None and proc.returncode != retcode:
raise CalledProcessError(cmd, retcode, proc.returncode, stdout, stderr)
return stdout, stderr, proc.returncode


def execute(cmd, silent=False):
if silent:
params = {
'stdout': devnull,
'stderr': devnull,
}
else:
params = {}
retcode = subprocess.call(cmd.split(), **params)
return retcode


def exists(cmd):
return execute('which %s' % cmd, silent=True) == 0