run-hook.cmd 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. :; command -v bash >/dev/null 2>&1 || exit 0
  2. :; [ $# -ge 1 ] || exit 0
  3. :; SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
  4. :; SCRIPT_NAME="$1"; shift; exec bash "${SCRIPT_DIR}/${SCRIPT_NAME}" "$@"
  5. @echo off
  6. REM Cross-platform polyglot wrapper for hook scripts.
  7. REM On Unix: POSIX shells execute the ":;" lines above and exec away before
  8. REM reaching this batch block (":" is a no-op; cmd.exe treats lines starting
  9. REM with ":" as labels and skips them). If bash or the script name is
  10. REM missing, the wrapper exits 0 silently - hooks are optional context, never
  11. REM a session breaker.
  12. REM On Windows: cmd.exe runs this batch portion, which finds and calls bash.
  13. REM
  14. REM Heredocs are banned in this file and every hooks/ executable: bash 5.1+
  15. REM delivers them via a pre-fork pipe write that deadlocks on macOS under
  16. REM pipe pressure (issue #571). tests/hooks/test-no-heredocs-in-hooks.sh is
  17. REM the fence.
  18. REM
  19. REM Hook scripts use extensionless filenames (e.g. "session-start" not
  20. REM "session-start.sh") so Claude Code's Windows auto-detection -- which
  21. REM prepends "bash" to any command containing .sh -- doesn't interfere.
  22. REM
  23. REM Usage: run-hook.cmd <script-name> [args...]
  24. if "%~1"=="" (
  25. echo run-hook.cmd: missing script name >&2
  26. exit /b 1
  27. )
  28. set "HOOK_DIR=%~dp0"
  29. REM Try Git for Windows bash in standard locations
  30. if exist "C:\Program Files\Git\bin\bash.exe" (
  31. "C:\Program Files\Git\bin\bash.exe" "%HOOK_DIR%%~1" %2 %3 %4 %5 %6 %7 %8 %9
  32. exit /b %ERRORLEVEL%
  33. )
  34. if exist "C:\Program Files (x86)\Git\bin\bash.exe" (
  35. "C:\Program Files (x86)\Git\bin\bash.exe" "%HOOK_DIR%%~1" %2 %3 %4 %5 %6 %7 %8 %9
  36. exit /b %ERRORLEVEL%
  37. )
  38. REM Try bash on PATH (e.g. user-installed Git Bash, MSYS2, Cygwin)
  39. where bash >nul 2>nul
  40. if %ERRORLEVEL% equ 0 (
  41. bash "%HOOK_DIR%%~1" %2 %3 %4 %5 %6 %7 %8 %9
  42. exit /b %ERRORLEVEL%
  43. )
  44. REM No bash found - exit silently rather than error
  45. exit /b 0