从Windows控制台应用程序退出代码在git hook脚本中不可用

时间:2013-12-27 05:56:48

标签: .net windows git bash githooks

我在Windows中有以下git预提交挂钩:

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

Verification.exe是一个.Net控制台应用程序,我将其归结为测试目的:

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

问题是bash脚本似乎没有使得控制台应用程序(即-1)的退出代码可用于$?变量。 exe运行,但if-condition在脚本中始终为true。我尝试从带有“echo%errorlevel%”的Windows批处理文件运行Verification.exe,并按预期返回-1。

如何测试预提交脚本中的退出代码?

1 个答案:

答案 0 :(得分:2)

应用程序的返回代码通常是无符号字节。通常,从应用程序返回-1会将-1255-1的二进制表示视为无符号整数)。在这种情况下,至少看起来像git附带的shell版本错误地处理负值(这可能与退出代码在Windows上表示为32位无符号整数的事实有关)。更改示例代码以返回1而非-1,使其在bash中运行。

通常,总是将非负数作为退出代码返回以避免此类问题。

P.S。

同时结帐"Useless Use of Test" award。你的钩子看起来更像是:

#!/bin/sh

if ! ./bin/Verification.exe
then 
    echo "Failed verfication: canceling commit"
    exit 1
fi
exit 0