run-hook.cmd 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. : << 'CMDBLOCK'
  2. @echo off
  3. REM Cross-platform polyglot wrapper for hook scripts.
  4. REM On Windows: cmd.exe runs the batch portion, which finds and calls bash.
  5. REM On Unix: the shell interprets this as a script (: is a no-op in bash).
  6. REM
  7. REM Hook scripts use extensionless filenames (e.g. "session-start" not
  8. REM "session-start.sh") so Claude Code's Windows auto-detection -- which
  9. REM prepends "bash" to any command containing .sh -- doesn't interfere.
  10. REM
  11. REM Usage: run-hook.cmd <script-name> [args...]
  12. if "%~1"=="" (
  13. echo run-hook.cmd: missing script name >&2
  14. exit /b 1
  15. )
  16. set "HOOK_DIR=%~dp0"
  17. REM Try Git for Windows bash in standard locations
  18. if exist "C:\Program Files\Git\bin\bash.exe" (
  19. "C:\Program Files\Git\bin\bash.exe" "%HOOK_DIR%%~1" %2 %3 %4 %5 %6 %7 %8 %9
  20. exit /b %ERRORLEVEL%
  21. )
  22. if exist "C:\Program Files (x86)\Git\bin\bash.exe" (
  23. "C:\Program Files (x86)\Git\bin\bash.exe" "%HOOK_DIR%%~1" %2 %3 %4 %5 %6 %7 %8 %9
  24. exit /b %ERRORLEVEL%
  25. )
  26. REM Per-user Git for Windows installer ("Only for me" / winget user scope)
  27. if exist "%LOCALAPPDATA%\Programs\Git\bin\bash.exe" (
  28. "%LOCALAPPDATA%\Programs\Git\bin\bash.exe" "%HOOK_DIR%%~1" %2 %3 %4 %5 %6 %7 %8 %9
  29. exit /b %ERRORLEVEL%
  30. )
  31. REM Scoop user install (`scoop install git`)
  32. if exist "%USERPROFILE%\scoop\apps\git\current\usr\bin\bash.exe" (
  33. "%USERPROFILE%\scoop\apps\git\current\usr\bin\bash.exe" "%HOOK_DIR%%~1" %2 %3 %4 %5 %6 %7 %8 %9
  34. exit /b %ERRORLEVEL%
  35. )
  36. REM Try bash on PATH (e.g. user-installed Git Bash, MSYS2, Cygwin). Note that
  37. REM on stock Windows 10/11 `where bash` resolves to C:\Windows\System32\bash.exe
  38. REM (the WSL launcher), which fails on Windows-style script paths. The explicit
  39. REM probes above must therefore be exhausted first.
  40. where bash >nul 2>nul
  41. if %ERRORLEVEL% equ 0 (
  42. bash "%HOOK_DIR%%~1" %2 %3 %4 %5 %6 %7 %8 %9
  43. exit /b %ERRORLEVEL%
  44. )
  45. REM No bash found - exit silently rather than error
  46. REM (plugin still works, just without SessionStart context injection)
  47. exit /b 0
  48. CMDBLOCK
  49. # Unix: run the named script directly
  50. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  51. SCRIPT_NAME="$1"
  52. shift
  53. exec bash "${SCRIPT_DIR}/${SCRIPT_NAME}" "$@"