#!/bin/sh
# ============================================================
# Pandaone AI Agent pre-commit hook (L3 防御) — Phase 4.6+ 强化版
# ============================================================
# 安装方式: pandaone install-hook
# 作用: 调用 pre-commit-check.py 拒绝绕过审计的提交
#
# [Fix #49] 跨平台 hook 触发兼容
# --------------------------------------------------------------
# Git 在不同平台触发 hook 的环境差异:
#   - Linux/macOS: /bin/sh + 完整 PATH, python3/python 通常在 PATH
#   - Git for Windows: mingw64 sh + 受限 PATH, 中文系统下 POSIX 路径解析失败;
#                     而且 sh 直接 exec "python" 还会被 Windows App Execution
#                     Aliases (MS Store 跳转) 抢走, 报 "Python was not found".
#
# 策略: 优先 cmd //c (能拿到完整 Windows PATH, 绕开 MS Store 抢占 +
#          mingw sh POSIX 路径解析问题); 再 fallback sh 直调 python3/python
#          (Linux/macOS 上才走这个分支).
# python 内部用 _resolve_git_exe() 解决 git 路径 (不依赖 sh).
# ============================================================

# 兜底 1: cmd //c (Windows 优先; cmd 能拿到完整 Windows PATH, 绕开
#          MS Store App Execution Aliases + mingw sh POSIX 路径解析问题)
if command -v cmd >/dev/null 2>&1; then
    exec cmd //c "set PANDAONE_PRE_COMMIT=1 && python .pandaone/pre-commit-check.py %*"
fi

# 兜底 2: sh 直接调 python (Linux/macOS / 没 cmd 的极简环境)
PY=""
if command -v python3 >/dev/null 2>&1; then
    PY="python3"
elif command -v python >/dev/null 2>&1; then
    PY="python"
fi

if [ -n "$PY" ]; then
    exec "$PY" ".pandaone/pre-commit-check.py" "$@"
fi

echo "[Pandaone] 拒绝执行: 找不到 python 或 cmd"
exit 1
