2022-09-01 20:25:41 +02:00
|
|
|
package git_commands
|
|
|
|
|
|
|
|
import (
|
2022-09-03 05:08:36 +02:00
|
|
|
"path/filepath"
|
2022-09-01 20:25:41 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
type WorktreeLoader struct {
|
|
|
|
*common.Common
|
|
|
|
cmd oscommands.ICmdObjBuilder
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWorktreeLoader(
|
|
|
|
common *common.Common,
|
|
|
|
cmd oscommands.ICmdObjBuilder,
|
|
|
|
) *WorktreeLoader {
|
|
|
|
return &WorktreeLoader{
|
|
|
|
Common: common,
|
|
|
|
cmd: cmd,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *WorktreeLoader) GetWorktrees() ([]*models.Worktree, error) {
|
|
|
|
cmdArgs := NewGitCmd("worktree").Arg("list", "--porcelain", "-z").ToArgv()
|
|
|
|
worktreesOutput, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
splitLines := strings.Split(worktreesOutput, "\x00")
|
|
|
|
|
|
|
|
var worktrees []*models.Worktree
|
|
|
|
var currentWorktree *models.Worktree
|
|
|
|
for _, splitLine := range splitLines {
|
|
|
|
if len(splitLine) == 0 && currentWorktree != nil {
|
|
|
|
worktrees = append(worktrees, currentWorktree)
|
|
|
|
currentWorktree = nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(splitLine, "worktree ") {
|
2022-09-02 18:07:24 +02:00
|
|
|
path := strings.SplitN(splitLine, " ", 2)[1]
|
2022-09-01 20:25:41 +02:00
|
|
|
currentWorktree = &models.Worktree{
|
2023-07-16 03:36:50 +02:00
|
|
|
IsMain: len(worktrees) == 0,
|
|
|
|
Path: path,
|
2022-09-01 20:25:41 +02:00
|
|
|
}
|
2022-09-03 05:08:36 +02:00
|
|
|
} else if strings.HasPrefix(splitLine, "branch ") {
|
|
|
|
branch := strings.SplitN(splitLine, " ", 2)[1]
|
|
|
|
currentWorktree.Branch = filepath.Base(branch)
|
2022-09-01 20:25:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return worktrees, nil
|
|
|
|
}
|