#!/bin/bash
# glass.ext.extend pre-commit: run the package test suite when package files are staged.
# The commit is blocked if any test fails.
# Install from the repository root: cp hooks/pre-commit .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit
# Uses the repository's .venv if present, otherwise python on PATH.

CHANGED=$(git diff --cached --name-only)
if echo "$CHANGED" | grep -qE "^(glass/|tests/|pyproject\.toml)"; then
    echo "pre-commit: package files staged, running test suite..."
    cd "$(git rev-parse --show-toplevel)" || exit 1
    PY=python
    [ -x .venv/bin/python ] && PY=.venv/bin/python
    PYTHONPATH=. "$PY" -m pytest tests/ -q
    RC=$?
    if [ $RC -ne 0 ]; then
        echo ""
        echo "pre-commit: tests failed, commit blocked."
        echo "  (to bypass: git commit --no-verify)"
        exit $RC
    fi
    echo "pre-commit: tests passed, proceeding."
fi
exit 0
