From 631aace7a81fa2d144b5d1860fa4a58d0f8b1201 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Wed, 20 Oct 2021 22:20:47 +0200 Subject: [PATCH] fix(git): cache command on windows --- src/segment_git.go | 10 ++++-- src/segment_git_test.go | 74 +++++++++++++++++++++++++++++------------ 2 files changed, 60 insertions(+), 24 deletions(-) diff --git a/src/segment_git.go b/src/segment_git.go index c932c8131e64..36f9677dd226 100644 --- a/src/segment_git.go +++ b/src/segment_git.go @@ -128,7 +128,7 @@ const ( ) func (g *git) enabled() bool { - if !g.env.hasCommand("git") { + if !g.env.hasCommand(g.getGitCommand()) { return false } gitdir, err := g.env.hasParentFilePath(".git") @@ -320,7 +320,7 @@ func (g *git) getStatusColor(defaultValue string) string { return defaultValue } -func (g *git) getGitCommandOutput(args ...string) string { +func (g *git) getGitCommand() string { inWSLSharedDrive := func(env environmentInfo) bool { return env.isWsl() && strings.HasPrefix(env.getcwd(), "/mnt/") } @@ -328,8 +328,12 @@ func (g *git) getGitCommandOutput(args ...string) string { if g.env.getRuntimeGOOS() == windowsPlatform || inWSLSharedDrive(g.env) { gitCommand = "git.exe" } + return gitCommand +} + +func (g *git) getGitCommandOutput(args ...string) string { args = append([]string{"--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false"}, args...) - val, _ := g.env.runCommand(gitCommand, args...) + val, _ := g.env.runCommand(g.getGitCommand(), args...) return val } diff --git a/src/segment_git_test.go b/src/segment_git_test.go index 1ad7ff5be552..161725e83b00 100644 --- a/src/segment_git_test.go +++ b/src/segment_git_test.go @@ -14,6 +14,8 @@ const ( func TestEnabledGitNotFound(t *testing.T) { env := new(MockedEnvironment) env.On("hasCommand", "git").Return(false) + env.On("getRuntimeGOOS", nil).Return("") + env.On("isWsl", nil).Return(false) g := &git{ env: env, } @@ -23,6 +25,8 @@ func TestEnabledGitNotFound(t *testing.T) { func TestEnabledInWorkingDirectory(t *testing.T) { env := new(MockedEnvironment) env.On("hasCommand", "git").Return(true) + env.On("getRuntimeGOOS", nil).Return("") + env.On("isWsl", nil).Return(false) fileInfo := &fileInfo{ path: "/dir/hello", parentFolder: "/dir", @@ -39,6 +43,8 @@ func TestEnabledInWorkingDirectory(t *testing.T) { func TestEnabledInWorkingTree(t *testing.T) { env := new(MockedEnvironment) env.On("hasCommand", "git").Return(true) + env.On("getRuntimeGOOS", nil).Return("") + env.On("isWsl", nil).Return(false) fileInfo := &fileInfo{ path: "/dir/hello", parentFolder: "/dir", @@ -900,6 +906,39 @@ func TestGetBranchStatus(t *testing.T) { } } +func TestShouldIgnoreRootRepository(t *testing.T) { + cases := []struct { + Case string + Dir string + Expected bool + }{ + {Case: "inside excluded", Dir: "/home/bill/repo"}, + {Case: "oustide excluded", Dir: "/home/melinda"}, + {Case: "excluded exact match", Dir: "/home/gates", Expected: true}, + {Case: "excluded inside match", Dir: "/home/gates/bill", Expected: true}, + } + + for _, tc := range cases { + props := map[Property]interface{}{ + ExcludeFolders: []string{ + "/home/bill", + "/home/gates.*", + }, + } + env := new(MockedEnvironment) + env.On("homeDir", nil).Return("/home/bill") + env.On("getRuntimeGOOS", nil).Return(windowsPlatform) + git := &git{ + props: &properties{ + values: props, + }, + env: env, + } + got := git.shouldIgnoreRootRepository(tc.Dir) + assert.Equal(t, tc.Expected, got, tc.Case) + } +} + func TestTruncateBranch(t *testing.T) { cases := []struct { Case string @@ -926,35 +965,28 @@ func TestTruncateBranch(t *testing.T) { } } -func TestShouldIgnoreRootRepository(t *testing.T) { +func TestGetGitCommand(t *testing.T) { cases := []struct { Case string - Dir string - Expected bool + Expected string + IsWSL bool + GOOS string + CWD string }{ - {Case: "inside excluded", Dir: "/home/bill/repo"}, - {Case: "oustide excluded", Dir: "/home/melinda"}, - {Case: "excluded exact match", Dir: "/home/gates", Expected: true}, - {Case: "excluded inside match", Dir: "/home/gates/bill", Expected: true}, + {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"}, } for _, tc := range cases { - props := map[Property]interface{}{ - ExcludeFolders: []string{ - "/home/bill", - "/home/gates.*", - }, - } env := new(MockedEnvironment) - env.On("homeDir", nil).Return("/home/bill") - env.On("getRuntimeGOOS", nil).Return(windowsPlatform) - git := &git{ - props: &properties{ - values: props, - }, + env.On("isWsl", nil).Return(tc.IsWSL) + env.On("getRuntimeGOOS", nil).Return(tc.GOOS) + env.On("getcwd", nil).Return(tc.CWD) + g := &git{ env: env, } - got := git.shouldIgnoreRootRepository(tc.Dir) - assert.Equal(t, tc.Expected, got, tc.Case) + assert.Equal(t, tc.Expected, g.getGitCommand(), tc.Case) } }