PowerShell的混帐挂钩退出代码
问题描述:
我在我的.git /挂钩以下/预提交文件PowerShell的混帐挂钩退出代码
#!/bin/sh
exec c:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -ExecutionPolicy RemoteSigned -Command " Get-Location | % { '$_\pre-commit-hook.ps1'} | % { & $_ }"
exit
这成功地执行在pre-commit-hook.ps1
文件在同一目录下的代码,但没有捕获退出码。根据tldp.org,如果仅指定退出,则将返回最后的退出代码。如果退出代码非零,Git挂钩将失败,但即使我的powershell脚本返回状态码1,它总是成功。我能做些什么来捕获PowerShell脚本中的退出代码,以便钩子能够正常工作?
答
保持简单的ps1脚本的调用,你应该有它的工作。对我来说,以下工作:
#!/bin/sh
echo
exec powershell.exe -ExecutionPolicy RemoteSigned -File '.\.git\hooks\pre-commit-hook.ps1'
exit
PS1的脚本只是有一个exit 1
和承诺都没有发生。
当你正在做的东西像-command
,PowerShell是不知道的正常工作,你可能需要做一些像-command {& .\test.ps1; exit $lastexitcode}
+1
是的工作。我认为这里的关键是,即使“预先提交”本身已经在.git/hooks中运行,当前目录也是您的项目的根目录。这绝对不是在文档中;) – 2012-02-18 03:40:08
在脚本中尝试加入'[环境] ::退出(1)'返回给调用者的非零代码。如果这项工作,我会让我的答案:-) – 2012-02-18 03:04:28
nope不起作用。我已经在我的脚本中只有'退出1',但这也没有工作 – 2012-02-18 03:09:32
我想你可能...因为有时它[不起作用](http://stackoverflow.com/questions/8902004/powershell -fails到返回正确的退出码/ 8902329#8902329)。 – 2012-02-18 03:14:52