【问题标题】:clone a git repository with SSH and libgit2sharp使用 SSH 和 libgit2sharp 克隆一个 git 存储库
【发布时间】:2016-11-20 02:43:25
【问题描述】:

我正在尝试使用库“libgit2sharp”通过 SSH 密钥克隆存储库......我找不到任何东西......我可以通过“https”克隆它,但我想要什么做的是使用 SSH 密钥。是否支持还真不清楚。

【问题讨论】:

  • 好吧,我在发帖之前看到了这个帖子,但不是很清楚。我从中检查了 PR,确实看起来他们一直在致力于 SSH 支持,但我找不到它。我检查了其他朝着那个方向发展的 PR,但似乎外部库支持 SSH 存在问题,并且没有确切说明它是否受支持......
  • 您是否正在构建自己的 libgit2 共享库?
  • 呃不,我只是在使用 libgit2sharp,我想使用 SSH 密钥进行克隆。

标签: libgit2sharp


【解决方案1】:

到目前为止,有一个使用 libssh2 库的 SSH 实现。你可以在这里找到它LibGit2Sharp - SSH

您应该在您的项目上添加 libgit2sharp-ssh 依赖项才能使用它。它以金块的形式提供:https://www.nuget.org/packages/LibGit2Sharp-SSH

免责声明:我还没有找到正式的使用指南,我所知道的是通过 LibGit2 论坛将其他用户问题的点点滴滴整理出来。

据我了解,您需要使用SshUserKeyCredentialsSshAgentCredentials 创建一个新凭据以使用SSH 进行身份验证,并将其作为CloneOptions 的一部分传递。

在示例代码中,我使用“git”作为用户,只是因为远程类似于 git@bitbucket.org:project/reponame.git ,在哪种情况“git”是正确的用户,否则你会得到一个错误提示

$exception  {"username does not match previous request"}LibGit2Sharp.LibGit2SharpException

使用 SSH 克隆 repo 的代码应该是这样的:

public CloneOptions cloningSSHAuthentication(string username, string path_to_public_key_file, string path_to_private_key_file)
    {
        CloneOptions options = new CloneOptions();
        SshUserKeyCredentials credentials = new SshUserKeyCredentials();
        credentials.Username = username;
        credentials.PublicKey = path_to_public_key_file;
        credentials.PrivateKey =  path_to_private_key_file;
        credentials.Passphrase = "ssh_key_password";
        options.CredentialsProvider = new LibGit2Sharp.Handlers.CredentialsHandler((url, usernameFromUrl, types) =>  credentials) ;
        return options;
    }

public CloneOptions cloneSSHAgent(string username){
        CloneOptions options = new CloneOptions();
        SshAgentCredentials credentials = new SshAgentCredentials();
        credentials.Username = username;
        var handler = new LibGit2Sharp.Handlers.CredentialsHandler((url, usernameFromUrl, types) => credentials);
        options.CredentialsProvider = handler;
        return options;

}

public void CloneRepo(string remotePath, string localPath){
    CloneOptions options = cloningSSHAuthentication("git", "C:\\folder\\id_rsa.pub", "C:\\folder\\id_rsa");
    Repository.Clone(remotePath, localPath, options);
}

【讨论】:

  • 这种方法还有效吗?我已经尝试过这种方法,但出现以下异常:无法验证 SSH 会话:回调在 c:\projects\libgit2sharp\LibGit2Sharp\Core\Ensure.cs:line 中的 LibGit2Sharp.Core.Ensure.HandleError(Int32 结果) 处返回错误137
  • 我认为最好将它作为一个单独的问题发布,并附上您的代码和 libgit2sharp 版本。自从我上次查看该代码以来已经有很长时间了。
猜你喜欢
  • 2017-08-07
  • 1970-01-01
  • 1970-01-01
  • 2011-01-26
  • 1970-01-01
  • 2019-10-03
  • 2012-04-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多