Access remote git repo via ssh, using libgit2sharp

1.1k views Asked by At

I'm trying to get a simple working example for cloning or accessing a remote git repository via ssh. After adding nuget package LibGit2Sharp-SSH v1.0.22, got a .Net Framework v4.6.1 console application like this:

using LibGit2Sharp;
using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string localPath = Path.Combine(Path.GetTempPath(), "Example");
        string repoPath = "[email protected]:Projects/Project1.git";
        Repository.Clone(repoPath, localPath, new CloneOptions() { CredentialsProvider = CredentialsHandler });
    }

    private static Credentials CredentialsHandler(string url, string username, SupportedCredentialTypes types)
    {
        var sshDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".ssh");
        return new SshUserKeyCredentials()
        {
            Username = username,
            Passphrase = string.Empty,
            PublicKey = Path.Combine(sshDir, "id_rsa.pub"),
            PrivateKey = Path.Combine(sshDir, "id_rsa")
        };
    }
}

This returns 'Failed to start SSH session: Unable to exchange encryption keys'.

If I use instead

repoPath = "ssh://[email protected]:Projects/Project1.git";

Then it throws Malformed URL 'ssh://[email protected]:Projects/Project1.git'.

1

There are 1 answers

0
Igor Tesliuk On

Try to use '/' instead of second ':', to fix 'Malformed URL'

repoPath = "ssh://[email protected]/Projects/Project1.git";

As for 'Failed to start SSH session', that's a problem with libgit not LibGit2Sharp, should be tuned on OS level.