mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-28 16:02:01 +03:00
Style missing worktree as red and display better error when trying to switch to them
Use a broken link icon for missing worktrees
This commit is contained in:
committed by
Jesse Duffield
parent
9a79154d05
commit
c679fd1924
@ -1,10 +1,6 @@
|
||||
package git_commands
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
||||
@ -28,11 +24,6 @@ func NewWorktreeLoader(
|
||||
}
|
||||
|
||||
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 {
|
||||
@ -51,25 +42,9 @@ func (self *WorktreeLoader) GetWorktrees() ([]*models.Worktree, error) {
|
||||
}
|
||||
if strings.HasPrefix(splitLine, "worktree ") {
|
||||
path := strings.SplitN(splitLine, " ", 2)[1]
|
||||
|
||||
if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) {
|
||||
// Ignore because the worktree is points to a non-existing filesystem location
|
||||
continue
|
||||
}
|
||||
|
||||
main := false
|
||||
name := "main"
|
||||
if len(worktrees) == 0 {
|
||||
main = true
|
||||
} else {
|
||||
name = filepath.Base(path)
|
||||
}
|
||||
|
||||
currentWorktree = &models.Worktree{
|
||||
Name: name,
|
||||
Path: path,
|
||||
Main: main,
|
||||
Current: path == currentDir,
|
||||
Id: len(worktrees),
|
||||
Path: path,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,22 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/go-errors/errors"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Worktree : A git worktree
|
||||
type Worktree struct {
|
||||
Name string
|
||||
Main bool
|
||||
Current bool
|
||||
Path string
|
||||
Id int
|
||||
Path string
|
||||
}
|
||||
|
||||
func (w *Worktree) RefName() string {
|
||||
return w.Name
|
||||
return w.Name()
|
||||
}
|
||||
|
||||
func (w *Worktree) ID() string {
|
||||
@ -19,3 +26,30 @@ func (w *Worktree) ID() string {
|
||||
func (w *Worktree) Description() string {
|
||||
return w.RefName()
|
||||
}
|
||||
|
||||
func (w *Worktree) Name() string {
|
||||
return filepath.Base(w.Path)
|
||||
}
|
||||
|
||||
func (w *Worktree) Main() bool {
|
||||
return w.Id == 0
|
||||
}
|
||||
|
||||
func (w *Worktree) Current() bool {
|
||||
pwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
log.Fatalln(err.Error())
|
||||
}
|
||||
|
||||
return pwd == w.Path
|
||||
}
|
||||
|
||||
func (w *Worktree) Missing() bool {
|
||||
if _, err := os.Stat(w.Path); err != nil {
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return true
|
||||
}
|
||||
log.Fatalln(fmt.Errorf("failed to check if worktree path `%s` exists\n%w", w.Path, err).Error())
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
Reference in New Issue
Block a user