From b1cef59a89bacb96fc4b3ad6f00a94b846145881 Mon Sep 17 00:00:00 2001 From: Arthur Albuquerque <20231694+troclaux@users.noreply.github.com> Date: Sun, 10 May 2026 21:26:10 -0300 Subject: [PATCH] fix: guard against nil remote branch pointers in display strings During concurrent remote refresh, stale nil pointers can appear in the branches slice, causing a panic in GetRemoteBranchListDisplayStrings when branch.FullName() is called. Skip nil entries with lo.FilterMap instead of lo.Map. Fixes #5370 --- pkg/gui/presentation/remote_branches.go | 7 +++++-- pkg/gui/presentation/remote_branches_test.go | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 pkg/gui/presentation/remote_branches_test.go diff --git a/pkg/gui/presentation/remote_branches.go b/pkg/gui/presentation/remote_branches.go index 55e4bc1137e..76980217cc2 100644 --- a/pkg/gui/presentation/remote_branches.go +++ b/pkg/gui/presentation/remote_branches.go @@ -8,9 +8,12 @@ import ( ) func GetRemoteBranchListDisplayStrings(branches []*models.RemoteBranch, diffName string) [][]string { - return lo.Map(branches, func(branch *models.RemoteBranch, _ int) []string { + return lo.FilterMap(branches, func(branch *models.RemoteBranch, _ int) ([]string, bool) { + if branch == nil { + return nil, false + } diffed := branch.FullName() == diffName - return getRemoteBranchDisplayStrings(branch, diffed) + return getRemoteBranchDisplayStrings(branch, diffed), true }) } diff --git a/pkg/gui/presentation/remote_branches_test.go b/pkg/gui/presentation/remote_branches_test.go new file mode 100644 index 00000000000..57143951039 --- /dev/null +++ b/pkg/gui/presentation/remote_branches_test.go @@ -0,0 +1,18 @@ +package presentation + +import ( + "testing" + + "github.com/jesseduffield/lazygit/pkg/commands/models" + "github.com/stretchr/testify/assert" +) + +func TestGetRemoteBranchListDisplayStrings_NilBranch(t *testing.T) { + branch := &models.RemoteBranch{Name: "main", RemoteName: "origin"} + branches := []*models.RemoteBranch{nil, branch, nil} + + result := GetRemoteBranchListDisplayStrings(branches, "") + + // nil entries must be skipped; only the valid branch produces a row + assert.Len(t, result, 1) +}