diff --git a/pkg/commands/files.go b/pkg/commands/files.go index 4071c8c76..8c86cfcf7 100644 --- a/pkg/commands/files.go +++ b/pkg/commands/files.go @@ -318,7 +318,11 @@ func (c *GitCommand) ResetAndClean() error { } func (c *GitCommand) EditFileCmdStr(filename string) (string, error) { - editor := c.GetConfigValue("core.editor") + editor := c.Config.GetUserConfig().OS.EditCommand + + if editor == "" { + editor = c.GetConfigValue("core.editor") + } if editor == "" { editor = c.OSCommand.Getenv("GIT_EDITOR") diff --git a/pkg/commands/files_test.go b/pkg/commands/files_test.go index 20c18a9a0..ba2632997 100644 --- a/pkg/commands/files_test.go +++ b/pkg/commands/files_test.go @@ -720,6 +720,7 @@ func TestGitCommandRemoveUntrackedFiles(t *testing.T) { func TestEditFileCmdStr(t *testing.T) { type scenario struct { filename string + configEditCommand string command func(string, ...string) *exec.Cmd getenv func(string) string getGitConfigValue func(string) (string, error) @@ -729,6 +730,7 @@ func TestEditFileCmdStr(t *testing.T) { scenarios := []scenario{ { "test", + "", func(name string, arg ...string) *exec.Cmd { return secureexec.Command("exit", "1") }, @@ -744,6 +746,25 @@ func TestEditFileCmdStr(t *testing.T) { }, { "test", + "nano", + func(name string, args ...string) *exec.Cmd { + assert.Equal(t, "which", name) + return secureexec.Command("echo") + }, + func(env string) string { + return "" + }, + func(cf string) (string, error) { + return "", nil + }, + func(cmdStr string, err error) { + assert.NoError(t, err) + assert.Equal(t, "nano \"test\"", cmdStr) + }, + }, + { + "test", + "", func(name string, arg ...string) *exec.Cmd { assert.Equal(t, "which", name) return secureexec.Command("exit", "1") @@ -761,6 +782,7 @@ func TestEditFileCmdStr(t *testing.T) { }, { "test", + "", func(name string, arg ...string) *exec.Cmd { assert.Equal(t, "which", name) return secureexec.Command("exit", "1") @@ -781,6 +803,7 @@ func TestEditFileCmdStr(t *testing.T) { }, { "test", + "", func(name string, arg ...string) *exec.Cmd { assert.Equal(t, "which", name) return secureexec.Command("exit", "1") @@ -802,6 +825,7 @@ func TestEditFileCmdStr(t *testing.T) { }, { "test", + "", func(name string, arg ...string) *exec.Cmd { assert.Equal(t, "which", name) return secureexec.Command("echo") @@ -819,6 +843,7 @@ func TestEditFileCmdStr(t *testing.T) { }, { "file/with space", + "", func(name string, args ...string) *exec.Cmd { assert.Equal(t, "which", name) return secureexec.Command("echo") @@ -838,6 +863,7 @@ func TestEditFileCmdStr(t *testing.T) { for _, s := range scenarios { gitCmd := NewDummyGitCommand() + gitCmd.Config.GetUserConfig().OS.EditCommand = s.configEditCommand gitCmd.OSCommand.Command = s.command gitCmd.OSCommand.Getenv = s.getenv gitCmd.getGitConfigValue = s.getGitConfigValue diff --git a/pkg/config/config_default_platform.go b/pkg/config/config_default_platform.go index e9e2f63ba..fabdf8a88 100644 --- a/pkg/config/config_default_platform.go +++ b/pkg/config/config_default_platform.go @@ -5,6 +5,7 @@ package config // GetPlatformDefaultConfig gets the defaults for the platform func GetPlatformDefaultConfig() OSConfig { return OSConfig{ + EditCommand: ``, OpenCommand: "open {{filename}}", OpenLinkCommand: "open {{link}}", } diff --git a/pkg/config/config_linux.go b/pkg/config/config_linux.go index b3d7f42b8..8399e0e47 100644 --- a/pkg/config/config_linux.go +++ b/pkg/config/config_linux.go @@ -3,6 +3,7 @@ package config // GetPlatformDefaultConfig gets the defaults for the platform func GetPlatformDefaultConfig() OSConfig { return OSConfig{ + EditCommand: ``, OpenCommand: `sh -c "xdg-open {{filename}} >/dev/null"`, OpenLinkCommand: `sh -c "xdg-open {{link}} >/dev/null"`, } diff --git a/pkg/config/config_windows.go b/pkg/config/config_windows.go index cea722424..f6780eb6b 100644 --- a/pkg/config/config_windows.go +++ b/pkg/config/config_windows.go @@ -3,6 +3,7 @@ package config // GetPlatformDefaultConfig gets the defaults for the platform func GetPlatformDefaultConfig() OSConfig { return OSConfig{ + EditCommand: ``, OpenCommand: `cmd /c "start "" {{filename}}"`, OpenLinkCommand: `cmd /c "start "" {{link}}"`, } diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index cf193011b..b36c68bb5 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -248,6 +248,9 @@ type KeybindingSubmodulesConfig struct { // OSConfig contains config on the level of the os type OSConfig struct { + // EditCommand is the command for editing a file + EditCommand string `yaml:"editCommand,omitempty"` + // OpenCommand is the command for opening a file OpenCommand string `yaml:"openCommand,omitempty"`