4

我正在尝试获取一个简单的工作示例,用于通过 ssh 克隆或访问远程 git 存储库。添加 nuget package 后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'

4

1 回答 1

1

尝试使用“/”而不是第二个“:”来修复“格式错误的 URL”

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

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

于 2018-06-12T09:05:46.897 回答