如何通过 libgit2sharp 在本地克隆 GitHub 存储库?

Bal*_*tar 2 git github git-clone 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" );
Run Code Online (Sandbox Code Playgroud)

抛出异常:

{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)
Run Code Online (Sandbox Code Playgroud)

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

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

我的函数调用语法正确吗?

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

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

Car*_*eto 5

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

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

您没有为 libgit2(sharp) 提供任何方式来询问您的用户凭据。您需要传入CloneOptions您拥有一CredentialsProvider组授权用户凭据的地方。