1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-28 16:02:01 +03:00

do dependency injection up front and in one place

This commit is contained in:
Jesse Duffield
2022-01-02 10:21:32 +11:00
parent 2cb8aff940
commit 194ff1630c
9 changed files with 49 additions and 43 deletions

View File

@ -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

View File

@ -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:] {

View File

@ -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 {