Skip to content

Commit

Permalink
use cached git config
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Oct 22, 2021
1 parent 5011cac commit b6a5e9d
Show file tree
Hide file tree
Showing 145 changed files with 584 additions and 278 deletions.
20 changes: 11 additions & 9 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@ import (
"bufio"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"

"github.com/aybabtme/humanlog"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/commands/git_config"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/env"
"github.com/jesseduffield/lazygit/pkg/gui"
"github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/jesseduffield/lazygit/pkg/updates"
"github.com/sirupsen/logrus"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)

// App struct
Expand Down Expand Up @@ -125,7 +127,7 @@ func NewApp(config config.AppConfigurer, filterPath string) (*App, error) {
return app, err
}

app.GitCommand, err = commands.NewGitCommand(app.Log, app.OSCommand, app.Tr, app.Config)
app.GitCommand, err = commands.NewGitCommand(app.Log, app.OSCommand, app.Tr, app.Config, git_config.NewStdCachedGitConfig(app.Log))
if err != nil {
return app, err
}
Expand Down
18 changes: 3 additions & 15 deletions pkg/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@ func (c *GitCommand) ConfiguredPager() string {
if os.Getenv("PAGER") != "" {
return os.Getenv("PAGER")
}
output, err := c.RunCommandWithOutput("git config --get-all core.pager")
if err != nil {
return ""
}
trimmedOutput := strings.TrimSpace(output)
return strings.Split(trimmedOutput, "\n")[0]
output := c.GitConfig.Get("core.pager")
return strings.Split(output, "\n")[0]
}

