Search code examples
c#gitlibgit2sharp

Mirror a repo using libgit2sharp (or other c# library)


I want to clone a repo, and I want the clone to have all the tags from the original. I can do this manually like so

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

It seems libgit2sharp is the best way to do this, but if there's a better way let me know.

I don't understand how to do it with libgit2sharp It seems I have to do a clone, then somehow copy refs Then I have to iterate over all of those refs and stage them all... then commit? I started working on doing all this but it feels like I'm re-inventing the wheel...

Where I looked so far:


Solution

  • I don't know if this is the ideal solution but this seems to do the trick:

    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);
        }
    }