fix(git): cache command on windows

This commit is contained in:
Jan De Dobbeleer 2021-10-20 22:20:47 +02:00 committed by Jan De Dobbeleer
parent cdc2998ed8
commit 631aace7a8
2 changed files with 65 additions and 29 deletions

View file

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

View file

@ -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,32 +906,6 @@ func TestGetBranchStatus(t *testing.T) {
}
}
func TestTruncateBranch(t *testing.T) {
cases := []struct {
Case string
Expected string
Branch string
MaxLength interface{}
}{
{Case: "No limit", Expected: "all-your-base-are-belong-to-us", Branch: "all-your-base-are-belong-to-us"},
{Case: "No limit - larger", Expected: "all-your-base", Branch: "all-your-base-are-belong-to-us", MaxLength: 13.0},
{Case: "No limit - smaller", Expected: "all-your-base", Branch: "all-your-base", MaxLength: 13.0},
{Case: "Invalid setting", Expected: "all-your-base", Branch: "all-your-base", MaxLength: "burp"},
{Case: "Lower than limit", Expected: "all-your-base", Branch: "all-your-base", MaxLength: 20.0},
}
for _, tc := range cases {
g := &git{
props: &properties{
values: map[Property]interface{}{
BranchMaxLength: tc.MaxLength,
},
},
}
assert.Equal(t, tc.Expected, g.truncateBranch(tc.Branch), tc.Case)
}
}
func TestShouldIgnoreRootRepository(t *testing.T) {
cases := []struct {
Case string
@ -958,3 +938,55 @@ func TestShouldIgnoreRootRepository(t *testing.T) {
assert.Equal(t, tc.Expected, got, tc.Case)
}
}
func TestTruncateBranch(t *testing.T) {
cases := []struct {
Case string
Expected string
Branch string
MaxLength interface{}
}{
{Case: "No limit", Expected: "all-your-base-are-belong-to-us", Branch: "all-your-base-are-belong-to-us"},
{Case: "No limit - larger", Expected: "all-your-base", Branch: "all-your-base-are-belong-to-us", MaxLength: 13.0},
{Case: "No limit - smaller", Expected: "all-your-base", Branch: "all-your-base", MaxLength: 13.0},
{Case: "Invalid setting", Expected: "all-your-base", Branch: "all-your-base", MaxLength: "burp"},
{Case: "Lower than limit", Expected: "all-your-base", Branch: "all-your-base", MaxLength: 20.0},
}
for _, tc := range cases {
g := &git{
props: &properties{
values: map[Property]interface{}{
BranchMaxLength: tc.MaxLength,
},
},
}
assert.Equal(t, tc.Expected, g.truncateBranch(tc.Branch), tc.Case)
}
}
func TestGetGitCommand(t *testing.T) {
cases := []struct {
Case string
Expected string
IsWSL 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"},
}
for _, tc := range cases {
env := new(MockedEnvironment)
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,
}
assert.Equal(t, tc.Expected, g.getGitCommand(), tc.Case)
}
}