Repository: JanDeDobbeleer/oh-my-posh
Pull request #589 (2021-03-27 16:04:28)

feat(pwsh): posh-git segment
### Prerequisites

- [ ] I have read and understand the `CONTRIBUTING` guide
- [ ] The commit message follows the [conventional commits][cc] guidelines
- [ ] Tests for the changes have been added (for bug fixes / features)
- [ ] Docs have been added / updated (for bug fixes / features)

### Description

[Description of the change]

[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary



Patch:
diff --git a/.github/workflows/code.yml b/.github/workflows/code.yml
index a23f3fb6..deb77ca8 100644
--- a/.github/workflows/code.yml
+++ b/.github/workflows/code.yml
@@ -32,7 +32,7 @@ jobs:
     - name: Golang CI
       uses: golangci/golangci-lint-action@v2
       with:
-        version: v1.31
+        version: latest
         working-directory: src
     - name: Unit Tests
       run: go test . -v
diff --git a/docs/docs/segment-posh-git.mdx b/docs/docs/segment-posh-git.mdx
new file mode 100644
index 00000000..dc5b5327
--- /dev/null
+++ b/docs/docs/segment-posh-git.mdx
@@ -0,0 +1,27 @@
+---
+id: poshgit
+title: Posh-Git
+sidebar_label: Posh-Git
+---
+
+## What
+
+Display the [posh-git][posh-git] prompt.
+
+:::info
+This segment only works within Powershell and requires the posh-git module to be installed and imported.
+:::
+
+## Sample Configuration
+
+```json
+{
+  "type": "poshgit",
+  "style": "powerline",
+  "powerline_symbol": "\uE0B0",
+  "foreground": "#ffffff",
+  "background": "#0077c2"
+}
+```
+
+[posh-git]: https://github.com/dahlbyk/posh-git
diff --git a/docs/sidebars.js b/docs/sidebars.js
index 7c8d32bb..f46d74e1 100644
--- a/docs/sidebars.js
+++ b/docs/sidebars.js
@@ -25,6 +25,7 @@ module.exports = {
         "node",
         "os",
         "path",
+        "poshgit",
         "python",
         "root",
         "ruby",
diff --git a/src/.golangci.yml b/src/.golangci.yml
index 1fe63d8c..f048ae37 100644
--- a/src/.golangci.yml
+++ b/src/.golangci.yml
@@ -20,13 +20,12 @@ linters:
     - gosimple
     - govet
     - ineffassign
-    - interfacer
     - misspell
     - nakedret
     - noctx
     - nolintlint
     - rowserrcheck
-    - scopelint
+    - exportloopref
     - staticcheck
     - structcheck
     - typecheck
diff --git a/src/init/omp.ps1 b/src/init/omp.ps1
index 53ba1b77..647958ff 100644
--- a/src/init/omp.ps1
+++ b/src/init/omp.ps1
@@ -19,7 +19,8 @@ function global:Set-PoshContext {}
 function global:Set-PoshGitStatus {
     if (Get-Module -Name "posh-git") {
         [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSProvideCommentHelp', '', Justification = 'Variable used later(not in this scope)')]
-        $Global:GitStatus = Get-GitStatus
+        $global:GitStatus = Get-GitStatus
+        $env:POSH_GIT_STATUS = Write-GitStatus -Status $global:GitStatus
     }
 }
 
@@ -46,6 +47,7 @@ function global:Set-PoshGitStatus {
             $executionTime = ($history.EndExecutionTime - $history.StartExecutionTime).TotalMilliseconds
             $global:omp_lastHistoryId = $history.Id
     }
+    Set-PoshGitStatus
     $omp = "::OMP::"
     $config = $global:PoshSettings.Theme
     $cleanPWD = $PWD.ProviderPath.TrimEnd("\")
@@ -53,7 +55,6 @@ function global:Set-PoshGitStatus {
     $standardOut = @(&$omp --error="$errorCode" --pwd="$cleanPWD" --pswd="$cleanPSWD" --execution-time="$executionTime" --config="$config" 2>&1)
     # the output can be multiline, joining these ensures proper rendering by adding line breaks with `n
     $standardOut -join "`n"
-    Set-PoshGitStatus
     $global:LASTEXITCODE = $realLASTEXITCODE
     #remove temp variables
     Remove-Variable realLASTEXITCODE -Confirm:$false
diff --git a/src/segment.go b/src/segment.go
index 3e36b768..9d725779 100644
--- a/src/segment.go
+++ b/src/segment.go
@@ -96,6 +96,8 @@ const (
 	Aws SegmentType = "aws"
 	// Java writes the active java version
 	Java SegmentType = "java"
+	// PoshGit writes the posh git prompt
+	PoshGit SegmentType = "poshgit"
 )
 
 func (segment *Segment) string() string {
@@ -219,6 +221,7 @@ func (segment *Segment) mapSegmentWithWriter(env environmentInfo) error {
 		Ruby:          &ruby{},
 		Aws:           &aws{},
 		Java:          &java{},
+		PoshGit:       &poshgit{},
 	}
 	if writer, ok := functions[segment.Type]; ok {
 		props := &properties{
diff --git a/src/segment_posh_git.go b/src/segment_posh_git.go
new file mode 100644
index 00000000..32ee1ef5
--- /dev/null
+++ b/src/segment_posh_git.go
@@ -0,0 +1,28 @@
+package main
+
+import "strings"
+
+type poshgit struct {
+	props     *properties
+	env       environmentInfo
+	gitStatus string
+}
+
+const (
+	poshGitEnv = "POSH_GIT_STATUS"
+)
+
+func (p *poshgit) enabled() bool {
+	status := p.env.getenv(poshGitEnv)
+	p.gitStatus = strings.TrimSpace(status)
+	return p.gitStatus != ""
+}
+
+func (p *poshgit) string() string {
+	return p.gitStatus
+}
+
+func (p *poshgit) init(props *properties, env environmentInfo) {
+	p.props = props
+	p.env = env
+}
diff --git a/themes/schema.json b/themes/schema.json
index 050d23a8..794745e2 100644
--- a/themes/schema.json
+++ b/themes/schema.json
@@ -155,7 +155,8 @@
             "ytm",
             "executiontime",
             "aws",
-            "java"
+            "java",
+            "poshgit"
           ]
         },
         "style": {
@@ -1419,6 +1420,17 @@
               }
             }
           }
+        },
+        {
+          "if": {
+            "properties": {
+              "type": { "const": "poshgit" }
+            }
+          },
+          "then": {
+            "title": "Posh-Git Segment",
+            "description": "https://ohmyposh.dev/docs/poshgit"
+          }
         }
       ]
     }


Test patch:
diff --git a/src/segment_posh_git_test.go b/src/segment_posh_git_test.go
new file mode 100644
index 00000000..d8e3b3b7
--- /dev/null
+++ b/src/segment_posh_git_test.go
@@ -0,0 +1,32 @@
+package main
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestPoshGitSegment(t *testing.T) {
+	cases := []struct {
+		Case          string
+		PoshGitPrompt string
+		Expected      string
+		Enabled       bool
+	}{
+		{Case: "regular prompt", PoshGitPrompt: "my prompt", Expected: "my prompt", Enabled: true},
+		{Case: "prompt with spaces", PoshGitPrompt: "   my prompt", Expected: "my prompt", Enabled: true},
+		{Case: "no prompt", PoshGitPrompt: "", Enabled: false},
+	}
+
+	for _, tc := range cases {
+		env := new(MockedEnvironment)
+		env.On("getenv", poshGitEnv).Return(tc.PoshGitPrompt)
+		p := &poshgit{
+			env: env,
+		}
+		assert.Equal(t, tc.Enabled, p.enabled())
+		if tc.Enabled {
+			assert.Equal(t, tc.Expected, p.string())
+		}
+	}
+}