package main import ( "flag" "fmt" "os" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/config" "gopkg.in/yaml.v2" ) func main() { dir := flag.String("dir", "repo", "path to local git repo") configFile := flag.String("config", "config.yaml", "path to configuration") flag.Parse() file, err := os.Open(*configFile) if err != nil { panic(fmt.Sprintf("Could not open '%s': %s", *configFile, err)) } defer file.Close() cfg := new(configFormat) configDecoder := yaml.NewDecoder(file) err = configDecoder.Decode(&cfg) if err != nil { panic(fmt.Sprintf("Failed parsing '%s': %s", *configFile, err)) } _, 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) } fmt.Println(repo) }