From 194ff1630c11303e3de1c87f61103b3aadcd175d Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 2 Jan 2022 10:21:32 +1100 Subject: [PATCH] do dependency injection up front and in one place --- pkg/commands/git.go | 24 ++++++++++++++++++++++++ pkg/commands/loaders/branches.go | 13 +++++-------- pkg/commands/submodules.go | 24 ++++++++++++------------ pkg/gui/branches_panel.go | 10 ++-------- pkg/gui/commit_files_panel.go | 3 +-- pkg/gui/commits_panel.go | 8 ++------ pkg/gui/remotes_panel.go | 3 +-- pkg/gui/sub_commits_panel.go | 4 +--- pkg/gui/tags_panel.go | 3 +-- 9 files changed, 49 insertions(+), 43 deletions(-) diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 90fd7b262..aa06e6367 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -11,6 +11,7 @@ import ( gogit "github.com/jesseduffield/go-git/v5" "github.com/jesseduffield/lazygit/pkg/commands/git_config" + "github.com/jesseduffield/lazygit/pkg/commands/loaders" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/commands/patch" "github.com/jesseduffield/lazygit/pkg/common" @@ -24,6 +25,17 @@ import ( // and returns '264fc6f5' as the second match const CurrentBranchNameRegex = `(?m)^\*.*?([^ ]*?)\)?$` +type Loaders struct { + Commits *loaders.CommitLoader + Branches *loaders.BranchLoader + Files *loaders.FileLoader + CommitFiles *loaders.CommitFileLoader + Remotes *loaders.RemoteLoader + ReflogCommits *loaders.ReflogCommitLoader + Stash *loaders.StashLoader + Tags *loaders.TagLoader +} + // GitCommand is our main git interface type GitCommand struct { *common.Common @@ -33,6 +45,7 @@ type GitCommand struct { onSuccessfulContinue func() error PatchManager *patch.PatchManager GitConfig git_config.IGitConfig + Loaders Loaders // Push to current determines whether the user has configured to push to the remote branch of the same name as the current or not PushToCurrent bool @@ -82,6 +95,17 @@ func NewGitCommand( Cmd: cmd, } + gitCommand.Loaders = Loaders{ + Commits: loaders.NewCommitLoader(cmn, gitCommand), + Branches: loaders.NewBranchLoader(cmn, gitCommand), + Files: loaders.NewFileLoader(cmn, cmd, gitConfig), + CommitFiles: loaders.NewCommitFileLoader(cmn, cmd), + Remotes: loaders.NewRemoteLoader(cmn, cmd, gitCommand.Repo.Remotes), + ReflogCommits: loaders.NewReflogCommitLoader(cmn, cmd), + Stash: loaders.NewStashLoader(cmn, cmd), + Tags: loaders.NewTagLoader(cmn, cmd), + } + gitCommand.PatchManager = patch.NewPatchManager(gitCommand.Log, gitCommand.ApplyPatch, gitCommand.ShowFileDiff) return gitCommand, nil diff --git a/pkg/commands/loaders/branches.go b/pkg/commands/loaders/branches.go index 60e4f7aac..f0e2e43b5 100644 --- a/pkg/commands/loaders/branches.go +++ b/pkg/commands/loaders/branches.go @@ -25,7 +25,6 @@ type BranchLoader struct { *common.Common getRawBranches func() (string, error) getCurrentBranchName func() (string, string, error) - reflogCommits []*models.Commit } type BranchLoaderGitCommand interface { @@ -36,21 +35,19 @@ type BranchLoaderGitCommand interface { func NewBranchLoader( cmn *common.Common, gitCommand BranchLoaderGitCommand, - reflogCommits []*models.Commit, ) *BranchLoader { return &BranchLoader{ Common: cmn, getRawBranches: gitCommand.GetRawBranches, getCurrentBranchName: gitCommand.CurrentBranchName, - reflogCommits: reflogCommits, } } // Load the list of branches for the current repo -func (self *BranchLoader) Load() []*models.Branch { +func (self *BranchLoader) Load(reflogCommits []*models.Commit) []*models.Branch { branches := self.obtainBranches() - reflogBranches := self.obtainReflogBranches() + reflogBranches := self.obtainReflogBranches(reflogCommits) // loop through reflog branches. If there is a match, merge them, then remove it from the branches and keep it in the reflog branches branchesWithRecency := make([]*models.Branch, 0) @@ -154,11 +151,11 @@ func (self *BranchLoader) obtainBranches() []*models.Branch { // TODO: only look at the new reflog commits, and otherwise store the recencies in // int form against the branch to recalculate the time ago -func (self *BranchLoader) obtainReflogBranches() []*models.Branch { +func (self *BranchLoader) obtainReflogBranches(reflogCommits []*models.Commit) []*models.Branch { foundBranchesMap := map[string]bool{} re := regexp.MustCompile(`checkout: moving from ([\S]+) to ([\S]+)`) - reflogBranches := make([]*models.Branch, 0, len(self.reflogCommits)) - for _, commit := range self.reflogCommits { + reflogBranches := make([]*models.Branch, 0, len(reflogCommits)) + for _, commit := range 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/submodules.go b/pkg/commands/submodules.go index 7e957234b..29a9ab700 100644 --- a/pkg/commands/submodules.go +++ b/pkg/commands/submodules.go @@ -71,11 +71,11 @@ func (c *GitCommand) SubmoduleStash(submodule *models.SubmoduleConfig) error { return nil } - return c.Cmd.New("git -C " + c.OSCommand.Quote(submodule.Path) + " stash --include-untracked").Run() + return c.Cmd.New("git -C " + c.Cmd.Quote(submodule.Path) + " stash --include-untracked").Run() } func (c *GitCommand) SubmoduleReset(submodule *models.SubmoduleConfig) error { - return c.Cmd.New("git submodule update --init --force -- " + c.OSCommand.Quote(submodule.Path)).Run() + return c.Cmd.New("git submodule update --init --force -- " + c.Cmd.Quote(submodule.Path)).Run() } func (c *GitCommand) SubmoduleUpdateAll() error { @@ -86,13 +86,13 @@ func (c *GitCommand) SubmoduleUpdateAll() error { func (c *GitCommand) SubmoduleDelete(submodule *models.SubmoduleConfig) error { // based on https://gist.github.com/myusuf3/7f645819ded92bda6677 - if err := c.Cmd.New("git submodule deinit --force -- " + c.OSCommand.Quote(submodule.Path)).Run(); err != nil { + if err := c.Cmd.New("git submodule deinit --force -- " + c.Cmd.Quote(submodule.Path)).Run(); err != nil { if strings.Contains(err.Error(), "did not match any file(s) known to git") { - if err := c.Cmd.New("git config --file .gitmodules --remove-section submodule." + c.OSCommand.Quote(submodule.Name)).Run(); err != nil { + if err := c.Cmd.New("git config --file .gitmodules --remove-section submodule." + c.Cmd.Quote(submodule.Name)).Run(); err != nil { return err } - if err := c.Cmd.New("git config --remove-section submodule." + c.OSCommand.Quote(submodule.Name)).Run(); err != nil { + if err := c.Cmd.New("git config --remove-section submodule." + c.Cmd.Quote(submodule.Name)).Run(); err != nil { return err } @@ -115,20 +115,20 @@ func (c *GitCommand) SubmoduleAdd(name string, path string, url string) error { New( fmt.Sprintf( "git submodule add --force --name %s -- %s %s ", - c.OSCommand.Quote(name), - c.OSCommand.Quote(url), - c.OSCommand.Quote(path), + c.Cmd.Quote(name), + c.Cmd.Quote(url), + c.Cmd.Quote(path), )). Run() } func (c *GitCommand) SubmoduleUpdateUrl(name string, path string, newUrl string) error { // the set-url command is only for later git versions so we're doing it manually here - if err := c.Cmd.New("git config --file .gitmodules submodule." + c.OSCommand.Quote(name) + ".url " + c.OSCommand.Quote(newUrl)).Run(); err != nil { + if err := c.Cmd.New("git config --file .gitmodules submodule." + c.Cmd.Quote(name) + ".url " + c.Cmd.Quote(newUrl)).Run(); err != nil { return err } - if err := c.Cmd.New("git submodule sync -- " + c.OSCommand.Quote(path)).Run(); err != nil { + if err := c.Cmd.New("git submodule sync -- " + c.Cmd.Quote(path)).Run(); err != nil { return err } @@ -136,11 +136,11 @@ func (c *GitCommand) SubmoduleUpdateUrl(name string, path string, newUrl string) } func (c *GitCommand) SubmoduleInit(path string) error { - return c.Cmd.New("git submodule init -- " + c.OSCommand.Quote(path)).Run() + return c.Cmd.New("git submodule init -- " + c.Cmd.Quote(path)).Run() } func (c *GitCommand) SubmoduleUpdate(path string) error { - return c.Cmd.New("git submodule update --init -- " + c.OSCommand.Quote(path)).Run() + return c.Cmd.New("git submodule update --init -- " + c.Cmd.Quote(path)).Run() } func (c *GitCommand) SubmoduleBulkInitCmdObj() oscommands.ICmdObj { diff --git a/pkg/gui/branches_panel.go b/pkg/gui/branches_panel.go index 14e9e9743..62062043e 100644 --- a/pkg/gui/branches_panel.go +++ b/pkg/gui/branches_panel.go @@ -6,7 +6,6 @@ import ( "strings" "github.com/jesseduffield/lazygit/pkg/commands" - "github.com/jesseduffield/lazygit/pkg/commands/loaders" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/utils" @@ -56,18 +55,13 @@ func (gui *Gui) refreshBranches() { // which allows us to order them correctly. So if we're filtering we'll just // manually load all the reflog commits here var err error - reflogCommits, _, err = loaders.NewReflogCommitLoader(gui.Common, gui.GitCommand.Cmd).GetReflogCommits(nil, "") + reflogCommits, _, err = gui.GitCommand.Loaders.ReflogCommits.GetReflogCommits(nil, "") if err != nil { gui.Log.Error(err) } } - loader := loaders.NewBranchLoader( - gui.Common, - gui.GitCommand, - reflogCommits, - ) - gui.State.Branches = loader.Load() + gui.State.Branches = gui.GitCommand.Loaders.Branches.Load(reflogCommits) if err := gui.postRefreshUpdate(gui.State.Contexts.Branches); err != nil { gui.Log.Error(err) diff --git a/pkg/gui/commit_files_panel.go b/pkg/gui/commit_files_panel.go index 8c300fb4d..3194c436b 100644 --- a/pkg/gui/commit_files_panel.go +++ b/pkg/gui/commit_files_panel.go @@ -1,7 +1,6 @@ package gui import ( - "github.com/jesseduffield/lazygit/pkg/commands/loaders" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/patch" "github.com/jesseduffield/lazygit/pkg/gui/filetree" @@ -106,7 +105,7 @@ func (gui *Gui) refreshCommitFilesView() error { to := gui.State.Panels.CommitFiles.refName from, reverse := gui.getFromAndReverseArgsForDiff(to) - files, err := loaders.NewCommitFileLoader(gui.Common, gui.GitCommand.Cmd).GetFilesInDiff(from, to, reverse) + files, err := gui.GitCommand.Loaders.CommitFiles.GetFilesInDiff(from, to, reverse) if err != nil { return gui.surfaceError(err) } diff --git a/pkg/gui/commits_panel.go b/pkg/gui/commits_panel.go index ed1718c18..021c1557b 100644 --- a/pkg/gui/commits_panel.go +++ b/pkg/gui/commits_panel.go @@ -119,9 +119,7 @@ func (gui *Gui) refreshCommitsWithLimit() error { gui.Mutexes.BranchCommitsMutex.Lock() defer gui.Mutexes.BranchCommitsMutex.Unlock() - loader := loaders.NewCommitLoader(gui.Common, gui.GitCommand) - - commits, err := loader.GetCommits( + commits, err := gui.GitCommand.Loaders.Commits.GetCommits( loaders.GetCommitsOptions{ Limit: gui.State.Panels.Commits.LimitCommits, FilterPath: gui.State.Modes.Filtering.GetPath(), @@ -142,9 +140,7 @@ func (gui *Gui) refreshRebaseCommits() error { gui.Mutexes.BranchCommitsMutex.Lock() defer gui.Mutexes.BranchCommitsMutex.Unlock() - loader := loaders.NewCommitLoader(gui.Common, gui.GitCommand) - - updatedCommits, err := loader.MergeRebasingCommits(gui.State.Commits) + updatedCommits, err := gui.GitCommand.Loaders.Commits.MergeRebasingCommits(gui.State.Commits) if err != nil { return err } diff --git a/pkg/gui/remotes_panel.go b/pkg/gui/remotes_panel.go index 999ec1a10..0a654f365 100644 --- a/pkg/gui/remotes_panel.go +++ b/pkg/gui/remotes_panel.go @@ -4,7 +4,6 @@ import ( "fmt" "strings" - "github.com/jesseduffield/lazygit/pkg/commands/loaders" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/utils" @@ -41,7 +40,7 @@ func (gui *Gui) remotesRenderToMain() error { func (gui *Gui) refreshRemotes() error { prevSelectedRemote := gui.getSelectedRemote() - remotes, err := loaders.NewRemoteLoader(gui.Common, gui.GitCommand.Cmd, gui.GitCommand.Repo.Remotes).GetRemotes() + remotes, err := gui.GitCommand.Loaders.Remotes.GetRemotes() if err != nil { return gui.surfaceError(err) } diff --git a/pkg/gui/sub_commits_panel.go b/pkg/gui/sub_commits_panel.go index 83661100d..aede87f27 100644 --- a/pkg/gui/sub_commits_panel.go +++ b/pkg/gui/sub_commits_panel.go @@ -75,9 +75,7 @@ func (gui *Gui) handleViewSubCommitFiles() error { func (gui *Gui) switchToSubCommitsContext(refName string) error { // need to populate my sub commits - loader := loaders.NewCommitLoader(gui.Common, gui.GitCommand) - - commits, err := loader.GetCommits( + commits, err := gui.GitCommand.Loaders.Commits.GetCommits( loaders.GetCommitsOptions{ Limit: gui.State.Panels.Commits.LimitCommits, FilterPath: gui.State.Modes.Filtering.GetPath(), diff --git a/pkg/gui/tags_panel.go b/pkg/gui/tags_panel.go index 3fc028e6d..9516741e0 100644 --- a/pkg/gui/tags_panel.go +++ b/pkg/gui/tags_panel.go @@ -1,7 +1,6 @@ package gui import ( - "github.com/jesseduffield/lazygit/pkg/commands/loaders" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/utils" ) @@ -40,7 +39,7 @@ func (gui *Gui) tagsRenderToMain() error { // this is a controller: it can't access tags directly. Or can it? It should be able to get but not set. But that's exactly what I'm doing here, setting it. but through a mutator which encapsulates the event. func (gui *Gui) refreshTags() error { - tags, err := loaders.NewTagLoader(gui.Common, gui.GitCommand.Cmd).GetTags() + tags, err := gui.GitCommand.Loaders.Tags.GetTags() if err != nil { return gui.surfaceError(err) }