about summary refs log tree commit diff stats
path: root/internal/vcs
diff options
context:
space:
mode:
authorAlan Pearce2024-07-10 07:34:34 +0200
committerAlan Pearce2024-07-11 11:04:33 +0200
commit3cb9aa7f604b4fc4c732d01fc57a41b81f33bee0 (patch)
tree28a342d0731c460ee172ac99714b5ab329f5d2d6 /internal/vcs
parenta67a216bf8227c271828f901d5a04a600f865711 (diff)
downloadwebsite-3cb9aa7f604b4fc4c732d01fc57a41b81f33bee0.tar.lz
website-3cb9aa7f604b4fc4c732d01fc57a41b81f33bee0.tar.zst
website-3cb9aa7f604b4fc4c732d01fc57a41b81f33bee0.zip
use fetch + checkout instead of pull
Diffstat (limited to 'internal/vcs')
-rw-r--r--internal/vcs/repository.go34
1 files changed, 25 insertions, 9 deletions
diff --git a/internal/vcs/repository.go b/internal/vcs/repository.go
index e034ea4..5950e53 100644
--- a/internal/vcs/repository.go
+++ b/internal/vcs/repository.go
@@ -7,6 +7,7 @@ import (
 	"go.alanpearce.eu/x/log"
 
 	"github.com/go-git/go-git/v5"
+	"github.com/go-git/go-git/v5/plumbing"
 	"gitlab.com/tozd/go/errors"
 )
 
@@ -61,13 +62,8 @@ func (r *Repository) Update() (bool, error) {
 	}
 
 	r.log.Info("updating from", "rev", head.Hash().String())
-	wt, err := r.repo.Worktree()
-	if err != nil {
-		return false, err
-	}
-	err = wt.Pull(&git.PullOptions{
-		SingleBranch: true,
-		Force:        true,
+	err = r.repo.Fetch(&git.FetchOptions{
+		Prune: true,
 	})
 	if err != nil {
 		if errors.Is(err, git.NoErrAlreadyUpToDate) {
@@ -79,11 +75,31 @@ func (r *Repository) Update() (bool, error) {
 		return false, err
 	}
 
-	head, err = r.repo.Head()
+	rem, err := r.repo.Remote("origin")
 	if err != nil {
 		return false, err
 	}
-	r.log.Info("updated to", "rev", head.Hash().String())
+	refs, err := rem.List(&git.ListOptions{
+		Timeout: 5,
+	})
+
+	var hash plumbing.Hash
+	for _, ref := range refs {
+		if ref.Name() == plumbing.Main {
+			hash = ref.Hash()
+		}
+	}
+
+	wt, err := r.repo.Worktree()
+	if err != nil {
+		return false, err
+	}
+	wt.Checkout(&git.CheckoutOptions{
+		Hash:  hash,
+		Force: true,
+	})
+
+	r.log.Info("updated to", "rev", hash)
 
 	return true, r.Clean(wt)
 }