Skip to content

Commit ee0b6c0

Browse files
committed
Don't refresh pull requests when checking out a local branch
For esthetic reasons, checking out a branch (or other ref) blocks the UI until the refresh is done, so it's important that the refresh doesn't do unnecessary work. Refreshing pull requests is unnecessary (but costly, when waiting for it) when a branch is checked out that already existed locally. However, it is required when checking out a remote branch for the first time, so that the PR icon appears immediately when there is one.
1 parent 282a299 commit ee0b6c0

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

pkg/gui/controllers/helpers/refs_helper.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,19 @@ func (self *RefsHelper) CheckoutRef(ref string, options types.CheckoutRefOptions
5454
// loading a heap of commits is slow so we limit them whenever doing a reset
5555
self.c.Contexts().LocalCommits.SetLimitCommits(true)
5656

57-
self.c.Refresh(types.RefreshOptions{Mode: types.BLOCK_UI, KeepBranchSelectionIndex: true})
57+
scope := []types.RefreshableView{
58+
types.COMMITS,
59+
types.BRANCHES,
60+
types.FILES,
61+
types.REFLOG,
62+
types.WORKTREES,
63+
types.BISECT_INFO,
64+
types.STAGING,
65+
}
66+
if options.RefreshPullRequests {
67+
scope = append(scope, types.PULL_REQUESTS)
68+
}
69+
self.c.Refresh(types.RefreshOptions{Mode: types.BLOCK_UI, Scope: scope, KeepBranchSelectionIndex: true})
5870
}
5971

6072
localBranch, found := lo.Find(self.c.Model().Branches, func(branch *models.Branch) bool {
@@ -120,8 +132,8 @@ func (self *RefsHelper) CheckoutRef(ref string, options types.CheckoutRefOptions
120132

121133
// Shows a prompt to choose between creating a new branch or checking out a detached head
122134
func (self *RefsHelper) CheckoutRemoteBranch(fullBranchName string, localBranchName string) error {
123-
checkout := func(branchName string) error {
124-
return self.CheckoutRef(branchName, types.CheckoutRefOptions{})
135+
checkout := func(branchName string, refreshPullRequests bool) error {
136+
return self.CheckoutRef(branchName, types.CheckoutRefOptions{RefreshPullRequests: refreshPullRequests})
125137
}
126138

127139
// If a branch with this name already exists locally, just check it out. We
@@ -130,7 +142,7 @@ func (self *RefsHelper) CheckoutRemoteBranch(fullBranchName string, localBranchN
130142
if lo.ContainsBy(self.c.Model().Branches, func(branch *models.Branch) bool {
131143
return branch.Name == localBranchName
132144
}) {
133-
return checkout(localBranchName)
145+
return checkout(localBranchName, false)
134146
}
135147

136148
return self.c.Menu(types.CreateMenuOptions{
@@ -156,14 +168,14 @@ func (self *RefsHelper) CheckoutRemoteBranch(fullBranchName string, localBranchN
156168
Mode: types.SYNC,
157169
Scope: []types.RefreshableView{types.BRANCHES},
158170
})
159-
return checkout(localBranchName)
171+
return checkout(localBranchName, true)
160172
},
161173
},
162174
{
163175
Label: self.c.Tr.CheckoutTypeDetachedHead,
164176
Tooltip: self.c.Tr.CheckoutTypeDetachedHeadTooltip,
165177
OnPress: func() error {
166-
return checkout(fullBranchName)
178+
return checkout(fullBranchName, false)
167179
},
168180
},
169181
},

pkg/gui/types/common_commands.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@ type CheckoutRefOptions struct {
44
WaitingStatus string
55
EnvVars []string
66
OnRefNotFound func(ref string) error
7+
8+
// Refreshing pull requests is necessary when checking out a branch that doesn't exist locally
9+
// (e.g. checking out a remote branch), but it not needed when checking out an existing local
10+
// branch or a detached head (e.g. a tag).
11+
RefreshPullRequests bool
712
}

0 commit comments

Comments
 (0)