5

I have the following git pre-commit hook in Windows:

#!/bin/sh
./bin/Verification.exe
if [ $? -ne 0 ]
then 
    echo "Failed verfication: canceling commit"
    exit 1
fi
exit 0

Verification.exe is a .Net console app the I boiled down to this for testing purposes:

static int Main(string[] args)
{
    return -1;
}

The issue is that the bash script does not seem to make the exit code from the console app (i.e. -1) available in the $? variable. The exe runs, but the if-condition is always true in the script. I tried running Verification.exe from a Windows batch file with an "echo %errorlevel%" and it returns -1 as expected.

How do I test for the exit code inside the pre-commit script?

fredw
  • 1,409
  • 12
  • 23

1 Answers1

3

Return code of an application is usually an unsigned byte. Normally, returning -1 from an application gives either -1 or 255 (binary representation of -1 treated as unsigned integer). In this case, looks like at least shell version that comes with git handles negative value incorrectly (it might be related to the fact that exit codes are represented as 32-bit unsigned integers on Windows). Changing sample code to return 1 instead of -1 makes it work in bash.

Generally, it's a good idea to always return non-negative numbers as exit code to avoid such issues.

P.S.

Also checkout "Useless Use of Test" award. Your hook would better look like:

#!/bin/sh

if ! ./bin/Verification.exe
then 
    echo "Failed verfication: canceling commit"
    exit 1
fi
exit 0
xaizek
  • 5,098
  • 1
  • 34
  • 60