以下是我用来获取的代码:
public static void GitFetch()
{
var creds = new UsernamePasswordCredentials()
{Username = "user",
Password = "pass"};
var fetchOpts = new FetchOptions {Credentials = creds};
using (repo = new Repository(@"C:\project");)
{
repo.Network.Fetch(repo.Network.Remotes["origin"], fetchOpts);
}
}
但在获取期间失败,但出现以下异常:
LibGit2Sharp.LibGit2SharpException: Too many redirects or authentication replays
Result StackTrace:
at LibGit2Sharp.Core.Ensure.HandleError(Int32 result)
at LibGit2Sharp.Core.Proxy.git_remote_fetch(RemoteSafeHandle remote, Signature signature, String logMessage)
at LibGit2Sharp.Network.DoFetch(RemoteSafeHandle remoteHandle, FetchOptions options, Signature signature, String logMessage)
at LibGit2Sharp.Network.Fetch(Remote remote, FetchOptions options, Signature signature, String logMessage)
我已经验证配置文件具有所需的远程名称,并且git fetch可以从命令行运行。我发现异常来自libgit2\src\transport\winhttp.c
,但我无法提出解决方法/解决方案。
答案 0 :(得分:5)
我试过@Carlos'以下列方式提出建议:
public static void GitFetch()
{
var creds = new UsernamePasswordCredentials()
{Username = "user",
Password = "pass"};
CredentialsHandler credHandler = (_url, _user, _cred) => creds;
var fetchOpts = new FetchOptions { CredentialsProvider = credHandler };
using (repo = new Repository(@"C:\project");)
{
repo.Network.Fetch(repo.Network.Remotes["origin"], fetchOpts);
}
}
我可以从github上的公共存储库获取,也可以从bitbucket上的密码保护私有存储库获取;但是,对于工作中通过LAN托管的存储库,我无法做同样的事情。事实证明它们的配置方式不接受libgit2sharp提供的UsernamePasswordCredentials
。以下修改允许我通过LAN从存储库中获取:
CredentialsHandler credHandler = (_url, _user, _cred) => new DefaultCredentials();
(我试图找出两者之间的确切区别;如果我进一步了解它,我会更新答案。)
答案 1 :(得分:2)
应该使Credentials
选项正常工作的垫片当前是错误的(并且已被弃用),而是将CredentialsProvider
作为回调传递。