Repository: JanDeDobbeleer/oh-my-posh
Pull request #525 (2021-03-13 15:07:27)

fix(git): StatusColors without DisplayStatus
### Prerequisites

- [x] I have read and understand the `CONTRIBUTING` guide
- [x] The commit message follows the [conventional commits][cc] guidelines
- [x] Tests for the changes have been added (for bug fixes / features)
- [ ] n/a Docs have been added / updated (for bug fixes / features)

### Description

Fixes a use-case that was broken in #502 .

Prior to #502, when using the combination of `"display_status": false` and `"status_colors_enabled": true`, 
the branch name would display in the configured color for the  current git status. This PR restores that functionality, and adds a test-case to cover it.

Example segment:

```json
{
  "type": "git",
  "style": "plain",
  "foreground": "#13A10E",
  "properties": {
    "display_status": false,
    "display_stash_count": false,
    "display_upstream_icon": false,
    "status_colors_enabled": true,
    "color_background": false,
    "local_changes_color": "#C19C00",
    "ahead_and_behind_color": "#C50F1F",
    "behind_color": "#C50F1F",
    "ahead_color": "#881798"
  }
},
```


[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary


JanDeDobbeleer: That was actually a purposeful decision. No status also implies we don't fetch it. But I get there's a difference in expectations. 
TravisTX: Yeah, but this property does use the status.  This broke my personal theme, where I only used the color to know the status. I don't want the status display string.
JanDeDobbeleer: I'm reviewing later today, want to see this on my laptop 😅

Patch:
diff --git a/src/segment_git.go b/src/segment_git.go
index caafeedc..c7d072b0 100644
--- a/src/segment_git.go
+++ b/src/segment_git.go
@@ -139,11 +139,18 @@ func (g *git) enabled() bool {
 }
 
 func (g *git) string() string {
+	statusColorsEnabled := g.props.getBool(StatusColorsEnabled, false)
 	displayStatus := g.props.getBool(DisplayStatus, true)
+
+	if displayStatus || statusColorsEnabled {
+		g.setGitStatus()
+	}
+	if statusColorsEnabled {
+		g.SetStatusColor()
+	}
 	if !displayStatus {
 		return g.getPrettyHEADName()
 	}
-	g.setGitStatus()
 	buffer := new(bytes.Buffer)
 	// remote (if available)
 	if g.repo.upstream != "" && g.props.getBool(DisplayUpstreamIcon, false) {
@@ -166,9 +173,6 @@ 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()
 }
 


Test patch:
diff --git a/src/segment_git_test.go b/src/segment_git_test.go
index 66b692ee..3439d1ca 100644
--- a/src/segment_git_test.go
+++ b/src/segment_git_test.go
@@ -82,6 +82,7 @@ type detachedContext struct {
 	cherryPickSHA string
 	merge         bool
 	mergeHEAD     string
+	status        string
 }
 
 func setupHEADContextEnv(context *detachedContext) *git {
@@ -105,6 +106,7 @@ func setupHEADContextEnv(context *detachedContext) *git {
 	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.mockGitCommand(context.status, "status", "-unormal", "--short", "--branch")
 	env.On("getRuntimeGOOS", nil).Return("unix")
 	g := &git{
 		env: env,
@@ -560,7 +562,7 @@ func TestGetStatusColorDefault(t *testing.T) {
 	assert.Equal(t, expected, g.getStatusColor(expected))
 }
 
-func TestSetStatusColorBackground(t *testing.T) {
+func TestSetStatusColorForeground(t *testing.T) {
 	expected := changesColor
 	repo := &gitRepo{
 		staging: &gitStatus{
@@ -582,7 +584,7 @@ func TestSetStatusColorBackground(t *testing.T) {
 	assert.Equal(t, expected, g.props.foreground)
 }
 
-func TestSetStatusColorForeground(t *testing.T) {
+func TestSetStatusColorBackground(t *testing.T) {
 	expected := changesColor
 	repo := &gitRepo{
 		staging: &gitStatus{
@@ -604,6 +606,23 @@ func TestSetStatusColorForeground(t *testing.T) {
 	assert.Equal(t, expected, g.props.background)
 }
 
+func TestStatusColorsWithoutDisplayStatus(t *testing.T) {
+	expected := changesColor
+	context := &detachedContext{
+		status: "## main...origin/main [ahead 33]\n M myfile",
+	}
+	g := setupHEADContextEnv(context)
+	g.props = &properties{
+		values: map[Property]interface{}{
+			DisplayStatus:       false,
+			StatusColorsEnabled: true,
+			LocalChangesColor:   expected,
+		},
+	}
+	g.string()
+	assert.Equal(t, expected, g.props.background)
+}
+
 func TestGetStatusDetailStringDefault(t *testing.T) {
 	expected := "icon +1"
 	status := &gitStatus{