Skip to content

Commit 999e170

Browse files
committed
standardise how we read from the config
1 parent 7513bfb commit 999e170

File tree

8 files changed

+240
-234
lines changed

8 files changed

+240
-234
lines changed

pkg/commands/config.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,13 @@ func (c *GitCommand) colorArg() string {
4343
}
4444

4545
func (c *GitCommand) GetConfigValue(key string) string {
46-
output, err := c.OSCommand.RunCommandWithOutput("git config --get %s", key)
47-
if err != nil {
48-
// looks like this returns an error if there is no matching value which we're okay with
49-
return ""
46+
value, _ := c.getLocalGitConfig(key)
47+
// we get an error if the key doesn't exist which we don't care about
48+
49+
if value != "" {
50+
return value
5051
}
51-
return strings.TrimSpace(output)
52+
53+
value, _ = c.getGlobalGitConfig(key)
54+
return value
5255
}

pkg/commands/files.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package commands
22

33
import (
44
"fmt"
5+
"os/exec"
56
"path/filepath"
67
"strings"
78
"time"
89

910
"github.com/go-errors/errors"
1011
"github.com/jesseduffield/lazygit/pkg/commands/models"
1112
"github.com/jesseduffield/lazygit/pkg/utils"
13+
"github.com/mgutz/str"
1214
)
1315

1416
// CatFile obtains the content of a file
@@ -262,3 +264,28 @@ func (c *GitCommand) ResetAndClean() error {
262264

263265
return c.RemoveUntrackedFiles()
264266
}
267+
268+
// EditFile opens a file in a subprocess using whatever editor is available,
269+
// falling back to core.editor, VISUAL, EDITOR, then vi
270+
func (c *GitCommand) EditFile(filename string) (*exec.Cmd, error) {
271+
editor, _ := c.getGlobalGitConfig("core.editor")
272+
273+
if editor == "" {
274+
editor = c.OSCommand.Getenv("VISUAL")
275+
}
276+
if editor == "" {
277+
editor = c.OSCommand.Getenv("EDITOR")
278+
}
279+
if editor == "" {
280+
if err := c.OSCommand.RunCommand("which vi"); err == nil {
281+
editor = "vi"
282+
}
283+
}
284+
if editor == "" {
285+
return nil, errors.New("No editor defined in $VISUAL, $EDITOR, or git config")
286+
}
287+
288+
splitCmd := str.ToArgv(fmt.Sprintf("%s %s", editor, c.OSCommand.Quote(filename)))
289+
290+
return c.OSCommand.PrepareSubProcess(splitCmd[0], splitCmd[1:]...), nil
291+
}

pkg/commands/git_test.go

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,3 +2147,154 @@ func TestFindDotGitDir(t *testing.T) {
21472147
})
21482148
}
21492149
}
2150+
2151+
// TestEditFile is a function.
2152+
func TestEditFile(t *testing.T) {
2153+
type scenario struct {
2154+
filename string
2155+
command func(string, ...string) *exec.Cmd
2156+
getenv func(string) string
2157+
getGlobalGitConfig func(string) (string, error)
2158+
test func(*exec.Cmd, error)
2159+
}
2160+
2161+
scenarios := []scenario{
2162+
{
2163+
"test",
2164+
func(name string, arg ...string) *exec.Cmd {
2165+
return exec.Command("exit", "1")
2166+
},
2167+
func(env string) string {
2168+
return ""
2169+
},
2170+
func(cf string) (string, error) {
2171+
return "", nil
2172+
},
2173+
func(cmd *exec.Cmd, err error) {
2174+
assert.EqualError(t, err, "No editor defined in $VISUAL, $EDITOR, or git config")
2175+
},
2176+
},
2177+
{
2178+
"test",
2179+
func(name string, arg ...string) *exec.Cmd {
2180+
if name == "which" {
2181+
return exec.Command("exit", "1")
2182+
}
2183+
2184+
assert.EqualValues(t, "nano", name)
2185+
2186+
return nil
2187+
},
2188+
func(env string) string {
2189+
return ""
2190+
},
2191+
func(cf string) (string, error) {
2192+
return "nano", nil
2193+
},
2194+
func(cmd *exec.Cmd, err error) {
2195+
assert.NoError(t, err)
2196+
},
2197+
},
2198+
{
2199+
"test",
2200+
func(name string, arg ...string) *exec.Cmd {
2201+
if name == "which" {
2202+
return exec.Command("exit", "1")
2203+
}
2204+
2205+
assert.EqualValues(t, "nano", name)
2206+
2207+
return nil
2208+
},
2209+
func(env string) string {
2210+
if env == "VISUAL" {
2211+
return "nano"
2212+
}
2213+
2214+
return ""
2215+
},
2216+
func(cf string) (string, error) {
2217+
return "", nil
2218+
},
2219+
func(cmd *exec.Cmd, err error) {
2220+
assert.NoError(t, err)
2221+
},
2222+
},
2223+
{
2224+
"test",
2225+
func(name string, arg ...string) *exec.Cmd {
2226+
if name == "which" {
2227+
return exec.Command("exit", "1")
2228+
}
2229+
2230+
assert.EqualValues(t, "emacs", name)
2231+
2232+
return nil
2233+
},
2234+
func(env string) string {
2235+
if env == "EDITOR" {
2236+
return "emacs"
2237+
}
2238+
2239+
return ""
2240+
},
2241+
func(cf string) (string, error) {
2242+
return "", nil
2243+
},
2244+
func(cmd *exec.Cmd, err error) {
2245+
assert.NoError(t, err)
2246+
},
2247+
},
2248+
{
2249+
"test",
2250+
func(name string, arg ...string) *exec.Cmd {
2251+
if name == "which" {
2252+
return exec.Command("echo")
2253+
}
2254+
2255+
assert.EqualValues(t, "vi", name)
2256+
2257+
return nil
2258+
},
2259+
func(env string) string {
2260+
return ""
2261+
},
2262+
func(cf string) (string, error) {
2263+
return "", nil
2264+
},
2265+
func(cmd *exec.Cmd, err error) {
2266+
assert.NoError(t, err)
2267+
},
2268+
},
2269+
{
2270+
"file/with space",
2271+
func(name string, args ...string) *exec.Cmd {
2272+
if name == "which" {
2273+
return exec.Command("echo")
2274+
}
2275+
2276+
assert.EqualValues(t, "vi", name)
2277+
assert.EqualValues(t, "file/with space", args[0])
2278+
2279+
return nil
2280+
},
2281+
func(env string) string {
2282+
return ""
2283+
},
2284+
func(cf string) (string, error) {
2285+
return "", nil
2286+
},
2287+
func(cmd *exec.Cmd, err error) {
2288+
assert.NoError(t, err)
2289+
},
2290+
},
2291+
}
2292+
2293+
for _, s := range scenarios {
2294+
gitCmd := NewDummyGitCommand()
2295+
gitCmd.OSCommand.Command = s.command
2296+
gitCmd.OSCommand.Getenv = s.getenv
2297+
gitCmd.getGlobalGitConfig = s.getGlobalGitConfig
2298+
s.test(gitCmd.EditFile(s.filename))
2299+
}
2300+
}

