Determine shell type once by the basename
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent 159a37f · Browse files · 1 file·+22 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+22 added−7 removed
- other:+22 added−7 removed
Provenance
- Commit
7ed6c79- Tree
d8118b3- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
159a37f
1 file changed+22 added−7 removed
modified:src/util/util_windows.go+22 added-7 removed
4 unmodified lines5 import (6 "fmt"7 "os"8 "os/exec"9+ "path/filepath"10 "regexp"11 "strings"12 "sync/atomic"13 "syscall"14 )15 16+type shellType int17+18+const (19+ shellTypeUnknown shellType = iota20+ shellTypeCmd21+ shellTypePowerShell22+)23+24 type Executor struct {25 shell string26+ shellType shellType27 args []string28 shellPath atomic.Value29 }30 5 unmodified lines36 } else if len(shell) == 0 {37 shell = "cmd"38 }39 40+ shellType := shellTypeUnknown41+ basename := filepath.Base(shell)42 if len(args) > 0 {43 args = args[1:]32- } else if strings.Contains(shell, "cmd") {44+ } else if strings.HasPrefix(basename, "cmd") {45+ shellType = shellTypeCmd46 args = []string{"/v:on/s/c"}34- } else if strings.Contains(shell, "pwsh") || strings.Contains(shell, "powershell") {47+ } else if strings.HasPrefix(basename, "pwsh") || strings.HasPrefix(basename, "powershell") {48+ shellType = shellTypePowerShell49 args = []string{"-NoProfile", "-Command"}50 } else {51 args = []string{"-c"}52 }39- return &Executor{shell: shell, args: args}53+ return &Executor{shell: shell, shellType: shellType, args: args}54 }55 56 // ExecCommand executes the given command with $SHELL57 // FIXME: setpgid is unused. We set it in the Unix implementation so that we13 unmodified lines71 }72 x.shellPath.Store(shell)73 }74 var cmd *exec.Cmd61- if strings.Contains(shell, "cmd") {75+ if x.shellType == shellTypeCmd {76 cmd = exec.Command(shell)77 cmd.SysProcAttr = &syscall.SysProcAttr{78 HideWindow: false,79 CmdLine: fmt.Sprintf(`%s "%s"`, strings.Join(x.args, " "), command),29 unmodified lines109 Exit(0)110 }111 112 func (x *Executor) QuoteEntry(entry string) string {99- if strings.Contains(x.shell, "cmd") {113+ switch x.shellType {114+ case shellTypeCmd:115 // backslash escaping is done here for applications116 // (see ripgrep test case in terminal_test.go#TestWindowsCommands)117 escaped := strings.Replace(entry, `\`, `\\`, -1)118 escaped = `"` + strings.Replace(escaped, `"`, `\"`, -1) + `"`1 unmodified line120 r, _ := regexp.Compile(`[&|<>()@^%!"]`)121 return r.ReplaceAllStringFunc(escaped, func(match string) string {122 return "^" + match123 })109- } else if strings.Contains(x.shell, "pwsh") || strings.Contains(x.shell, "powershell") {124+ case shellTypePowerShell:125 escaped := strings.Replace(entry, `"`, `\"`, -1)126 return "'" + strings.Replace(escaped, "'", "''", -1) + "'"112- } else {127+ default:128 return "'" + strings.Replace(entry, "'", "'\\''", -1) + "'"129 }130 }131 23 unmodified lines