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

Make WorkingTreeState a struct, and add cherry-picking and reverting states

This commit is contained in:
Stefan Haller
2024-06-10 17:54:04 +02:00
parent 8af8f7754b
commit 542525743c
17 changed files with 176 additions and 95 deletions

View File

@ -171,7 +171,7 @@ func (self *CommitLoader) MergeRebasingCommits(commits []*models.Commit) ([]*mod
}
}
if !self.getWorkingTreeState().IsRebasing() {
if !self.getWorkingTreeState().Rebasing {
// not in rebase mode so return original commits
return result, nil
}

View File

@ -303,7 +303,7 @@ func TestGetCommits(t *testing.T) {
builder := &CommitLoader{
Common: common,
cmd: cmd,
getWorkingTreeState: func() models.WorkingTreeState { return models.WORKING_TREE_STATE_NONE },
getWorkingTreeState: func() models.WorkingTreeState { return models.WorkingTreeState{} },
dotGitDir: ".git",
readFile: func(filename string) ([]byte, error) {
return []byte(""), nil
@ -486,7 +486,7 @@ func TestCommitLoader_getConflictedCommitImpl(t *testing.T) {
builder := &CommitLoader{
Common: common,
cmd: oscommands.NewDummyCmdObjBuilder(oscommands.NewFakeRunner(t)),
getWorkingTreeState: func() models.WorkingTreeState { return models.WORKING_TREE_STATE_REBASING },
getWorkingTreeState: func() models.WorkingTreeState { return models.WorkingTreeState{Rebasing: true} },
dotGitDir: ".git",
readFile: func(filename string) ([]byte, error) {
return []byte(""), nil

View File

@ -228,7 +228,7 @@ func (self *PatchCommands) MovePatchIntoIndex(commits []*models.Commit, commitId
}
if err := self.ApplyCustomPatch(true, true); err != nil {
if self.status.WorkingTreeState() == models.WORKING_TREE_STATE_REBASING {
if self.status.WorkingTreeState().Rebasing {
_ = self.rebase.AbortRebase()
}
return err
@ -252,7 +252,7 @@ func (self *PatchCommands) MovePatchIntoIndex(commits []*models.Commit, commitId
self.rebase.onSuccessfulContinue = func() error {
// add patches to index
if err := self.ApplyPatch(patch, ApplyPatchOpts{Index: true, ThreeWay: true}); err != nil {
if self.status.WorkingTreeState() == models.WORKING_TREE_STATE_REBASING {
if self.status.WorkingTreeState().Rebasing {
_ = self.rebase.AbortRebase()
}
return err

View File

@ -21,15 +21,12 @@ func NewStatusCommands(
}
func (self *StatusCommands) WorkingTreeState() models.WorkingTreeState {
isInRebase, _ := self.IsInRebase()
if isInRebase {
return models.WORKING_TREE_STATE_REBASING
}
merging, _ := self.IsInMergeState()
if merging {
return models.WORKING_TREE_STATE_MERGING
}
return models.WORKING_TREE_STATE_NONE
result := models.WorkingTreeState{}
result.Rebasing, _ = self.IsInRebase()
result.Merging, _ = self.IsInMergeState()
result.CherryPicking, _ = self.IsInCherryPick()
result.Reverting, _ = self.IsInRevert()
return result
}
func (self *StatusCommands) IsBareRepo() bool {
@ -49,6 +46,42 @@ func (self *StatusCommands) IsInMergeState() (bool, error) {
return self.os.FileExists(filepath.Join(self.repoPaths.WorktreeGitDirPath(), "MERGE_HEAD"))
}
func (self *StatusCommands) IsInCherryPick() (bool, error) {
exists, err := self.os.FileExists(filepath.Join(self.repoPaths.WorktreeGitDirPath(), "CHERRY_PICK_HEAD"))
if err != nil || !exists {
return exists, err
}
// Sometimes, CHERRY_PICK_HEAD is present during rebases even if no
// cherry-pick is in progress. I suppose this is because rebase used to be
// implemented as a series of cherry-picks, so this could be remnants of
// code that is shared between cherry-pick and rebase, or something. The way
// to tell if this is the case is to check for the presence of the
// stopped-sha file, which records the sha of the last pick that was
// executed before the rebase stopped, and seeing if the sha in that file is
// the same as the one in CHERRY_PICK_HEAD.
cherryPickHead, err := os.ReadFile(filepath.Join(self.repoPaths.WorktreeGitDirPath(), "CHERRY_PICK_HEAD"))
if err != nil {
return false, err
}
stoppedSha, err := os.ReadFile(filepath.Join(self.repoPaths.WorktreeGitDirPath(), "rebase-merge", "stopped-sha"))
if err != nil {
// If we get an error we assume the file doesn't exist
return true, nil
}
cherryPickHeadStr := strings.TrimSpace(string(cherryPickHead))
stoppedShaStr := strings.TrimSpace(string(stoppedSha))
// Need to use HasPrefix here because the cherry-pick HEAD is a full sha1,
// but stopped-sha is an abbreviated sha1
if strings.HasPrefix(cherryPickHeadStr, stoppedShaStr) {
return false, nil
}
return true, nil
}
func (self *StatusCommands) IsInRevert() (bool, error) {
return self.os.FileExists(filepath.Join(self.repoPaths.WorktreeGitDirPath(), "REVERT_HEAD"))
}
// Full ref (e.g. "refs/heads/mybranch") of the branch that is currently
// being rebased, or empty string when we're not in a rebase
func (self *StatusCommands) BranchBeingRebased() string {