Compare commits

...

1 Commits

Author SHA1 Message Date
Franz Heinzmann (Frando)
552ee5bbd9 Fix branch checking logic
All checks were successful
continuous-integration/drone/pr Build is passing
See https://github.com/go-git/go-git/issues/518 for why this is needed.
2022-08-11 14:14:31 +02:00
2 changed files with 21 additions and 2 deletions

View File

@ -5,6 +5,25 @@ import (
"github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing"
) )
// Check if a branch exists in a repo.
// Use this and not repository.Branch(), because the latter does not
// actually check for existing branches.
// See https://github.com/go-git/go-git/issues/518
func HasBranch(repository *git.Repository, name string) bool {
var exist bool
if iter, err := repository.Branches(); err == nil {
iterFunc := func(reference *plumbing.Reference) error {
if name == reference.Name().Short() {
exist = true
return nil
}
return nil
}
_ = iter.ForEach(iterFunc)
}
return exist
}
// GetCurrentBranch retrieves the current branch of a repository // GetCurrentBranch retrieves the current branch of a repository
func GetCurrentBranch(repository *git.Repository) (string, error) { func GetCurrentBranch(repository *git.Repository) (string, error) {
branchRefs, err := repository.Branches() branchRefs, err := repository.Branches()

View File

@ -625,8 +625,8 @@ func GetDefaultBranch(repo *git.Repository, recipeName string) (plumbing.Referen
} }
branch := "master" branch := "master"
if _, err := repo.Branch("master"); err != nil { if !gitPkg.HasBranch(repo, "master") {
if _, err := repo.Branch("main"); err != nil { if !gitPkg.HasBranch(repo, "main") {
return "", fmt.Errorf("failed to select default branch in %s", recipeDir) return "", fmt.Errorf("failed to select default branch in %s", recipeDir)
} }
branch = "main" branch = "main"