1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-18 05:17:55 +02:00

74 lines
1.8 KiB
Go
Raw Normal View History

2022-01-08 14:00:36 +11:00
package git_commands
2020-09-29 20:03:39 +10:00
import (
"path/filepath"
2022-08-15 13:59:34 +01:00
"strconv"
"strings"
2020-09-29 20:03:39 +10:00
2022-08-15 13:59:34 +01:00
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
2021-12-30 13:35:10 +11:00
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
2020-11-16 20:38:26 +11:00
)
2022-01-02 10:34:33 +11:00
type StatusCommands struct {
*GitCommon
2022-01-02 10:34:33 +11:00
}
func NewStatusCommands(
gitCommon *GitCommon,
2022-01-02 10:34:33 +11:00
) *StatusCommands {
return &StatusCommands{
GitCommon: gitCommon,
2022-01-02 10:34:33 +11:00
}
}
2020-09-29 20:03:39 +10:00
// RebaseMode returns "" for non-rebase mode, "normal" for normal rebase
// and "interactive" for interactive rebase
2022-01-02 10:34:33 +11:00
func (self *StatusCommands) RebaseMode() (enums.RebaseMode, error) {
exists, err := self.os.FileExists(filepath.Join(self.dotGitDir, "rebase-apply"))
2020-09-29 20:03:39 +10:00
if err != nil {
2021-12-30 13:35:10 +11:00
return enums.REBASE_MODE_NONE, err
2020-09-29 20:03:39 +10:00
}
if exists {
2021-12-30 13:35:10 +11:00
return enums.REBASE_MODE_NORMAL, nil
2020-09-29 20:03:39 +10:00
}
exists, err = self.os.FileExists(filepath.Join(self.dotGitDir, "rebase-merge"))
2020-09-29 20:03:39 +10:00
if exists {
2021-12-30 13:35:10 +11:00
return enums.REBASE_MODE_INTERACTIVE, err
2020-09-29 20:03:39 +10:00
} else {
2021-12-30 13:35:10 +11:00
return enums.REBASE_MODE_NONE, err
2020-09-29 20:03:39 +10:00
}
}
2022-01-02 10:34:33 +11:00
func (self *StatusCommands) WorkingTreeState() enums.RebaseMode {
rebaseMode, _ := self.RebaseMode()
2021-12-30 13:35:10 +11:00
if rebaseMode != enums.REBASE_MODE_NONE {
return enums.REBASE_MODE_REBASING
2020-09-29 20:03:39 +10:00
}
2022-01-02 10:34:33 +11:00
merging, _ := self.IsInMergeState()
2020-09-29 20:03:39 +10:00
if merging {
2021-12-30 13:35:10 +11:00
return enums.REBASE_MODE_MERGING
2020-09-29 20:03:39 +10:00
}
2021-12-30 13:35:10 +11:00
return enums.REBASE_MODE_NONE
2020-09-29 20:03:39 +10:00
}
2022-08-15 13:59:34 +01:00
func (self *StatusCommands) IsBareRepo() (bool, error) {
return IsBareRepo(self.os)
}
func IsBareRepo(osCommand *oscommands.OSCommand) (bool, error) {
res, err := osCommand.Cmd.New(
NewGitCmd("rev-parse").Arg("--is-bare-repository").ToArgv(),
).DontLog().RunWithOutput()
2022-08-15 13:59:34 +01:00
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 20:03:39 +10:00
// IsInMergeState states whether we are still mid-merge
2022-01-02 10:34:33 +11:00
func (self *StatusCommands) IsInMergeState() (bool, error) {
return self.os.FileExists(filepath.Join(self.dotGitDir, "MERGE_HEAD"))
2020-09-29 20:03:39 +10:00
}