-
Notifications
You must be signed in to change notification settings - Fork 0
/
githook-pre-commit
executable file
·88 lines (79 loc) · 2.75 KB
/
githook-pre-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env bash
## Create pre-commit symlink if unset ##
GITDIR="";
if [ -d .git ]; then
GITDIR=".git";
elif [ -f .git ]; then
GITDIR=$(sed -n '/^gitdir:/{ s|.*: ||; p; }' .git);
fi
if [ ! -d "$GITDIR" ]; then
echo "${0##*/}: error: unable to find git directory" 1>&2;
exit 1;
fi
if [ ! -h "$GITDIR/hooks/pre-commit" ]; then
if [ $(realpath --help 2>&1 | grep -c relative) != 0 ]; then
HOOK=$(realpath --relative-to="$GITDIR/hooks" ./githook-pre-commit);
else
HOOK=$(readlink -f ./githook-pre-commit);
fi
ln -fs "$HOOK" "$GITDIR/hooks/pre-commit";
echo "${0##*/}: creating git pre-commit hook symlink" 1>&2;
exit 1;
fi
## Check files that changed ##
FILES=( $(git status --porcelain | sed -r 's|^ |_|; s|^(.) |\1_|; s|^(R[_M]) .* ->|\1|;' | grep -E '^([MRA]|.M)') );
PYLINT=$(which pylint pylint3 | head -n 1);
PYLINT+=" --errors-only --disable=no-member";
check_change_after_staged () {
[ "${2:1:1}" = "M" ] &&
echo "${0##*/}: error: changed after staged: $1" 1>&2 &&
exit 1;
}
n=1;
while [ "$n" -lt "${#FILES[@]}" ]; do
check_change_after_staged "${FILES[$n]}" "${FILES[$((n-1))]}";
case "${FILES[$n]}" in
*.py )
echo "${0##*/}: pycodestyle ${FILES[$n]}" 1>&2;
pycodestyle "${FILES[$n]}";
[ "$?" != "0" ] && exit 1;
echo "${0##*/}: $PYLINT ${FILES[$n]}" 1>&2;
$PYLINT "${FILES[$n]}";
;;
#.circleci/config.yml )
# echo "${0##*/}: circleci config validate -c .circleci/config.yml" 1>&2;
# circleci config validate -c .circleci/config.yml;
esac
[ "$?" != "0" ] && exit 1;
n=$((n+2));
done
## Run unit tests ##
./setup.py test_coverage;
[ "$?" != "0" ] && exit 1;
## Only when bumping version ##
if [ "${BUMPVERSION_NEW_VERSION+x}" != "" ]; then
## Check wheel ok for pypi ##
./setup.py bdist_wheel;
twine check dist/*.whl;
[ "$?" != "0" ] && exit 1;
## Check if schema version requires bump ##
pip3 uninstall -y narchi;
pip3 install -e .;
NEW_SCHEMA=$(narchi_cli.py schema);
NEW_MD5=$(echo "$NEW_SCHEMA" | sed '/\$id/d' | md5sum);
OLD_MD5=$(cat schema/narchi_schema.json | sed '/\$id/d' | md5sum);
if [ "$NEW_MD5" != "$NEW_MD5" ]; then
NEW_VERSION=$(echo "$NEW_SCHEMA" | sed -r -n '/"\$id":/{ s|.*/narchi/([0-9.]+)/schema.json.*|\1|; p; }');
OLD_VERSION=$(sed -n '/^current_version/{ s|.* ||; s|\.0$||; p; q; }' schema/.bumpversion.cfg);
if [ "$NEW_VERSION" = "$OLD_VERSION" ]; then
diff schema/narchi_schema.json <(echo "$NEW_SCHEMA") 1>&2;
echo "${0##*/}: error: schema change requires version bump" 1>&2;
exit 1;
fi
fi
echo "$NEW_SCHEMA" > schema/narchi_schema.json;
generate-schema-doc --no-minify schema/narchi_schema.json sphinx/_static/narchi_schema.html;
[ "$?" != "0" ] && exit 1;
git add schema sphinx/_static;
fi
exit 0;