From e8a4fa19b79b02241cbd7994ad16d8110da0f760 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Thu, 25 Nov 2021 14:19:23 +0100 Subject: [PATCH] fix(git): do not use git.exe on WSL 1 --- src/segment_git.go | 24 ++++++++++++++++++------ src/segment_git_test.go | 11 +++++++++-- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/segment_git.go b/src/segment_git.go index f72ca35e..2be71717 100644 --- a/src/segment_git.go +++ b/src/segment_git.go @@ -81,6 +81,8 @@ type git struct { gitWorkingFolder string // .git working folder, can be different of root if using worktree gitRootFolder string // .git root folder gitWorktreeFolder string // .git real worktree path + + gitCommand string } const ( @@ -284,14 +286,24 @@ func (g *git) setGitStatus() { } func (g *git) getGitCommand() string { - inWSLSharedDrive := func(env environmentInfo) bool { - return env.isWsl() && strings.HasPrefix(env.getcwd(), "/mnt/") + if len(g.gitCommand) > 0 { + return g.gitCommand } - gitCommand := "git" - if g.env.getRuntimeGOOS() == windowsPlatform || inWSLSharedDrive(g.env) { - gitCommand = "git.exe" + inWSL2SharedDrive := func(env environmentInfo) bool { + if !env.isWsl() { + return false + } + if !strings.HasPrefix(env.getcwd(), "/mnt/") { + return false + } + uname, _ := g.env.runCommand("uname", "-r") + return strings.Contains(uname, "WSL2") } - return gitCommand + g.gitCommand = "git" + if g.env.getRuntimeGOOS() == windowsPlatform || inWSL2SharedDrive(g.env) { + g.gitCommand = "git.exe" + } + return g.gitCommand } func (g *git) getGitCommandOutput(args ...string) string { diff --git a/src/segment_git_test.go b/src/segment_git_test.go index e941247d..3d5da5af 100644 --- a/src/segment_git_test.go +++ b/src/segment_git_test.go @@ -700,13 +700,15 @@ func TestGetGitCommand(t *testing.T) { Case string Expected string IsWSL bool + IsWSL1 bool GOOS string CWD string }{ {Case: "On Windows", Expected: "git.exe", GOOS: windowsPlatform}, {Case: "Non Windows", Expected: "git"}, - {Case: "Iside WSL, non shared", IsWSL: true, Expected: "git"}, - {Case: "Iside WSL, shared", Expected: "git.exe", IsWSL: true, CWD: "/mnt/bill"}, + {Case: "Iside WSL2, non shared", IsWSL: true, Expected: "git"}, + {Case: "Iside WSL2, shared", Expected: "git.exe", IsWSL: true, CWD: "/mnt/bill"}, + {Case: "Iside WSL1, shared", Expected: "git", IsWSL: true, IsWSL1: true, CWD: "/mnt/bill"}, } for _, tc := range cases { @@ -714,6 +716,11 @@ func TestGetGitCommand(t *testing.T) { env.On("isWsl", nil).Return(tc.IsWSL) env.On("getRuntimeGOOS", nil).Return(tc.GOOS) env.On("getcwd", nil).Return(tc.CWD) + wslUname := "5.10.60.1-microsoft-standard-WSL2" + if tc.IsWSL1 { + wslUname = "4.4.0-19041-Microsoft" + } + env.On("runCommand", "uname", []string{"-r"}).Return(wslUname, nil) g := &git{ env: env, }