Skip to content

Commit ce1488c

Browse files
committed
Add pre-commit hook template
Add type-check feature to makefile
1 parent c99abe8 commit ce1488c

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ include pyproject.toml
33
include poetry.lock
44
include Makefile
55
include MANIFEST.in
6+
include hooks/*
67
include pyshacl/assets/*.pickle
78
include pyshacl/*.spec
89
include test/*.py

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ else
5353
poetry run isort --check-only "$(FilePath)"
5454
endif
5555

56+
.PHONY: type-check
57+
type-check: venvcheck ## Validate with Black and isort in check-only mode
58+
ifeq ("$(FilePath)", "")
59+
poetry run python3 -m mypy --ignore-missing-imports pyshacl
60+
else
61+
poetry run python3 -m mypy --ignore-missing-imports "$(FilePath)"
62+
endif
63+
5664
.PHONY: upgrade
5765
upgrade: venvcheck ## Upgrade the dependencies
5866
poetry update

hooks/pre-commit

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/bin/sh
2+
#
3+
# To activate this git hook on your local dev environment:
4+
# copy or symlink this file to .git/hooks/pre-commit, and chmod +x to make executable
5+
6+
if git rev-parse --verify HEAD >/dev/null 2>&1
7+
then
8+
against=HEAD
9+
else
10+
# Initial commit: diff against an empty tree object
11+
against=$(git hash-object -t tree /dev/null)
12+
fi
13+
14+
# If you want to allow non-ASCII filenames set this variable to true.
15+
allownonascii=$(git config --bool hooks.allownonascii)
16+
17+
# Redirect output to stderr.
18+
exec 1>&2
19+
20+
# Cross platform projects tend to avoid non-ASCII filenames; prevent
21+
# them from being added to the repository. We exploit the fact that the
22+
# printable range starts at the space character and ends with tilde.
23+
if [ "$allownonascii" != "true" ] &&
24+
# Note that the use of brackets around a tr range is ok here, (it's
25+
# even required, for portability to Solaris 10's /usr/bin/tr), since
26+
# the square bracket bytes happen to fall in the designated range.
27+
test $(git diff --cached --name-only --diff-filter=A -z $against |
28+
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
29+
then
30+
cat <<\EOF
31+
Error: Attempt to add a non-ASCII file name.
32+
33+
This can cause problems if you want to work with people on other platforms.
34+
35+
To be portable it is advisable to rename the file.
36+
37+
If you know what you are doing you can disable this check using:
38+
39+
git config hooks.allownonascii true
40+
EOF
41+
exit 1
42+
fi
43+
44+
if [ -n "$VIRTUAL_ENV" ]; then
45+
echo "Trying to avtivate venv at $VIRTUAL_ENV"
46+
. "$VIRTUAL_ENV"/bin/activate
47+
else
48+
echo "Trying to avtivate venv at ./.venv"
49+
. ./.venv/bin/activate
50+
fi
51+
52+
OUT1=`make dev-test 1>&2`
53+
if [ $? -eq 0 ]; then
54+
echo "Tests passed\n"
55+
else
56+
echo "Tests failed! Cannot commit changes.\n"
57+
exit 1
58+
fi
59+
OUT2=`make lint 1>&2`
60+
if [ $? -eq 0 ]; then
61+
echo "Lints passed\n"
62+
else
63+
echo "Lints failed! Cannot commit changes.\n"
64+
exit 1
65+
fi
66+
OUT3=`make type-check 1>&2`
67+
if [ $? -eq 0 ]; then
68+
echo "Type Checking passed\n"
69+
else
70+
echo "Type Checking failed! Cannot commit changes.\n"
71+
exit 1
72+
fi
73+
74+
75+
# If there are whitespace errors, print the offending file names and fail.
76+
exec git diff-index --check --cached $against --

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ packages = [
3434

3535
include = [
3636
{ path = "pyshacl/assets/*.ttl", format = "sdist" },
37+
{ path = "hooks/*", format = "sdist" },
3738
{ path = "MANIFEST.in", format = "sdist" },
3839
{ path = "pyproject.toml", format = "sdist" },
3940
{ path = "poetry.lock", format = "sdist" },

0 commit comments

Comments
 (0)