Newer
Older
TelosDB / .githooks / pre-commit
@楽曲作りまくりおじさん 楽曲作りまくりおじさん 19 hours ago 954 bytes chore: add CONTRIBUTING, update .gitignore, add pre-commit guard hook
#!/usr/bin/env bash
# Simple pre-commit guard for common mistakes
# - Prevent committing or deleting .env files
# - Block deletions in protected directories (docs/, src/backend/)

set -e

# Get staged name-status
STAGED=$(git diff --cached --name-status)

# Prevent adding/modifying .env files
if echo "$STAGED" | grep -E '^[AM]\s+.*\.env(\.|$)\|^A\s+\.env$' >/dev/null; then
  echo "ERROR: .env files must not be added/modified in repository. Keep them local and add to .gitignore." >&2
  exit 1
fi

# Prevent deletion of protected paths
DELETED=$(echo "$STAGED" | awk '$1 == "D" {print $2}')
if [ -n "$DELETED" ]; then
  while read -r f; do
    case "$f" in
      docs/*|src/backend/*)
        echo "ERROR: Deletion detected in protected path: $f" >&2
        echo "Please review and confirm via PR before removing files in protected directories." >&2
        exit 1
        ;;
      *) ;;
    esac
  done <<< "$DELETED"
fi

# All checks passed
exit 0