1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-25 12:24:47 +02:00
This commit is contained in:
Jesse Duffield 2021-12-30 13:44:41 +11:00
parent 66e840bc3f
commit 96c2887fd0
7 changed files with 35 additions and 68 deletions

@ -1,28 +0,0 @@
package commands
import (
"io/ioutil"
"path/filepath"
"github.com/jesseduffield/lazygit/pkg/commands/loaders"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/common"
)
// this file defines constructors for loaders, passing in all the dependencies required based on a smaller set of arguments passed in by the client.
func NewCommitLoader(
cmn *common.Common,
gitCommand *GitCommand,
osCommand *oscommands.OSCommand,
) *loaders.CommitLoader {
return loaders.NewCommitLoader(
cmn,
gitCommand.Cmd,
gitCommand.CurrentBranchName,
gitCommand.RebaseMode,
ioutil.ReadFile,
filepath.Walk,
gitCommand.DotGitDir,
)
}

@ -1,9 +1,10 @@
package commands package loaders
import ( import (
"regexp" "regexp"
"strings" "strings"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/common"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
@ -20,32 +21,29 @@ import (
// if we find out we need to use one of these functions in the git.go file, we // if we find out we need to use one of these functions in the git.go file, we
// can just pull them out of here and put them there and then call them from in here // can just pull them out of here and put them there and then call them from in here
const SEPARATION_CHAR = "|" // BranchLoader returns a list of Branch objects for the current repo
type BranchLoader struct {
// BranchListBuilder returns a list of Branch objects for the current repo
type BranchListBuilder struct {
*common.Common *common.Common
getRawBranches func() (string, error) getRawBranches func() (string, error)
getCurrentBranchName func() (string, string, error) getCurrentBranchName func() (string, string, error)
reflogCommits []*models.Commit reflogCommits []*models.Commit
} }
func NewBranchListBuilder( func NewBranchLoader(
cmn *common.Common, cmn *common.Common,
getRawBranches func() (string, error), gitCommand *commands.GitCommand,
getCurrentBranchName func() (string, string, error),
reflogCommits []*models.Commit, reflogCommits []*models.Commit,
) *BranchListBuilder { ) *BranchLoader {
return &BranchListBuilder{ return &BranchLoader{
Common: cmn, Common: cmn,
getRawBranches: getRawBranches, getRawBranches: gitCommand.GetRawBranches,
getCurrentBranchName: getCurrentBranchName, getCurrentBranchName: gitCommand.CurrentBranchName,
reflogCommits: reflogCommits, reflogCommits: reflogCommits,
} }
} }
// Build the list of branches for the current repo // Load the list of branches for the current repo
func (b *BranchListBuilder) Build() []*models.Branch { func (b *BranchLoader) Load() []*models.Branch {
branches := b.obtainBranches() branches := b.obtainBranches()
reflogBranches := b.obtainReflogBranches() reflogBranches := b.obtainReflogBranches()
@ -89,7 +87,7 @@ outer:
return branches return branches
} }
func (b *BranchListBuilder) obtainBranches() []*models.Branch { func (b *BranchLoader) obtainBranches() []*models.Branch {
output, err := b.getRawBranches() output, err := b.getRawBranches()
if err != nil { if err != nil {
panic(err) panic(err)
@ -152,7 +150,7 @@ func (b *BranchListBuilder) obtainBranches() []*models.Branch {
// TODO: only look at the new reflog commits, and otherwise store the recencies in // TODO: only look at the new reflog commits, and otherwise store the recencies in
// int form against the branch to recalculate the time ago // int form against the branch to recalculate the time ago
func (b *BranchListBuilder) obtainReflogBranches() []*models.Branch { func (b *BranchLoader) obtainReflogBranches() []*models.Branch {
foundBranchesMap := map[string]bool{} foundBranchesMap := map[string]bool{}
re := regexp.MustCompile(`checkout: moving from ([\S]+) to ([\S]+)`) re := regexp.MustCompile(`checkout: moving from ([\S]+) to ([\S]+)`)
reflogBranches := make([]*models.Branch, 0, len(b.reflogCommits)) reflogBranches := make([]*models.Branch, 0, len(b.reflogCommits))

@ -2,12 +2,14 @@ package loaders
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/commands/types/enums" "github.com/jesseduffield/lazygit/pkg/commands/types/enums"
@ -35,23 +37,20 @@ type CommitLoader struct {
dotGitDir string dotGitDir string
} }
// making our dependencies explicit for the sake of easier testing
func NewCommitLoader( func NewCommitLoader(
common *common.Common, cmn *common.Common,
cmd oscommands.ICmdObjBuilder, gitCommand *commands.GitCommand,
getCurrentBranchName func() (string, string, error), osCommand *oscommands.OSCommand,
getRebaseMode func() (enums.RebaseMode, error),
readFile func(filename string) ([]byte, error),
walkFiles func(root string, fn filepath.WalkFunc) error,
dotGitDir string,
) *CommitLoader { ) *CommitLoader {
return &CommitLoader{ return &CommitLoader{
Common: common, Common: cmn,
cmd: cmd, cmd: gitCommand.Cmd,
getCurrentBranchName: getCurrentBranchName, getCurrentBranchName: gitCommand.CurrentBranchName,
getRebaseMode: getRebaseMode, getRebaseMode: gitCommand.RebaseMode,
readFile: readFile, readFile: ioutil.ReadFile,
walkFiles: walkFiles, walkFiles: filepath.Walk,
dotGitDir: dotGitDir, dotGitDir: gitCommand.DotGitDir,
} }
} }

@ -55,7 +55,7 @@ func TestOSCommandRun(t *testing.T) {
for _, s := range scenarios { for _, s := range scenarios {
c := NewDummyOSCommand() c := NewDummyOSCommand()
s.test(c.Cmd.New(s.command)).Run() s.test(c.Cmd.New(s.command).Run())
} }
} }

@ -6,6 +6,7 @@ import (
"strings" "strings"
"github.com/jesseduffield/lazygit/pkg/commands" "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/models"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
@ -61,13 +62,12 @@ func (gui *Gui) refreshBranches() {
} }
} }
builder := commands.NewBranchListBuilder( loader := loaders.NewBranchLoader(
gui.Common, gui.Common,
gui.GitCommand.GetRawBranches, gui.GitCommand,
gui.GitCommand.CurrentBranchName,
reflogCommits, reflogCommits,
) )
gui.State.Branches = builder.Build() gui.State.Branches = loader.Load()
if err := gui.postRefreshUpdate(gui.State.Contexts.Branches); err != nil { if err := gui.postRefreshUpdate(gui.State.Contexts.Branches); err != nil {
gui.Log.Error(err) gui.Log.Error(err)

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"sync" "sync"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/commands/loaders" "github.com/jesseduffield/lazygit/pkg/commands/loaders"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/commands/oscommands"
@ -120,7 +119,7 @@ func (gui *Gui) refreshCommitsWithLimit() error {
gui.Mutexes.BranchCommitsMutex.Lock() gui.Mutexes.BranchCommitsMutex.Lock()
defer gui.Mutexes.BranchCommitsMutex.Unlock() defer gui.Mutexes.BranchCommitsMutex.Unlock()
loader := commands.NewCommitLoader(gui.Common, gui.GitCommand, gui.OSCommand) loader := loaders.NewCommitLoader(gui.Common, gui.GitCommand, gui.OSCommand)
commits, err := loader.GetCommits( commits, err := loader.GetCommits(
loaders.GetCommitsOptions{ loaders.GetCommitsOptions{
@ -143,7 +142,7 @@ func (gui *Gui) refreshRebaseCommits() error {
gui.Mutexes.BranchCommitsMutex.Lock() gui.Mutexes.BranchCommitsMutex.Lock()
defer gui.Mutexes.BranchCommitsMutex.Unlock() defer gui.Mutexes.BranchCommitsMutex.Unlock()
loader := commands.NewCommitLoader(gui.Common, gui.GitCommand, gui.OSCommand) loader := loaders.NewCommitLoader(gui.Common, gui.GitCommand, gui.OSCommand)
updatedCommits, err := loader.MergeRebasingCommits(gui.State.Commits) updatedCommits, err := loader.MergeRebasingCommits(gui.State.Commits)
if err != nil { if err != nil {

@ -1,7 +1,6 @@
package gui package gui
import ( import (
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/commands/loaders" "github.com/jesseduffield/lazygit/pkg/commands/loaders"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
) )
@ -76,7 +75,7 @@ func (gui *Gui) handleViewSubCommitFiles() error {
func (gui *Gui) switchToSubCommitsContext(refName string) error { func (gui *Gui) switchToSubCommitsContext(refName string) error {
// need to populate my sub commits // need to populate my sub commits
loader := commands.NewCommitLoader(gui.Common, gui.GitCommand, gui.OSCommand) loader := loaders.NewCommitLoader(gui.Common, gui.GitCommand, gui.OSCommand)
commits, err := loader.GetCommits( commits, err := loader.GetCommits(
loaders.GetCommitsOptions{ loaders.GetCommitsOptions{