mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-06-15 00:15:32 +02:00
Initial addition of support for worktrees
This commit is contained in:
committed by
Jesse Duffield
parent
52447e5d46
commit
f8ba899b87
@ -2,6 +2,7 @@ package git_commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
@ -117,6 +118,11 @@ outer:
|
||||
}
|
||||
|
||||
func (self *BranchLoader) obtainBranches() []*models.Branch {
|
||||
currentDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
output, err := self.getRawBranches()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -138,6 +144,11 @@ func (self *BranchLoader) obtainBranches() []*models.Branch {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if len(split[6]) > 0 && split[6] != currentDir {
|
||||
// Ignore line because it is a branch checked out in a different worktree
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return obtainBranch(split), true
|
||||
})
|
||||
}
|
||||
@ -166,6 +177,7 @@ var branchFields = []string{
|
||||
"upstream:track",
|
||||
"subject",
|
||||
fmt.Sprintf("objectname:short=%d", utils.COMMIT_HASH_SHORT_SIZE),
|
||||
"worktreepath",
|
||||
}
|
||||
|
||||
// Obtain branch information from parsed line output of getRawBranches()
|
||||
|
80
pkg/commands/git_commands/worktree_loader.go
Normal file
80
pkg/commands/git_commands/worktree_loader.go
Normal file
@ -0,0 +1,80 @@
|
||||
package git_commands
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"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) {
|
||||
currentDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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 ") {
|
||||
main := false
|
||||
name := "main"
|
||||
path := strings.SplitN(splitLine, " ", 2)[1]
|
||||
if len(worktrees) == 0 {
|
||||
main = true
|
||||
} else {
|
||||
name = filepath.Base(path)
|
||||
}
|
||||
currentWorktree = &models.Worktree{
|
||||
Name: name,
|
||||
Path: path,
|
||||
Main: main,
|
||||
Current: path == currentDir,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
worktree /Users/jbaranick/Source/lazygit
|
||||
HEAD f6d6b5dec0432ffa953611700ab9b1ff0089f948
|
||||
branch refs/heads/worktree_support
|
||||
|
||||
worktree /Users/jbaranick/Source/lazygit/.worktrees/worktree_tests
|
||||
HEAD f6d6b5dec0432ffa953611700ab9b1ff0089f948
|
||||
branch refs/heads/worktree_tests
|
||||
*/
|
||||
|
||||
return worktrees, nil
|
||||
}
|
Reference in New Issue
Block a user