From 9a79154d05a81852a36ed336b6927ef60f835d6b Mon Sep 17 00:00:00 2001 From: Joel Baranick Date: Fri, 2 Sep 2022 09:07:24 -0700 Subject: [PATCH] Hide worktrees in the worktree panel if they point at a non-existing filesystem location. Remove unneeded check when filtering out branches from non-current worktrees from the branch panel. Add link icon for linked worktrees --- pkg/commands/git_commands/branch_loader.go | 4 +++- pkg/commands/git_commands/worktree_loader.go | 12 ++++++++-- pkg/gui/presentation/icons/git_icons.go | 23 +++++++++++--------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/pkg/commands/git_commands/branch_loader.go b/pkg/commands/git_commands/branch_loader.go index bab24bfa7..4b88611eb 100644 --- a/pkg/commands/git_commands/branch_loader.go +++ b/pkg/commands/git_commands/branch_loader.go @@ -144,8 +144,10 @@ func (self *BranchLoader) obtainBranches() []*models.Branch { return nil, false } - if len(split[6]) > 0 && split[6] != currentDir { + branchDir := split[6] + if len(branchDir) > 0 && branchDir != currentDir { // Ignore line because it is a branch checked out in a different worktree + // Branches which are not checked out will not have a path, so we should not ignore them. return nil, false } diff --git a/pkg/commands/git_commands/worktree_loader.go b/pkg/commands/git_commands/worktree_loader.go index 905754540..81df84398 100644 --- a/pkg/commands/git_commands/worktree_loader.go +++ b/pkg/commands/git_commands/worktree_loader.go @@ -1,6 +1,8 @@ package git_commands import ( + "errors" + "io/fs" "os" "path/filepath" "strings" @@ -43,20 +45,26 @@ func (self *WorktreeLoader) GetWorktrees() ([]*models.Worktree, error) { 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 ") { + 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" - path := strings.SplitN(splitLine, " ", 2)[1] if len(worktrees) == 0 { main = true } else { name = filepath.Base(path) } + currentWorktree = &models.Worktree{ Name: name, Path: path, diff --git a/pkg/gui/presentation/icons/git_icons.go b/pkg/gui/presentation/icons/git_icons.go index 0111ca2b5..6b5689295 100644 --- a/pkg/gui/presentation/icons/git_icons.go +++ b/pkg/gui/presentation/icons/git_icons.go @@ -7,14 +7,14 @@ import ( ) var ( - BRANCH_ICON = "\U000f062c" // 󰘬 - DETACHED_HEAD_ICON = "\ue729" //  - TAG_ICON = "\uf02b" //  - COMMIT_ICON = "\U000f0718" // 󰜘 - MERGE_COMMIT_ICON = "\U000f062d" // 󰘭 - DEFAULT_REMOTE_ICON = "\uf02a2" // 󰊢 - STASH_ICON = "\uf01c" //  - WORKTREE_ICON = "\uf02b" //  + BRANCH_ICON = "\U000f062c" // 󰘬 + DETACHED_HEAD_ICON = "\ue729" //  + TAG_ICON = "\uf02b" //  + COMMIT_ICON = "\U000f0718" // 󰜘 + MERGE_COMMIT_ICON = "\U000f062d" // 󰘭 + DEFAULT_REMOTE_ICON = "\uf02a2" // 󰊢 + STASH_ICON = "\uf01c" //  + LINKED_WORKTREE_ICON = "\uf838" //  ) var remoteIcons = map[string]string{ @@ -70,6 +70,9 @@ func IconForStash(stash *models.StashEntry) string { return STASH_ICON } -func IconForWorktree(tag *models.Worktree) string { - return WORKTREE_ICON +func IconForWorktree(worktree *models.Worktree) string { + if worktree.Main { + return "" + } + return LINKED_WORKTREE_ICON }