about summary refs log tree commit diff stats
path: root/internal/vcs/repository.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/vcs/repository.go')
-rw-r--r--internal/vcs/repository.go44
1 files changed, 33 insertions, 11 deletions
diff --git a/internal/vcs/repository.go b/internal/vcs/repository.go
index 8ee08b2..e034ea4 100644
--- a/internal/vcs/repository.go
+++ b/internal/vcs/repository.go
@@ -2,11 +2,12 @@ package vcs
 
 import (
 	"os"
-	"website/internal/config"
-	"website/internal/log"
+
+	"go.alanpearce.eu/website/internal/config"
+	"go.alanpearce.eu/x/log"
 
 	"github.com/go-git/go-git/v5"
-	"github.com/pkg/errors"
+	"gitlab.com/tozd/go/errors"
 )
 
 type Config struct {
@@ -17,9 +18,10 @@ type Config struct {
 
 type Repository struct {
 	repo *git.Repository
+	log  *log.Logger
 }
 
-func CloneOrUpdate(cfg *Config) (*Repository, error) {
+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,
@@ -34,8 +36,9 @@ func CloneOrUpdate(cfg *Config) (*Repository, error) {
 		}
 		repo := &Repository{
 			repo: gr,
+			log:  log,
 		}
-		_, err := repo.Update(cfg)
+		_, err := repo.Update()
 		if err != nil {
 			return nil, err
 		}
@@ -45,18 +48,19 @@ func CloneOrUpdate(cfg *Config) (*Repository, error) {
 
 	return &Repository{
 		repo: gr,
+		log:  log,
 	}, nil
 }
 
-func (r *Repository) Update(cfg *Config) (bool, error) {
-	log.Info("updating repository")
+func (r *Repository) Update() (bool, error) {
+	r.log.Info("updating repository")
 
 	head, err := r.repo.Head()
 	if err != nil {
 		return false, err
 	}
 
-	log.Info("updating from", "rev", head.Hash().String())
+	r.log.Info("updating from", "rev", head.Hash().String())
 	wt, err := r.repo.Worktree()
 	if err != nil {
 		return false, err
@@ -67,7 +71,7 @@ func (r *Repository) Update(cfg *Config) (bool, error) {
 	})
 	if err != nil {
 		if errors.Is(err, git.NoErrAlreadyUpToDate) {
-			log.Info("already up-to-date")
+			r.log.Info("already up-to-date")
 
 			return true, nil
 		}
@@ -79,7 +83,25 @@ func (r *Repository) Update(cfg *Config) (bool, error) {
 	if err != nil {
 		return false, err
 	}
-	log.Info("updated to", "rev", head.Hash().String())
+	r.log.Info("updated to", "rev", head.Hash().String())
+
+	return true, r.Clean(wt)
+}
+
+func (r *Repository) Clean(wt *git.Worktree) error {
+	st, err := wt.Status()
+	if err != nil {
+		return err
+	}
+
+	if !st.IsClean() {
+		err = wt.Clean(&git.CleanOptions{
+			Dir: true,
+		})
+		if err != nil {
+			return err
+		}
+	}
 
-	return true, nil
+	return nil
 }