From 37f835244dd3ba48b7167d0ee82471df5d5cae47 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 11 Jun 2024 14:20:32 +0200 Subject: [PATCH] Use WorkingTreeState instead of RebaseMode in CommitLoader We want to get rid of RebaseMode, and using the more general WorkingTreeState will later allow us to also show cherry-pick or revert todos. --- pkg/commands/git.go | 2 +- pkg/commands/git_commands/commit_loader.go | 41 ++++++++----------- .../git_commands/commit_loader_test.go | 16 ++++---- 3 files changed, 25 insertions(+), 34 deletions(-) diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 7e7d9354f..4aba9a7a8 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -136,7 +136,7 @@ func NewGitCommandAux( branchLoader := git_commands.NewBranchLoader(cmn, gitCommon, cmd, branchCommands.CurrentBranchInfo, configCommands) commitFileLoader := git_commands.NewCommitFileLoader(cmn, cmd) - commitLoader := git_commands.NewCommitLoader(cmn, cmd, statusCommands.RebaseMode, gitCommon) + commitLoader := git_commands.NewCommitLoader(cmn, cmd, statusCommands.WorkingTreeState, gitCommon) reflogCommitLoader := git_commands.NewReflogCommitLoader(cmn, cmd) remoteLoader := git_commands.NewRemoteLoader(cmn, cmd, repo.Remotes) worktreeLoader := git_commands.NewWorktreeLoader(gitCommon) diff --git a/pkg/commands/git_commands/commit_loader.go b/pkg/commands/git_commands/commit_loader.go index f9cfff141..6bce06360 100644 --- a/pkg/commands/git_commands/commit_loader.go +++ b/pkg/commands/git_commands/commit_loader.go @@ -31,10 +31,10 @@ type CommitLoader struct { *common.Common cmd oscommands.ICmdObjBuilder - getRebaseMode func() (enums.RebaseMode, error) - readFile func(filename string) ([]byte, error) - walkFiles func(root string, fn filepath.WalkFunc) error - dotGitDir string + getWorkingTreeState func() enums.RebaseMode + readFile func(filename string) ([]byte, error) + walkFiles func(root string, fn filepath.WalkFunc) error + dotGitDir string *GitCommon } @@ -42,16 +42,16 @@ type CommitLoader struct { func NewCommitLoader( cmn *common.Common, cmd oscommands.ICmdObjBuilder, - getRebaseMode func() (enums.RebaseMode, error), + getWorkingTreeState func() enums.RebaseMode, gitCommon *GitCommon, ) *CommitLoader { return &CommitLoader{ - Common: cmn, - cmd: cmd, - getRebaseMode: getRebaseMode, - readFile: os.ReadFile, - walkFiles: filepath.Walk, - GitCommon: gitCommon, + Common: cmn, + cmd: cmd, + getWorkingTreeState: getWorkingTreeState, + readFile: os.ReadFile, + walkFiles: filepath.Walk, + GitCommon: gitCommon, } } @@ -172,17 +172,12 @@ func (self *CommitLoader) MergeRebasingCommits(commits []*models.Commit) ([]*mod } } - rebaseMode, err := self.getRebaseMode() - if err != nil { - return nil, err - } - - if rebaseMode == enums.REBASE_MODE_NONE { + if !self.getWorkingTreeState().IsRebasing() { // not in rebase mode so return original commits return result, nil } - rebasingCommits, err := self.getHydratedRebasingCommits(rebaseMode) + rebasingCommits, err := self.getHydratedRebasingCommits() if err != nil { return nil, err } @@ -248,8 +243,8 @@ func (self *CommitLoader) extractCommitFromLine(line string, showDivergence bool } } -func (self *CommitLoader) getHydratedRebasingCommits(rebaseMode enums.RebaseMode) ([]*models.Commit, error) { - commits := self.getRebasingCommits(rebaseMode) +func (self *CommitLoader) getHydratedRebasingCommits() ([]*models.Commit, error) { + commits := self.getRebasingCommits() if len(commits) == 0 { return nil, nil @@ -310,11 +305,7 @@ func (self *CommitLoader) getHydratedRebasingCommits(rebaseMode enums.RebaseMode // git-rebase-todo example: // pick ac446ae94ee560bdb8d1d057278657b251aaef17 ac446ae // pick afb893148791a2fbd8091aeb81deba4930c73031 afb8931 -func (self *CommitLoader) getRebasingCommits(rebaseMode enums.RebaseMode) []*models.Commit { - if rebaseMode != enums.REBASE_MODE_INTERACTIVE { - return nil - } - +func (self *CommitLoader) getRebasingCommits() []*models.Commit { bytesContent, err := self.readFile(filepath.Join(self.repoPaths.WorktreeGitDirPath(), "rebase-merge/git-rebase-todo")) if err != nil { self.Log.Error(fmt.Sprintf("error occurred reading git-rebase-todo: %s", err.Error())) diff --git a/pkg/commands/git_commands/commit_loader_test.go b/pkg/commands/git_commands/commit_loader_test.go index a6b4e58ae..73cae73a2 100644 --- a/pkg/commands/git_commands/commit_loader_test.go +++ b/pkg/commands/git_commands/commit_loader_test.go @@ -302,10 +302,10 @@ func TestGetCommits(t *testing.T) { cmd := oscommands.NewDummyCmdObjBuilder(scenario.runner) builder := &CommitLoader{ - Common: common, - cmd: cmd, - getRebaseMode: func() (enums.RebaseMode, error) { return enums.REBASE_MODE_NONE, nil }, - dotGitDir: ".git", + Common: common, + cmd: cmd, + getWorkingTreeState: func() enums.RebaseMode { return enums.REBASE_MODE_NONE }, + dotGitDir: ".git", readFile: func(filename string) ([]byte, error) { return []byte(""), nil }, @@ -485,10 +485,10 @@ func TestCommitLoader_getConflictedCommitImpl(t *testing.T) { common := utils.NewDummyCommon() builder := &CommitLoader{ - Common: common, - cmd: oscommands.NewDummyCmdObjBuilder(oscommands.NewFakeRunner(t)), - getRebaseMode: func() (enums.RebaseMode, error) { return enums.REBASE_MODE_INTERACTIVE, nil }, - dotGitDir: ".git", + Common: common, + cmd: oscommands.NewDummyCmdObjBuilder(oscommands.NewFakeRunner(t)), + getWorkingTreeState: func() enums.RebaseMode { return enums.REBASE_MODE_INTERACTIVE }, + dotGitDir: ".git", readFile: func(filename string) ([]byte, error) { return []byte(""), nil },