4

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 = "git@example.server.com: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://git@example.server.com:Projects/Project1.git";

Then it throws Malformed URL 'ssh://git@example.server.com:Projects/Project1.git'.

SamuGG
  • 479
  • 2
  • 8
  • 20
  • The colon in the URL should be followed by a port number. If you're not supplying a custom port, then don't use the colon. Also, you need a forward slash between the host and the path. So either 'git@example.com:portNumber/Projects' or 'git@example.com/Projects'. – Triynko Nov 20 '18 at 15:56

1 Answers1

1

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

repoPath = "ssh://git@example.server.com/Projects/Project1.git";

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