fix(branch): get branch from detached HEAD

This commit is contained in:
Nightapes
2019-06-18 22:59:07 +02:00
parent 0eafe84d68
commit e4ba4834eb
2 changed files with 25 additions and 3 deletions

View File

@@ -1,5 +1,8 @@
dist: xenial dist: xenial
git:
depth: false
language: go language: go
go: go:
- 1.12.x - 1.12.x
@@ -23,7 +26,6 @@ before_script:
script: script:
- golangci-lint run ./... - golangci-lint run ./...
- git checkout master
- go test -v ./... - go test -v ./...
- go build -o build/go-semantic-release-temp ./cmd/go-semantic-release/ - go build -o build/go-semantic-release-temp ./cmd/go-semantic-release/
- echo "Building version `./build/go-semantic-release-temp next`" - echo "Building version `./build/go-semantic-release-temp next`"

View File

@@ -54,9 +54,29 @@ func (g *GitUtil) GetBranch() (string, error) {
} }
if !ref.Name().IsBranch() { if !ref.Name().IsBranch() {
return "", fmt.Errorf("no branch found, found %s, please checkout a branch (git checkout <BRANCH>)", ref.Name().String()) branches, err := g.Repository.Branches()
if err != nil {
return "", err
} }
var currentBranch string
found := branches.ForEach(func(p *plumbing.Reference) error {
if p.Name().IsBranch() && p.Name().Short() != "origin" {
currentBranch = p.Name().Short()
return fmt.Errorf("break")
}
return nil
})
if found != nil {
log.Debugf("Found branch from HEAD %s", currentBranch)
return currentBranch, nil
}
return "", fmt.Errorf("no branch found, found %s, please checkout a branch (git checkout -b <BRANCH>)", ref.Name().String())
}
log.Debugf("Found branch %s", ref.Name().Short())
return ref.Name().Short(), nil return ref.Name().Short(), nil
} }