all repos — homestead @ de8e02d2a10ce0a01817729e47f31e7b37103af2

Code for my website

split options of website/server

Alan Pearce
commit

de8e02d2a10ce0a01817729e47f31e7b37103af2

parent

5434a11ad1bf6693705a9ba38ec9464f2d001da9

1 file changed, 20 insertions(+), 29 deletions(-)

changed files
M internal/vcs/repository.gointernal/vcs/repository.go
@@ -1,6 +1,7 @@
package vcs import ( + "io/fs" "os" "go.alanpearce.eu/website/internal/config"
@@ -11,10 +12,10 @@ "github.com/go-git/go-git/v5/plumbing"
"gitlab.com/tozd/go/errors" ) -type Config struct { +type Options struct { LocalPath string RemoteURL config.URL - Branch string `conf:"default:main"` + Branch string } type Repository struct {
@@ -22,35 +23,24 @@ repo *git.Repository
log *log.Logger } -func CloneOrUpdate(cfg *Config, log *log.Logger) (*Repository, error) { - gr, err := git.PlainClone(cfg.LocalPath, false, &git.CloneOptions{ - URL: cfg.RemoteURL.String(), - Progress: os.Stdout, - }) - if err != nil { - if !errors.Is(err, git.ErrRepositoryAlreadyExists) { - return nil, err - } - gr, err = git.PlainOpen(cfg.LocalPath) - if err != nil { - return nil, err - } - repo := &Repository{ - repo: gr, - log: log, - } - _, err := repo.Update() - if err != nil { - return nil, err - } - - return repo, nil +func CloneOrOpen(cfg *Options, log *log.Logger) (repo *Repository, err error) { + stat, err := os.Stat(cfg.LocalPath) + if err != nil && errors.Is(err, fs.ErrNotExist) { + return nil, err + } + repo = &Repository{ + log: log, + } + if stat == nil { + repo.repo, err = git.PlainClone(cfg.LocalPath, false, &git.CloneOptions{ + URL: cfg.RemoteURL.String(), + Progress: os.Stdout, + }) + } else { + repo.repo, err = git.PlainOpen(cfg.LocalPath) } - return &Repository{ - repo: gr, - log: log, - }, nil + return } func (r *Repository) Update() (bool, error) {
@@ -90,6 +80,7 @@ var hash plumbing.Hash
for _, ref := range refs { if ref.Name() == plumbing.Main { hash = ref.Hash() + break } }