diff options
Diffstat (limited to 'internal/vcs/repository.go')
-rw-r--r-- | internal/vcs/repository.go | 34 |
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) } |