#!/bin/bash

# Half-ORM pre-commit hook
# Protects ho-prod branch from direct commits
# Generated by half_orm_dev

# Get current branch
CURRENT_BRANCH=$(git symbolic-ref --short HEAD 2>/dev/null)

# Check if we're on ho-prod branch
if [ "$CURRENT_BRANCH" != "ho-prod" ]; then
    # Not on ho-prod, allow commit
    exit 0
fi

# Check if this is a temporary validation tag commit (allows automated promotion)
CURRENT_TAG=$(git describe --exact-match --tags HEAD 2>/dev/null | grep -E '^temp-valid-')

if [ -n "$CURRENT_TAG" ]; then
    # This is an automated promotion commit with temp tag, allow it
    exit 0
fi

# Check if there's an active lock on ho-prod
# Lock tags follow pattern: lock-ho-prod-{timestamp}
LOCK_TAG=$(git tag -l 'lock-ho-prod-*' 2>/dev/null | head -n 1)

if [ -n "$LOCK_TAG" ]; then
    # Lock is active, allow commit from Half-ORM workflow
    exit 0
fi

# Direct commit on ho-prod is not allowed
cat << 'EOF'
❌ Direct commits on 'ho-prod' are not allowed

The ho-prod branch is a protected production branch. Changes must be
promoted through the official Half-ORM release workflow.

To create a patch:
  $ half_orm dev patch new <patch_id>

To add a patch to a release:
  $ half_orm dev patch <patch_id> add-to-release [release]

To promote a stage release to rc/production:
  $ half_orm dev release promote <rc|prod>

This workflow ensures:
  • All patches are validated and tested
  • Code is properly merged from patch branches
  • Active patch branches are notified to rebase
  • Production deploys are tracked and auditable

For more information, see the Half-ORM documentation on release management.

EOF

exit 1
