2020-09-29 20:03:39 +10:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
|
2020-10-06 20:50:54 +11:00
|
|
|
gogit "github.com/jesseduffield/go-git/v5"
|
2020-09-29 20:03:39 +10:00
|
|
|
)
|
|
|
|
|
2021-12-30 13:11:58 +11:00
|
|
|
type RebaseMode int
|
2021-12-30 12:10:09 +11:00
|
|
|
|
2020-11-16 20:38:26 +11:00
|
|
|
const (
|
2021-12-30 13:11:58 +11:00
|
|
|
// this means we're neither rebasing nor merging
|
|
|
|
REBASE_MODE_NONE RebaseMode = iota
|
|
|
|
// this means normal rebase as opposed to interactive rebase
|
|
|
|
REBASE_MODE_NORMAL
|
|
|
|
REBASE_MODE_INTERACTIVE
|
|
|
|
// REBASE_MODE_REBASING is a general state that captures both REBASE_MODE_NORMAL and REBASE_MODE_INTERACTIVE
|
|
|
|
REBASE_MODE_REBASING
|
|
|
|
REBASE_MODE_MERGING
|
2020-11-16 20:38:26 +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
|
2021-12-30 12:10:09 +11:00
|
|
|
func (c *GitCommand) RebaseMode() (RebaseMode, error) {
|
2020-09-29 20:03:39 +10:00
|
|
|
exists, err := c.OSCommand.FileExists(filepath.Join(c.DotGitDir, "rebase-apply"))
|
|
|
|
if err != nil {
|
2021-12-30 13:11:58 +11:00
|
|
|
return REBASE_MODE_NONE, err
|
2020-09-29 20:03:39 +10:00
|
|
|
}
|
|
|
|
if exists {
|
2020-11-16 20:38:26 +11:00
|
|
|
return REBASE_MODE_NORMAL, nil
|
2020-09-29 20:03:39 +10:00
|
|
|
}
|
|
|
|
exists, err = c.OSCommand.FileExists(filepath.Join(c.DotGitDir, "rebase-merge"))
|
|
|
|
if exists {
|
2020-11-16 20:38:26 +11:00
|
|
|
return REBASE_MODE_INTERACTIVE, err
|
2020-09-29 20:03:39 +10:00
|
|
|
} else {
|
2021-12-30 13:11:58 +11:00
|
|
|
return REBASE_MODE_NONE, err
|
2020-09-29 20:03:39 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-30 12:10:09 +11:00
|
|
|
func (c *GitCommand) WorkingTreeState() RebaseMode {
|
2020-09-29 20:03:39 +10:00
|
|
|
rebaseMode, _ := c.RebaseMode()
|
2021-12-30 13:11:58 +11:00
|
|
|
if rebaseMode != REBASE_MODE_NONE {
|
2020-11-16 20:38:26 +11:00
|
|
|
return REBASE_MODE_REBASING
|
2020-09-29 20:03:39 +10:00
|
|
|
}
|
|
|
|
merging, _ := c.IsInMergeState()
|
|
|
|
if merging {
|
2020-11-16 20:38:26 +11:00
|
|
|
return REBASE_MODE_MERGING
|
2020-09-29 20:03:39 +10:00
|
|
|
}
|
2021-12-30 13:11:58 +11:00
|
|
|
return REBASE_MODE_NONE
|
2020-09-29 20:03:39 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsInMergeState states whether we are still mid-merge
|
|
|
|
func (c *GitCommand) IsInMergeState() (bool, error) {
|
|
|
|
return c.OSCommand.FileExists(filepath.Join(c.DotGitDir, "MERGE_HEAD"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *GitCommand) IsBareRepo() bool {
|
|
|
|
// note: could use `git rev-parse --is-bare-repository` if we wanna drop go-git
|
|
|
|
_, err := c.Repo.Worktree()
|
|
|
|
return err == gogit.ErrIsBareRepository
|
|
|
|
}
|