-
Notifications
You must be signed in to change notification settings - Fork 1
/
pre-commit
executable file
·86 lines (72 loc) · 1.81 KB
/
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
#!/usr/bin/env bash
set -u
# set -x
set -o pipefail
test -f "${GIT_DIR:-.git}/REBASE_HEAD" && exit 0
if [[ $(uname) = 'Darwin' ]]; then
# no readlink -f on osx :(
rl=$(python -c 'import os,sys;print(os.path.realpath(sys.argv[1]))' "$0")
else
rl=$(readlink -f "$0")
fi
readonly HERE=$(dirname "$rl")
warn() {
if [[ -t 2 ]]; then
echo -n -e '\033[1;33mWARN\033[0m ' >&2
else
echo -n 'WARN ' >&2
fi
echo "$@" >&2
}
error() {
if [[ -t 2 ]]; then
echo -n -e '\033[1;31mERROR\033[0m ' >&2
else
echo -n 'ERROR ' >&2
fi
echo "$@" >&2
exit 1
}
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# builtin trailing spaces and conflits markers check
if ! git diff-index --check --cached $against -- >&2; then
error "git diff --check failed"
fi
diff=$(git diff -z --cached --diff-filter=ACM $against)
debugger=$(echo "$diff" | grep -E -c '^\+.*\b(debugger|set_trace|pu\.db|breakpoint|import q)\b')
if [[ $debugger -gt 0 ]]; then
error "debugging keyword found"
fi
status=0
# check with flake8
if [[ -n "$(command -v flake8)" ]]; then
output="cat"
if [[ -t 2 ]]; then
# fd 2 is terminal => colored output
colout=$(command -v colout)
if [[ -n "$colout" ]]; then
output="$colout -T $HERE -t flake8"
fi
fi
echo "$diff" | filterdiff -i '*.py' --clean | flake8 --diff -j1 | $output >&2
status=$?
else
warn "flake8 not found"
fi
# check with eslint
if [[ -n "$(command -v eslint)" ]]; then
echo "$diff" | "$HERE"/eslint-diff.py >&2
es_status=$?
if [[ $es_status != 0 ]]; then
status=$es_status
fi
else
warn "eslint not found"
fi
exit $status