如何通过 libgit2sharp 在本地克隆 GitHub repo?

how to clone GitHub repo locally via libgit2sharp?

我编写代码通过 libgit2sharp ( v0.21.0.176 )

以编程方式将 GitHub 私人仓库克隆到本地仓库
var cloneOptions = new CloneOptions { BranchName = "master", Checkout = true };
var cloneResult = Repository.Clone( @"https://github.com/my-organization/my-repo.git", @"c:\github\my-organization\my-repo" );

抛出异常:

{LibGit2Sharp.LibGit2SharpException: Request failed with status code: 401
   at LibGit2Sharp.Core.Ensure.HandleError(Int32 result)
   at LibGit2Sharp.Core.Ensure.ZeroResult(Int32 result)
   at LibGit2Sharp.Core.Proxy.git_clone(String url, String workdir, GitCloneOptions& opts)
   at LibGit2Sharp.Repository.Clone(String sourceUrl, String workdirPath, CloneOptions options)

cloneResult 值为空(由于抛出异常而从未设置)

请注意,无论 \my-repo\ 文件夹是否存在,都会引发相同的 401 异常。

我的函数调用语法是否正确?

从命令行执行,git clone 首先创建一个要克隆到的本地目录文件夹。

libgit2sharp Repository.Clone() 的工作方式不同吗?

401 是 HTTP 代码,意味着您需要对服务器进行身份验证。 libgit2 中的代码路径并不总是正确地将这种情况转换为 GIT_AUTH,但这就是它的意思。

GitHub(和其他服务)也可以 return 当存储库不存在时出现此错误,以避免泄露有关私有存储库存在的信息。

您没有为 libgit2(sharp) 提供任何方式来询问您用户的凭据。您需要在 CloneOptions 中传递 CredentialsProvider 设置为授权用户的凭据。