-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit-flake8
executable file
·56 lines (40 loc) · 1.08 KB
/
pre-commit-flake8
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
#!/usr/bin/env bash
DEBUG=0
debug_echo() {
if [ "$DEBUG" -ne "0" ]; then
echo "$*"
fi
}
# flake8 config location
CONFIG_FILE=''
RET_VAL=0
RESTORE='\033[0m'
RED='\033[00;31m'
BOLD=$(tty -s && tput bold)
trap cleanup EXIT SIGHUP SIGINT SIGTERM
cleanup() {
# cleanup after yourself!
debug_echo "deleting ${temp_files[@]}"
rm -f "${temp_files[@]}"
}
lint_files=()
temp_files=()
for file in `git diff --name-only --cached --diff-filter=d | grep '\.py$'`; do
if `git diff --quiet "$file"`; then
# no dirty version in working tree,
lint_files+=("$file")
else
# need to leave working tree untouched, so
# save staged contents of file to temp file
temp_and_orig_files=(`git checkout-index --temp "$file"`)
temp_file=${temp_and_orig_files[0]}
temp_files+=("$temp_file")
lint_files+=("$temp_file")
fi
done
echo ${lint_files[@]} | xargs flake8 --config="$CONFIG_FILE" --quiet
RET_VAL=$?
if [ "$RET_VAL" -ne 0 ]; then
echo -e "${BOLD}flake8: ${RED}FAILED ${RESTORE}checks"
fi
exit "$RET_VAL"