pkg/commands/oscommands/os.go

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/jesseduffield/lazygit/pkg/utils"
2020
"github.com/mgutz/str"
2121
"github.com/sirupsen/logrus"
22-
gitconfig "github.com/tcnksm/go-gitconfig"
2322
)
2423

2524
// Platform stores the os state
@@ -36,25 +35,23 @@ type Platform struct {
3635

3736
// OSCommand holds all the os commands
3837
type OSCommand struct {
39-
Log *logrus.Entry
40-
Platform *Platform
41-
Config config.AppConfigurer
42-
Command func(string, ...string) *exec.Cmd
43-
BeforeExecuteCmd func(*exec.Cmd)
44-
GetGlobalGitConfig func(string) (string, error)
45-
Getenv func(string) string
38+
Log *logrus.Entry
39+
Platform *Platform
40+
Config config.AppConfigurer
41+
Command func(string, ...string) *exec.Cmd
42+
BeforeExecuteCmd func(*exec.Cmd)
43+
Getenv func(string) string
4644
}
4745

4846
// NewOSCommand os command runner
4947
func NewOSCommand(log *logrus.Entry, config config.AppConfigurer) *OSCommand {
5048
return &OSCommand{
51-
Log: log,
52-
Platform: getPlatform(),
53-
Config: config,
54-
Command: exec.Command,
55-
BeforeExecuteCmd: func(*exec.Cmd) {},
56-
GetGlobalGitConfig: gitconfig.Global,
57-
Getenv: os.Getenv,
49+
Log: log,
50+
Platform: getPlatform(),
51+
Config: config,
52+
Command: exec.Command,
53+
BeforeExecuteCmd: func(*exec.Cmd) {},
54+
Getenv: os.Getenv,
5855
}
5956
}
6057

@@ -235,31 +232,6 @@ func (c *OSCommand) OpenLink(link string) error {
235232
return err
236233
}
237234

238-
// EditFile opens a file in a subprocess using whatever editor is available,
239-
// falling back to core.editor, VISUAL, EDITOR, then vi
240-
func (c *OSCommand) EditFile(filename string) (*exec.Cmd, error) {
241-
editor, _ := c.GetGlobalGitConfig("core.editor")
242-
243-
if editor == "" {
244-
editor = c.Getenv("VISUAL")
245-
}
246-
if editor == "" {
247-
editor = c.Getenv("EDITOR")
248-
}
249-
if editor == "" {
250-
if err := c.RunCommand("which vi"); err == nil {
251-
editor = "vi"
252-
}
253-
}
254-
if editor == "" {
255-
return nil, errors.New("No editor defined in $VISUAL, $EDITOR, or git config")
256-
}
257-
258-
splitCmd := str.ToArgv(fmt.Sprintf("%s %s", editor, c.Quote(filename)))
259-
260-
return c.PrepareSubProcess(splitCmd[0], splitCmd[1:]...), nil
261-
}
262-
263235
// PrepareSubProcess iniPrepareSubProcessrocess then tells the Gui to switch to it
264236
// TODO: see if this needs to exist, given that ExecutableFromString does the same things
265237
func (c *OSCommand) PrepareSubProcess(cmdName string, commandArgs ...string) *exec.Cmd {

0 commit comments

Comments
 (0)