#!/bin/bash

# Hook pre-commit : Vérifie qu'un tag est présent avant d'autoriser un commit sur certaines branches
# À placer dans : .git/hooks/pre-commit

# Configuration : branches nécessitant un tag
PROTECTED_BRANCHES=("ho-prod")

# Récupérer la branche actuelle
CURRENT_BRANCH=$(git symbolic-ref --short HEAD 2>/dev/null)

# Vérifier si la branche actuelle est protégée
BRANCH_PROTECTED=false
for branch in "${PROTECTED_BRANCHES[@]}"; do
    if [ "$CURRENT_BRANCH" = "$branch" ]; then
        BRANCH_PROTECTED=true
        break
    fi
done

# Si la branche n'est pas protégée, autoriser le commit
if [ "$BRANCH_PROTECTED" = false ]; then
    exit 0
fi

# Vérifier si HEAD pointe vers un tag
CURRENT_TAG=$(git describe --exact-match --tags HEAD | grep temp-valid 2>/dev/null)

if [ -z "$CURRENT_TAG" ]; then
    echo "❌ COMMIT REFUSED on branch '$CURRENT_BRANCH', use :"
    echo ""
    echo "half_orm dev commit -m ..."
    echo ""
    exit 1
fi

echo "✅ Tag détecté : $CURRENT_TAG"
echo "Commit autorisé sur la branche '$CURRENT_BRANCH'"
exit 0