func (c *GitCommand) GetPager(width int) string {
Expand All @@ -42,11 +38,6 @@ func (c *GitCommand) colorArg() string {
return c.Config.GetUserConfig().Git.Paging.ColorArg
}

func (c *GitCommand) GetConfigValue(key string) string {
output, _ := c.getGitConfigValue(key)
return output
}

// UsingGpg tells us whether the user has gpg enabled so that we can know
// whether we need to run a subprocess to allow them to enter their password
func (c *GitCommand) UsingGpg() bool {
Expand All @@ -55,8 +46,5 @@ func (c *GitCommand) UsingGpg() bool {
return false
}

gpgsign := c.GetConfigValue("commit.gpgsign")
value := strings.ToLower(gpgsign)

return value == "true" || value == "1" || value == "yes" || value == "on"
return c.GitConfig.GetBool("commit.gpgsign")
}
70 changes: 0 additions & 70 deletions pkg/commands/config_test.go

This file was deleted.

11 changes: 6 additions & 5 deletions pkg/commands/dummies.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"github.com/jesseduffield/lazygit/pkg/commands/git_config"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/i18n"
Expand All @@ -16,10 +17,10 @@ func NewDummyGitCommand() *GitCommand {
func NewDummyGitCommandWithOSCommand(osCommand *oscommands.OSCommand) *GitCommand {
newAppConfig := config.NewDummyAppConfig()
return &GitCommand{
Log: utils.NewDummyLog(),
OSCommand: osCommand,
Tr: i18n.NewTranslationSet(utils.NewDummyLog(), newAppConfig.GetUserConfig().Gui.Language),
Config: newAppConfig,
getGitConfigValue: func(string) (string, error) { return "", nil },
Log: utils.NewDummyLog(),
OSCommand: osCommand,
Tr: i18n.NewTranslationSet(utils.NewDummyLog(), newAppConfig.GetUserConfig().Gui.Language),
Config: newAppConfig,
GitConfig: git_config.NewFakeGitConfig(map[string]string{}),
}
}
2 changes: 1 addition & 1 deletion pkg/commands/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func (c *GitCommand) EditFileCmdStr(filename string, lineNumber int) (string, er
editor := c.Config.GetUserConfig().OS.EditCommand

if editor == "" {
editor = c.GetConfigValue("core.editor")
editor = c.GitConfig.Get("core.editor")
}

if editor == "" {
Expand Down
66 changes: 22 additions & 44 deletions pkg/commands/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os/exec"
"testing"

"github.com/jesseduffield/lazygit/pkg/commands/git_config"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/secureexec"
"github.com/jesseduffield/lazygit/pkg/test"
Expand Down Expand Up @@ -523,24 +524,21 @@ func TestGitCommandApplyPatch(t *testing.T) {
}
}

// TestGitCommandDiscardOldFileChanges is a function.
func TestGitCommandDiscardOldFileChanges(t *testing.T) {
type scenario struct {
testName string
getGitConfigValue func(string) (string, error)
commits []*models.Commit
commitIndex int
fileName string
command func(string, ...string) *exec.Cmd
test func(error)
testName string
gitConfigMockResponses map[string]string
commits []*models.Commit
commitIndex int
fileName string
command func(string, ...string) *exec.Cmd
test func(error)
}

scenarios := []scenario{
{
"returns error when index outside of range of commits",
func(string) (string, error) {
return "", nil
},
nil,
[]*models.Commit{},
0,
"test999.txt",
Expand All @@ -551,9 +549,7 @@ func TestGitCommandDiscardOldFileChanges(t *testing.T) {
},
{
"returns error when using gpg",
func(string) (string, error) {
return "true", nil
},
map[string]string{"commit.gpgsign": "true"},
[]*models.Commit{{Name: "commit", Sha: "123456"}},
0,
"test999.txt",
Expand All @@ -564,9 +560,7 @@ func TestGitCommandDiscardOldFileChanges(t *testing.T) {
},
{
"checks out file if it already existed",
func(string) (string, error) {
return "", nil
},
nil,
[]*models.Commit{
{Name: "commit", Sha: "123456"},
{Name: "commit2", Sha: "abcdef"},
Expand Down Expand Up @@ -608,7 +602,7 @@ func TestGitCommandDiscardOldFileChanges(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd.OSCommand.Command = s.command
gitCmd.getGitConfigValue = s.getGitConfigValue
gitCmd.GitConfig = git_config.NewFakeGitConfig(s.gitConfigMockResponses)
s.test(gitCmd.DiscardOldFileChanges(s.commits, s.commitIndex, s.fileName))
})
}
Expand Down Expand Up @@ -725,7 +719,7 @@ func TestEditFileCmdStr(t *testing.T) {
configEditCommandTemplate string
command func(string, ...string) *exec.Cmd
getenv func(string) string
getGitConfigValue func(string) (string, error)
gitConfigMockResponses map[string]string
test func(string, error)
}

Expand All @@ -740,9 +734,7 @@ func TestEditFileCmdStr(t *testing.T) {
func(env string) string {
return ""
},
func(cf string) (string, error) {
return "", nil
},
nil,
func(cmdStr string, err error) {
assert.EqualError(t, err, "No editor defined in config file, $GIT_EDITOR, $VISUAL, $EDITOR, or git config")
},
Expand All @@ -758,9 +750,7 @@ func TestEditFileCmdStr(t *testing.T) {
func(env string) string {
return ""
},
func(cf string) (string, error) {
return "", nil
},
nil,
func(cmdStr string, err error) {
assert.NoError(t, err)
assert.Equal(t, "nano "+gitCmd.OSCommand.Quote("test"), cmdStr)
Expand All @@ -777,9 +767,7 @@ func TestEditFileCmdStr(t *testing.T) {
func(env string) string {
return ""
},
func(cf string) (string, error) {
return "nano", nil
},
map[string]string{"core.editor": "nano"},
func(cmdStr string, err error) {
assert.NoError(t, err)
assert.Equal(t, "nano "+gitCmd.OSCommand.Quote("test"), cmdStr)
Expand All @@ -800,9 +788,7 @@ func TestEditFileCmdStr(t *testing.T) {

return ""
},
func(cf string) (string, error) {
return "", nil
},
nil,
func(cmdStr string, err error) {
assert.NoError(t, err)
},
Expand All @@ -822,9 +808,7 @@ func TestEditFileCmdStr(t *testing.T) {

return ""
},
func(cf string) (string, error) {
return "", nil
},
nil,
func(cmdStr string, err error) {
assert.NoError(t, err)
assert.Equal(t, "emacs "+gitCmd.OSCommand.Quote("test"), cmdStr)
Expand All @@ -841,9 +825,7 @@ func TestEditFileCmdStr(t *testing.T) {
func(env string) string {
return ""
},
func(cf string) (string, error) {
return "", nil
},
nil,
func(cmdStr string, err error) {
assert.NoError(t, err)
assert.Equal(t, "vi "+gitCmd.OSCommand.Quote("test"), cmdStr)
Expand All @@ -860,9 +842,7 @@ func TestEditFileCmdStr(t *testing.T) {
func(env string) string {
return ""
},
func(cf string) (string, error) {
return "", nil
},
nil,
func(cmdStr string, err error) {
assert.NoError(t, err)
assert.Equal(t, "vi "+gitCmd.OSCommand.Quote("file/with space"), cmdStr)
Expand All @@ -879,9 +859,7 @@ func TestEditFileCmdStr(t *testing.T) {
func(env string) string {
return ""
},
func(cf string) (string, error) {
return "", nil
},
nil,
func(cmdStr string, err error) {
assert.NoError(t, err)
assert.Equal(t, "vim +1 "+gitCmd.OSCommand.Quote("open file/at line"), cmdStr)
Expand All @@ -894,7 +872,7 @@ func TestEditFileCmdStr(t *testing.T) {
gitCmd.Config.GetUserConfig().OS.EditCommandTemplate = s.configEditCommandTemplate
gitCmd.OSCommand.Command = s.command
gitCmd.OSCommand.Getenv = s.getenv
gitCmd.getGitConfigValue = s.getGitConfigValue
gitCmd.GitConfig = git_config.NewFakeGitConfig(s.gitConfigMockResponses)
s.test(gitCmd.EditFileCmdStr(s.filename, 1))
}
}
Loading

0 comments on commit b6a5e9d

Please sign in to comment.