mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-28 16:02:01 +03:00
Remove IO logic from presentation code for worktrees
We're doing all the IO in our workers loader method so that we don't need to do any in our presentation code
This commit is contained in:
@ -65,7 +65,7 @@ func (self *FileLoader) GetStatusFiles(opts GetStatusFileOptions) []*models.File
|
||||
files = append(files, file)
|
||||
}
|
||||
|
||||
// Go through the worktrees to see if any of these files are actually worktrees
|
||||
// Go through the files to see if any of these files are actually worktrees
|
||||
// so that we can render them correctly
|
||||
worktreePaths := linkedWortkreePaths()
|
||||
for _, file := range files {
|
||||
|
@ -1,7 +1,6 @@
|
||||
package git_commands
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
@ -59,36 +58,6 @@ func (self *WorktreeCommands) Detach(worktreePath string) error {
|
||||
return self.cmd.New(cmdArgs).Run()
|
||||
}
|
||||
|
||||
func (self *WorktreeCommands) IsCurrentWorktree(path string) bool {
|
||||
return IsCurrentWorktree(path)
|
||||
}
|
||||
|
||||
func IsCurrentWorktree(path string) bool {
|
||||
pwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return EqualPath(pwd, path)
|
||||
}
|
||||
|
||||
func (self *WorktreeCommands) IsWorktreePathMissing(path string) bool {
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return true
|
||||
}
|
||||
self.Log.Errorf("failed to check if worktree path `%s` exists\n%v", path, err)
|
||||
return false
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// checks if two paths are equal
|
||||
// TODO: support relative paths
|
||||
func EqualPath(a string, b string) bool {
|
||||
return a == b
|
||||
}
|
||||
|
||||
func WorktreeForBranch(branch *models.Branch, worktrees []*models.Worktree) (*models.Worktree, bool) {
|
||||
for _, worktree := range worktrees {
|
||||
if worktree.Branch == branch.Name {
|
||||
@ -105,7 +74,7 @@ func CheckedOutByOtherWorktree(branch *models.Branch, worktrees []*models.Worktr
|
||||
return false
|
||||
}
|
||||
|
||||
return !IsCurrentWorktree(worktree.Path)
|
||||
return !worktree.IsCurrent
|
||||
}
|
||||
|
||||
// If in a non-bare repo, this returns the path of the main worktree
|
||||
|
@ -1,10 +1,12 @@
|
||||
package git_commands
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/go-errors/errors"
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
||||
"github.com/jesseduffield/lazygit/pkg/common"
|
||||
@ -30,6 +32,11 @@ func NewWorktreeLoader(
|
||||
func (self *WorktreeLoader) GetWorktrees() ([]*models.Worktree, error) {
|
||||
currentRepoPath := GetCurrentRepoPath()
|
||||
|
||||
pwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cmdArgs := NewGitCmd("worktree").Arg("list", "--porcelain").ToArgv()
|
||||
worktreesOutput, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
|
||||
if err != nil {
|
||||
@ -54,6 +61,8 @@ func (self *WorktreeLoader) GetWorktrees() ([]*models.Worktree, error) {
|
||||
if strings.HasPrefix(splitLine, "worktree ") {
|
||||
path := strings.SplitN(splitLine, " ", 2)[1]
|
||||
isMain := path == currentRepoPath
|
||||
isCurrent := path == pwd
|
||||
isPathMissing := self.pathExists(path)
|
||||
|
||||
var gitDir string
|
||||
if isMain {
|
||||
@ -67,9 +76,11 @@ func (self *WorktreeLoader) GetWorktrees() ([]*models.Worktree, error) {
|
||||
}
|
||||
|
||||
current = &models.Worktree{
|
||||
IsMain: path == currentRepoPath,
|
||||
Path: path,
|
||||
GitDir: gitDir,
|
||||
IsMain: isMain,
|
||||
IsCurrent: isCurrent,
|
||||
IsPathMissing: isPathMissing,
|
||||
Path: path,
|
||||
GitDir: gitDir,
|
||||
}
|
||||
} else if strings.HasPrefix(splitLine, "branch ") {
|
||||
branch := strings.SplitN(splitLine, " ", 2)[1]
|
||||
@ -85,14 +96,9 @@ func (self *WorktreeLoader) GetWorktrees() ([]*models.Worktree, error) {
|
||||
worktree.NameField = names[index]
|
||||
}
|
||||
|
||||
pwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// move current worktree to the top
|
||||
for i, worktree := range worktrees {
|
||||
if EqualPath(worktree.Path, pwd) {
|
||||
if worktree.IsCurrent {
|
||||
worktrees = append(worktrees[:i], worktrees[i+1:]...)
|
||||
worktrees = append([]*models.Worktree{worktree}, worktrees...)
|
||||
break
|
||||
@ -130,6 +136,17 @@ func (self *WorktreeLoader) GetWorktrees() ([]*models.Worktree, error) {
|
||||
return worktrees, nil
|
||||
}
|
||||
|
||||
func (self *WorktreeLoader) pathExists(path string) bool {
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return true
|
||||
}
|
||||
self.Log.Errorf("failed to check if worktree path `%s` exists\n%v", path, err)
|
||||
return false
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func rebaseBranch(worktree *models.Worktree) (string, bool) {
|
||||
for _, dir := range []string{"rebase-merge", "rebase-apply"} {
|
||||
if bytesContent, err := os.ReadFile(filepath.Join(worktree.GitDir, dir, "head-name")); err == nil {
|
||||
|
Reference in New Issue
Block a user