From fb23114666d22afcd036b955fcd1b27f3e60d0e7 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Wed, 26 Jun 2024 10:32:36 +0200 Subject: fix repository not updating --- internal/vcs/repository.go | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) (limited to 'internal/vcs') diff --git a/internal/vcs/repository.go b/internal/vcs/repository.go index 6e8f079..8ee08b2 100644 --- a/internal/vcs/repository.go +++ b/internal/vcs/repository.go @@ -6,8 +6,6 @@ import ( "website/internal/log" "github.com/go-git/go-git/v5" - gitc "github.com/go-git/go-git/v5/config" - "github.com/go-git/go-git/v5/plumbing" "github.com/pkg/errors" ) @@ -52,26 +50,36 @@ func CloneOrUpdate(cfg *Config) (*Repository, error) { func (r *Repository) Update(cfg *Config) (bool, error) { log.Info("updating repository") - err := r.repo.Fetch(&git.FetchOptions{ - RefSpecs: []gitc.RefSpec{ - gitc.RefSpec( - "+refs/heads/" + cfg.Branch + ":refs/remotes/origin/" + cfg.Branch, - ), - }, - }) - if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) { + + head, err := r.repo.Head() + if err != nil { return false, err } - ref, err := r.repo.Reference(plumbing.ReferenceName("refs/remotes/origin/"+cfg.Branch), true) + log.Info("updating from", "rev", head.Hash().String()) + wt, err := r.repo.Worktree() if err != nil { return false, err } - - log.Info("applying changes", "hash", ref.Hash().String()) - err = r.repo.Merge(*ref, git.MergeOptions{ - Strategy: git.FastForwardMerge, + err = wt.Pull(&git.PullOptions{ + SingleBranch: true, + Force: true, }) + if err != nil { + if errors.Is(err, git.NoErrAlreadyUpToDate) { + log.Info("already up-to-date") + + return true, nil + } + + return false, err + } + + head, err = r.repo.Head() + if err != nil { + return false, err + } + log.Info("updated to", "rev", head.Hash().String()) return true, nil } -- cgit 1.4.1