Timeout error cloning Git repository using LibGit2Sharp
Asked Answered
P

1

7

My organization runs its own GitHub server and web proxies. I have configured git so that I can work with both github.com and our internal GitHub from the command line. But using LibGit2Sharp, I cannot perform operations against our GitHub server. The only callback from CloneOptions that is invoked is RepositoryOperationStarting. No additional callbacks are invoked. I've posted the relevant code and configuration below (names have been changed to preserve anonymity). I am using LibGit2Sharp v0.25.2 from NuGet.

Code using LibGit2Sharp. Comments indicate which callbacks fire when hitting our internal github. When hitting github.com, all callbacks are invoked as expected.

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

Cloning from the command-line works:

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.

Here are the relevant pieces of my git config. I have also tried with HTTP_PROXY and HTTPS_PROXY environment variables, but no luck.

[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

Using WireShark, I see that command-line git does indeed hit my proxy server. However, my .NET program using LibGit2Sharp does not hit the proxy server at all.

Provinciality answered 11/5, 2017 at 19:53 Comment(12)
Maybe passing user name and credential with clone options var co = new CloneOptions();Recording
Yu follow this example: var co = new CloneOptions(); co.CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials { Username = "Username", Password = "Password" }; Repository.Clone("https://github.com/libgit2/libgit2sharp.git", "path/to/repo", co);Recording
Also I don't see the .git on your codeRecording
Weird...let me take a look at this once I got home...I am going to create a sample project in order to help you with this issueRecording
I am looking at the tests github.com/libgit2/libgit2sharp/blob/master/LibGit2Sharp.Tests/…Recording
Please note that the comments above are all outdated. I deleted comments that I had made in response to these questions. All relevant information is now in the post.Provinciality
@MichaelGunter Have you considered checking ServicePointManager related callback in case it is a certificate issue?Restrained
@Restrained I was hopeful, but no. I tried ServicePointManager.ServerCertificateValidationCallback, but it's not invoked either.Provinciality
1. You could run Wireshark and capture the traffic to make sure the ports are hit correctly. If it works in commandline but not in code, I would guess the communication is incorrect via code. What packets are sent via commandline? What packets are sent via your code and the lib? That might give you a hint as to what is missing in your code.Basically
@Basically I'm a little out of my element here, but from what I can tell from WireShark, LibGit2Sharp is not using the proxy server at all when hitting my internal github. Using git from the command line, it does get hit.Provinciality
@Recording Any other thoughts here? It appears LibGit2Sharp just isn't honoring the proxy information in .gitconfig.Provinciality
@MichaelGunter Then there is your answer. I googled LibGit2Sharp and found this: github.com/libgit2/libgit2sharp/issues/1429 If LibGit2Sharp isn't handling a proxy, then you have to code that yourself. It looks like LibGit2Sharp is just incomplete or has a bug. It seems it has code to read the proxy, just it isn't using it. You just need a method to check if a proxy is defined and if so, use it. Probably not what you want to here, but your best bet is to pull LibGit2Sharp from GitHub, compile and debug it, and start a dialogue with the project owners.Basically
M
0

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

That seems to be the case, from libgit2/libgit2sharp issue 1429, where Brandon Ording (bording) comments:

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) confirms:

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.

So: commit 0d72f67f2 does mention:

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.

That was in response to issue 3110, which involved a month later commit 1dc4491, with a proxy configuration example, but with libgit2 and a Java program.

HTTP(S)_PROXY should be picked up though, but: for an internal service like your own GitHub server, do check if you actually need a proxy.
Most often, you would set NO_PROXY=localhost,.mycompany.com in order to not use any proxy when contacting an internal (LAN instead of WAN) server.

Misgive answered 13/10, 2018 at 4:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.