#!/bin/bash
# Pre-commit hook that runs rubocop on staged Ruby files

echo "Running rubocop on staged files..."

# Get list of staged Ruby files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.rb$|\.rake$')

# If no Ruby files are staged, exit successfully
if [ -z "$STAGED_FILES" ]; then
  echo "No Ruby files staged, skipping rubocop."
  exit 0
fi

# Run rubocop on staged files
echo "$STAGED_FILES" | xargs bundle exec rubocop --force-exclusion

RUBOCOP_EXIT=$?

# If rubocop failed, prevent commit
if [ $RUBOCOP_EXIT -ne 0 ]; then
  echo ""
  echo "❌ Rubocop found issues. Please fix them before committing."
  echo "   You can run 'bundle exec rubocop -A' to auto-fix some issues."
  echo "   To skip this hook, use 'git commit --no-verify'"
  exit 1
fi

echo "✅ Rubocop passed!"
exit 0
