Skip to content

Commit 9f98b3e

Browse files
committed
chore: add tracked Git pre-commit hook for Ruff
checks
1 parent 1246137 commit 9f98b3e

4 files changed

Lines changed: 114 additions & 0 deletions

File tree

‎.gitattributes‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* text=auto
2+
.githooks/* text eol=lf

‎.githooks/pre-commit‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env bash
2+
3+
abort() {
4+
printf '%s\n' "$@" >&2
5+
exit 1
6+
}
7+
8+
tmpdir=$(mktemp -d "${TMPDIR:-/tmp}/morepath-ruff.XXXXXX") ||
9+
abort 'Ruff pre-commit check: could not create a temporary directory.'
10+
trap 'rm -rf "$tmpdir"' EXIT
11+
12+
list_file=$tmpdir/staged-files
13+
staged_file=$tmpdir/staged
14+
formatted_file=$tmpdir/formatted
15+
16+
# Write the list to a file first so a git failure is not silently ignored.
17+
git diff --cached --name-only --diff-filter=ACMR -z -- '*.py' '*.pyi' \
18+
>"$list_file" ||
19+
abort 'Ruff pre-commit check: could not list the staged files.'
20+
21+
python_files=()
22+
while IFS= read -r -d '' path; do
23+
python_files+=("$path")
24+
done <"$list_file"
25+
26+
if (( ${#python_files[@]} == 0 )); then
27+
exit 0
28+
fi
29+
30+
if [[ -x .venv/bin/ruff ]]; then
31+
ruff_cmd=.venv/bin/ruff
32+
elif [[ -f .venv/Scripts/ruff.exe ]]; then
33+
ruff_cmd=.venv/Scripts/ruff.exe
34+
elif command -v ruff >/dev/null 2>&1; then
35+
ruff_cmd=$(command -v ruff)
36+
elif command -v ruff.exe >/dev/null 2>&1; then
37+
ruff_cmd=$(command -v ruff.exe)
38+
else
39+
abort 'Ruff not found in .venv or PATH.' \
40+
"Install it with pip install -e '.[lint]'"
41+
fi
42+
43+
# report <label> <fix label> <fix command> <files...>
44+
report() {
45+
local label=$1 fixlabel=$2 fix=$3
46+
shift 3
47+
printf '\n%s (%d):' "$label" "$#"
48+
printf ' %q' "$@"
49+
printf '\n%s: %s' "$fixlabel" "$fix"
50+
printf ' %q' "$@"
51+
printf '\n'
52+
}
53+
54+
format_files=()
55+
lint_files=()
56+
57+
for path in "${python_files[@]}"; do
58+
git show ":$path" >"$staged_file" ||
59+
abort "Could not read the staged content of $path."
60+
61+
# Format the staged content; a non-zero exit is a real failure here
62+
# (for example a syntax error or a broken config).
63+
# --force-exclude makes ruff honor its exclude settings for stdin input.
64+
errors=$("$ruff_cmd" format --force-exclude --stdin-filename "$path" - \
65+
<"$staged_file" 2>&1 >"$formatted_file") ||
66+
abort "Ruff format failed on $path:" "$errors"
67+
68+
if ! cmp -s "$staged_file" "$formatted_file"; then
69+
format_files+=("$path")
70+
fi
71+
72+
# Lint the formatted content, so that violations the formatter
73+
# fixes anyway (e.g. trailing whitespace) are not reported twice.
74+
output=$("$ruff_cmd" check --force-exclude --no-fix \
75+
--stdin-filename "$path" - <"$formatted_file" 2>&1)
76+
case $? in
77+
0) ;;
78+
1) lint_files+=("$path") ;;
79+
*) abort "Ruff check failed on $path:" "$output" ;;
80+
esac
81+
done
82+
83+
if (( ${#format_files[@]} || ${#lint_files[@]} )); then
84+
printf '%s\n' 'Ruff checks failed - no files were changed.'
85+
if (( ${#format_files[@]} )); then
86+
report 'Needs formatting' 'Fix with' 'ruff format' "${format_files[@]}"
87+
fi
88+
if (( ${#lint_files[@]} )); then
89+
report 'Lint errors' 'Try' 'ruff check --fix' "${lint_files[@]}"
90+
fi
91+
printf '\n%s\n' 'Review and stage the fixes, then retry the commit.'
92+
exit 1
93+
fi

‎CHANGES.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ CHANGES
3939

4040
.. _Ruff: https://docs.astral.sh/ruff
4141

42+
- Add a tracked Git pre-commit hook for Ruff checks on staged Python files.
43+
4244
- Remove coveralls integration.
4345

4446
- Adjust the deployment strategy:

‎doc/developing.rst‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@ To apply lint fixes and format the code, run::
5555
Ruff uses the project settings in ``pyproject.toml``. The ``lint`` tox
5656
environment runs both checks.
5757

58+
To run the checks automatically on staged Python files before each
59+
commit, enable the repository's optional Git hook once in your clone::
60+
61+
(.venv) $ git config core.hooksPath .githooks
62+
63+
The hook checks the staged contents without modifying files.
64+
First it checks for formatting issues and then for remaining linting issues.
65+
It looks for Ruff in ``.venv`` and after on ``PATH``.
66+
On failure, it lists affected files and exact Ruff fix commands.
67+
Review and stage the changes before committing again.
68+
If needed install Ruff with::
69+
70+
(.venv) $ pip install -e .[lint]
71+
72+
Hooks are local conveniences and can be bypassed. CI continues to run the full
73+
project lint and formatting checks.
74+
5875
.. _Ruff: https://docs.astral.sh/ruff/
5976

6077
Running the tests

0 commit comments

Comments
 (0)