how to clone GitHub repo locally via libgit2sharp?

3.8k views Asked by At

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

There are 1 answers

1
Carlos Martín Nieto On BEST ANSWER

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.