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

91 lines
2.5 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 (
2023-07-12 08:47:50 +02:00
"os"
2020-09-29 12:03:39 +02:00
"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) {
ok, err := self.IsInNormalRebase()
if err == nil && ok {
2021-12-30 04:35:10 +02:00
return enums.REBASE_MODE_NORMAL, nil
2020-09-29 12:03:39 +02:00
}
ok, err = self.IsInInteractiveRebase()
if err == nil && ok {
2021-12-30 04:35:10 +02:00
return enums.REBASE_MODE_INTERACTIVE, err
2020-09-29 12:03:39 +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(
NewGitCmd("rev-parse").Arg("--is-bare-repository").ToArgv(),
).DontLog().RunWithOutput()
2022-08-15 14:59:34 +02: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))
}
func (self *StatusCommands) IsInNormalRebase() (bool, error) {
return self.os.FileExists(filepath.Join(self.repoPaths.WorktreeGitDirPath(), "rebase-apply"))
}
func (self *StatusCommands) IsInInteractiveRebase() (bool, error) {
return self.os.FileExists(filepath.Join(self.repoPaths.WorktreeGitDirPath(), "rebase-merge"))
}
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.repoPaths.WorktreeGitDirPath(), "MERGE_HEAD"))
2020-09-29 12:03:39 +02:00
}
2023-07-12 08:47:50 +02:00
// 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 {
for _, dir := range []string{"rebase-merge", "rebase-apply"} {
if bytesContent, err := os.ReadFile(filepath.Join(self.repoPaths.WorktreeGitDirPath(), dir, "head-name")); err == nil {
return strings.TrimSpace(string(bytesContent))
}
}
return ""
}