Skip to content

Commit

Permalink
remove go-gitconfig package
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Feb 8, 2021
1 parent 09f32d4 commit 7886764
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 440 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ require (
github.com/sirupsen/logrus v1.4.2
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad
github.com/stretchr/testify v1.4.0
github.com/tcnksm/go-gitconfig v0.1.2
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0 // indirect
golang.org/x/net v0.0.0-20201002202402-0a1ea396d57c // indirect
golang.org/x/sys v0.0.0-20201005172224-997123666555 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/tcnksm/go-gitconfig v0.1.2 h1:iiDhRitByXAEyjgBqsKi9QU4o2TNtv9kPP3RgPgXBPw=
github.com/tcnksm/go-gitconfig v0.1.2/go.mod h1:/8EhP4H7oJZdIPyT+/UIsG87kTzrzM4UsLGSItWYCpE=
github.com/urfave/cli v1.20.1-0.20180226030253-8e01ec4cd3e2/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70=
github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4=
Expand Down
11 changes: 2 additions & 9 deletions pkg/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ func (c *GitCommand) colorArg() string {
}

func (c *GitCommand) GetConfigValue(key string) string {
value, _ := c.getLocalGitConfig(key)
// we get an error if the key doesn't exist which we don't care about

if value != "" {
return value
}

value, _ = c.getGlobalGitConfig(key)
return value
output, _ := c.getGitConfigValue(key)
return output
}
13 changes: 6 additions & 7 deletions pkg/commands/dummies.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ func NewDummyGitCommand() *GitCommand {
// NewDummyGitCommandWithOSCommand creates a new dummy GitCommand for testing
func NewDummyGitCommandWithOSCommand(osCommand *oscommands.OSCommand) *GitCommand {
return &GitCommand{
Log: utils.NewDummyLog(),
OSCommand: osCommand,
Tr: i18n.NewTranslationSet(utils.NewDummyLog()),
Config: config.NewDummyAppConfig(),
getGlobalGitConfig: func(string) (string, error) { return "", nil },
getLocalGitConfig: func(string) (string, error) { return "", nil },
removeFile: func(string) error { return nil },
Log: utils.NewDummyLog(),
OSCommand: osCommand,
Tr: i18n.NewTranslationSet(utils.NewDummyLog()),
Config: config.NewDummyAppConfig(),
getGitConfigValue: func(string) (string, error) { return "", nil },
removeFile: func(string) error { return nil },
}
}
2 changes: 1 addition & 1 deletion pkg/commands/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (c *GitCommand) ResetAndClean() error {
// 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")
editor := c.GetConfigValue("core.editor")

if editor == "" {
editor = c.OSCommand.Getenv("VISUAL")
Expand Down
23 changes: 10 additions & 13 deletions pkg/commands/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/sirupsen/logrus"
gitconfig "github.com/tcnksm/go-gitconfig"
)

// this takes something like:
Expand All @@ -32,8 +31,7 @@ type GitCommand struct {
Repo *gogit.Repository
Tr *i18n.TranslationSet
Config config.AppConfigurer
getGlobalGitConfig func(string) (string, error)
getLocalGitConfig func(string) (string, error)
getGitConfigValue func(string) (string, error)
removeFile func(string) error
DotGitDir string
onSuccessfulContinue func() error
Expand Down Expand Up @@ -74,16 +72,15 @@ func NewGitCommand(log *logrus.Entry, osCommand *oscommands.OSCommand, tr *i18n.
}

gitCommand := &GitCommand{
Log: log,
OSCommand: osCommand,
Tr: tr,
Repo: repo,
Config: config,
getGlobalGitConfig: gitconfig.Global,
getLocalGitConfig: gitconfig.Local,
removeFile: os.RemoveAll,
DotGitDir: dotGitDir,
PushToCurrent: pushToCurrent,
Log: log,
OSCommand: osCommand,
Tr: tr,
Repo: repo,
Config: config,
getGitConfigValue: getGitConfigValue,
removeFile: os.RemoveAll,
DotGitDir: dotGitDir,
PushToCurrent: pushToCurrent,
}

gitCommand.PatchManager = patch.NewPatchManager(log, gitCommand.ApplyPatch, gitCommand.ShowFileDiff)
Expand Down
126 changes: 31 additions & 95 deletions pkg/commands/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,45 +700,24 @@ func TestGitCommandMerge(t *testing.T) {
// TestGitCommandUsingGpg is a function.
func TestGitCommandUsingGpg(t *testing.T) {
type scenario struct {
testName string
getLocalGitConfig func(string) (string, error)
getGlobalGitConfig func(string) (string, error)
test func(bool)
testName string
getGitConfigValue func(string) (string, error)
test func(bool)
}

scenarios := []scenario{
{
"Option global and local config commit.gpgsign is not set",
func(string) (string, error) {
return "", nil
},
func(string) (string, error) {
return "", nil
},
func(string) (string, error) { return "", nil },
func(gpgEnabled bool) {
assert.False(t, gpgEnabled)
},
},
{
"Option global config commit.gpgsign is not set, fallback on local config",
func(string) (string, error) {
return "", nil
},
func(string) (string, error) {
return "true", nil
},
func(gpgEnabled bool) {
assert.True(t, gpgEnabled)
},
},
{
"Option commit.gpgsign is true",
func(string) (string, error) {
return "True", nil
},
func(string) (string, error) {
return "", nil
},
func(gpgEnabled bool) {
assert.True(t, gpgEnabled)
},
Expand All @@ -748,9 +727,6 @@ func TestGitCommandUsingGpg(t *testing.T) {
func(string) (string, error) {
return "ON", nil
},
func(string) (string, error) {
return "", nil
},
func(gpgEnabled bool) {
assert.True(t, gpgEnabled)
},
Expand All @@ -760,9 +736,6 @@ func TestGitCommandUsingGpg(t *testing.T) {
func(string) (string, error) {
return "YeS", nil
},
func(string) (string, error) {
return "", nil
},
func(gpgEnabled bool) {
assert.True(t, gpgEnabled)
},
Expand All @@ -772,9 +745,6 @@ func TestGitCommandUsingGpg(t *testing.T) {
func(string) (string, error) {
return "1", nil
},
func(string) (string, error) {
return "", nil
},
func(gpgEnabled bool) {
assert.True(t, gpgEnabled)
},
Expand All @@ -784,8 +754,7 @@ func TestGitCommandUsingGpg(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.getGlobalGitConfig = s.getGlobalGitConfig
gitCmd.getLocalGitConfig = s.getLocalGitConfig
gitCmd.getGitConfigValue = s.getGitConfigValue
s.test(gitCmd.usingGpg())
})
}
Expand All @@ -794,11 +763,11 @@ func TestGitCommandUsingGpg(t *testing.T) {
// TestGitCommandCommit is a function.
func TestGitCommandCommit(t *testing.T) {
type scenario struct {
testName string
command func(string, ...string) *exec.Cmd
getGlobalGitConfig func(string) (string, error)
test func(*exec.Cmd, error)
flags string
testName string
command func(string, ...string) *exec.Cmd
getGitConfigValue func(string) (string, error)
test func(*exec.Cmd, error)
flags string
}

scenarios := []scenario{
Expand Down Expand Up @@ -875,7 +844,7 @@ func TestGitCommandCommit(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.getGlobalGitConfig = s.getGlobalGitConfig
gitCmd.getGitConfigValue = s.getGitConfigValue
gitCmd.OSCommand.Command = s.command
s.test(gitCmd.Commit("test", s.flags))
})
Expand All @@ -885,10 +854,10 @@ func TestGitCommandCommit(t *testing.T) {
// TestGitCommandAmendHead is a function.
func TestGitCommandAmendHead(t *testing.T) {
type scenario struct {
testName string
command func(string, ...string) *exec.Cmd
getGlobalGitConfig func(string) (string, error)
test func(*exec.Cmd, error)
testName string
command func(string, ...string) *exec.Cmd
getGitConfigValue func(string) (string, error)
test func(*exec.Cmd, error)
}

scenarios := []scenario{
Expand Down Expand Up @@ -945,7 +914,7 @@ func TestGitCommandAmendHead(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.getGlobalGitConfig = s.getGlobalGitConfig
gitCmd.getGitConfigValue = s.getGitConfigValue
gitCmd.OSCommand.Command = s.command
s.test(gitCmd.AmendHead())
})
Expand All @@ -955,12 +924,11 @@ func TestGitCommandAmendHead(t *testing.T) {
// TestGitCommandPush is a function.
func TestGitCommandPush(t *testing.T) {
type scenario struct {
testName string
getLocalGitConfig func(string) (string, error)
getGlobalGitConfig func(string) (string, error)
command func(string, ...string) *exec.Cmd
forcePush bool
test func(error)
testName string
getGitConfigValue func(string) (string, error)
command func(string, ...string) *exec.Cmd
forcePush bool
test func(error)
}

scenarios := []scenario{
Expand All @@ -969,9 +937,6 @@ func TestGitCommandPush(t *testing.T) {
func(string) (string, error) {
return "", nil
},
func(string) (string, error) {
return "", nil
},
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"push", "--follow-tags"}, args)
Expand All @@ -988,9 +953,6 @@ func TestGitCommandPush(t *testing.T) {
func(string) (string, error) {
return "", nil
},
func(string) (string, error) {
return "", nil
},
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"push", "--follow-tags", "--force-with-lease"}, args)
Expand All @@ -1003,13 +965,10 @@ func TestGitCommandPush(t *testing.T) {
},
},
{
"Push with force disabled, follow-tags off locally",
"Push with force disabled, follow-tags off",
func(string) (string, error) {
return "false", nil
},
func(string) (string, error) {
return "", nil
},
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"push"}, args)
Expand All @@ -1021,33 +980,11 @@ func TestGitCommandPush(t *testing.T) {
assert.NoError(t, err)
},
},
{
"Push with force enabled, follow-tags off globally",
func(string) (string, error) {
return "", nil
},
func(string) (string, error) {
return "false", nil
},
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"push", "--force-with-lease"}, args)

return secureexec.Command("echo")
},
true,
func(err error) {
assert.NoError(t, err)
},
},
{
"Push with an error occurring, follow-tags on",
func(string) (string, error) {
return "", nil
},
func(string) (string, error) {
return "", nil
},
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"push", "--follow-tags"}, args)
Expand All @@ -1064,8 +1001,7 @@ func TestGitCommandPush(t *testing.T) {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.OSCommand.Command = s.command
gitCmd.getLocalGitConfig = s.getLocalGitConfig
gitCmd.getGlobalGitConfig = s.getGlobalGitConfig
gitCmd.getGitConfigValue = s.getGitConfigValue
err := gitCmd.Push("test", s.forcePush, "", "", func(passOrUname string) string {
return "\n"
})
Expand Down Expand Up @@ -1790,7 +1726,7 @@ func TestGitCommandCheckoutFile(t *testing.T) {
func TestGitCommandDiscardOldFileChanges(t *testing.T) {
type scenario struct {
testName string
getLocalGitConfig func(string) (string, error)
getGitConfigValue func(string) (string, error)
commits []*models.Commit
commitIndex int
fileName string
Expand Down Expand Up @@ -1871,7 +1807,7 @@ func TestGitCommandDiscardOldFileChanges(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd.OSCommand.Command = s.command
gitCmd.getLocalGitConfig = s.getLocalGitConfig
gitCmd.getGitConfigValue = s.getGitConfigValue
s.test(gitCmd.DiscardOldFileChanges(s.commits, s.commitIndex, s.fileName))
})
}
Expand Down Expand Up @@ -2164,11 +2100,11 @@ func TestFindDotGitDir(t *testing.T) {
// TestEditFile is a function.
func TestEditFile(t *testing.T) {
type scenario struct {
filename string
command func(string, ...string) *exec.Cmd
getenv func(string) string
getGlobalGitConfig func(string) (string, error)
test func(*exec.Cmd, error)
filename string
command func(string, ...string) *exec.Cmd
getenv func(string) string
getGitConfigValue func(string) (string, error)
test func(*exec.Cmd, error)
}

scenarios := []scenario{
Expand Down Expand Up @@ -2307,7 +2243,7 @@ func TestEditFile(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.OSCommand.Command = s.command
gitCmd.OSCommand.Getenv = s.getenv
gitCmd.getGlobalGitConfig = s.getGlobalGitConfig
gitCmd.getGitConfigValue = s.getGitConfigValue
s.test(gitCmd.EditFile(s.filename))
}
}
Loading

0 comments on commit 7886764

Please sign in to comment.