Skip to content

Commit a21db7a

Browse files
committed
fixup! When checking out a remote branch by name, ask the user how
Refactor to move the condition for checking out an existing local branch into CheckoutRemoteBranch; we want to reuse it in the remote branches controller. Also, switch to the branches view if it isn't showing already. Again, we'll need that when using this in the remote branches view.
1 parent feae89b commit a21db7a

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

pkg/gui/controllers/branches_controller.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,7 @@ func (self *BranchesController) checkoutByName() error {
437437
HandleConfirm: func(response string) error {
438438
self.c.LogAction("Checkout branch")
439439
_, branchName, found := self.c.Helpers().Refs.ParseRemoteBranchName(response)
440-
if found && !lo.ContainsBy(self.c.Model().Branches, func(branch *models.Branch) bool {
441-
return branch.Name == branchName
442-
}) {
440+
if found {
443441
return self.c.Helpers().Refs.CheckoutRemoteBranch(response, branchName)
444442
}
445443
return self.c.Helpers().Refs.CheckoutRef(response, types.CheckoutRefOptions{

pkg/gui/controllers/helpers/refs_helper.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,25 @@ func (self *RefsHelper) CheckoutRef(ref string, options types.CheckoutRefOptions
9898

9999
// Shows a prompt to choose between creating a new branch or checking out a detached head
100100
func (self *RefsHelper) CheckoutRemoteBranch(fullBranchName string, localBranchName string) error {
101+
checkout := func(branchName string) error {
102+
if err := self.CheckoutRef(branchName, types.CheckoutRefOptions{}); err != nil {
103+
return err
104+
}
105+
if self.c.CurrentContext() != self.c.Contexts().Branches {
106+
return self.c.PushContext(self.c.Contexts().Branches)
107+
}
108+
return nil
109+
}
110+
111+
// If a branch with this name already exists locally, just check it out. We
112+
// don't bother checking whether it actually tracks this remote branch, since
113+
// it's very unlikely that it doesn't.
114+
if lo.ContainsBy(self.c.Model().Branches, func(branch *models.Branch) bool {
115+
return branch.Name == localBranchName
116+
}) {
117+
return checkout(localBranchName)
118+
}
119+
101120
return self.c.Menu(types.CreateMenuOptions{
102121
Title: self.c.Tr.RemoteBranchCheckoutType,
103122
Items: []*types.MenuItem{
@@ -111,13 +130,13 @@ func (self *RefsHelper) CheckoutRemoteBranch(fullBranchName string, localBranchN
111130
if err := self.c.Git().Branch.CreateWithUpstream(localBranchName, fullBranchName); err != nil {
112131
return self.c.Error(err)
113132
}
114-
return self.CheckoutRef(localBranchName, types.CheckoutRefOptions{})
133+
return checkout(localBranchName)
115134
},
116135
},
117136
{
118137
Label: self.c.Tr.CheckoutTypeDetachedHead,
119138
OnPress: func() error {
120-
return self.CheckoutRef(fullBranchName, types.CheckoutRefOptions{})
139+
return checkout(fullBranchName)
121140
},
122141
},
123142
},

0 commit comments

Comments
 (0)