使用libgit2sharp

时间:2017-10-13 12:28:06

标签: libgit2sharp-ssh

我试图通过ssh获得克隆或访问远程git存储库的简单工作示例。 添加nuget包LibGit2Sharp-SSH v1.0.22后,得到了一个.Net Framework v4.6.1控制台应用程序,如下所示:

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")
        };
    }
}

返回'Failed to start SSH session: Unable to exchange encryption keys'

如果我改用

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

然后抛出Malformed URL 'ssh://git@example.server.com:Projects/Project1.git'

1 个答案:

答案 0 :(得分:1)

尝试使用' /'而不是第二个':',以修复格式错误的网址'

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

对于'无法启动SSH会话',这是libgit而不是LibGit2Sharp的问题,应该在操作系统级别进行调整。