From 38d236f9437f3d966e0a0493f054377d883b6086 Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Mon, 5 Jun 2017 13:09:12 +0200 Subject: [PATCH] Use git 'plumbing' command to avoid with color codes. (Fixes #26) --- statusline.go | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/statusline.go b/statusline.go index 08021db7..b5d0c5cb 100644 --- a/statusline.go +++ b/statusline.go @@ -265,7 +265,8 @@ func (s *StatuslineGit) Redraw(force bool) (int, int) { s.file = file dir := filepath.Dir(file) - out, err := exec.Command("git", "-C", dir, "branch").Output() + // prints branch name or "HEAD" if we are in a detached state. + out, err := exec.Command("git", "-C", dir, "rev-parse", "--symbolic-full-name", "--abbrev-ref", "HEAD").Output() if err != nil { if s.branch == "" { return 0, 0 @@ -276,16 +277,7 @@ func (s *StatuslineGit) Redraw(force bool) (int, int) { return 0, 0 } - branch := "" - for _, line := range strings.Split(string(out), "\n") { - if strings.HasPrefix(line, "* ") { - if strings.HasPrefix(line, "* (HEAD detached at ") { - branch = line[20 : len(line)-1] - } else { - branch = line[2:] - } - } - } + branch := strings.TrimSpace(string(out)) _, err = exec.Command("git", "-C", dir, "diff", "--quiet").Output() if err != nil { branch += "*"