Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ ignore:
- .venv
- dist

editor: code # options: code,nvim,lazygit,vim,cursor
editor: code # options: code,nvim,lazygit,vim,cursor — use {path} to place the repo path, e.g. "lazygit --path {path}"
```

-----
Expand Down
39 changes: 39 additions & 0 deletions internal/tui/editor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package tui

import (
"os/exec"
"strings"
)

// cwdOnlyEditors reject a positional repo path (lazygit parses it as a
// subcommand, #23) but open whatever repo they are started in.
var cwdOnlyEditors = map[string]bool{"lazygit": true, "gitui": true, "tig": true}

// editorArgs builds argv for opening repoPath. A {path} token in the config
// is substituted; otherwise the path is appended unless the tool is cwd-only.
func editorArgs(fields []string, repoPath string) []string {
args := make([]string, 0, len(fields)+1)
substituted := false
for _, f := range fields {
if strings.Contains(f, "{path}") {
f = strings.ReplaceAll(f, "{path}", repoPath)
substituted = true
}
args = append(args, f)
}
name := strings.TrimSuffix(strings.ToLower(fields[0][strings.LastIndexAny(fields[0], `/\`)+1:]), ".exe")
// ponytail: cwd-only applies to the bare command, so existing
// "lazygit --path" configs keep getting the path appended.
cwdOnly := len(fields) == 1 && cwdOnlyEditors[name]
if !substituted && !cwdOnly {
args = append(args, repoPath)
}
return args
}

func editorCmd(fields []string, repoPath string) *exec.Cmd {
args := editorArgs(fields, repoPath)
c := exec.Command(args[0], args[1:]...)
c.Dir = repoPath
return c
}
36 changes: 36 additions & 0 deletions internal/tui/editor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package tui

import (
"reflect"
"testing"
)

func TestEditorArgs(t *testing.T) {
const p = "/repos/app"
tests := []struct {
name string
fields []string
want []string
}{
{"plain editor appends path", []string{"code"}, []string{"code", p}},
{"flags kept before path", []string{"nvim", "-R"}, []string{"nvim", "-R", p}},
{"lazygit uses cwd only", []string{"lazygit"}, []string{"lazygit"}},
{"lazygit.exe on windows", []string{`C:\bin\LazyGit.exe`}, []string{`C:\bin\LazyGit.exe`}},
{"path token substituted", []string{"lazygit", "--path", "{path}"}, []string{"lazygit", "--path", p}},
{"path token inside arg", []string{"tool", "--dir={path}"}, []string{"tool", "--dir=" + p}},
{"legacy trailing flag still works", []string{"lazygit", "--path"}, []string{"lazygit", "--path", p}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := editorArgs(tt.fields, p); !reflect.DeepEqual(got, tt.want) {
t.Errorf("editorArgs(%q) = %q, want %q", tt.fields, got, tt.want)
}
})
}
}

func TestEditorCmdSetsDir(t *testing.T) {
if c := editorCmd([]string{"vim"}, "/repos/app"); c.Dir != "/repos/app" {
t.Errorf("Dir = %q, want repo path", c.Dir)
}
}
4 changes: 1 addition & 3 deletions internal/tui/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
}

args := append(fields[1:], msg.path)
c := exec.Command(fields[0], args...)
return m, tea.ExecProcess(c, func(err error) tea.Msg {
return m, tea.ExecProcess(editorCmd(fields, msg.path), func(err error) tea.Msg {
if err != nil {
return editorClosedMsg{err: err}
}
Expand Down
Loading