fix(git): do not use git.exe on WSL 1

This commit is contained in:
Jan De Dobbeleer 2021-11-25 14:19:23 +01:00 committed by Jan De Dobbeleer
parent b94e96dd15
commit e8a4fa19b7
2 changed files with 27 additions and 8 deletions

View file

@ -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 {

View file

@ -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,
}