From 3cb9aa7f604b4fc4c732d01fc57a41b81f33bee0 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Wed, 10 Jul 2024 07:34:34 +0200 Subject: use fetch + checkout instead of pull --- internal/vcs/repository.go | 34 +++++++++++++++++++++++++--------- 1 file 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) } -- cgit 1.4.1