-
Notifications
You must be signed in to change notification settings - Fork 9
/
pre-commit
36 lines (30 loc) · 1.06 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
#!/bin/sh
# Verify that paths in index (i.e. files being committed) comply
# with the pipeline codestyle checks and create a commit
# only if all checks pass
# https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---diff-filterACDMRTUXB82308203
files_changed=$(git diff --diff-filter=ACM --cached --name-only -- "*.py")
files_change_py=$(git diff --diff-filter=ACM --cached --name-only -- "*.py")
files_changed_no_tests=$(git diff --diff-filter=ACM --cached --name-only -- "*.py" |
grep -ve "test_[^\\\/]\+\.py")
exit_on_error() {
exit_code=$1
if [ $exit_code -ne 0 ]; then
>&2 echo "Codestyle checks failed. Commit aborted!"
exit $exit_code
fi
}
# Exit with 0 if no files changed in index (e.g. if amending the commit message only)
[ -z "$files_changed" ] && exit 0
if [ -n "$files_change_py" ]; then
echo "Checking ruff..."
ruff check --fix $files_change_py
exit_on_error $?
fi
if [ -n "$files_change_py" ]; then
echo "Formatting with ruff..."
ruff format $files_change_py
exit_on_error $?
fi
git add $files_changed
exit 0