3

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 ?

Magnus Bäck
  • 11,381
  • 3
  • 47
  • 59
BaltoStar
  • 8,165
  • 17
  • 59
  • 91
  • mods : shouldn't down-votes be required to leave a comment explaining why ? i don't see what's wrong with my question – BaltoStar Jun 13 '15 at 01:46
  • Due to the '\', perhaps you should use a verbatim string for the path by prefixing it with `@` – Philippe Jun 13 '15 at 07:16
  • Your exception stack trace doesn't seem to match Your code sample. What version of LibGit2Sharp are tyou leveraging? What does `cloneResult` contain? – nulltoken Jun 13 '15 at 11:08
  • Very strange but the stack-trace I originally posted is incorrect. I've updated with correct stack-trace and libgit2sharp version v0.21.0.176. Because exception thrown, `cloneResult` is never set and so its value remains null. The paths are actually dynamically generated, but I tried with static-paths @"{path}" and the same exception was thrown. – BaltoStar Jun 15 '15 at 22:12
  • Here you have a working example with explanation: https://stackoverflow.com/a/72602335/6158341 – Kebechet Jun 13 '22 at 11:54

1 Answers1

5

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.

Carlos Martín Nieto
  • 5,207
  • 1
  • 15
  • 16