how to clone GitHub repo locally via libgit2sharp?

蓝咒 提交于 2020-01-06 19:27:05

问题


I wrote code to programmatically clone a GitHub private repo to a local repo via libgit2sharp ( v0.21.0.176 )

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

exception thrown :

{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 value is null ( never set due to exception thrown )

Note that the identical 401 exception is thrown whether or not the \my-repo\ folder is present.

Is my function call syntax correct ?

Executed from the command line, git clone first creates a local dir folder to clone into.

Does libgit2sharp Repository.Clone() work differently ?


回答1:


401 is the HTTP code meaning that you need to authenticate against the server. There are code paths in libgit2 which do not always correctly convert this situation into GIT_AUTH, but that's what it means.

GitHub (and other services) can also return this error when the repository does not exist, as a way to avoid leaking information about the existence of private repositories.

You're not providing any way for libgit2(sharp) to ask you for the user's credentials. You need to pass in CloneOptions where you have a CredentialsProvider set to an authorized user's credentials.



来源:https://stackoverflow.com/questions/30814173/how-to-clone-github-repo-locally-via-libgit2sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!