Repository: JanDeDobbeleer/oh-my-posh
Pull request #2076 (2022-04-11 18:55:18)

feat(git): --separate-git-dir support
### Prerequisites

- [x] I have read and understood 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)
- [ ] Docs have been added / updated (for bug fixes/features)

### Description

<!--TemplateBody-->
https://github.com/JanDeDobbeleer/oh-my-posh/issues/2074

should not break existing code: new fallback added after worktree and submodule if a gitdir ending with ".git" is found.
**Still have to reboot to test it with wsl/wsl2.**

<!---

Tips:

If you're not comfortable with working with Git, we're working a guide (https://ohmyposh.dev/docs/contributing_git) to help you out.
Oh My Posh advises GitKraken (https://www.gitkraken.com/invite/nQmDPR9D) as your preferred cross platform Git GUI power tool.

-->

[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary


JanDeDobbeleer: @lnu let me know when you're confident.
lnu: @JanDeDobbeleer so I tested it on windows/wsl with and without mounted folder. So it works until we find another submodule/worktree/separate-git-dir scenario :).


Patch:
diff --git a/src/segments/git.go b/src/segments/git.go
index e0b71e76..02b328f7 100644
--- a/src/segments/git.go
+++ b/src/segments/git.go
@@ -190,6 +190,15 @@ func (g *Git) shouldDisplay() bool {
 			return true
 		}
 
+		// check for separate git folder(--separate-git-dir)
+		// check if the folder contains a HEAD file
+		if g.env.HasFilesInDir(g.gitWorkingFolder, "HEAD") {
+			gitFolder := strings.TrimSuffix(g.gitRootFolder, ".git")
+			g.gitRootFolder = g.gitWorkingFolder
+			g.gitWorkingFolder = gitFolder
+			g.gitRealFolder = gitFolder
+			return true
+		}
 		return false
 	}
 	return false


Test patch:
diff --git a/src/segments/git_test.go b/src/segments/git_test.go
index ca5cab39..fa384bd4 100644
--- a/src/segments/git_test.go
+++ b/src/segments/git_test.go
@@ -109,6 +109,33 @@ func TestEnabledInSubmodule(t *testing.T) {
 	assert.Equal(t, "/dev/parent/test-submodule/../.git/modules/test-submodule", g.gitRootFolder)
 }
 
+func TestEnabledInSeparateGitDir(t *testing.T) {
+	env := new(mock.MockedEnvironment)
+	env.On("InWSLSharedDrive").Return(false)
+	env.On("HasCommand", "git").Return(true)
+	env.On("GOOS").Return("")
+	env.On("IsWsl").Return(false)
+	fileInfo := &environment.FileInfo{
+		Path:         "/dev/parent/test-separate-git-dir/.git",
+		ParentFolder: "/dev/parent/test-separate-git-dir",
+		IsDir:        false,
+	}
+	env.On("HasFilesInDir", "/dev/separate-git-dir", "HEAD").Return(true)
+	env.On("FileContent", "/dev/parent/test-separate-git-dir//HEAD").Return("")
+	env.MockGitCommand("/dev/parent/test-separate-git-dir/", "", "describe", "--tags", "--exact-match")
+	env.On("HasParentFilePath", ".git").Return(fileInfo, nil)
+	env.On("FileContent", "/dev/parent/test-separate-git-dir/.git").Return("gitdir: /dev/separate-git-dir")
+	g := &Git{
+		scm: scm{
+			env:   env,
+			props: properties.Map{},
+		},
+	}
+	assert.True(t, g.Enabled())
+	assert.Equal(t, "/dev/parent/test-separate-git-dir/", g.gitWorkingFolder)
+	assert.Equal(t, "/dev/parent/test-separate-git-dir/", g.gitRealFolder)
+	assert.Equal(t, "/dev/separate-git-dir", g.gitRootFolder)
+}
 func TestGetGitOutputForCommand(t *testing.T) {
 	args := []string{"-C", "", "--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false"}
 	commandArgs := []string{"symbolic-ref", "--short", "HEAD"}