diff --git a/pkg/app/app.go b/pkg/app/app.go index d4e3e15f7..9fae9e2d0 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -17,6 +17,7 @@ import ( "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/common" "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/env" "github.com/jesseduffield/lazygit/pkg/gui" @@ -27,14 +28,12 @@ import ( // App struct type App struct { - closers []io.Closer - + *common.Common + closers []io.Closer Config config.AppConfigurer - Log *logrus.Entry OSCommand *oscommands.OSCommand GitCommand *commands.GitCommand Gui *gui.Gui - Tr *i18n.TranslationSet Updater *updates.Updater // may only need this on the Gui ClientContext string } @@ -97,27 +96,33 @@ func newLogger(config config.AppConfigurer) *logrus.Entry { // NewApp bootstrap a new application func NewApp(config config.AppConfigurer, filterPath string) (*App, error) { - app := &App{ closers: []io.Closer{}, Config: config, } var err error - app.Log = newLogger(config) - app.Tr, err = i18n.NewTranslationSetFromConfig(app.Log, config.GetUserConfig().Gui.Language) + log := newLogger(config) + tr, err := i18n.NewTranslationSetFromConfig(log, config.GetUserConfig().Gui.Language) if err != nil { return app, err } + app.Common = &common.Common{ + Log: log, + Tr: tr, + UserConfig: config.GetUserConfig(), + Debug: config.GetDebug(), + } + // if we are being called in 'demon' mode, we can just return here app.ClientContext = os.Getenv("LAZYGIT_CLIENT_COMMAND") if app.ClientContext != "" { return app, nil } - app.OSCommand = oscommands.NewOSCommand(app.Log, config) + app.OSCommand = oscommands.NewOSCommand(app.Common) - app.Updater, err = updates.NewUpdater(app.Log, config, app.OSCommand, app.Tr) + app.Updater, err = updates.NewUpdater(log, config, app.OSCommand, app.Tr) if err != nil { return app, err } @@ -128,9 +133,8 @@ func NewApp(config config.AppConfigurer, filterPath string) (*App, error) { } app.GitCommand, err = commands.NewGitCommand( - app.Log, + app.Common, app.OSCommand, - app.Tr, app.Config, git_config.NewStdCachedGitConfig(app.Log), ) @@ -138,7 +142,7 @@ func NewApp(config config.AppConfigurer, filterPath string) (*App, error) { return app, err } - app.Gui, err = gui.NewGui(app.Log, app.GitCommand, app.OSCommand, app.Tr, config, app.Updater, filterPath, showRecentRepos) + app.Gui, err = gui.NewGui(app.Common, app.GitCommand, app.OSCommand, config, app.Updater, filterPath, showRecentRepos) if err != nil { return app, err } diff --git a/pkg/commands/branches.go b/pkg/commands/branches.go index cc513a974..20938621d 100644 --- a/pkg/commands/branches.go +++ b/pkg/commands/branches.go @@ -165,3 +165,7 @@ func (c *GitCommand) ResetMixed(ref string) error { func (c *GitCommand) RenameBranch(oldName string, newName string) error { return c.Run(c.NewCmdObj(fmt.Sprintf("git branch --move %s %s", c.OSCommand.Quote(oldName), c.OSCommand.Quote(newName)))) } + +func (c *GitCommand) GetRawBranches() (string, error) { + return c.RunWithOutput(c.NewCmdObj(`git for-each-ref --sort=-committerdate --format="%(HEAD)|%(refname:short)|%(upstream:short)|%(upstream:track)" refs/heads`)) +} diff --git a/pkg/commands/dummies.go b/pkg/commands/dummies.go index ba052fe9b..2e74dbc12 100644 --- a/pkg/commands/dummies.go +++ b/pkg/commands/dummies.go @@ -7,7 +7,6 @@ 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" "github.com/jesseduffield/lazygit/pkg/utils" ) @@ -20,9 +19,8 @@ func NewDummyGitCommand() *GitCommand { func NewDummyGitCommandWithOSCommand(osCommand *oscommands.OSCommand) *GitCommand { newAppConfig := config.NewDummyAppConfig() return &GitCommand{ - Log: utils.NewDummyLog(), + Common: utils.NewDummyCommon(), OSCommand: osCommand, - Tr: i18n.NewTranslationSet(utils.NewDummyLog(), newAppConfig.GetUserConfig().Gui.Language), Config: newAppConfig, GitConfig: git_config.NewFakeGitConfig(map[string]string{}), GetCmdWriter: func() io.Writer { return ioutil.Discard }, diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 7d4cc6c86..a29f519d6 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -14,11 +14,10 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/git_config" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/commands/patch" + "github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/env" - "github.com/jesseduffield/lazygit/pkg/i18n" "github.com/jesseduffield/lazygit/pkg/utils" - "github.com/sirupsen/logrus" ) // this takes something like: @@ -29,10 +28,9 @@ const CurrentBranchNameRegex = `(?m)^\*.*?([^ ]*?)\)?$` // GitCommand is our main git interface type GitCommand struct { - Log *logrus.Entry + *common.Common OSCommand *oscommands.OSCommand Repo *gogit.Repository - Tr *i18n.TranslationSet Config config.AppConfigurer DotGitDir string onSuccessfulContinue func() error @@ -50,9 +48,8 @@ type GitCommand struct { // NewGitCommand it runs git commands func NewGitCommand( - log *logrus.Entry, + cmn *common.Common, osCommand *oscommands.OSCommand, - tr *i18n.TranslationSet, config config.AppConfigurer, gitConfig git_config.IGitConfig, ) (*GitCommand, error) { @@ -65,7 +62,7 @@ func NewGitCommand( } var err error - if repo, err = setupRepository(gogit.PlainOpen, tr.GitconfigParseErr); err != nil { + if repo, err = setupRepository(gogit.PlainOpen, cmn.Tr.GitconfigParseErr); err != nil { return nil, err } @@ -75,9 +72,8 @@ func NewGitCommand( } gitCommand := &GitCommand{ - Log: log, + Common: cmn, OSCommand: osCommand, - Tr: tr, Repo: repo, Config: config, DotGitDir: dotGitDir, @@ -86,7 +82,7 @@ func NewGitCommand( GetCmdWriter: func() io.Writer { return ioutil.Discard }, } - gitCommand.PatchManager = patch.NewPatchManager(log, gitCommand.ApplyPatch, gitCommand.ShowFileDiff) + gitCommand.PatchManager = patch.NewPatchManager(gitCommand.Log, gitCommand.ApplyPatch, gitCommand.ShowFileDiff) return gitCommand, nil } diff --git a/pkg/commands/loading_branches.go b/pkg/commands/loading_branches.go index 5453db2de..b79fd5640 100644 --- a/pkg/commands/loading_branches.go +++ b/pkg/commands/loading_branches.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/jesseduffield/lazygit/pkg/commands/models" + "github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/utils" "github.com/sirupsen/logrus" ) @@ -22,23 +23,31 @@ import ( // BranchListBuilder returns a list of Branch objects for the current repo type BranchListBuilder struct { - Log *logrus.Entry - GitCommand *GitCommand - ReflogCommits []*models.Commit + *common.Common + log *logrus.Entry + getRawBranches func() (string, error) + getCurrentBranchName func() (string, string, error) + reflogCommits []*models.Commit } -// NewBranchListBuilder builds a new branch list builder -func NewBranchListBuilder(log *logrus.Entry, gitCommand *GitCommand, reflogCommits []*models.Commit) (*BranchListBuilder, error) { +// common things: log, user config, Tr. + +func NewBranchListBuilder( + cmn *common.Common, + getRawBranches func() (string, error), + getCurrentBranchName func() (string, string, error), + reflogCommits []*models.Commit, +) *BranchListBuilder { return &BranchListBuilder{ - Log: log, - GitCommand: gitCommand, - ReflogCommits: reflogCommits, - }, nil + Common: cmn, + getRawBranches: getRawBranches, + getCurrentBranchName: getCurrentBranchName, + reflogCommits: reflogCommits, + } } func (b *BranchListBuilder) obtainBranches() []*models.Branch { - cmdStr := `git for-each-ref --sort=-committerdate --format="%(HEAD)|%(refname:short)|%(upstream:short)|%(upstream:track)" refs/heads` - output, err := b.GitCommand.RunWithOutput(b.GitCommand.NewCmdObj(cmdStr)) + output, err := b.getRawBranches() if err != nil { panic(err) } @@ -134,7 +143,7 @@ outer: } } if !foundHead { - currentBranchName, currentBranchDisplayName, err := b.GitCommand.CurrentBranchName() + currentBranchName, currentBranchDisplayName, err := b.getCurrentBranchName() if err != nil { panic(err) } @@ -148,8 +157,8 @@ outer: func (b *BranchListBuilder) obtainReflogBranches() []*models.Branch { foundBranchesMap := map[string]bool{} re := regexp.MustCompile(`checkout: moving from ([\S]+) to ([\S]+)`) - reflogBranches := make([]*models.Branch, 0, len(b.ReflogCommits)) - for _, commit := range b.ReflogCommits { + reflogBranches := make([]*models.Branch, 0, len(b.reflogCommits)) + for _, commit := range b.reflogCommits { if match := re.FindStringSubmatch(commit.Name); len(match) == 3 { recency := utils.UnixToTimeAgo(commit.UnixTimestamp) for _, branchName := range match[1:] { diff --git a/pkg/commands/oscommands/dummies.go b/pkg/commands/oscommands/dummies.go index bb6f741f1..cd2e4eca3 100644 --- a/pkg/commands/oscommands/dummies.go +++ b/pkg/commands/oscommands/dummies.go @@ -1,11 +1,10 @@ package oscommands import ( - "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/utils" ) // NewDummyOSCommand creates a new dummy OSCommand for testing func NewDummyOSCommand() *OSCommand { - return NewOSCommand(utils.NewDummyLog(), config.NewDummyAppConfig()) + return NewOSCommand(utils.NewDummyCommon()) } diff --git a/pkg/commands/oscommands/os.go b/pkg/commands/oscommands/os.go index 15e551133..f28f28c06 100644 --- a/pkg/commands/oscommands/os.go +++ b/pkg/commands/oscommands/os.go @@ -9,16 +9,14 @@ import ( "path/filepath" "strings" "sync" - "testing" "github.com/go-errors/errors" "github.com/atotto/clipboard" - "github.com/jesseduffield/lazygit/pkg/config" + "github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/secureexec" "github.com/jesseduffield/lazygit/pkg/utils" "github.com/mgutz/str" - "github.com/sirupsen/logrus" ) // Platform stores the os state @@ -44,9 +42,8 @@ func (self *RealCommander) Run(cmdObj ICmdObj) error { // OSCommand holds all the os commands type OSCommand struct { - Log *logrus.Entry + *common.Common Platform *Platform - Config config.AppConfigurer Command func(string, ...string) *exec.Cmd Getenv func(string) string @@ -92,11 +89,10 @@ func NewCmdLogEntry(cmdStr string, span string, commandLine bool) CmdLogEntry { } // NewOSCommand os command runner -func NewOSCommand(log *logrus.Entry, config config.AppConfigurer) *OSCommand { +func NewOSCommand(common *common.Common) *OSCommand { c := &OSCommand{ - Log: log, + Common: common, Platform: getPlatform(), - Config: config, Command: secureexec.Command, Getenv: os.Getenv, removeFile: os.RemoveAll, @@ -161,7 +157,7 @@ func (c *OSCommand) FileType(path string) string { // OpenFile opens a file with the given func (c *OSCommand) OpenFile(filename string) error { - commandTemplate := c.Config.GetUserConfig().OS.OpenCommand + commandTemplate := c.UserConfig.OS.OpenCommand templateValues := map[string]string{ "filename": c.Quote(filename), } @@ -173,7 +169,7 @@ func (c *OSCommand) OpenFile(filename string) error { // OpenLink opens a file with the given func (c *OSCommand) OpenLink(link string) error { c.LogCommand(fmt.Sprintf("Opening link '%s'", link), false) - commandTemplate := c.Config.GetUserConfig().OS.OpenLinkCommand + commandTemplate := c.UserConfig.OS.OpenLinkCommand templateValues := map[string]string{ "link": c.Quote(link), } @@ -429,14 +425,6 @@ type IRunner interface { type RunExpectation func(ICmdObj) (string, error) -type FakeRunner struct { - expectations []RunExpectation -} - -func (self *RealRunner) Run(cmdObj ICmdObj) error { - -} - type RealRunner struct { c *OSCommand } diff --git a/pkg/commands/rebasing.go b/pkg/commands/rebasing.go index 8e00f4283..eb1db85cb 100644 --- a/pkg/commands/rebasing.go +++ b/pkg/commands/rebasing.go @@ -62,7 +62,7 @@ func (c *GitCommand) PrepareInteractiveRebaseCommand(baseSha string, todo string ex := c.OSCommand.GetLazygitPath() debug := "FALSE" - if c.OSCommand.Config.GetDebug() { + if c.Debug { debug = "TRUE" } diff --git a/pkg/commands/tags.go b/pkg/commands/tags.go index bd1f55c65..b4728d68c 100644 --- a/pkg/commands/tags.go +++ b/pkg/commands/tags.go @@ -9,7 +9,7 @@ func (c *GitCommand) CreateLightweightTag(tagName string, commitSha string) erro } func (c *GitCommand) CreateAnnotatedTag(tagName, commitSha, msg string) error { - return c.RunCommand("git tag %s %s -m %s", tagName, commitSha, c.OSCommand.Quote(msg)) + return c.Run(c.NewCmdObj(fmt.Sprintf("git tag %s %s -m %s", tagName, commitSha, c.OSCommand.Quote(msg)))) } func (c *GitCommand) DeleteTag(tagName string) error { diff --git a/pkg/common/common.go b/pkg/common/common.go new file mode 100644 index 000000000..e8251a85d --- /dev/null +++ b/pkg/common/common.go @@ -0,0 +1,15 @@ +package common + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + "github.com/jesseduffield/lazygit/pkg/i18n" + "github.com/sirupsen/logrus" +) + +// Commonly used things wrapped into one struct for convenience when passing it around +type Common struct { + Log *logrus.Entry + Tr *i18n.TranslationSet + UserConfig *config.UserConfig + Debug bool +} diff --git a/pkg/gui/branches_panel.go b/pkg/gui/branches_panel.go index 7b712b2ba..8dff14ac1 100644 --- a/pkg/gui/branches_panel.go +++ b/pkg/gui/branches_panel.go @@ -61,10 +61,12 @@ func (gui *Gui) refreshBranches() { } } - builder, err := commands.NewBranchListBuilder(gui.Log, gui.GitCommand, reflogCommits) - if err != nil { - _ = gui.surfaceError(err) - } + builder := commands.NewBranchListBuilder( + gui.Common, + gui.GitCommand.GetRawBranches, + gui.GitCommand.CurrentBranchName, + reflogCommits, + ) gui.State.Branches = builder.Build() if err := gui.postRefreshUpdate(gui.State.Contexts.Branches); err != nil { diff --git a/pkg/gui/dummies.go b/pkg/gui/dummies.go index 3c993836a..92a9c4d25 100644 --- a/pkg/gui/dummies.go +++ b/pkg/gui/dummies.go @@ -18,6 +18,6 @@ func NewDummyUpdater() *updates.Updater { func NewDummyGui() *Gui { newAppConfig := config.NewDummyAppConfig() - dummyGui, _ := NewGui(utils.NewDummyLog(), commands.NewDummyGitCommand(), oscommands.NewDummyOSCommand(), i18n.NewTranslationSet(utils.NewDummyLog(), newAppConfig.GetUserConfig().Gui.Language), newAppConfig, NewDummyUpdater(), "", false) + dummyGui, _ := NewGui(utils.NewDummyCommon(), commands.NewDummyGitCommand(), oscommands.NewDummyOSCommand(), newAppConfig, NewDummyUpdater(), "", false) return dummyGui } diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 39d2a9763..259bfb411 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -14,6 +14,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" + "github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/gui/filetree" "github.com/jesseduffield/lazygit/pkg/gui/lbl" @@ -25,12 +26,10 @@ import ( "github.com/jesseduffield/lazygit/pkg/gui/presentation/graph" "github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/gui/types" - "github.com/jesseduffield/lazygit/pkg/i18n" "github.com/jesseduffield/lazygit/pkg/tasks" "github.com/jesseduffield/lazygit/pkg/theme" "github.com/jesseduffield/lazygit/pkg/updates" "github.com/jesseduffield/lazygit/pkg/utils" - "github.com/sirupsen/logrus" "gopkg.in/ozeidan/fuzzy-patricia.v3/patricia" ) @@ -67,8 +66,8 @@ type Repo string // Gui wraps the gocui Gui object which handles rendering and events type Gui struct { + *common.Common g *gocui.Gui - Log *logrus.Entry GitCommand *commands.GitCommand OSCommand *oscommands.OSCommand @@ -79,7 +78,6 @@ type Gui struct { // gui state when returning from a subrepo RepoStateMap map[Repo]*guiState Config config.AppConfigurer - Tr *i18n.TranslationSet Updater *updates.Updater statusManager *statusManager credentials credentials @@ -431,13 +429,12 @@ func (gui *Gui) resetState(filterPath string, reuseState bool) { // for now the split view will always be on // NewGui builds a new gui handler -func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *oscommands.OSCommand, tr *i18n.TranslationSet, config config.AppConfigurer, updater *updates.Updater, filterPath string, showRecentRepos bool) (*Gui, error) { +func NewGui(cmn *common.Common, gitCommand *commands.GitCommand, oSCommand *oscommands.OSCommand, config config.AppConfigurer, updater *updates.Updater, filterPath string, showRecentRepos bool) (*Gui, error) { gui := &Gui{ - Log: log, + Common: cmn, GitCommand: gitCommand, OSCommand: oSCommand, Config: config, - Tr: tr, Updater: updater, statusManager: &statusManager{}, viewBufferManagerMap: map[string]*tasks.ViewBufferManager{}, diff --git a/pkg/gui/recent_repos_panel.go b/pkg/gui/recent_repos_panel.go index ae5d35948..2019af461 100644 --- a/pkg/gui/recent_repos_panel.go +++ b/pkg/gui/recent_repos_panel.go @@ -73,7 +73,7 @@ func (gui *Gui) dispatchSwitchToRepo(path string, reuse bool) error { return err } - newGitCommand, err := commands.NewGitCommand(gui.Log, gui.OSCommand, gui.Tr, gui.Config, git_config.NewStdCachedGitConfig(gui.Log)) + newGitCommand, err := commands.NewGitCommand(gui.Common, gui.OSCommand, gui.Config, git_config.NewStdCachedGitConfig(gui.Log)) if err != nil { return err } diff --git a/pkg/utils/dummies.go b/pkg/utils/dummies.go index ab6d041dd..b05f444b4 100644 --- a/pkg/utils/dummies.go +++ b/pkg/utils/dummies.go @@ -3,6 +3,9 @@ package utils import ( "io/ioutil" + "github.com/jesseduffield/lazygit/pkg/common" + "github.com/jesseduffield/lazygit/pkg/config" + "github.com/jesseduffield/lazygit/pkg/i18n" "github.com/sirupsen/logrus" ) @@ -12,3 +15,13 @@ func NewDummyLog() *logrus.Entry { log.Out = ioutil.Discard return log.WithField("test", "test") } + +func NewDummyCommon() *common.Common { + tr := i18n.EnglishTranslationSet() + newAppConfig := config.NewDummyAppConfig() + return &common.Common{ + Log: NewDummyLog(), + Tr: &tr, + UserConfig: newAppConfig.GetUserConfig(), + } +}