From 9bd4ce751f972d26a0857683060d6734c7d4b0e2 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 21 Jan 2025 21:00:24 +0100 Subject: [PATCH] Fix possible crash when deleting a branch while filtering is active The code that tries to reselect the same branch again uses GetItems, which in case of filtering is the filtered list. After replacing the branches slice with a new one, the filtered list is no longer up to date, so we must reapply the filter before working with it. It so happens that refreshView does that, so simply call that before setting the selection again; I don't think the order matters in this case. Otherwise we'd have to insert another call to ReApplyFilter before the call to GetItems, which we can avoid this way. Note that this doesn't actually make anything work better in the case of deleting a branch, since we can't reselect the deleted branch anyway of course. But it avoids a possible crash if the branch that was deleted was the last one in the unfiltered list. --- pkg/gui/controllers/helpers/refresh_helper.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/gui/controllers/helpers/refresh_helper.go b/pkg/gui/controllers/helpers/refresh_helper.go index 46965bddd25..270e552dc5f 100644 --- a/pkg/gui/controllers/helpers/refresh_helper.go +++ b/pkg/gui/controllers/helpers/refresh_helper.go @@ -490,6 +490,8 @@ func (self *RefreshHelper) refreshBranches(refreshWorktrees bool, keepBranchSele self.refreshView(self.c.Contexts().Worktrees) } + self.refreshView(self.c.Contexts().Branches) + if !keepBranchSelectionIndex && prevSelectedBranch != nil { _, idx, found := lo.FindIndexOf(self.c.Contexts().Branches.GetItems(), func(b *models.Branch) bool { return b.Name == prevSelectedBranch.Name }) @@ -498,8 +500,6 @@ func (self *RefreshHelper) refreshBranches(refreshWorktrees bool, keepBranchSele } } - self.refreshView(self.c.Contexts().Branches) - // Need to re-render the commits view because the visualization of local // branch heads might have changed self.c.Mutexes().LocalCommitsMutex.Lock()