Powershell Git钩子退出代码

6
我在.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

这将在同一目录中成功执行pre-commit-hook.ps1文件中的代码,但不会捕获退出代码。根据tldp.org ,仅指定退出时将返回上一个退出代码。如果退出代码为非零,则Git钩子将失败,但即使我的powershell脚本返回状态代码1,它也总是成功的。我该怎么做才能捕获来自powershell脚本的退出代码,以使钩子正确运行?

在你的脚本中尝试添加 [environment]::Exit(1),以返回非零代码给调用者。如果这行得通,我会将其作为我的答案 :-) - Andy Arismendi
不行,不起作用。我已经在我的脚本中只加了 exit 1,但那也没用。 - Matt Phillips
我想你可能已经知道了...因为有时它无法正常工作。 - Andy Arismendi
如果您尝试执行 exec c:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -ExecutionPolicy RemoteSigned -Command "[environment]::Exit(1)",它是否有效? - Andy Arismendi
是的,那确实可以解决问题,但是并没有解决根本问题。您介意去聊天吗?http://chat.stackoverflow.com/rooms/info/7875/matt?tab=general - Matt Phillips
1个回答

8

保持对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
可以的,我认为关键在于即使“pre-commit”本身已经在.git/hooks中运行,当前目录仍然是您项目的根目录。这绝对不在文档中 ;) - Matt Phillips

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接