successfully use sh for windows aliases

This commit is contained in:
nate smith 2020-07-14 15:26:15 -05:00 committed by vilmibm
parent f99b54a731
commit 4bd0435c38
2 changed files with 19 additions and 27 deletions

View file

@ -229,7 +229,7 @@ func TestExpandAlias_shell_windows(t *testing.T) {
}
cfg := `---
aliases:
ig: '!gh issue list | select-string -Pattern cool'
ig: '!gh issue list | grep cool'
`
initBlankContext(cfg, "OWNER/REPO", "trunk")
@ -245,7 +245,7 @@ aliases:
t.Fatalf("unexpected error: %s", err)
}
expected := []string{"pwsh", "-Command", "Invoke-Command -ScriptBlock { gh issue list | select-string -Pattern cool } "}
expected := []string{"C:\\Program Files\\Git\\bin\\sh.exe", "-c", "gh issue list | grep cool"}
assert.Equal(t, expected, expanded)
}
@ -257,7 +257,7 @@ func TestExpandAlias_shell_windows_extra_args(t *testing.T) {
cfg := `---
aliases:
co: pr checkout
ig: '!gh issue list --label=$args[0] | select-string -Pattern $args[1]'
ig: '!gh issue list --label=$1 | grep $2'
`
initBlankContext(cfg, "OWNER/REPO", "trunk")
@ -269,7 +269,7 @@ aliases:
t.Fatalf("unexpected error: %s", err)
}
expected := []string{"pwsh", "-Command", "Invoke-Command -ScriptBlock { gh issue list --label=$args[0] | select-string -Pattern $args[1] } -ArgumentList @('bug','foo')"}
expected := []string{"C:\\Program Files\\Git\\bin\\sh.exe", "-c", "gh issue list --label=$1 | grep $2", "--", "bug", "foo"}
assert.Equal(t, expected, expanded)
}

View file

@ -7,6 +7,7 @@ import (
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"runtime/debug"
@ -410,32 +411,23 @@ func ExpandAlias(args []string) (expanded []string, isShell bool, err error) {
isShell = true
expanded = []string{"sh", "-c", expansion[1:]}
if runtime.GOOS == "windows" {
//argList := ""
//if len(args[2:]) > 0 {
// argList = " -ArgumentList @("
// for i, arg := range args[2:] {
// argList += fmt.Sprintf("'%s'", arg)
// if i < len(args[2:])-1 {
// argList += ","
// }
// }
// argList += ")"
//}
//invoke := fmt.Sprintf("Invoke-Command -ScriptBlock { %s } %s", expansion[1:], argList)
//expanded = []string{"pwsh", "-Command", invoke}
executable, err := os.Executable()
if err != nil {
return
// Need to use absolute path for sh on windows
shPath, lookErr := exec.LookPath("sh")
if lookErr != nil {
gitPath, lookErr := exec.LookPath("git")
if lookErr != nil {
// TODO this error could be better probably
err = fmt.Errorf("unable to find sh. you will not be able to use shell aliases on this platform.")
return
}
shPath = filepath.Join(filepath.Dir(gitPath), "..", "bin", "sh.exe")
}
invoke := fmt.Sprintf("gh () { %s \"$@\" }; %s", executable, expansion[1:])
expanded[0] = shPath
}
expanded = []string{"bash", "--posix", "-c", invoke, "--"}
if len(args[2:]) > 0 {
expanded = append(expanded, "--")
expanded = append(expanded, args[2:]...)
} else {
if len(args[2:]) > 0 {
expanded = append(expanded, "--")
expanded = append(expanded, args[2:]...)
}
}
return