Powershell Git Hook退出代码

Mat*_*ips 6 windows git bash powershell

我的.git/hooks/pre-commit文件中有以下内容

#!/bin/sh
exec c:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -ExecutionPolicy RemoteSigned -Command " Get-Location | % { '$_\pre-commit-hook.ps1'} | % { & $_ }"
exit
Run Code Online (Sandbox Code Playgroud)

这成功执行pre-commit-hook.ps1同一目录中文件中的代码,但不捕获退出代码.根据tldp.org,如果只指定了exit,将返回最后一个退出代码.如果退出代码非零,Git挂钩将失败,但即使我的powershell脚本返回状态代码1,它也总是成功.我该怎么做才能从powershell脚本中捕获退出代码,以便钩子正常运行?

man*_*lds 7

保持ps1脚本的调用简单,你应该让它工作.以下适用于我:

#!/bin/sh
echo 
exec powershell.exe -ExecutionPolicy RemoteSigned -File '.\.git\hooks\pre-commit-hook.ps1'
exit
Run Code Online (Sandbox Code Playgroud)

ps1脚本只有一个exit 1,并且提交没有发生.

当你正在做类似的事情时-command,不知道Powershell是否正常工作,你可能需要做类似的事情-command {& .\test.ps1; exit $lastexitcode}