Skip to content

Commit a4290fa

Browse files
committed
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.
1 parent 21b8b28 commit a4290fa

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

pkg/gui/controllers/helpers/refresh_helper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,6 @@ func (self *RefreshHelper) refreshBranches(refreshWorktrees bool, keepBranchSele
448448
self.c.Mutexes().RefreshingBranchesMutex.Lock()
449449
defer self.c.Mutexes().RefreshingBranchesMutex.Unlock()
450450

451-
prevSelectedBranch := self.c.Contexts().Branches.GetSelected()
452-
453451
reflogCommits := self.c.Model().FilteredReflogCommits
454452
if self.c.Modes().Filtering.Active() && self.c.AppState.LocalBranchSortOrder == "recency" {
455453
// 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
484482
self.c.Log.Error(err)
485483
}
486484

485+
prevSelectedBranch := self.c.Contexts().Branches.GetSelected()
486+
487487
self.c.Model().Branches = branches
488488

489489
if refreshWorktrees {

0 commit comments

Comments
 (0)