Skip to content

Commit

Permalink
fix(git): cache command on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Oct 20, 2021
1 parent cdc2998 commit 631aace
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 24 deletions.
10 changes: 7 additions & 3 deletions src/segment_git.go
Expand Up @@ -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")
Expand Down Expand Up @@ -320,16 +320,20 @@ 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/")
}
gitCommand := "git"
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
}

Expand Down
74 changes: 53 additions & 21 deletions src/segment_git_test.go
Expand Up @@ -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,
}
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}

0 comments on commit 631aace

Please sign in to comment.