update source repository if worktree exists
3 files changed, 36 insertions(+), 29 deletions(-)
M internal/config/config.go → internal/config/config.go
@@ -46,11 +46,7 @@ WildcardDomain string `toml:"wildcard_domain"` OIDCHost URL `toml:"oidc_host"` Taxonomies []Taxonomy CSP *CSP `toml:"content-security-policy"` - VCS struct { - Branch string - RemoteURL URL `toml:"remote_url"` - } - Extra struct { + Extra struct { Headers map[string]string } Menus map[string][]MenuItem
M internal/vcs/repository.go → internal/vcs/repository.go
@@ -23,10 +23,11 @@ repo *git.Repository log *log.Logger } -func CloneOrOpen(cfg *Options, log *log.Logger) (repo *Repository, err error) { +func CloneOrOpen(cfg *Options, log *log.Logger) (repo *Repository, err error, exists bool) { stat, err := os.Stat(cfg.LocalPath) - if err != nil && errors.Is(err, fs.ErrNotExist) { - return nil, err + exists = err == nil + if err != nil && !errors.Is(err, fs.ErrNotExist) { + return } repo = &Repository{ log: log,@@ -43,15 +44,8 @@ return } -func (r *Repository) Update() (bool, error) { - r.log.Info("updating repository") - - head, err := r.repo.Head() - if err != nil { - return false, err - } - - r.log.Info("updating from", "rev", head.Hash().String()) +func (r *Repository) Update() (updated bool, err error) { + r.log.Info("updating repository", "from", r.headSHA()) err = r.repo.Fetch(&git.FetchOptions{ Prune: true, })@@ -119,3 +113,12 @@ } return nil } + +func (r *Repository) headSHA() string { + head, err := r.repo.Head() + if err != nil { + return "" + } + + return head.Hash().String() +}
M internal/website/mux.go → internal/website/mux.go
@@ -32,6 +32,10 @@ Destination string `conf:"default:public"` Redirect bool `conf:"default:true"` Development bool `conf:"default:false,flag:dev"` BaseURL config.URL `conf:"default:localhost"` + VCS struct { + Branch string `conf:"default:main"` + RemoteURL config.URL `conf:"default:https://git.alanpearce.eu/website"` + } } type Website struct {@@ -96,6 +100,22 @@ } templates.Setup() + repo, err, exists := vcs.CloneOrOpen(&vcs.Options{ + LocalPath: opts.Source, + RemoteURL: opts.VCS.RemoteURL, + Branch: opts.VCS.Branch, + }, log.Named("vcs")) + if err != nil { + return nil, errors.WithMessage(err, "could not open repository") + } + + if exists && !opts.Development { + _, err := repo.Update() + if err != nil { + return nil, errors.WithMessage(err, "could not update repository") + } + } + log.Debug("getting config from source", "source", opts.Source) cfg, err := config.GetConfig(opts.Source, log) if err != nil {@@ -120,18 +140,6 @@ cfg.BaseURL = opts.BaseURL } website.Domain = cfg.BaseURL.Hostname() - - repo, err := vcs.CloneOrOpen(&vcs.Options{ - LocalPath: opts.Source, - RemoteURL: cfg.VCS.RemoteURL, - Branch: cfg.VCS.Branch, - }, log.Named("vcs")) - if err != nil { - return nil, errors.WithMessage(err, "could not update repository") - } - - _ = repo - // config may have changed, reload err = rebuild(builderOptions, cfg, log) if err != nil {