# invar:begin
# Invar Pre-commit Hooks v{{ version }}
# Install: pre-commit install
#
# Default runs full verification (static + doctests + CrossHair + Hypothesis).
# Incremental mode makes this fast: ~5s first commit, ~2s subsequent (cached).
#
# Smart Guard: Detects rule-affecting file changes and runs full guard when needed.
# Structure Protection: Warns if INVAR.md is modified directly.

repos:
  - repo: local
    hooks:
      # Warn if INVAR.md is modified directly (use --no-verify if intentional)
      - id: invar-md-protected
        name: INVAR.md Protection
        entry: bash -c 'if git diff --cached --name-only | grep -q "^INVAR.md$"; then echo "Warning - INVAR.md was modified directly. Use git commit --no-verify if intentional."; exit 1; fi'
        language: system
        pass_filenames: false
        stages: [pre-commit]

      # Invar Guard (full verification by default)
      # Uses incremental mode: only verifies changed files, caches results
      # Smart mode: runs full guard when rule files change, --changed otherwise
      - id: invar-guard
        name: Invar Guard
        entry: bash -c '
          RULE_FILES="rule_meta.py rules.py contracts.py purity.py pyproject.toml"
          STAGED=$(git diff --cached --name-only)
          FULL=false
          for f in $RULE_FILES; do
            if echo "$STAGED" | grep -q "$f"; then FULL=true; break; fi
          done
          if [ "$FULL" = true ]; then
            echo "⚠️  Rule change detected - running FULL guard"
            invar guard
          else
            invar guard --changed
          fi
        '
        language: python
        additional_dependencies: ['invar-tools']
        pass_filenames: false
        stages: [pre-commit]
        types: [python]
# invar:end
