diff --git a/command/alias_test.go b/command/alias_test.go index f140a13f8..753e94055 100644 --- a/command/alias_test.go +++ b/command/alias_test.go @@ -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) } diff --git a/command/root.go b/command/root.go index 92a933595..71de36589 100644 --- a/command/root.go +++ b/command/root.go @@ -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