From 0b3f4da41e7b08471a47da457d0a5f751847b5ec Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Tue, 9 Mar 2021 19:42:47 +0100 Subject: [PATCH] feat(git): only show HEAD context on display_status=false --- src/segment_git.go | 23 ++++++++++++++--------- src/segment_git_test.go | 1 + 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/segment_git.go b/src/segment_git.go index b730530dabde..b18491eae8b1 100644 --- a/src/segment_git.go +++ b/src/segment_git.go @@ -139,20 +139,18 @@ func (g *git) enabled() bool { } func (g *git) string() string { - g.setGitStatus() - if g.props.getBool(StatusColorsEnabled, false) { - g.SetStatusColor() + displayStatus := g.props.getBool(DisplayStatus, true) + if !displayStatus { + return g.getPrettyHEADName() } + g.setGitStatus() buffer := new(bytes.Buffer) - // branchName + // remote (if available) if g.repo.upstream != "" && g.props.getBool(DisplayUpstreamIcon, false) { fmt.Fprintf(buffer, "%s", g.getUpstreamSymbol()) } + // branchName fmt.Fprintf(buffer, "%s", g.repo.HEAD) - displayStatus := g.props.getBool(DisplayStatus, true) - if !displayStatus { - return buffer.String() - } if g.props.getBool(DisplayBranchStatus, true) { buffer.WriteString(g.getBranchStatus()) } @@ -168,6 +166,9 @@ func (g *git) string() string { if g.repo.stashCount != 0 { fmt.Fprintf(buffer, " %s%d", g.props.getString(StashCountIcon, "\uF692 "), g.repo.stashCount) } + if g.props.getBool(StatusColorsEnabled, false) { + g.SetStatusColor() + } return buffer.String() } @@ -343,8 +344,12 @@ func (g *git) getGitRefFileSymbolicName(refFile string) string { } func (g *git) getPrettyHEADName() string { + ref := g.getGitCommandOutput("branch", "--show-current") + if ref != "" { + return fmt.Sprintf("%s%s", g.props.getString(BranchIcon, "\uE0A0"), ref) + } // check for tag - ref := g.getGitCommandOutput("describe", "--tags", "--exact-match") + ref = g.getGitCommandOutput("describe", "--tags", "--exact-match") if ref != "" { return fmt.Sprintf("%s%s", g.props.getString(TagIcon, "\uF412"), ref) } diff --git a/src/segment_git_test.go b/src/segment_git_test.go index df713c896199..66b692ee7638 100644 --- a/src/segment_git_test.go +++ b/src/segment_git_test.go @@ -104,6 +104,7 @@ func setupHEADContextEnv(context *detachedContext) *git { env.mockGitCommand(context.tagName, "describe", "--tags", "--exact-match") env.mockGitCommand(context.origin, "name-rev", "--name-only", "--exclude=tags/*", context.origin) env.mockGitCommand(context.onto, "name-rev", "--name-only", "--exclude=tags/*", context.onto) + env.mockGitCommand(context.branchName, "branch", "--show-current") env.On("getRuntimeGOOS", nil).Return("unix") g := &git{ env: env,