diff --git a/gerritcp.go b/gerritcp.go index 40fcaac..dcc878c 100644 --- a/gerritcp.go +++ b/gerritcp.go @@ -5,8 +5,6 @@ import ( "fmt" "os" - "github.com/go-git/go-git/v5" - "github.com/go-git/go-git/v5/config" "gopkg.in/yaml.v2" ) @@ -29,43 +27,7 @@ func main() { 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) - } + repo := openRepo(dir, cfg) fmt.Println(repo) } diff --git a/repo.go b/repo.go new file mode 100644 index 0000000..d12ea26 --- /dev/null +++ b/repo.go @@ -0,0 +1,48 @@ +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 +}