From 0f24553c7296c2099be6762a9befbb9287204a0a Mon Sep 17 00:00:00 2001 From: Inhere Date: Tue, 17 May 2022 19:23:34 +0800 Subject: [PATCH] fix: get branch name error on checkout a tag --- .github/workflows/go.yml | 2 +- _examples/some.md | 10 ++++++++++ repo.go | 7 ++++--- 3 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 _examples/some.md diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index b91e407..f231305 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -61,4 +61,4 @@ jobs: run: | pwd go test -v -cover ./... - go run ./cmd/chlog last head +# go run ./cmd/chlog last head diff --git a/_examples/some.md b/_examples/some.md new file mode 100644 index 0000000..4ed375a --- /dev/null +++ b/_examples/some.md @@ -0,0 +1,10 @@ +# git commands + +get commit for a tag: + +```shell +% git rev-parse refs/tags/v0.2.0 +a0e902f129d1f9dbf2190ee89e9fc6fd387cb885 + +``` + diff --git a/repo.go b/repo.go index 154e73e..8e3a7f3 100644 --- a/repo.go +++ b/repo.go @@ -1,7 +1,6 @@ package gitw import ( - "path" "strings" "github.com/gookit/goutil/arrutil" @@ -122,14 +121,16 @@ func (r *Repo) CurrentBranch() string { // cat .git/HEAD // OR // git symbolic-ref HEAD // out: refs/heads/fea_pref - str, err := r.Cmd("symbolic-ref", "HEAD").Output() + // git symbolic-ref --short -q HEAD // on checkout tag, run will error + // git rev-parse --abbrev-ref -q HEAD + str, err := r.gw.RevParse("--abbrev-ref", "-q", "HEAD").Output() if err != nil { r.setErr(err) return "" } // eg: fea_pref - brName = path.Base(FirstLine(str)) + brName = FirstLine(str) r.cache.Set(cacheCurrentBranch, brName) return brName