【问题标题】:Mirror a repo using libgit2sharp (or other c# library)使用 libgit2sharp(或其他 c# 库)镜像 repo
【发布时间】:2019-02-19 05:15:21
【问题描述】:

我想克隆一个 repo,并且我希望克隆具有原始标签中的所有标签。 我可以像这样手动执行此操作

$ git clone --mirror https://github.com/{org}/{SourceProjectName}.git
$ cd {SourceProjectName}.git
$ git push --mirror https://github.com/{org}/{ProjectName}

似乎 libgit2sharp 是最好的方法,但如果有更好的方法,请告诉我。

我不明白如何使用 libgit2sharp 看来我必须做一个克隆,然后以某种方式复制参考 然后我必须遍历所有这些裁判并将它们全部上演......然后提交? 我开始努力做这一切,但感觉就像我在重新发明轮子......

到目前为止我看到的地方:

【问题讨论】:

    标签: c# git libgit2sharp


    【解决方案1】:

    我不知道这是否是理想的解决方案,但这似乎可以解决问题:

    private void DuplicateGitHubRepo()
    {
        var clonePath = Path.Combine(Path.GetTempPath(), "Temp-" + Guid.NewGuid() + ".git");
        var co = new CloneOptions
        {
            CredentialsProvider = GetGitCredentials()
        };
    
        Repository.Clone(SourceProjectUrl+".git", clonePath, co);
        using (var repo = new Repository(clonePath))
        {
            repo.Network.Remotes.Update("origin", x => x.Url = TargetProjectUrl);
            var options = new PushOptions
            {
                CredentialsProvider = GetGitCredentials()
            };
            repo.Network.Push(repo.Network.Remotes["origin"],repo.Refs.Select(x=>x.CanonicalName),options);
        }
    }
    

    【讨论】:

      【解决方案2】:

      您的链接问题有解决方案。

      git 镜像克隆只是一个远程源设置为+refs/*:refs/* 的克隆

      using (var repo = new Repository(Repository.Init(@"path\to\local.git", true)))
      {
          var remote = repo.Network.Remotes.Add("origin", "https://github.com/{org}/{SourceProjectName}.git", "+refs/*:refs/*");
          repo.Network.Fetch(remote /* anything for report progress */);
      }
      

      RemoteCollection.Add() 方法如下所示: public virtual Remote Add(string name, string url, string fetchRefSpec)

      基本上第三个参数是您需要设置特殊参考规范的地方。

      【讨论】:

      • 我认为这只是解决方案的克隆部分,而不是涉及更多的推送部分。
      • 另外......该示例在最新版本的 libgit2sharp 中不起作用(所以我试图了解如何重写它)......但我使用命令行发现它没有不管我是克隆 --bare 还是克隆 --mirror,只要我在推送时使用 --mirror 参数,然后我就会得到标签(很困惑:-D)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-17
      • 1970-01-01
      • 2018-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多