超时错误使用LibGit2Sharp克隆Git存储库

时间:2017-05-11 19:53:58

标签: c# git certificate http-proxy libgit2sharp

我的组织运行自己的GitHub服务器和Web代理。我已经配置了git,以便我可以从命令行使用github.com和我们的内部GitHub。但是使用LibGit2Sharp,我无法对我们的GitHub服务器执行操作。来自CloneOptions的唯一回调是RepositoryOperationStarting。不会调用其他回调。我已在下面发布了相关的代码和配置(名称已更改以保持匿名)。我正在使用NuGet的LibGit2Sharp v0.25.2。

使用LibGit2Sharp的代码。注释表明在击中我们的内部github时会触发哪些回调。点击github.com时,会按预期调用所有回调。

private static void Main(string[] args)
{
    var options = new CloneOptions
    {
        CertificateCheck = (certificate, valid, host) => true, // never called
        CredentialsProvider = (url, fromUrl, types) => null, // never called
        OnCheckoutProgress = (path, steps, totalSteps) => { }, // never called
        OnProgress = output => true, // never called
        OnTransferProgress = progress => true, // never called
        OnUpdateTips = (name, id, newId) => true, // never called
        RepositoryOperationCompleted = context => { }, // never called
        RepositoryOperationStarting = context => true // ONLY THIS ONE IS CALLED
    };

    ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true; // never called

    Repository.Clone(args[0], args[1], options);
}

例外:

Unhandled Exception: LibGit2Sharp.LibGit2SharpException: failed to send request: The operation timed out
   at LibGit2Sharp.Core.Ensure.HandleError(Int32 result) in C:\projects\libgit2sharp\LibGit2Sharp\Core\Ensure.cs:line 136
   at LibGit2Sharp.Core.Proxy.git_clone(String url, String workdir, GitCloneOptions& opts) in C:\projects\libgit2sharp\LibGit2Sharp\Core\Proxy.cs:line 354
   at LibGit2Sharp.Repository.Clone(String sourceUrl, String workdirPath, CloneOptions options) in C:\projects\libgit2sharp\LibGit2Sharp\Repository.cs:line 715
   at gitex.Program.Main(String[] args) in D:\dev\mgunter\gitex\gitex\Program.cs:line 71

从命令行克隆工作:

C:\> git clone https://github.my-domain.com/organization/project
Cloning into 'project'...
remote: Counting objects: 1373, done.
remote: Compressing objects: 100% (23/23), done.
remote: Total 1373 (delta 8), reused 0 (delta 0), pack-reused 1350R
Receiving objects: 100% (1373/1373), 383.10 KiB | 0 bytes/s, done.
Resolving deltas: 100% (862/862), done.

以下是我的git配置的相关部分。我也试过了HTTP_PROXYHTTPS_PROXY环境变量,但没有运气。

[http "https://github.my-domain.com"]
    proxy = http://proxy-for-internal.my-domain.com:80
[http "https://github.com"]
    proxy = http://proxy-for-external.my-domain.com:81
    sslCAinfo = ~/certificates/my-domain-root-ca.cer
[credential]
    helper = wincred
[hub]
    host = github.my-domain.com
    protocol = https

使用WireShark,我发现命令行git确实命中了我的代理服务器。但是,我使用LibGit2Sharp的.NET程序根本没有命中代理服务器。

1 个答案:

答案 0 :(得分:0)

  

这里还有其他想法吗?看来LibGit2Sharp只是不遵守.gitconfig

中的代理信息

libgit2/libgit2sharp issue 1429中似乎有这种情况,其中Brandon Ording (bording)评论:

  

似乎GitProxyOptions中已添加88cf9a7,并且它们已在Commands.Fetch as you can see here中使用。

     

但是,看来当前在代码库中的任何地方,GitProxyOptions的{​​{3}}总是被初始化为0,即None
  查看GitProxyType的libgit2代码,似乎None不能使用任何代理。

git_proxy_t确认:

  

是的,看起来确实是在禁用代理... Windows(WinHTTP)上的默认传输不支持代理IIRC,因此,这里基本上没有问题。但是,当使用libcurl支持构建libgit2本身时,打开自动支持会有所帮助。

所以:Edward Thomson (ethomson)确实提到了:

  

代理:请勿在类型中指定协议

     

我们将其留给url字段中的方案。
  该类型仅应告诉我们是否需要代理以及是否要自动检测它。

这是对commit 0d72f67f2的回应,该事件涉及issue 3110一个月后,它带有一个代理配置示例,但带有libgit2和一个Java程序。

虽然应该选择

HTTP(S)_PROXY,但是:对于像您自己的GitHub服务器这样的内部服务,请检查您是否确实需要代理。
通常,您设置NO_PROXY=localhost,.mycompany.com以便在与内部(LAN而非WAN)服务器联系时使用任何代理。