-
Notifications
You must be signed in to change notification settings - Fork 5
/
pre-commit.githook
executable file
·74 lines (61 loc) · 1.42 KB
/
pre-commit.githook
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
#!/bin/bash
# shellcheck disable=SC1091
source .venv/bin/activate
RUFF_CMD='uv run ruff check'
DEPTRY_CMD='uv run deptry src'
PYTEST_CMD='uv run pytest -qq'
CYAN=$(echo -e $"\033[0;36m")
RED=$(echo -e $"\033[1;31m")
WHITE=$(echo -e $"\033[1;37m")
RESET=$(echo -e $"\033[0;0m")
STATUS=0
_get_files() {
local i
unset FILES
while IFS= read -r -d $'\0' file; do
FILES[i++]="$file"
done < <(git diff --name-only --diff-filter=ACMR --staged -z "$@")
}
# ruff
_get_files '*.py'
if [[ ${#FILES[@]} -ne 0 ]]; then
if ! RESULT=$(CLICOLOR_FORCE=1 $RUFF_CMD "${FILES[@]}"); then
STATUS=1
echo "${RED}There are ruff issues in your code:${RESET}"
echo
echo "$RESULT"
echo
fi
fi
# deptry
_get_files '*.py'
if [[ ${#FILES[@]} -ne 0 ]]; then
if ! RESULT=$($DEPTRY_CMD 2>&1); then
STATUS=1
echo "${RED}There are deptry issues in your code:${RESET}"
echo
echo "$RESULT"
echo
fi
fi
# pytest
if ! RESULT=$($PYTEST_CMD); then
STATUS=1
echo "${RED}Some tests failed:${RESET}"
echo
echo "$RESULT"
echo
fi
if [[ $STATUS != 0 ]] ; then
# claim stdin back
exec < /dev/tty
echo
read -r -p "${RED}Do you wish to commit anyway ${CYAN}[${WHITE}y${CYAN}/${WHITE}N${CYAN}]${RESET}? " yn
case $yn in
[Yy]* ) exit 0;;
[Nn]* ) exit $STATUS;;
* ) exit $STATUS;;
esac
fi
deactivate
exit $STATUS