From 830c48f7687cd59609ebc114eda5dff95e24eeb1 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 26 Sep 2023 17:16:11 +0200 Subject: [PATCH] Avoid rendering branches view twice when refreshing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refreshWorktrees re-renders the branches view, because the branches view shows worktrees against branches. This means that when both BRANCHES and WORKTREES are requested to be refreshed, the branches view would be rendered twice in short succession. This causes an ugly visual glitch when force-pushing a branch, because when pushing is done, we would see the ↑4↓9 status come back from under the Pushing status for a brief moment, to be replaced with a green checkmark a moment later. Fix this by including the worktree refresh in the branches refresh when both are requested. This means that the two are no longer running in parallel for an async refresh, but hopefully that's not so bad. --- pkg/gui/controllers/helpers/refresh_helper.go | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/pkg/gui/controllers/helpers/refresh_helper.go b/pkg/gui/controllers/helpers/refresh_helper.go index 51a9e66a9..ca514e935 100644 --- a/pkg/gui/controllers/helpers/refresh_helper.go +++ b/pkg/gui/controllers/helpers/refresh_helper.go @@ -115,12 +115,15 @@ func (self *RefreshHelper) Refresh(options types.RefreshOptions) error { } } + includeWorktreesWithBranches := false if scopeSet.Includes(types.COMMITS) || scopeSet.Includes(types.BRANCHES) || scopeSet.Includes(types.REFLOG) || scopeSet.Includes(types.BISECT_INFO) { // whenever we change commits, we should update branches because the upstream/downstream // counts can change. Whenever we change branches we should also change commits // e.g. in the case of switching branches. refresh("commits and commit files", self.refreshCommitsAndCommitFiles) - refresh("reflog and branches", self.refreshReflogAndBranches) + + includeWorktreesWithBranches = scopeSet.Includes(types.WORKTREES) + refresh("reflog and branches", func() { self.refreshReflogAndBranches(includeWorktreesWithBranches) }) } else if scopeSet.Includes(types.REBASE_COMMITS) { // the above block handles rebase commits so we only need to call this one // if we've asked specifically for rebase commits and not those other things @@ -157,7 +160,7 @@ func (self *RefreshHelper) Refresh(options types.RefreshOptions) error { refresh("remotes", func() { _ = self.refreshRemotes() }) } - if scopeSet.Includes(types.WORKTREES) { + if scopeSet.Includes(types.WORKTREES) && !includeWorktreesWithBranches { refresh("worktrees", func() { _ = self.refreshWorktrees() }) } @@ -242,7 +245,7 @@ func (self *RefreshHelper) refreshReflogCommitsConsideringStartup() { case types.INITIAL: self.c.OnWorker(func(_ gocui.Task) { _ = self.refreshReflogCommits() - self.refreshBranches() + self.refreshBranches(false) self.c.State().GetRepoState().SetStartupStage(types.COMPLETE) }) @@ -251,10 +254,10 @@ func (self *RefreshHelper) refreshReflogCommitsConsideringStartup() { } } -func (self *RefreshHelper) refreshReflogAndBranches() { +func (self *RefreshHelper) refreshReflogAndBranches(refreshWorktrees bool) { self.refreshReflogCommitsConsideringStartup() - self.refreshBranches() + self.refreshBranches(refreshWorktrees) } func (self *RefreshHelper) refreshCommitsAndCommitFiles() { @@ -419,7 +422,7 @@ func (self *RefreshHelper) refreshStateSubmoduleConfigs() error { // self.refreshStatus is called at the end of this because that's when we can // be sure there is a State.Model.Branches array to pick the current branch from -func (self *RefreshHelper) refreshBranches() { +func (self *RefreshHelper) refreshBranches(refreshWorktrees bool) { self.c.Mutexes().RefreshingBranchesMutex.Lock() defer self.c.Mutexes().RefreshingBranchesMutex.Unlock() @@ -443,6 +446,13 @@ func (self *RefreshHelper) refreshBranches() { self.c.Model().Branches = branches + if refreshWorktrees { + self.loadWorktrees() + if err := self.c.PostRefreshUpdate(self.c.Contexts().Worktrees); err != nil { + self.c.Log.Error(err) + } + } + if err := self.c.PostRefreshUpdate(self.c.Contexts().Branches); err != nil { self.c.Log.Error(err) } @@ -634,15 +644,18 @@ func (self *RefreshHelper) refreshRemotes() error { return nil } -func (self *RefreshHelper) refreshWorktrees() error { +func (self *RefreshHelper) loadWorktrees() { worktrees, err := self.c.Git().Loaders.Worktrees.GetWorktrees() if err != nil { self.c.Log.Error(err) self.c.Model().Worktrees = []*models.Worktree{} - return nil } self.c.Model().Worktrees = worktrees +} + +func (self *RefreshHelper) refreshWorktrees() error { + self.loadWorktrees() // need to refresh branches because the branches view shows worktrees against // branches