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

标签 c# git certificate http-proxy libgit2sharp

我的组织运行自己的 GitHub 服务器和网络代理。我已经配置了 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);
}

异常(exception):

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 程序根本没有访问代理服务器。

最佳答案

Any other thoughts here? It appears LibGit2Sharp just isn't honoring the proxy information in .gitconfig

好像是这样,来自libgit2/libgit2sharp issue 1429 , 其中Brandon Ording (bording)评论:

It looks like GitProxyOptions were added in 88cf9a7, and they are being used in Commands.Fetch, as you can see here.

However, it appears that currently everywhere in the codebase, the GitProxyType of GitProxyOptions is always being initialized to 0, which is None.
Looking at the libgit2 code for git_proxy_t, it appears that None disables using any proxies.

Edward Thomson (ethomson)确认:

Yes, it does look like this is disabling proxies... The default transport on Windows (WinHTTP) does not support proxies, IIRC, so this is basically a noop there. But turning on auto support would help when libgit2 itself is built with libcurl support.

所以:commit 0d72f67f2确实提到:

proxy: don't specify the protocol in the type

We leave this up to the scheme in the url field.
The type should only tell us about whether we want a proxy and whether we want to auto-detect it.

那是对 issue 3110 的回应,一个月后涉及commit 1dc4491 ,带有代理配置示例,但带有 libgit2 和 Java 程序。

HTTP(S)_PROXY 虽然应该选择,但是:对于像您自己的 GitHub 服务器这样的内部服务,请检查您是否真的需要代理。< br/> 大多数情况下,您会设置 NO_PROXY=localhost,.mycompany.com 以便在联系内部(LAN 而不是 WAN)服务器时使用任何代理。

关于c# - 使用 LibGit2Sharp 克隆 Git 存储库超时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43924425/

相关文章:

java - 叶证书和子证书的用途是什么以及如何使用它们?

c# - 如何使用 AutoMapper 和 OData 扩展子级别?

git - 远程服务器上的 git 存储库中的代码安全吗?如何保证它的安全?

ios - 如何在不设置 Apple Developer 帐户和团队的情况下构建 iOS 二进制文件?

Xcode: "The working copy ____ has uncommitted changes"与 git 状态: "nothing to commit, working directory clean"

Android 向项目添加自定义 lint 规则 (git)

certificate - 如何从 CertEnroll 导出二进制 PFX?

c# - 找不到类型或命名空间名称 'MembershipUser'

c# - 对数据库的更改已成功提交...ObjectContext 可能处于不一致状态

c# - 尝试访问 x.IndexOf(x);获取 "no overload for method IndexOf takes ' 1' arguments"