#!/bin/sh
# Run Python formatters and linters
# Activate virtualenv if it exists
if [ -f .venv/bin/activate ]; then
. .venv/bin/activate
fi
# Get list of staged Python files
STAGED_PY_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.py$' || true)
if [ -n "$STAGED_PY_FILES" ]; then
echo "Running formatters and linters on staged files..."
# Run isort to sort imports
python -m isort $STAGED_PY_FILES || exit 1
# Run black formatter
python -m black $STAGED_PY_FILES || exit 1
# Run ruff linter
python -m ruff check --fix $STAGED_PY_FILES || exit 1
# Re-add files that were formatted
git add $STAGED_PY_FILES
echo "Pre-commit checks passed!"
fi