Repository: JanDeDobbeleer/oh-my-posh
Pull request #4866 (2024-04-22 06:54:47)

fix(path): construct currentpath correctly on Windows
### Prerequisites

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

### Description

relates to #4727

<!---

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.

-->

[CONTRIBUTING.md]: https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/CONTRIBUTING.md
[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary



Patch:
diff --git a/src/segments/path.go b/src/segments/path.go
index c3d0a674..122c0ee6 100644
--- a/src/segments/path.go
+++ b/src/segments/path.go
@@ -710,11 +710,20 @@ func (pt *Path) splitPath() Folders {
 
 	folderFormatMap := pt.makeFolderFormatMap()
 
-	currentPath := pt.root
-	if currentPath == "~" {
-		currentPath = pt.env.Home() + pt.env.PathSeparator()
+	getCurrentPath := func() string {
+		if pt.root == "~" {
+			return pt.env.Home() + pt.env.PathSeparator()
+		}
+
+		if pt.env.GOOS() == platform.WINDOWS {
+			return pt.root + pt.env.PathSeparator()
+		}
+
+		return pt.root
 	}
 
+	currentPath := getCurrentPath()
+
 	var display bool
 
 	for _, folder := range folders {


Test patch:
diff --git a/src/segments/path_test.go b/src/segments/path_test.go
index b4854293..414d61a9 100644
--- a/src/segments/path_test.go
+++ b/src/segments/path_test.go
@@ -1506,6 +1506,7 @@ func TestReplaceMappedLocations(t *testing.T) {
 func TestSplitPath(t *testing.T) {
 	cases := []struct {
 		Case         string
+		GOOS         string
 		Relative     string
 		Root         string
 		GitDir       *platform.FileInfo
@@ -1517,6 +1518,7 @@ func TestSplitPath(t *testing.T) {
 			Case:     "Regular directory",
 			Root:     "/",
 			Relative: "c/d",
+			GOOS:     platform.DARWIN,
 			Expected: Folders{
 				{Name: "c", Path: "/c"},
 				{Name: "d", Path: "/c/d"},
@@ -1526,6 +1528,7 @@ func TestSplitPath(t *testing.T) {
 			Case:         "Home directory - git folder",
 			Root:         "~",
 			Relative:     "c/d",
+			GOOS:         platform.DARWIN,
 			GitDir:       &platform.FileInfo{IsDir: true, ParentFolder: "/a/b/c"},
 			GitDirFormat: "<b>%s</b>",
 			Expected: Folders{
@@ -1533,6 +1536,20 @@ func TestSplitPath(t *testing.T) {
 				{Name: "d", Path: "/a/b/c/d"},
 			},
 		},
+		{
+			Case:         "Home directory - git folder on Windows",
+			Root:         "C:",
+			Relative:     "a/b/c/d",
+			GOOS:         platform.WINDOWS,
+			GitDir:       &platform.FileInfo{IsDir: true, ParentFolder: "C:/a/b/c"},
+			GitDirFormat: "<b>%s</b>",
+			Expected: Folders{
+				{Name: "a", Path: "C:/a"},
+				{Name: "b", Path: "C:/a/b"},
+				{Name: "<b>c</b>", Path: "C:/a/b/c", Display: true},
+				{Name: "d", Path: "C:/a/b/c/d"},
+			},
+		},
 	}
 
 	for _, tc := range cases {
@@ -1540,6 +1557,7 @@ func TestSplitPath(t *testing.T) {
 		env.On("PathSeparator").Return("/")
 		env.On("Home").Return("/a/b")
 		env.On("HasParentFilePath", ".git").Return(tc.GitDir, nil)
+		env.On("GOOS").Return(tc.GOOS)
 		path := &Path{
 			env: env,
 			props: properties.Map{