【问题标题】:How to Git Fetch <remote> using libgit2sharp?如何使用 libgit2sharp 获取 <remote>?
【发布时间】:2021-08-05 21:58:49
【问题描述】:

我现有的 Fetch 方法如下所示。

public void Fetch(string remote) => CommandLine.Run($"git fetch {remote}", _repoFolder);

我想使用 libgit2sharp 实现相同的功能。

这是我想出的:

public void Fetch(string remote)
{
    var repo = new Repository(_folder);
    var options = new FetchOptions();
    options.Prune = true;
    options.TagFetchMode = TagFetchMode.Auto;
    var refSpecs = $"+refs/heads/*:refs/remotes/{remote}/*";
    Commands.Fetch(repo, remote, new [] {refSpecs}, options, "Fetching remote");
}

但是,这会失败并显示以下错误消息: System.InvalidOperationException: 'authentication cancelled'

我也尝试过使用 libgit2sharp-ssh 包,结果是错误代码 401(未经授权的客户端)。

我认为 git 命令行工具可以工作,因为它知道如何授权访问(因为已经有一个遥控器)。如何使用 libgit2sharp 达到同样的效果?

【问题讨论】:

    标签: libgit2sharp


    【解决方案1】:

    你有没有尝试过类似的东西

    using(var repo = new Repository(_folder))
    {
        var options = new FetchOptions();
        options.Prune = true;
        options.TagFetchMode = TagFetchMode.Auto;
        options.CredentialsProvider = new CredentialsHandler(
            (url, usernameFromUrl, types) =>
                new UsernamePasswordCredentials()
                {
                    Username = "username",
                    Password = "password"
                });
    
        var remote = repo.Network.Remotes["origin"];
        var msg = "Fetching remote";
        var refSpecs = remote.FetchRefSpecs.Select(x => x.Specification);
        Commands.Fetch(repo, remote.Name, refSpecs, options, msg);                
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      相关资源
      最近更新 更多