mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-08-10 22:42:00 +02:00
Allow using shell aliases in interactive custom commands (#3793)
- **PR Description** When executing an interactive custom command, use the user's shell rather than "bash", and pass the -i flag. This makes it possible to use shell aliases or shell functions which are not available in non-interactive shells. In previous attempts to solve this, concerns were brought up: [this one](https://github.com/jesseduffield/lazygit/pull/2096#issuecomment-1257072541) is addressed by using the interactive shell only for custom commands but not anything else. [This one](https://github.com/jesseduffield/lazygit/pull/2096#issuecomment-1343341795) is a little dubious and unconfirmed, so I'm not very worried about it. Supersedes #2096 and #3299. Fixes #770, #899, and #1642.
This commit is contained in:
@@ -38,6 +38,10 @@ func (self *gitCmdObjBuilder) NewShell(cmdStr string) oscommands.ICmdObj {
|
|||||||
return self.innerBuilder.NewShell(cmdStr).AddEnvVars(defaultEnvVar)
|
return self.innerBuilder.NewShell(cmdStr).AddEnvVars(defaultEnvVar)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *gitCmdObjBuilder) NewInteractiveShell(cmdStr string) oscommands.ICmdObj {
|
||||||
|
return self.innerBuilder.NewInteractiveShell(cmdStr).AddEnvVars(defaultEnvVar)
|
||||||
|
}
|
||||||
|
|
||||||
func (self *gitCmdObjBuilder) Quote(str string) string {
|
func (self *gitCmdObjBuilder) Quote(str string) string {
|
||||||
return self.innerBuilder.Quote(str)
|
return self.innerBuilder.Quote(str)
|
||||||
}
|
}
|
||||||
|
@@ -14,6 +14,8 @@ type ICmdObjBuilder interface {
|
|||||||
New(args []string) ICmdObj
|
New(args []string) ICmdObj
|
||||||
// NewShell takes a string like `git commit` and returns an executable shell command for it e.g. `sh -c 'git commit'`
|
// NewShell takes a string like `git commit` and returns an executable shell command for it e.g. `sh -c 'git commit'`
|
||||||
NewShell(commandStr string) ICmdObj
|
NewShell(commandStr string) ICmdObj
|
||||||
|
// Like NewShell, but uses the user's shell rather than "bash", and passes -i to it
|
||||||
|
NewInteractiveShell(commandStr string) ICmdObj
|
||||||
// Quote wraps a string in quotes with any necessary escaping applied. The reason for bundling this up with the other methods in this interface is that we basically always need to make use of this when creating new command objects.
|
// Quote wraps a string in quotes with any necessary escaping applied. The reason for bundling this up with the other methods in this interface is that we basically always need to make use of this when creating new command objects.
|
||||||
Quote(str string) string
|
Quote(str string) string
|
||||||
}
|
}
|
||||||
@@ -43,10 +45,23 @@ func (self *CmdObjBuilder) NewWithEnviron(args []string, env []string) ICmdObj {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (self *CmdObjBuilder) NewShell(commandStr string) ICmdObj {
|
func (self *CmdObjBuilder) NewShell(commandStr string) ICmdObj {
|
||||||
var quotedCommand string
|
quotedCommand := self.quotedCommandString(commandStr)
|
||||||
|
cmdArgs := str.ToArgv(fmt.Sprintf("%s %s %s", self.platform.Shell, self.platform.ShellArg, quotedCommand))
|
||||||
|
|
||||||
|
return self.New(cmdArgs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *CmdObjBuilder) NewInteractiveShell(commandStr string) ICmdObj {
|
||||||
|
quotedCommand := self.quotedCommandString(commandStr)
|
||||||
|
cmdArgs := str.ToArgv(fmt.Sprintf("%s %s %s %s", self.platform.InteractiveShell, self.platform.InteractiveShellArg, self.platform.ShellArg, quotedCommand))
|
||||||
|
|
||||||
|
return self.New(cmdArgs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *CmdObjBuilder) quotedCommandString(commandStr string) string {
|
||||||
// Windows does not seem to like quotes around the command
|
// Windows does not seem to like quotes around the command
|
||||||
if self.platform.OS == "windows" {
|
if self.platform.OS == "windows" {
|
||||||
quotedCommand = strings.NewReplacer(
|
return strings.NewReplacer(
|
||||||
"^", "^^",
|
"^", "^^",
|
||||||
"&", "^&",
|
"&", "^&",
|
||||||
"|", "^|",
|
"|", "^|",
|
||||||
@@ -54,13 +69,9 @@ func (self *CmdObjBuilder) NewShell(commandStr string) ICmdObj {
|
|||||||
">", "^>",
|
">", "^>",
|
||||||
"%", "^%",
|
"%", "^%",
|
||||||
).Replace(commandStr)
|
).Replace(commandStr)
|
||||||
} else {
|
|
||||||
quotedCommand = self.Quote(commandStr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cmdArgs := str.ToArgv(fmt.Sprintf("%s %s %s", self.platform.Shell, self.platform.ShellArg, quotedCommand))
|
return self.Quote(commandStr)
|
||||||
|
|
||||||
return self.New(cmdArgs)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *CmdObjBuilder) CloneWithNewRunner(decorate func(ICmdObjRunner) ICmdObjRunner) *CmdObjBuilder {
|
func (self *CmdObjBuilder) CloneWithNewRunner(decorate func(ICmdObjRunner) ICmdObjRunner) *CmdObjBuilder {
|
||||||
|
@@ -53,7 +53,9 @@ func NewDummyCmdObjBuilder(runner ICmdObjRunner) *CmdObjBuilder {
|
|||||||
var dummyPlatform = &Platform{
|
var dummyPlatform = &Platform{
|
||||||
OS: "darwin",
|
OS: "darwin",
|
||||||
Shell: "bash",
|
Shell: "bash",
|
||||||
|
InteractiveShell: "bash",
|
||||||
ShellArg: "-c",
|
ShellArg: "-c",
|
||||||
|
InteractiveShellArg: "-i",
|
||||||
OpenCommand: "open {{filename}}",
|
OpenCommand: "open {{filename}}",
|
||||||
OpenLinkCommand: "open {{link}}",
|
OpenLinkCommand: "open {{link}}",
|
||||||
}
|
}
|
||||||
|
@@ -37,7 +37,9 @@ type OSCommand struct {
|
|||||||
type Platform struct {
|
type Platform struct {
|
||||||
OS string
|
OS string
|
||||||
Shell string
|
Shell string
|
||||||
|
InteractiveShell string
|
||||||
ShellArg string
|
ShellArg string
|
||||||
|
InteractiveShellArg string
|
||||||
OpenCommand string
|
OpenCommand string
|
||||||
OpenLinkCommand string
|
OpenLinkCommand string
|
||||||
}
|
}
|
||||||
|
@@ -4,6 +4,7 @@
|
|||||||
package oscommands
|
package oscommands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -11,8 +12,18 @@ func GetPlatform() *Platform {
|
|||||||
return &Platform{
|
return &Platform{
|
||||||
OS: runtime.GOOS,
|
OS: runtime.GOOS,
|
||||||
Shell: "bash",
|
Shell: "bash",
|
||||||
|
InteractiveShell: getUserShell(),
|
||||||
ShellArg: "-c",
|
ShellArg: "-c",
|
||||||
|
InteractiveShellArg: "-i",
|
||||||
OpenCommand: "open {{filename}}",
|
OpenCommand: "open {{filename}}",
|
||||||
OpenLinkCommand: "open {{link}}",
|
OpenLinkCommand: "open {{link}}",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getUserShell() string {
|
||||||
|
if shell := os.Getenv("SHELL"); shell != "" {
|
||||||
|
return shell
|
||||||
|
}
|
||||||
|
|
||||||
|
return "bash"
|
||||||
|
}
|
||||||
|
@@ -4,6 +4,8 @@ func GetPlatform() *Platform {
|
|||||||
return &Platform{
|
return &Platform{
|
||||||
OS: "windows",
|
OS: "windows",
|
||||||
Shell: "cmd",
|
Shell: "cmd",
|
||||||
|
InteractiveShell: "cmd",
|
||||||
ShellArg: "/c",
|
ShellArg: "/c",
|
||||||
|
InteractiveShellArg: "",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -31,7 +31,7 @@ func (self *CustomCommandAction) Call() error {
|
|||||||
|
|
||||||
self.c.LogAction(self.c.Tr.Actions.CustomCommand)
|
self.c.LogAction(self.c.Tr.Actions.CustomCommand)
|
||||||
return self.c.RunSubprocessAndRefresh(
|
return self.c.RunSubprocessAndRefresh(
|
||||||
self.c.OS().Cmd.NewShell(command),
|
self.c.OS().Cmd.NewInteractiveShell(command),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
HandleDeleteSuggestion: func(index int) error {
|
HandleDeleteSuggestion: func(index int) error {
|
||||||
|
Reference in New Issue
Block a user