From e4ba4834ebb24973d3492f363be499f635361ce3 Mon Sep 17 00:00:00 2001 From: Nightapes Date: Tue, 18 Jun 2019 22:59:07 +0200 Subject: [PATCH] fix(branch): get branch from detached HEAD --- .travis.yml | 4 +++- internal/gitutil/gitutil.go | 24 ++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 41e48b2..29643a9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,8 @@ dist: xenial +git: + depth: false + language: go go: - 1.12.x @@ -23,7 +26,6 @@ before_script: script: - golangci-lint run ./... - - git checkout master - go test -v ./... - go build -o build/go-semantic-release-temp ./cmd/go-semantic-release/ - echo "Building version `./build/go-semantic-release-temp next`" diff --git a/internal/gitutil/gitutil.go b/internal/gitutil/gitutil.go index a721f4c..8e41517 100644 --- a/internal/gitutil/gitutil.go +++ b/internal/gitutil/gitutil.go @@ -54,9 +54,29 @@ func (g *GitUtil) GetBranch() (string, error) { } if !ref.Name().IsBranch() { - return "", fmt.Errorf("no branch found, found %s, please checkout a branch (git checkout )", 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 )", ref.Name().String()) + } + log.Debugf("Found branch %s", ref.Name().Short()) return ref.Name().Short(), nil }