fix: use one function for up-to-date checks

This commit is contained in:
decentral1se 2021-12-25 23:45:52 +01:00
parent 8735a8f0ea
commit ba8138079f
Signed by: decentral1se
GPG Key ID: 03789458B3D0C410
3 changed files with 24 additions and 64 deletions

View File

@ -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)
}

View File

@ -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
}

View File

@ -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),
}