使用 libgit2sharp(或其他 c# 库)镜像一个 repo
Mirror a repo using libgit2sharp (or other c# library)
我想克隆一个存储库,我希望克隆具有原始存储库的所有标签。
我可以像这样手动完成
$ git clone --mirror https://github.com/{org}/{SourceProjectName}.git
$ cd {SourceProjectName}.git
$ git push --mirror https://github.com/{org}/{ProjectName}
看来 libgit2sharp 是执行此操作的最佳方法,但如果有更好的方法请告诉我。
我不明白如何使用 libgit2sharp
看来我必须做一个克隆,然后以某种方式复制 refs
然后我必须遍历所有这些引用并将它们全部暂存……然后提交?
我开始着手做这一切,但感觉就像是在重新发明轮子...
到目前为止我看过的地方:
您的链接问题有解决方案。
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)
基本上第三个参数是您需要设置特殊 refspec 的地方。
我不知道这是否是理想的解决方案,但这似乎可以解决问题:
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);
}
}
我想克隆一个存储库,我希望克隆具有原始存储库的所有标签。 我可以像这样手动完成
$ git clone --mirror https://github.com/{org}/{SourceProjectName}.git
$ cd {SourceProjectName}.git
$ git push --mirror https://github.com/{org}/{ProjectName}
看来 libgit2sharp 是执行此操作的最佳方法,但如果有更好的方法请告诉我。
我不明白如何使用 libgit2sharp 看来我必须做一个克隆,然后以某种方式复制 refs 然后我必须遍历所有这些引用并将它们全部暂存……然后提交? 我开始着手做这一切,但感觉就像是在重新发明轮子...
到目前为止我看过的地方:
您的链接问题有解决方案。
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)
基本上第三个参数是您需要设置特殊 refspec 的地方。
我不知道这是否是理想的解决方案,但这似乎可以解决问题:
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);
}
}