1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2026-04-24 20:56:17 +02:00
Files
lazygit/pkg/gui/presentation/worktrees_test.go
T
Ruud Kamphuis 727b4b56dd Show branch name and detached HEAD in worktrees tab
The worktrees tab now displays the checked-out branch (or detached HEAD
state) for each worktree, making it much easier to see which worktree
holds which branch.

This is useful when you need to "unlock" a branch that's occupied by
another worktree: you can now clearly see which worktrees are on a
branch vs detached, without having to select each one individually.

Also rename the "(main)" label to "(main worktree)" to avoid confusion
with the common "main" branch name, and move it after the branch column.
2026-03-08 12:51:46 +01:00

112 lines
2.8 KiB
Go

package presentation
import (
"testing"
"github.com/gookit/color"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/stretchr/testify/assert"
"github.com/xo/terminfo"
)
func Test_GetWorktreeDisplayString(t *testing.T) {
tr := &i18n.TranslationSet{
MainWorktree: "(main worktree)",
MissingWorktree: "(missing)",
}
scenarios := []struct {
testName string
worktree *models.Worktree
expected []string
}{
{
testName: "Current worktree on a branch",
worktree: &models.Worktree{
Name: "my-worktree",
Branch: "my-branch",
IsCurrent: true,
},
expected: []string{" *", "my-worktree", "my-branch"},
},
{
testName: "Non-current worktree on a branch",
worktree: &models.Worktree{
Name: "my-worktree",
Branch: "feature-branch",
},
expected: []string{"", "my-worktree", "feature-branch"},
},
{
testName: "Main worktree on a branch",
worktree: &models.Worktree{
Name: "repo",
Branch: "main",
IsMain: true,
},
expected: []string{"", "repo", "main (main worktree)"},
},
{
testName: "Detached HEAD worktree",
worktree: &models.Worktree{
Name: "my-worktree",
Head: "d85cc9d281fa6ae1665c68365fc70e75e82a042d",
},
expected: []string{"", "my-worktree", "HEAD detached at d85cc9d2"},
},
{
testName: "Detached HEAD on main worktree",
worktree: &models.Worktree{
Name: "repo",
IsMain: true,
Head: "d85cc9d281fa6ae1665c68365fc70e75e82a042d",
},
expected: []string{"", "repo", "HEAD detached at d85cc9d2 (main worktree)"},
},
{
testName: "Worktree with branch takes precedence over head",
worktree: &models.Worktree{
Name: "my-worktree",
Branch: "my-branch",
Head: "d85cc9d281fa6ae1665c68365fc70e75e82a042d",
},
expected: []string{"", "my-worktree", "my-branch"},
},
{
testName: "Missing worktree",
worktree: &models.Worktree{
Name: "my-worktree",
IsPathMissing: true,
Head: "d85cc9d281fa6ae1665c68365fc70e75e82a042d",
},
expected: []string{"", "my-worktree (missing)", "HEAD detached at d85cc9d2"},
},
{
testName: "Worktree with no branch and no head",
worktree: &models.Worktree{
Name: "my-worktree",
},
expected: []string{"", "my-worktree", ""},
},
{
testName: "Main worktree with no branch and no head",
worktree: &models.Worktree{
Name: "my-worktree",
IsMain: true,
},
expected: []string{"", "my-worktree", " (main worktree)"},
},
}
oldColorLevel := color.ForceSetColorLevel(terminfo.ColorLevelNone)
defer color.ForceSetColorLevel(oldColorLevel)
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
result := GetWorktreeDisplayString(tr, s.worktree)
assert.Equal(t, s.expected, result)
})
}
}