我在 Windows 中有以下 git 预提交钩子(Hook):
#!/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 条件始终为真。我尝试从带有“echo %errorlevel%”的 Windows 批处理文件运行 Verification.exe,它按预期返回 -1。
如何在预提交脚本中测试退出代码?
最佳答案
应用程序的返回码通常是一个无符号字节。通常,从应用程序返回 -1
会得到 -1
或 255
(-1
的二进制表示被视为无符号整数)。在这种情况下,看起来至少 git 附带的 shell 版本错误地处理了负值(这可能与退出代码在 Windows 上表示为 32 位无符号整数这一事实有关)。更改示例代码以返回 1
而不是 -1
使其在 bash 中工作。
通常,始终返回非负数作为退出代码以避免此类问题是个好主意。
附言
也 checkout "Useless Use of Test" award .你的钩子(Hook)最好看起来像:
#!/bin/sh
if ! ./bin/Verification.exe
then
echo "Failed verfication: canceling commit"
exit 1
fi
exit 0
关于.net - 来自 Windows 控制台应用程序的退出代码在 git 钩子(Hook)脚本中不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20795592/