summaryrefslogtreecommitdiffstats
path: root/pkg/commands
diff options
context:
space:
mode:
authorJoel Baranick <joel.baranick@ensighten.com>2022-09-02 09:35:08 -0700
committerJesse Duffield <jessedduffield@gmail.com>2023-07-30 18:35:21 +1000
commitc679fd1924e9669a30d038fbdd164d948393c301 (patch)
treedac9a92eb38ee225d90c98446bf16067051e6c8b /pkg/commands
parent9a79154d05a81852a36ed336b6927ef60f835d6b (diff)
Style missing worktree as red and display better error when trying to switch to them
Use a broken link icon for missing worktrees
Diffstat (limited to 'pkg/commands')
-rw-r--r--pkg/commands/git_commands/worktree_loader.go29
-rw-r--r--pkg/commands/models/worktree.go44
2 files changed, 41 insertions, 32 deletions
diff --git a/pkg/commands/git_commands/worktree_loader.go b/pkg/commands/git_commands/worktree_loader.go
index 81df84398..8dc7dcbe5 100644
--- a/pkg/commands/git_commands/worktree_loader.go
+++ b/pkg/commands/git_commands/worktree_loader.go
@@ -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,
}
}
}
diff --git a/pkg/commands/models/worktree.go b/pkg/commands/models/worktree.go
index f38c67e07..c4e31a9d3 100644
--- a/pkg/commands/models/worktree.go
+++ b/pkg/commands/models/worktree.go
@@ -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
+}