Skip to content

Commit 2c7fe1f

Browse files
committed
Show worktree name next to branch in branches list
When a branch is checked out by another worktree, show the worktree name in the label, e.g. "(worktree cosmos2)" instead of just "(worktree)", so you can immediately see which worktree holds it.
1 parent 9046d5e commit 2c7fe1f

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

pkg/gui/presentation/branches.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ func getBranchDisplayStrings(
5757
showCommitHash := fullDescription || userConfig.Gui.ShowBranchCommitHash
5858
branchStatus := BranchStatus(b, itemOperation, tr, now, userConfig)
5959
divergence := divergenceStr(b, itemOperation, tr, userConfig)
60-
worktreeIcon := lo.Ternary(icons.IsIconEnabled(), icons.LINKED_WORKTREE_ICON, fmt.Sprintf("(%s)", tr.LcWorktree))
60+
61+
worktreeIcon := ""
62+
if checkedOutByWorkTree {
63+
if icons.IsIconEnabled() {
64+
worktreeIcon = icons.LINKED_WORKTREE_ICON
65+
} else if wt, ok := git_commands.WorktreeForBranch(b, worktrees); ok {
66+
worktreeIcon = fmt.Sprintf("(%s %s)", tr.LcWorktree, wt.Name)
67+
}
68+
}
6169

6270
// Recency is always three characters, plus one for the space
6371
availableWidth := viewWidth - 4

pkg/gui/presentation/branches_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func Test_getBranchDisplayStrings(t *testing.T) {
6262
useIcons: false,
6363
checkedOutByWorktree: true,
6464
showDivergenceCfg: "none",
65-
expected: []string{"1m", "branch_name (worktree)"},
65+
expected: []string{"1m", "branch_name (worktree other-worktree)"},
6666
},
6767
{
6868
branch: &models.Branch{Name: "branch_name", Recency: "1m"},
@@ -104,7 +104,7 @@ func Test_getBranchDisplayStrings(t *testing.T) {
104104
useIcons: false,
105105
checkedOutByWorktree: true,
106106
showDivergenceCfg: "none",
107-
expected: []string{"1m", "branch_name (worktree) ↓5↑3"},
107+
expected: []string{"1m", "branch_name (worktree other-worktree) ↓5↑3"},
108108
},
109109
{
110110
branch: &models.Branch{
@@ -213,7 +213,7 @@ func Test_getBranchDisplayStrings(t *testing.T) {
213213
useIcons: false,
214214
checkedOutByWorktree: true,
215215
showDivergenceCfg: "none",
216-
expected: []string{"1m", "bra… (worktree)"},
216+
expected: []string{"1m", "bra… (worktree other-worktree)"},
217217
},
218218
{
219219
branch: &models.Branch{Name: "branch_name", Recency: "1m"},
@@ -272,7 +272,7 @@ func Test_getBranchDisplayStrings(t *testing.T) {
272272
useIcons: false,
273273
checkedOutByWorktree: true,
274274
showDivergenceCfg: "none",
275-
expected: []string{"1m", "branch_na… (worktree) ↓5↑3"},
275+
expected: []string{"1m", "bra… (worktree other-worktree) ↓5↑3"},
276276
},
277277
{
278278
branch: &models.Branch{Name: "branch_name", Recency: "1m"},
@@ -347,7 +347,7 @@ func Test_getBranchDisplayStrings(t *testing.T) {
347347

348348
worktrees := []*models.Worktree{}
349349
if s.checkedOutByWorktree {
350-
worktrees = append(worktrees, &models.Worktree{Branch: s.branch.Name})
350+
worktrees = append(worktrees, &models.Worktree{Branch: s.branch.Name, Name: "other-worktree"})
351351
}
352352

353353
t.Run(fmt.Sprintf("getBranchDisplayStrings_%d", i), func(t *testing.T) {

0 commit comments

Comments
 (0)