all repos — homestead @ a9f47f4e4ed3223e86dc1d5c3edd005b8d9ce589

Code for my website

refactor: return errors with stack traces, where appropriate

Alan Pearce
commit

a9f47f4e4ed3223e86dc1d5c3edd005b8d9ce589

parent

f17c0bd9ce8ad16b39c9c5ba80c917049e135bed

1 file changed, 26 insertions(+), 25 deletions(-)

changed files
M internal/vcs/repository.gointernal/vcs/repository.go
@@ -1,10 +1,10 @@
package vcs import ( - "io/fs" "os" "go.alanpearce.eu/homestead/internal/config" + "go.alanpearce.eu/homestead/internal/file" "go.alanpearce.eu/x/log" "github.com/go-git/go-git/v5"
@@ -24,31 +24,32 @@ log *log.Logger
remoteURL config.URL } -func CloneOrOpen(cfg *Options, log *log.Logger) (repo *Repository, exists bool, err error) { - stat, err := os.Stat(cfg.LocalPath) - exists = err == nil - if err != nil && !errors.Is(err, fs.ErrNotExist) { - return - } - repo = &Repository{ - log: log, - remoteURL: cfg.RemoteURL, - } - if stat == nil { - repo.repo, err = git.PlainClone(cfg.LocalPath, false, &git.CloneOptions{ +func CloneOrOpen(cfg *Options, log *log.Logger) (*Repository, bool, errors.E) { + var r *git.Repository + var err error + exists := file.Exists(cfg.LocalPath) + if !exists { + r, err = git.PlainClone(cfg.LocalPath, false, &git.CloneOptions{ URL: cfg.RemoteURL.String(), Progress: os.Stdout, }) } else { - repo.repo, err = git.PlainOpen(cfg.LocalPath) + r, err = git.PlainOpen(cfg.LocalPath) + } + if err != nil { + return nil, exists, errors.WithStack(err) } - return + return &Repository{ + log: log, + remoteURL: cfg.RemoteURL, + repo: r, + }, exists, nil } -func (r *Repository) Update(rev string) (updated bool, err error) { +func (r *Repository) Update(rev string) (bool, errors.E) { r.log.Info("updating repository", "from", r.HeadSHA()) - err = r.repo.Fetch(&git.FetchOptions{ + err := r.repo.Fetch(&git.FetchOptions{ Prune: true, }) if err != nil {
@@ -58,18 +59,18 @@
return true, nil } - return false, err + return false, errors.WithStack(err) } rem, err := r.repo.Remote("origin") if err != nil { - return false, err + return false, errors.WithStack(err) } refs, err := rem.List(&git.ListOptions{ Timeout: 5, }) if err != nil { - return false, err + return false, errors.WithStack(err) } var hash plumbing.Hash
@@ -87,14 +88,14 @@ }
wt, err := r.repo.Worktree() if err != nil { - return false, err + return false, errors.WithStack(err) } err = wt.Checkout(&git.CheckoutOptions{ Hash: hash, Force: true, }) if err != nil { - return false, err + return false, errors.WithStack(err) } r.log.Info("updated to", "rev", hash)
@@ -102,10 +103,10 @@
return true, r.Clean(wt) } -func (r *Repository) Clean(wt *git.Worktree) error { +func (r *Repository) Clean(wt *git.Worktree) errors.E { st, err := wt.Status() if err != nil { - return err + return errors.WithStack(err) } if !st.IsClean() {
@@ -113,7 +114,7 @@ err = wt.Clean(&git.CleanOptions{
Dir: true, }) if err != nil { - return err + return errors.WithStack(err) } }