summaryrefslogtreecommitdiffstats
path: root/pkg/commands/files.go
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2020-11-25 08:52:00 +1100
committerJesse Duffield <jessedduffield@gmail.com>2020-11-28 10:45:30 +1100
commit999e170f1d17b652dad231f1cbde6ab4bbeae8c7 (patch)
tree3a7ea186c3b868661242731a029dce3df396de1e /pkg/commands/files.go
parent7513bfb13a48c566e9ab0c31e259ea73d562c292 (diff)
standardise how we read from the config
Diffstat (limited to 'pkg/commands/files.go')
-rw-r--r--pkg/commands/files.go27
1 files changed, 27 insertions, 0 deletions
diff --git a/pkg/commands/files.go b/pkg/commands/files.go
index 05aa89978..93d1ccf03 100644
--- a/pkg/commands/files.go
+++ b/pkg/commands/files.go
@@ -2,6 +2,7 @@ package commands
import (
"fmt"
+ "os/exec"
"path/filepath"
"strings"
"time"
@@ -9,6 +10,7 @@ import (
"github.com/go-errors/errors"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/utils"
+ "github.com/mgutz/str"
)
// CatFile obtains the content of a file
@@ -262,3 +264,28 @@ func (c *GitCommand) ResetAndClean() error {
return c.RemoveUntrackedFiles()
}
+
+// EditFile opens a file in a subprocess using whatever editor is available,
+// falling back to core.editor, VISUAL, EDITOR, then vi
+func (c *GitCommand) EditFile(filename string) (*exec.Cmd, error) {
+ editor, _ := c.getGlobalGitConfig("core.editor")
+
+ if editor == "" {
+ editor = c.OSCommand.Getenv("VISUAL")
+ }
+ if editor == "" {
+ editor = c.OSCommand.Getenv("EDITOR")
+ }
+ if editor == "" {
+ if err := c.OSCommand.RunCommand("which vi"); err == nil {
+ editor = "vi"
+ }
+ }
+ if editor == "" {
+ return nil, errors.New("No editor defined in $VISUAL, $EDITOR, or git config")
+ }
+
+ splitCmd := str.ToArgv(fmt.Sprintf("%s %s", editor, c.OSCommand.Quote(filename)))
+
+ return c.OSCommand.PrepareSubProcess(splitCmd[0], splitCmd[1:]...), nil
+}