#!/bin/bash
# Git pre-push hook to prevent accidental pushes to main/master branch
#
# This hook is installed in .git/hooks/pre-push and prevents direct pushes
# to protected branches (main, master).
#
# Installation:
#   bash .claude/scripts/install-git-hooks.sh
#
# Manual installation:
#   cp .claude/git-hooks/pre-push .git/hooks/pre-push
#   chmod +x .git/hooks/pre-push
#
# Bypass (not recommended):
#   git push --no-verify

set -euo pipefail

# Get current branch
CURRENT_BRANCH=$(git branch --show-current)

# Protected branches that cannot be pushed to directly
PROTECTED_BRANCHES=("main" "master")

# Check if current branch is protected
for protected in "${PROTECTED_BRANCHES[@]}"; do
    if [ "$CURRENT_BRANCH" = "$protected" ]; then
        echo "" >&2
        echo "❌ ERROR: Direct push to '$CURRENT_BRANCH' branch is FORBIDDEN!" >&2
        echo "" >&2
        echo "You must use pull requests to update this branch." >&2
        echo "" >&2
        echo "Correct workflow:" >&2
        echo "  1. Create a feature branch:" >&2
        echo "     git checkout -b feature/my-change" >&2
        echo "" >&2
        echo "  2. Make your changes and commit:" >&2
        echo "     git add ." >&2
        echo "     git commit -m \"feat: description\"" >&2
        echo "" >&2
        echo "  3. Push to the feature branch:" >&2
        echo "     git push -u origin feature/my-change" >&2
        echo "" >&2
        echo "  4. Create a pull request:" >&2
        echo "     gh pr create" >&2
        echo "" >&2
        echo "If you're already on main and have uncommitted changes:" >&2
        echo "  git checkout -b feature/fix-name" >&2
        echo "  git add ." >&2
        echo "  git commit -m \"feat: description\"" >&2
        echo "  git push -u origin feature/fix-name" >&2
        echo "" >&2
        echo "To bypass this hook (NOT RECOMMENDED):" >&2
        echo "  git push --no-verify" >&2
        echo "" >&2
        exit 1
    fi
done

# Allow push to non-protected branches
exit 0
