gerritcp/repo.go

49 lines
982 B
Go
Raw Normal View History

2021-10-29 19:00:31 +00:00
package main
import (
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
)
func openRepo(dir *string, cfg *configFormat) *git.Repository {
_, err := git.PlainClone(*dir, true, &git.CloneOptions{
URL: cfg.Sites.Upstream.FullURL(),
Auth: nil,
RemoteName: "upstream",
SingleBranch: false,
NoCheckout: true,
})
if err != nil && err != git.ErrRepositoryAlreadyExists {
panic(err)
}
repo, err := git.PlainOpen(*dir)
if err != nil {
panic(err)
}
_, err = repo.CreateRemote(&config.RemoteConfig{
Name: "downstream",
URLs: []string{cfg.Sites.Downstream.FullURL()},
})
if err != nil && err != git.ErrRemoteExists {
panic(err)
}
err = repo.Fetch(&git.FetchOptions{
RemoteName: "upstream",
})
if err != nil && err != git.NoErrAlreadyUpToDate {
panic(err)
}
err = repo.Fetch(&git.FetchOptions{
RemoteName: "downstream",
})
if err != nil && err != git.NoErrAlreadyUpToDate {
panic(err)
}
return repo
}