diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2021-12-30 13:35:10 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-01-04 09:07:15 +1100 |
commit | 66e840bc3f83903852408ed8db0be0c6153b5487 (patch) | |
tree | 2e3578e0ceff2fb44b8a7060f87400808eedaef7 /pkg/commands | |
parent | 5b3572424380418f86df88d201347b54634be530 (diff) |
more refactoring
Diffstat (limited to 'pkg/commands')
-rw-r--r-- | pkg/commands/loader_adapters.go | 28 | ||||
-rw-r--r-- | pkg/commands/loaders/commits.go (renamed from pkg/commands/loading_commits.go) | 72 | ||||
-rw-r--r-- | pkg/commands/loaders/commits_test.go (renamed from pkg/commands/loading_commits_test.go) | 19 | ||||
-rw-r--r-- | pkg/commands/loading_branches.go | 2 | ||||
-rw-r--r-- | pkg/commands/oscommands/fake_cmd_obj_runner.go (renamed from pkg/commands/oscommands/fake_runner.go) | 2 | ||||
-rw-r--r-- | pkg/commands/patch_rebases.go | 5 | ||||
-rw-r--r-- | pkg/commands/status.go | 34 | ||||
-rw-r--r-- | pkg/commands/types/enums/enums.go | 14 |
8 files changed, 108 insertions, 68 deletions
diff --git a/pkg/commands/loader_adapters.go b/pkg/commands/loader_adapters.go new file mode 100644 index 000000000..166bf012d --- /dev/null +++ b/pkg/commands/loader_adapters.go @@ -0,0 +1,28 @@ +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, + ) +} diff --git a/pkg/commands/loading_commits.go b/pkg/commands/loaders/commits.go index af93509c5..5fc2caa98 100644 --- a/pkg/commands/loading_commits.go +++ b/pkg/commands/loaders/commits.go @@ -1,8 +1,7 @@ -package commands +package loaders import ( "fmt" - "io/ioutil" "os" "path/filepath" "regexp" @@ -11,6 +10,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" + "github.com/jesseduffield/lazygit/pkg/commands/types/enums" "github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/gui/style" ) @@ -23,31 +23,35 @@ import ( const SEPARATION_CHAR = "|" -// CommitListBuilder returns a list of Commit objects for the current repo -type CommitListBuilder struct { +// CommitLoader returns a list of Commit objects for the current repo +type CommitLoader struct { *common.Common cmd oscommands.ICmdObjBuilder getCurrentBranchName func() (string, string, error) - getRebaseMode func() (RebaseMode, error) + getRebaseMode func() (enums.RebaseMode, error) readFile func(filename string) ([]byte, error) walkFiles func(root string, fn filepath.WalkFunc) error dotGitDir string } -func NewCommitListBuilder( - cmn *common.Common, - gitCommand *GitCommand, - osCommand *oscommands.OSCommand, -) *CommitListBuilder { - return &CommitListBuilder{ - Common: cmn, - cmd: gitCommand.Cmd, - getCurrentBranchName: gitCommand.CurrentBranchName, - getRebaseMode: gitCommand.RebaseMode, - dotGitDir: gitCommand.DotGitDir, - readFile: ioutil.ReadFile, - walkFiles: filepath.Walk, +func NewCommitLoader( + common *common.Common, + cmd oscommands.ICmdObjBuilder, + getCurrentBranchName func() (string, string, error), + getRebaseMode func() (enums.RebaseMode, error), + readFile func(filename string) ([]byte, error), + walkFiles func(root string, fn filepath.WalkFunc) error, + dotGitDir string, +) *CommitLoader { + return &CommitLoader{ + Common: common, + cmd: cmd, + getCurrentBranchName: getCurrentBranchName, + getRebaseMode: getRebaseMode, + readFile: readFile, + walkFiles: walkFiles, + dotGitDir: dotGitDir, } } @@ -61,7 +65,7 @@ type GetCommitsOptions struct { } // GetCommits obtains the commits of the current branch -func (self *CommitListBuilder) GetCommits(opts GetCommitsOptions) ([]*models.Commit, error) { +func (self *CommitLoader) GetCommits(opts GetCommitsOptions) ([]*models.Commit, error) { commits := []*models.Commit{} var rebasingCommits []*models.Commit rebaseMode, err := self.getRebaseMode() @@ -104,7 +108,7 @@ func (self *CommitListBuilder) GetCommits(opts GetCommitsOptions) ([]*models.Com return commits, nil } - if rebaseMode != REBASE_MODE_NONE { + if rebaseMode != enums.REBASE_MODE_NONE { currentCommit := commits[len(rebasingCommits)] youAreHere := style.FgYellow.Sprintf("<-- %s ---", self.Tr.YouAreHere) currentCommit.Name = fmt.Sprintf("%s %s", youAreHere, currentCommit.Name) @@ -118,7 +122,7 @@ func (self *CommitListBuilder) GetCommits(opts GetCommitsOptions) ([]*models.Com return commits, nil } -func (self *CommitListBuilder) MergeRebasingCommits(commits []*models.Commit) ([]*models.Commit, error) { +func (self *CommitLoader) MergeRebasingCommits(commits []*models.Commit) ([]*models.Commit, error) { // chances are we have as many commits as last time so we'll set the capacity to be the old length result := make([]*models.Commit, 0, len(commits)) for i, commit := range commits { @@ -133,7 +137,7 @@ func (self *CommitListBuilder) MergeRebasingCommits(commits []*models.Commit) ([ return nil, err } - if rebaseMode == REBASE_MODE_NONE { + if rebaseMode == enums.REBASE_MODE_NONE { // not in rebase mode so return original commits return result, nil } @@ -153,7 +157,7 @@ func (self *CommitListBuilder) MergeRebasingCommits(commits []*models.Commit) ([ // then puts them into a commit object // example input: // 8ad01fe32fcc20f07bc6693f87aa4977c327f1e1|10 hours ago|Jesse Duffield| (HEAD -> master, tag: v0.15.2)|refresh commits when adding a tag -func (self *CommitListBuilder) extractCommitFromLine(line string) *models.Commit { +func (self *CommitLoader) extractCommitFromLine(line string) *models.Commit { split := strings.Split(line, SEPARATION_CHAR) sha := split[0] @@ -186,7 +190,7 @@ func (self *CommitListBuilder) extractCommitFromLine(line string) *models.Commit } } -func (self *CommitListBuilder) getHydratedRebasingCommits(rebaseMode RebaseMode) ([]*models.Commit, error) { +func (self *CommitLoader) getHydratedRebasingCommits(rebaseMode enums.RebaseMode) ([]*models.Commit, error) { commits, err := self.getRebasingCommits(rebaseMode) if err != nil { return nil, err @@ -232,18 +236,18 @@ func (self *CommitListBuilder) getHydratedRebasingCommits(rebaseMode RebaseMode) } // getRebasingCommits obtains the commits that we're in the process of rebasing -func (self *CommitListBuilder) getRebasingCommits(rebaseMode RebaseMode) ([]*models.Commit, error) { +func (self *CommitLoader) getRebasingCommits(rebaseMode enums.RebaseMode) ([]*models.Commit, error) { switch rebaseMode { - case REBASE_MODE_MERGING: + case enums.REBASE_MODE_MERGING: return self.getNormalRebasingCommits() - case REBASE_MODE_INTERACTIVE: + case enums.REBASE_MODE_INTERACTIVE: return self.getInteractiveRebasingCommits() default: return nil, nil } } -func (self *CommitListBuilder) getNormalRebasingCommits() ([]*models.Commit, error) { +func (self *CommitLoader) getNormalRebasingCommits() ([]*models.Commit, error) { rewrittenCount := 0 bytesContent, err := self.readFile(filepath.Join(self.dotGitDir, "rebase-apply/rewritten")) if err == nil { @@ -296,7 +300,7 @@ func (self *CommitListBuilder) getNormalRebasingCommits() ([]*models.Commit, err // getInteractiveRebasingCommits takes our git-rebase-todo and our git-rebase-todo.backup files // and extracts out the sha and names of commits that we still have to go // in the rebase: -func (self *CommitListBuilder) getInteractiveRebasingCommits() ([]*models.Commit, error) { +func (self *CommitLoader) getInteractiveRebasingCommits() ([]*models.Commit, error) { bytesContent, err := self.readFile(filepath.Join(self.dotGitDir, "rebase-merge/git-rebase-todo")) if err != nil { self.Log.Error(fmt.Sprintf("error occurred reading git-rebase-todo: %s", err.Error())) @@ -330,7 +334,7 @@ func (self *CommitListBuilder) getInteractiveRebasingCommits() ([]*models.Commit // From: Lazygit Tester <test@example.com> // Date: Wed, 5 Dec 2018 21:03:23 +1100 // Subject: second commit on master -func (self *CommitListBuilder) commitFromPatch(content string) (*models.Commit, error) { +func (self *CommitLoader) commitFromPatch(content string) (*models.Commit, error) { lines := strings.Split(content, "\n") sha := strings.Split(lines[0], " ")[1] name := strings.TrimPrefix(lines[3], "Subject: ") @@ -341,7 +345,7 @@ func (self *CommitListBuilder) commitFromPatch(content string) (*models.Commit, }, nil } -func (self *CommitListBuilder) setCommitMergedStatuses(refName string, commits []*models.Commit) ([]*models.Commit, error) { +func (self *CommitLoader) setCommitMergedStatuses(refName string, commits []*models.Commit) ([]*models.Commit, error) { ancestor, err := self.getMergeBase(refName) if err != nil { return nil, err @@ -364,7 +368,7 @@ func (self *CommitListBuilder) setCommitMergedStatuses(refName string, commits [ return commits, nil } -func (self *CommitListBuilder) getMergeBase(refName string) (string, error) { +func (self *CommitLoader) getMergeBase(refName string) (string, error) { currentBranch, _, err := self.getCurrentBranchName() if err != nil { return "", err @@ -392,7 +396,7 @@ func ignoringWarnings(commandOutput string) string { // getFirstPushedCommit returns the first commit SHA which has been pushed to the ref's upstream. // all commits above this are deemed unpushed and marked as such. -func (self *CommitListBuilder) getFirstPushedCommit(refName string) (string, error) { +func (self *CommitLoader) getFirstPushedCommit(refName string) (string, error) { output, err := self.cmd. New( fmt.Sprintf("git merge-base %s %s@{u}", self.cmd.Quote(refName), self.cmd.Quote(refName)), @@ -406,7 +410,7 @@ func (self *CommitListBuilder) getFirstPushedCommit(refName string) (string, err } // getLog gets the git log. -func (self *CommitListBuilder) getLogCmd(opts GetCommitsOptions) oscommands.ICmdObj { +func (self *CommitLoader) getLogCmd(opts GetCommitsOptions) oscommands.ICmdObj { limitFlag := "" if opts.Limit { limitFlag = " -300" diff --git a/pkg/commands/loading_commits_test.go b/pkg/commands/loaders/commits_test.go index 29f8ef992..f793567f7 100644 --- a/pkg/commands/loading_commits_test.go +++ b/pkg/commands/loaders/commits_test.go @@ -1,4 +1,4 @@ -package commands +package loaders import ( "path/filepath" @@ -6,18 +6,19 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" + "github.com/jesseduffield/lazygit/pkg/commands/types/enums" "github.com/jesseduffield/lazygit/pkg/utils" "github.com/stretchr/testify/assert" ) -func NewDummyCommitListBuilder() *CommitListBuilder { +func NewDummyCommitLoader() *CommitLoader { cmn := utils.NewDummyCommon() - return &CommitListBuilder{ + return &CommitLoader{ Common: cmn, cmd: nil, getCurrentBranchName: func() (string, string, error) { return "master", "master", nil }, - getRebaseMode: func() (RebaseMode, error) { return REBASE_MODE_NONE, nil }, + getRebaseMode: func() (enums.RebaseMode, error) { return enums.REBASE_MODE_NONE, nil }, dotGitDir: ".git", readFile: func(filename string) ([]byte, error) { return []byte(""), nil @@ -43,7 +44,7 @@ func TestGetCommits(t *testing.T) { runner oscommands.ICmdObjRunner expectedCommits []*models.Commit expectedError error - rebaseMode RebaseMode + rebaseMode enums.RebaseMode currentBranchName string opts GetCommitsOptions } @@ -51,7 +52,7 @@ func TestGetCommits(t *testing.T) { scenarios := []scenario{ { testName: "should return no commits if there are none", - rebaseMode: REBASE_MODE_NONE, + rebaseMode: enums.REBASE_MODE_NONE, currentBranchName: "master", opts: GetCommitsOptions{RefName: "HEAD", IncludeRebaseCommits: false}, runner: oscommands.NewFakeRunner(t). @@ -63,7 +64,7 @@ func TestGetCommits(t *testing.T) { }, { testName: "should return commits if they are present", - rebaseMode: REBASE_MODE_NONE, + rebaseMode: enums.REBASE_MODE_NONE, currentBranchName: "master", opts: GetCommitsOptions{RefName: "HEAD", IncludeRebaseCommits: false}, runner: oscommands.NewFakeRunner(t). @@ -186,13 +187,13 @@ func TestGetCommits(t *testing.T) { for _, scenario := range scenarios { t.Run(scenario.testName, func(t *testing.T) { - builder := &CommitListBuilder{ + builder := &CommitLoader{ Common: utils.NewDummyCommon(), cmd: oscommands.NewCmdObjBuilderDummy(scenario.runner), getCurrentBranchName: func() (string, string, error) { return scenario.currentBranchName, scenario.currentBranchName, nil }, - getRebaseMode: func() (RebaseMode, error) { return scenario.rebaseMode, nil }, + getRebaseMode: func() (enums.RebaseMode, error) { return scenario.rebaseMode, nil }, dotGitDir: ".git", readFile: func(filename string) ([]byte, error) { return []byte(""), nil diff --git a/pkg/commands/loading_branches.go b/pkg/commands/loading_branches.go index 802bdb30e..62f758bcd 100644 --- a/pkg/commands/loading_branches.go +++ b/pkg/commands/loading_branches.go @@ -20,6 +20,8 @@ import ( // 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 +const SEPARATION_CHAR = "|" + // BranchListBuilder returns a list of Branch objects for the current repo type BranchListBuilder struct { *common.Common diff --git a/pkg/commands/oscommands/fake_runner.go b/pkg/commands/oscommands/fake_cmd_obj_runner.go index c18378c8f..09431bb20 100644 --- a/pkg/commands/oscommands/fake_runner.go +++ b/pkg/commands/oscommands/fake_cmd_obj_runner.go @@ -10,6 +10,8 @@ import ( "github.com/stretchr/testify/assert" ) +// for use in testing + type FakeCmdObjRunner struct { t *testing.T expectedCmds []func(ICmdObj) (string, error) diff --git a/pkg/commands/patch_rebases.go b/pkg/commands/patch_rebases.go index 8f33479bf..c03c8d0a4 100644 --- a/pkg/commands/patch_rebases.go +++ b/pkg/commands/patch_rebases.go @@ -6,6 +6,7 @@ import ( "github.com/go-errors/errors" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/patch" + "github.com/jesseduffield/lazygit/pkg/commands/types/enums" ) // DeletePatchesFromCommit applies a patch in reverse for a commit @@ -149,7 +150,7 @@ func (c *GitCommand) MovePatchIntoIndex(commits []*models.Commit, commitIdx int, } if err := p.ApplyPatches(true); err != nil { - if c.WorkingTreeState() == REBASE_MODE_REBASING { + if c.WorkingTreeState() == enums.REBASE_MODE_REBASING { if err := c.GenericMergeOrRebaseAction("rebase", "abort"); err != nil { return err } @@ -169,7 +170,7 @@ func (c *GitCommand) MovePatchIntoIndex(commits []*models.Commit, commitIdx int, c.onSuccessfulContinue = func() error { // add patches to index if err := p.ApplyPatches(false); err != nil { - if c.WorkingTreeState() == REBASE_MODE_REBASING { + if c.WorkingTreeState() == enums.REBASE_MODE_REBASING { if err := c.GenericMergeOrRebaseAction("rebase", "abort"); err != nil { return err } diff --git a/pkg/commands/status.go b/pkg/commands/status.go index 0d7259572..392ce3797 100644 --- a/pkg/commands/status.go +++ b/pkg/commands/status.go @@ -4,49 +4,37 @@ import ( "path/filepath" gogit "github.com/jesseduffield/go-git/v5" -) - -type RebaseMode int - -const ( - // this means we're neither rebasing nor merging - REBASE_MODE_NONE RebaseMode = iota - // this means normal rebase as opposed to interactive rebase - REBASE_MODE_NORMAL - REBASE_MODE_INTERACTIVE - // REBASE_MODE_REBASING is a general state that captures both REBASE_MODE_NORMAL and REBASE_MODE_INTERACTIVE - REBASE_MODE_REBASING - REBASE_MODE_MERGING + "github.com/jesseduffield/lazygit/pkg/commands/types/enums" ) // RebaseMode returns "" for non-rebase mode, "normal" for normal rebase // and "interactive" for interactive rebase -func (c *GitCommand) RebaseMode() (RebaseMode, error) { +func (c *GitCommand) RebaseMode() (enums.RebaseMode, error) { exists, err := c.OSCommand.FileExists(filepath.Join(c.DotGitDir, "rebase-apply")) if err != nil { - return REBASE_MODE_NONE, err + return enums.REBASE_MODE_NONE, err } if exists { - return REBASE_MODE_NORMAL, nil + return enums.REBASE_MODE_NORMAL, nil } exists, err = c.OSCommand.FileExists(filepath.Join(c.DotGitDir, "rebase-merge")) if exists { - return REBASE_MODE_INTERACTIVE, err + return enums.REBASE_MODE_INTERACTIVE, err } else { - return REBASE_MODE_NONE, err + return enums.REBASE_MODE_NONE, err } } -func (c *GitCommand) WorkingTreeState() RebaseMode { +func (c *GitCommand) WorkingTreeState() enums.RebaseMode { rebaseMode, _ := c.RebaseMode() - if rebaseMode != REBASE_MODE_NONE { - return REBASE_MODE_REBASING + if rebaseMode != enums.REBASE_MODE_NONE { + return enums.REBASE_MODE_REBASING } merging, _ := c.IsInMergeState() if merging { - return REBASE_MODE_MERGING + return enums.REBASE_MODE_MERGING } - return REBASE_MODE_NONE + return enums.REBASE_MODE_NONE } // IsInMergeState states whether we are still mid-merge diff --git a/pkg/commands/types/enums/enums.go b/pkg/commands/types/enums/enums.go new file mode 100644 index 000000000..7e8a81798 --- /dev/null +++ b/pkg/commands/types/enums/enums.go @@ -0,0 +1,14 @@ +package enums + +type RebaseMode int + +const ( + // this means we're neither rebasing nor merging + REBASE_MODE_NONE RebaseMode = iota + // this means normal rebase as opposed to interactive rebase + REBASE_MODE_NORMAL + REBASE_MODE_INTERACTIVE + // REBASE_MODE_REBASING is a general state that captures both REBASE_MODE_NORMAL and REBASE_MODE_INTERACTIVE + REBASE_MODE_REBASING + REBASE_MODE_MERGING +) |