.net - 来自 Windows 控制台应用程序的退出代码在 git 钩子(Hook)脚本中不可用

标签 .net windows git bash githooks

我在 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 会得到 -1255(-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/

相关文章:

GIT XCODE4 - 身份验证错误

git - 如何有不可见的 git meta 提交?

.net - 创建 NuGet 包 : NU5128 exception - not clear on how to fix

.NET SOAP 扩展在 MethodInfo 中抛出 NullReferenceException?

python - 执行周期性 Action

windows - 如果不支持升级,是否有必要为所有产品版本使用唯一的 WIX 产品 ID

git - ssh key 密码在 windows 中有效,但在 linux 中无效

c# - 如何提示用户他们可以滚动?

.net - Windows 上的 .NET 应用程序的 GitHub?

windows - Inno Setup 忽略注册表重定向?