From ba8138079f07d305b6f1e0b6b01d3e98002c3270 Mon Sep 17 00:00:00 2001 From: cellarspoon Date: Sat, 25 Dec 2021 23:45:52 +0100 Subject: [PATCH] fix: use one function for up-to-date checks --- cli/catalogue/catalogue.go | 2 +- pkg/git/clone.go | 63 -------------------------------------- pkg/recipe/recipe.go | 23 ++++++++++++++ 3 files changed, 24 insertions(+), 64 deletions(-) diff --git a/cli/catalogue/catalogue.go b/cli/catalogue/catalogue.go index 03503371..77663e1e 100644 --- a/cli/catalogue/catalogue.go +++ b/cli/catalogue/catalogue.go @@ -146,7 +146,7 @@ A new catalogue copy can be published to the recipes repository by passing the logrus.Fatalf("%s has locally unstaged changes", rm.Name) } - if err := gitPkg.EnsureUpToDate(recipeDir); err != nil { + if err := recipe.EnsureUpToDate(recipeDir); err != nil { logrus.Fatal(err) } diff --git a/pkg/git/clone.go b/pkg/git/clone.go index e933318e..67359840 100644 --- a/pkg/git/clone.go +++ b/pkg/git/clone.go @@ -7,7 +7,6 @@ import ( "strings" "github.com/go-git/go-git/v5" - "github.com/go-git/go-git/v5/config" "github.com/go-git/go-git/v5/plumbing" "github.com/sirupsen/logrus" ) @@ -39,65 +38,3 @@ func Clone(dir, url string) error { return nil } - -// EnsureUpToDate ensures that a git repo on disk has the latest changes (git-fetch). -func EnsureUpToDate(dir string) error { - repo, err := git.PlainOpen(dir) - if err != nil { - return err - } - - recipeName := filepath.Base(dir) - isClean, err := IsClean(recipeName) - if err != nil { - return err - } - - if !isClean { - return fmt.Errorf("%s has locally unstaged changes", recipeName) - } - - branch, err := GetCurrentBranch(repo) - if err != nil { - return err - } - - logrus.Debugf("choosing %s as main git branch in %s", branch, dir) - - worktree, err := repo.Worktree() - if err != nil { - return err - } - - checkOutOpts := &git.CheckoutOptions{ - Create: false, - Force: true, - Branch: plumbing.ReferenceName(branch), - } - if err := worktree.Checkout(checkOutOpts); err != nil { - logrus.Debugf("failed to check out %s in %s", branch, dir) - return err - } - - logrus.Debugf("successfully checked out %s in %s", branch, dir) - - remote, err := repo.Remote("origin") - if err != nil { - return err - } - - fetchOpts := &git.FetchOptions{ - RemoteName: "origin", - RefSpecs: []config.RefSpec{"refs/heads/*:refs/remotes/origin/*"}, - Force: true, - } - if err := remote.Fetch(fetchOpts); err != nil { - if !strings.Contains(err.Error(), "already up-to-date") { - return err - } - } - - logrus.Debugf("successfully fetched all changes in %s", dir) - - return nil -} diff --git a/pkg/recipe/recipe.go b/pkg/recipe/recipe.go index 3a0c82ff..073876c0 100644 --- a/pkg/recipe/recipe.go +++ b/pkg/recipe/recipe.go @@ -314,6 +314,15 @@ func GetVersionLabelLocal(recipe Recipe) (string, error) { func EnsureUpToDate(recipeName string) error { recipeDir := path.Join(config.RECIPES_DIR, recipeName) + isClean, err := gitPkg.IsClean(recipeName) + if err != nil { + return err + } + + if !isClean { + return fmt.Errorf("%s has locally unstaged changes", recipeName) + } + repo, err := git.PlainOpen(recipeDir) if err != nil { return err @@ -329,6 +338,19 @@ func EnsureUpToDate(recipeName string) error { return err } + checkOutOpts := &git.CheckoutOptions{ + Create: false, + Force: true, + Branch: plumbing.ReferenceName(branch), + } + + if err := worktree.Checkout(checkOutOpts); err != nil { + logrus.Debugf("failed to check out %s in %s", branch, recipeDir) + return err + } + + logrus.Debugf("successfully checked out %s in %s", branch, recipeDir) + remotes, err := repo.Remotes() if err != nil { return err @@ -340,6 +362,7 @@ func EnsureUpToDate(recipeName string) error { } opts := &git.PullOptions{ + Force: true, ReferenceName: plumbing.ReferenceName(branch), }