#!/bin/sh
# BeatBoard pre-commit hook
# Runs: ruff format . + ruff check . + ruff format --check .
# Auto-stages formatting changes so the commit includes them.

set -e

# Locate ruff
if command -v ruff >/dev/null 2>&1; then
  RUFF="ruff"
elif python -m ruff --version >/dev/null 2>&1; then
  RUFF="python -m ruff"
elif python3 -m ruff --version >/dev/null 2>&1; then
  RUFF="python3 -m ruff"
else
  echo "ruff not found (tried 'ruff' and 'python -m ruff'). Skipping hook."
  exit 0
fi

echo "▶ ruff format ."
$RUFF format .

# If formatting changed any tracked files, stage the changes.
# `git diff --quiet` checks working tree vs index (unstaged).
if ! git diff --quiet 2>/dev/null; then
  git add -u
  echo "  → staged formatting changes (git add -u)"
fi

echo "▶ ruff check ."
$RUFF check .

echo "▶ ruff format --check ."
$RUFF format --check .

echo "✓ ruff checks passed"
