#!/bin/sh

# --- Protected Branches ---
PROTECTED_BRANCHES="^(main|dev)$"

# Read stdin to get push details
while read local_ref local_sha remote_ref remote_sha; do
    # Extract the branch name from the remote ref (e.g., refs/heads/branch-name)
    branch_name=$(echo "$remote_ref" | sed 's!refs/heads/!!')

    # Check if the pushed branch matches our protected branches
    if echo "$branch_name" | grep -qE "$PROTECTED_BRANCHES"; then
        echo "----------------------------------------------------------------"
        echo "❌ ERROR: Direct pushes to the '$branch_name' branch are forbidden."
        echo "Please use a pull request to merge your changes."
        echo "----------------------------------------------------------------"
        exit 1 # Abort the push
    fi
done

exit 0 # Allow the push
