-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpython-git-pre-commit.sh
executable file
·66 lines (53 loc) · 1.36 KB
/
python-git-pre-commit.sh
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
#! /bin/bash
# Author: Huoty <sudohuoty@gmail.com>
# CreateTime: 2018-04-20 14:30:56
# Script starts from here:
if git rev-parse --verify HEAD >/dev/null 2>&1; then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
# or `git hash-object -t tree /dev/null`
fi
ret=0
files=$(git diff --cached --name-only $against | grep '\.py$')
if test -z "$files"; then
exit $ret
fi
RED="\033[1;31m"
GREEN="\033[1;32m"
YELLOW="\033[1;33m"
NOCOLOR="\033[0m"
function show_title() {
printf "${GREEN}"
echo $*
printf "${NOCOLOR}"
}
function check_debug_statements() {
printf "${RED}"
grep -n "$1" $files
if [ $? -eq 0 ]; then
let ret=$ret+1
fi
printf "${NOCOLOR}"
}
function check_code_style() {
printf "${RED}"
$* $files
if [ $? -ne 0 ]; then
let ret=$ret+1
fi
printf "${NOCOLOR}"
}
show_title "### Checking for pdb and ipdbs..."
check_debug_statements "import [i]*pdb"
show_title "### Checking for print statements..."
check_debug_statements "^\s*\bprint"
show_title "### Running PyCodeStyle..."
check_code_style pycodestyle -r --ignore=E402,E501,E731,W293
show_title "### Running PyFlakes..."
check_code_style pyflakes
if [ $ret -ne 0 ]; then
echo -e "\n${YELLOW}Commit failed, above problems need to be solved${NOCOLOR}"
fi
exit $ret