#!/bin/sh

# Half-ORM reference-transaction hook
# Blocks tag creation on production servers (read-only mode).
# Generated by half_orm_dev

# Only act on the "prepared" phase (before the transaction is committed).
if [ "$1" != "prepared" ]; then
    exit 0
fi

REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null)"
if [ ! -f "${REPO_ROOT}/.hop/production" ]; then
    exit 0
fi

# Allow tag updates that originate from an internal hop fetch operation.
# hop sets .hop/.fetching before calling git fetch and removes it after.
if [ -f "${REPO_ROOT}/.hop/.fetching" ]; then
    exit 0
fi

# Block any local tag creation.
while read -r _ _ ref_name; do
    case "$ref_name" in
        refs/tags/*)
            echo "ERROR: git tag is not allowed on a production server." >&2
            echo "       This repository is read-only (production mode)." >&2
            exit 1
            ;;
    esac
done

exit 0
