Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Git Hook Exit Code

I have the following in my .git/hooks/pre-commit file

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

This successfully executes the code in the pre-commit-hook.ps1 file in the same directory, but does not capture the exit code. According to tldp.org the last exit code will be returned if only exit is specified. Git hooks will fail if the exit code is non-zero, but even though my powershell script returns a status code of 1, it always succeeds. What can I do to capture the exit code from the powershell script so the hook will function correctly?

like image 478
Matt Phillips Avatar asked Feb 18 '12 03:02

Matt Phillips


1 Answers

Keep the invocation of the ps1 script simple and you should have it working. The following works for me:

#!/bin/sh
echo 
exec powershell.exe -ExecutionPolicy RemoteSigned -File '.\.git\hooks\pre-commit-hook.ps1'
exit

The ps1 script just had an exit 1 and the commit did not happen.

When you are doing stuff like -command, Powershell is not known to work properly and you might have to do something like -command {& .\test.ps1; exit $lastexitcode}

like image 53
manojlds Avatar answered Nov 15 '22 14:11

manojlds