#!/bin/bash
# gxpm commit-msg hook. $1 is the path to .git/COMMIT_EDITMSG
set -e

if ! command -v gxpm >/dev/null 2>&1; then
  exit 0
fi

current_branch="$(git symbolic-ref --short HEAD 2>/dev/null || echo '')"
issue_id=$(printf '%s' "$current_branch" | grep -oEi '(gxg|gxpm)-[0-9]+' | head -1 | tr 'a-z' 'A-Z')

if [ -n "$issue_id" ] && [ -f ".gxpm/issues/${issue_id}/state.json" ]; then
  exec gxpm gate commit-msg "$1" --issue "$issue_id"
fi

exit 0
