fix: get branch is now more robust
This commit is contained in:
parent
a85cfe40d0
commit
9b8ff1ddcd
@ -9,6 +9,7 @@ import (
|
|||||||
"coopcloud.tech/abra/pkg/client"
|
"coopcloud.tech/abra/pkg/client"
|
||||||
"coopcloud.tech/abra/pkg/config"
|
"coopcloud.tech/abra/pkg/config"
|
||||||
"coopcloud.tech/abra/pkg/dns"
|
"coopcloud.tech/abra/pkg/dns"
|
||||||
|
"coopcloud.tech/abra/pkg/git"
|
||||||
"coopcloud.tech/abra/pkg/recipe"
|
"coopcloud.tech/abra/pkg/recipe"
|
||||||
"coopcloud.tech/abra/pkg/upstream/stack"
|
"coopcloud.tech/abra/pkg/upstream/stack"
|
||||||
"github.com/AlecAivazis/survey/v2"
|
"github.com/AlecAivazis/survey/v2"
|
||||||
@ -58,7 +59,11 @@ func DeployAction(c *cli.Context) error {
|
|||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
version = "latest commit"
|
head, err := git.GetRecipeHead(app.Type)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
version = head.String()[:8]
|
||||||
logrus.Warn("no versions detected, using latest commit")
|
logrus.Warn("no versions detected, using latest commit")
|
||||||
if err := recipe.EnsureLatest(app.Type); err != nil {
|
if err := recipe.EnsureLatest(app.Type); err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
|
@ -15,6 +15,7 @@ import (
|
|||||||
"coopcloud.tech/abra/cli/formatter"
|
"coopcloud.tech/abra/cli/formatter"
|
||||||
"coopcloud.tech/abra/pkg/client"
|
"coopcloud.tech/abra/pkg/client"
|
||||||
"coopcloud.tech/abra/pkg/config"
|
"coopcloud.tech/abra/pkg/config"
|
||||||
|
gitPkg "coopcloud.tech/abra/pkg/git"
|
||||||
"coopcloud.tech/abra/pkg/recipe"
|
"coopcloud.tech/abra/pkg/recipe"
|
||||||
"coopcloud.tech/abra/pkg/web"
|
"coopcloud.tech/abra/pkg/web"
|
||||||
"github.com/docker/distribution/reference"
|
"github.com/docker/distribution/reference"
|
||||||
@ -600,20 +601,15 @@ func GetRecipeVersions(recipeName string) (RecipeVersions, error) {
|
|||||||
return versions, err
|
return versions, err
|
||||||
}
|
}
|
||||||
|
|
||||||
branch := "master"
|
branch, err := gitPkg.GetCurrentBranch(repo)
|
||||||
if _, err := repo.Branch("master"); err != nil {
|
if err != nil {
|
||||||
if _, err := repo.Branch("main"); err != nil {
|
return versions, err
|
||||||
logrus.Debugf("failed to select branch in %s", recipeDir)
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
branch = "main"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
refName := fmt.Sprintf("refs/heads/%s", branch)
|
|
||||||
checkOutOpts := &git.CheckoutOptions{
|
checkOutOpts := &git.CheckoutOptions{
|
||||||
Create: false,
|
Create: false,
|
||||||
Force: true,
|
Force: true,
|
||||||
Branch: plumbing.ReferenceName(refName),
|
Branch: plumbing.ReferenceName(branch),
|
||||||
}
|
}
|
||||||
if err := worktree.Checkout(checkOutOpts); err != nil {
|
if err := worktree.Checkout(checkOutOpts); err != nil {
|
||||||
logrus.Debugf("failed to check out %s in %s", branch, recipeDir)
|
logrus.Debugf("failed to check out %s in %s", branch, recipeDir)
|
||||||
|
35
pkg/git/branch.go
Normal file
35
pkg/git/branch.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package git
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/go-git/go-git/v5"
|
||||||
|
"github.com/go-git/go-git/v5/plumbing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetCurrentBranch retrieves the current branch of a repository
|
||||||
|
func GetCurrentBranch(repository *git.Repository) (string, error) {
|
||||||
|
branchRefs, err := repository.Branches()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
headRef, err := repository.Head()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
var currentBranchName string
|
||||||
|
err = branchRefs.ForEach(func(branchRef *plumbing.Reference) error {
|
||||||
|
if branchRef.Hash() == headRef.Hash() {
|
||||||
|
currentBranchName = branchRef.Name().String()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return currentBranchName, nil
|
||||||
|
}
|
@ -213,20 +213,15 @@ func EnsureLatest(recipeName string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
branch := "master"
|
branch, err := gitPkg.GetCurrentBranch(repo)
|
||||||
if _, err := repo.Branch("master"); err != nil {
|
if err != nil {
|
||||||
if _, err := repo.Branch("main"); err != nil {
|
return err
|
||||||
logrus.Debugf("failed to select branch in %s", path.Join(config.APPS_DIR, recipeName))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
branch = "main"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
refName := fmt.Sprintf("refs/heads/%s", branch)
|
|
||||||
checkOutOpts := &git.CheckoutOptions{
|
checkOutOpts := &git.CheckoutOptions{
|
||||||
Create: false,
|
Create: false,
|
||||||
Force: true,
|
Force: true,
|
||||||
Branch: plumbing.ReferenceName(refName),
|
Branch: plumbing.ReferenceName(branch),
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := worktree.Checkout(checkOutOpts); err != nil {
|
if err := worktree.Checkout(checkOutOpts); err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user