set -eu
if [ "${DOCTRINE_ALLOW_DELETE:-}" = "1" ]; then
:
else
DOCTRINE="${DOCTRINE_BIN:-doctrine}"
if ! command -v "$DOCTRINE" >/dev/null 2>&1; then
echo "doctrine coord hook: '$DOCTRINE' not found — REFUSING (fail-closed)." >&2
echo " A coordination worktree needs doctrine to enforce the safe-commit guard;" >&2
echo " degrading to a deletion-only check would reopen the ISS-234 reversion hole." >&2
echo " Fix: put doctrine on PATH (or set DOCTRINE_BIN). Route authored writes" >&2
echo " through 'dispatch commit'. A DELIBERATE act may set DOCTRINE_ALLOW_DELETE=1." >&2
exit 1
fi
if ! "$DOCTRINE" dispatch hook-check; then
exit 1
fi
fi
chained=""
for scope in --local --global --system; do
hp="$(git config "$scope" --get core.hooksPath 2>/dev/null || true)"
if [ -n "$hp" ]; then
chained="$hp"
break
fi
done
if [ -z "$chained" ]; then
chained="$(git rev-parse --git-common-dir)/hooks"
fi
case "$chained" in
/*) : ;;
*) chained="$(git rev-parse --show-toplevel)/$chained" ;;
esac
next="$chained/pre-commit"
resolve() { [ -e "$1" ] && printf '%s' "$(cd "$(dirname "$1")" && pwd)/$(basename "$1")"; }
self="$(resolve "$0")"
next_real="$(resolve "$next" || true)"
if [ -x "$next" ] && [ "$next_real" != "$self" ]; then
exec "$next" "$@"
fi
exit 0