fre*_*edw 5 .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
Run Code Online (Sandbox Code Playgroud)
Verification.exe 是一个 .Net 控制台应用程序,出于测试目的,我将其归结为:
static int Main(string[] args)
{
return -1;
}
Run Code Online (Sandbox Code Playgroud)
问题是 bash 脚本似乎无法在 $? 中提供控制台应用程序的退出代码(即 -1)。多变的。exe 运行,但脚本中的 if 条件始终为 true。我尝试从 Windows 批处理文件中运行 Verification.exe,并显示“echo %errorlevel%”,它按预期返回 -1。
如何测试预提交脚本中的退出代码?
应用程序的返回码通常是无符号字节。通常,-1从应用程序返回的结果是-1或(被视为无符号整数255的二进制表示形式)。-1在这种情况下,看起来至少 git 附带的 shell 版本不能正确处理负值(这可能与退出代码在 Windows 上表示为 32 位无符号整数有关)。将示例代码更改为 return1而不是-1使其在 bash 中工作。
通常,最好始终返回非负数作为退出代码以避免此类问题。
还可以查看“无用测试”奖。你的钩子最好看起来像:
#!/bin/sh
if ! ./bin/Verification.exe
then
echo "Failed verfication: canceling commit"
exit 1
fi
exit 0
Run Code Online (Sandbox Code Playgroud)