Skip to content

Commit

Permalink
feat(git): only show HEAD context on display_status=false
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Mar 9, 2021
1 parent 8343e9a commit 0b3f4da
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/segment_git.go
Expand Up @@ -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())
}
Expand All @@ -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()
}

Expand Down Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions src/segment_git_test.go
Expand Up @@ -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,
Expand Down

0 comments on commit 0b3f4da

Please sign in to comment.