|
| 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 |
0 commit comments