1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-10 04:07:18 +02:00
lazygit/pkg/commands/git_commands/status.go

72 lines
1.8 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"
2022-08-15 14:59:34 +02:00
"strconv"
"strings"
2020-09-29 12:03:39 +02:00
2022-08-15 14:59:34 +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"
2020-11-16 11:38:26 +02:00
)
2022-01-02 01:34:33 +02:00
type StatusCommands struct {
*GitCommon
2022-01-02 01:34:33 +02:00
}
func NewStatusCommands(
gitCommon *GitCommon,
2022-01-02 01:34:33 +02:00
) *StatusCommands {
return &StatusCommands{
GitCommon: gitCommon,
2022-01-02 01:34:33 +02:00
}
}
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.os.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
}
exists, err = self.os.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
}
2022-08-15 14:59:34 +02:00
func (self *StatusCommands) IsBareRepo() (bool, error) {
return IsBareRepo(self.os)
}
func IsBareRepo(osCommand *oscommands.OSCommand) (bool, error) {
res, err := osCommand.Cmd.New("git rev-parse --is-bare-repository").DontLog().RunWithOutput()
if err != nil {
return false, err
}
// The command returns output with a newline, so we need to strip
return strconv.ParseBool(strings.TrimSpace(res))
}
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.os.FileExists(filepath.Join(self.dotGitDir, "MERGE_HEAD"))
2020-09-29 12:03:39 +02:00
}