1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-06 03:53:59 +02:00
lazygit/pkg/commands/git_commands/status.go

73 lines
1.9 KiB
Go
Raw Normal View History

2022-01-08 05:00:36 +02:00
package git_commands
2020-09-29 12:03:39 +02:00
import (
"path/filepath"
2020-10-06 11:50:54 +02:00
gogit "github.com/jesseduffield/go-git/v5"
2022-01-02 01:34:33 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
2021-12-30 04:35:10 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
2022-01-02 01:34:33 +02:00
"github.com/jesseduffield/lazygit/pkg/common"
2020-11-16 11:38:26 +02:00
)
2022-01-02 01:34:33 +02:00
type StatusCommands struct {
*common.Common
osCommand *oscommands.OSCommand
repo *gogit.Repository
dotGitDir string
}
func NewStatusCommands(
common *common.Common,
osCommand *oscommands.OSCommand,
repo *gogit.Repository,
dotGitDir string,
) *StatusCommands {
return &StatusCommands{
Common: common,
osCommand: osCommand,
repo: repo,
dotGitDir: dotGitDir,
}
}
2020-09-29 12:03:39 +02:00
// RebaseMode returns "" for non-rebase mode, "normal" for normal rebase
// and "interactive" for interactive rebase
2022-01-02 01:34:33 +02:00
func (self *StatusCommands) RebaseMode() (enums.RebaseMode, error) {
exists, err := self.osCommand.FileExists(filepath.Join(self.dotGitDir, "rebase-apply"))
2020-09-29 12:03:39 +02:00
if err != nil {
2021-12-30 04:35:10 +02:00
return enums.REBASE_MODE_NONE, err
2020-09-29 12:03:39 +02:00
}
if exists {
2021-12-30 04:35:10 +02:00
return enums.REBASE_MODE_NORMAL, nil
2020-09-29 12:03:39 +02:00
}
2022-01-02 01:34:33 +02:00
exists, err = self.osCommand.FileExists(filepath.Join(self.dotGitDir, "rebase-merge"))
2020-09-29 12:03:39 +02:00
if exists {
2021-12-30 04:35:10 +02:00
return enums.REBASE_MODE_INTERACTIVE, err
2020-09-29 12:03:39 +02:00
} else {
2021-12-30 04:35:10 +02:00
return enums.REBASE_MODE_NONE, err
2020-09-29 12:03:39 +02:00
}
}
2022-01-02 01:34:33 +02:00
func (self *StatusCommands) WorkingTreeState() enums.RebaseMode {
rebaseMode, _ := self.RebaseMode()
2021-12-30 04:35:10 +02:00
if rebaseMode != enums.REBASE_MODE_NONE {
return enums.REBASE_MODE_REBASING
2020-09-29 12:03:39 +02:00
}
2022-01-02 01:34:33 +02:00
merging, _ := self.IsInMergeState()
2020-09-29 12:03:39 +02:00
if merging {
2021-12-30 04:35:10 +02:00
return enums.REBASE_MODE_MERGING
2020-09-29 12:03:39 +02:00
}
2021-12-30 04:35:10 +02:00
return enums.REBASE_MODE_NONE
2020-09-29 12:03:39 +02:00
}
// IsInMergeState states whether we are still mid-merge
2022-01-02 01:34:33 +02:00
func (self *StatusCommands) IsInMergeState() (bool, error) {
return self.osCommand.FileExists(filepath.Join(self.dotGitDir, "MERGE_HEAD"))
2020-09-29 12:03:39 +02:00
}
2022-01-02 01:34:33 +02:00
func (self *StatusCommands) IsBareRepo() bool {
2020-09-29 12:03:39 +02:00
// note: could use `git rev-parse --is-bare-repository` if we wanna drop go-git
2022-01-02 01:34:33 +02:00
_, err := self.repo.Worktree()
2020-09-29 12:03:39 +02:00
return err == gogit.ErrIsBareRepository
}