diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..e5c6b4a --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +* text=auto +.githooks/* text eol=lf diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..2b3ce42 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,93 @@ +#!/usr/bin/env bash + +abort() { + printf '%s\n' "$@" >&2 + exit 1 +} + +tmpdir=$(mktemp -d "${TMPDIR:-/tmp}/morepath-ruff.XXXXXX") || + abort 'Ruff pre-commit check: could not create a temporary directory.' +trap 'rm -rf "$tmpdir"' EXIT + +list_file=$tmpdir/staged-files +staged_file=$tmpdir/staged +formatted_file=$tmpdir/formatted + +# Write the list to a file first so a git failure is not silently ignored. +git diff --cached --name-only --diff-filter=ACMR -z -- '*.py' '*.pyi' \ + >"$list_file" || + abort 'Ruff pre-commit check: could not list the staged files.' + +python_files=() +while IFS= read -r -d '' path; do + python_files+=("$path") +done <"$list_file" + +if (( ${#python_files[@]} == 0 )); then + exit 0 +fi + +if [[ -x .venv/bin/ruff ]]; then + ruff_cmd=.venv/bin/ruff +elif [[ -f .venv/Scripts/ruff.exe ]]; then + ruff_cmd=.venv/Scripts/ruff.exe +elif command -v ruff >/dev/null 2>&1; then + ruff_cmd=$(command -v ruff) +elif command -v ruff.exe >/dev/null 2>&1; then + ruff_cmd=$(command -v ruff.exe) +else + abort 'Ruff not found in .venv or PATH.' \ + "Install it with pip install -e '.[lint]'" +fi + +# report