#!/usr/bin/env bash

# Half-ORM pre-commit hook
# 1. Checks if current ho-* branch exists on remote origin
# 2. 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 current ho-* branch (except ho-prod) still exists on remote origin
# This prevents committing to ho-* branches that were deleted remotely
if [[ "$CURRENT_BRANCH" == ho-* ]] && [ "$CURRENT_BRANCH" != "ho-prod" ]; then
    # Fetch remote refs quietly (only updates remote tracking, doesn't modify local branches)
    git fetch origin --prune --quiet 2>/dev/null || true

    # Check if branch exists on remote
    if ! git show-ref --verify --quiet "refs/remotes/origin/$CURRENT_BRANCH"; then
        cat << EOF
❌ Branch '$CURRENT_BRANCH' no longer exists on remote origin

This Half-ORM branch was deleted on the remote repository (likely cleaned up
after a release promotion). You cannot commit to a branch that doesn't exist
remotely.

Common causes:
  • ho-release/<version>/<patch_id> branches are deleted after promotion to prod
  • ho-patch/<patch_id> branches are renamed to ho-release/<version>/<patch_id> after integration
  • Branch was manually deleted by another team member

What to do:
  1. Save your changes if needed:
     $ git stash

  2. Switch to ho-prod:
     $ git checkout ho-prod

  3. Delete this stale local branch:
     $ git branch -D $CURRENT_BRANCH

  4. Create a new patch and restore your changes if you stashed them:
     $ git stash pop

For more information about branch lifecycle, see Half-ORM documentation.

EOF
        exit 1
    fi
fi

# 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
