#!/bin/sh
# Git pre-merge-commit dispatcher (Option A). core.hooksPath points at
# .git-hooks/; fleet/ and repo/ are peers under it. Runs the fleet hook first,
# then the repo hook if present. Either returning non-zero aborts the merge
# before the merge commit is created. Git passes no arguments. This is the only
# stage that gates a merge commit: pre-commit and commit-msg never run for
# `git merge`.

# Fail on the first unhandled non-zero status. Without it a fallible
# command that drops its status turns a refusal into a printed notice the
# shell ignores — how the security step's refusals ran unheeded. Enforced
# by scripts/fleet/check/git-hooks-have-exit-status-propagation.mts.
set -e

# HUSKY=0 skips this hook for the current invocation — the husky-compatible
# whole-chain escape (details in the pre-commit dispatcher).
if [ "${HUSKY-}" = "0" ]; then
  echo "[git-hooks] HUSKY=0 — skipping pre-merge-commit."
  exit 0
fi

DIR=$(dirname "$0")
if [ -x "$DIR/fleet/pre-merge-commit" ]; then
  "$DIR/fleet/pre-merge-commit" "$@" || exit $?
fi
if [ -x "$DIR/repo/pre-merge-commit" ]; then
  "$DIR/repo/pre-merge-commit" "$@" || exit $?
fi
