use fetch + checkout instead of pull
Alan Pearce alan@alanpearce.eu
Wed, 10 Jul 2024 07:34:34 +0200
1 files changed, 25 insertions(+), 9 deletions(-)
jump to
M internal/vcs/repository.go → internal/vcs/repository.go
@@ -7,6 +7,7 @@ "go.alanpearce.eu/website/internal/config" "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 @@ return false, err } 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 @@ return false, err } - head, err = r.repo.Head() + rem, err := r.repo.Remote("origin") + if err != nil { + return false, err + } + 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 } - r.log.Info("updated to", "rev", head.Hash().String()) + wt.Checkout(&git.CheckoutOptions{ + Hash: hash, + Force: true, + }) + + r.log.Info("updated to", "rev", hash) return true, r.Clean(wt) }