From fce4816a0d11b82444fc6f69f858a39023b19c2d Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Mon, 26 May 2025 14:17:01 +0200 Subject: [PATCH] Fix branch selection jumping back on background fetch When refreshing the branches list, we have code to keep the same branch selected even when the refresh changes the sort order; this code remembers the selected branch before the refresh, and then tries to select it again afterwards (looking it up by name) if it is still there. However, we stored the previously selected branch too early, before even obtaining the branches list; if the user moved the selection between that point and the end of the refresh, it would jump back. Fix this by remembering the previous selection only at the last moment, right before assigning the new branches slice. We still have a race condition here between the UI code that manages the selection as the user presses arrow keys, and the background thread doing the refresh that reads and restores the selection; however, the race was there before, and we make it neither better nor worse with this PR. It doesn't seem to be a problem in practice. --- 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 64bed0713..0e19149c1 100644 --- a/pkg/gui/controllers/helpers/refresh_helper.go +++ b/pkg/gui/controllers/helpers/refresh_helper.go @@ -448,8 +448,6 @@ func (self *RefreshHelper) refreshBranches(refreshWorktrees bool, keepBranchSele self.c.Mutexes().RefreshingBranchesMutex.Lock() defer self.c.Mutexes().RefreshingBranchesMutex.Unlock() - prevSelectedBranch := self.c.Contexts().Branches.GetSelected() - reflogCommits := self.c.Model().FilteredReflogCommits if self.c.Modes().Filtering.Active() && self.c.AppState.LocalBranchSortOrder == "recency" { // in filter mode we filter our reflog commits to just those containing the path @@ -484,6 +482,8 @@ func (self *RefreshHelper) refreshBranches(refreshWorktrees bool, keepBranchSele self.c.Log.Error(err) } + prevSelectedBranch := self.c.Contexts().Branches.GetSelected() + self.c.Model().Branches = branches if refreshWorktrees {