Skip to content

Commit 11c7203

Browse files
authored
Switch to branches view when checking out a commit (#5048)
We move the code to push the branches context into CheckoutRef, this way it works consistently no matter where we call it from. Previously, checking out remote branches or tags would switch to the branches view, but checking out a commit did not. Note that it now also takes effect for undoing or redoing a checkout, which may be a bit questionable; but I still think it makes sense for this, too.
2 parents 9f930ee + 221e025 commit 11c7203

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

pkg/gui/controllers/helpers/refs_helper.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ func (self *RefsHelper) CheckoutRef(ref string, options types.CheckoutRefOptions
6161
return self.c.WithWaitingStatus(waitingStatus, f)
6262
}
6363

64+
// Switch to the branches context _before_ starting to check out the branch, so that we see the
65+
// inline status. This is a no-op if the branches panel is already focused.
66+
self.c.Context().Push(self.c.Contexts().Branches, types.OnFocusOpts{})
67+
6468
return withCheckoutStatus(func(gocui.Task) error {
6569
if err := self.c.Git().Branch.Checkout(ref, cmdOptions); err != nil {
6670
// note, this will only work for english-language git commands. If we force git to use english, and the error isn't this one, then the user will receive an english command they may not understand. I'm not sure what the best solution to this is. Running the command once in english and a second time in the native language is one option
@@ -109,11 +113,6 @@ func (self *RefsHelper) CheckoutRef(ref string, options types.CheckoutRefOptions
109113
// Shows a prompt to choose between creating a new branch or checking out a detached head
110114
func (self *RefsHelper) CheckoutRemoteBranch(fullBranchName string, localBranchName string) error {
111115
checkout := func(branchName string) error {
112-
// Switch to the branches context _before_ starting to check out the
113-
// branch, so that we see the inline status
114-
if self.c.Context().Current() != self.c.Contexts().Branches {
115-
self.c.Context().Push(self.c.Contexts().Branches, types.OnFocusOpts{})
116-
}
117116
return self.CheckoutRef(branchName, types.CheckoutRefOptions{})
118117
}
119118

pkg/gui/controllers/tags_controller.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ func (self *TagsController) checkout(tag *models.Tag) error {
155155
if err := self.c.Helpers().Refs.CheckoutRef(tag.FullRefName(), types.CheckoutRefOptions{}); err != nil {
156156
return err
157157
}
158-
self.c.Context().Push(self.c.Contexts().Branches, types.OnFocusOpts{})
159158
return nil
160159
}
161160

pkg/integration/tests/commit/checkout.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,17 @@ var Checkout = NewIntegrationTest(NewIntegrationTestArgs{
4242
Tooltip(Contains("Disabled: No branches found at selected commit.")).
4343
Select(MatchesRegexp("Checkout commit [a-f0-9]+ as detached head")).
4444
Confirm()
45-
t.Views().Branches().Lines(
46-
Contains("* (HEAD detached at"),
47-
Contains("branch1"),
48-
Contains("branch2"),
49-
Contains("master"),
50-
)
45+
t.Views().Branches().
46+
IsFocused().
47+
Lines(
48+
Contains("* (HEAD detached at").IsSelected(),
49+
Contains("branch1"),
50+
Contains("branch2"),
51+
Contains("master"),
52+
)
5153

5254
t.Views().Commits().
55+
Focus().
5356
NavigateToLine(Contains("two")).
5457
PressPrimaryAction()
5558

@@ -63,10 +66,12 @@ var Checkout = NewIntegrationTest(NewIntegrationTestArgs{
6366
).
6467
Select(Contains("Checkout branch 'master'")).
6568
Confirm()
66-
t.Views().Branches().Lines(
67-
Contains("master"),
68-
Contains("branch1"),
69-
Contains("branch2"),
70-
)
69+
t.Views().Branches().
70+
IsFocused().
71+
Lines(
72+
Contains("master").IsSelected(),
73+
Contains("branch1"),
74+
Contains("branch2"),
75+
)
7176
},
7277
})

pkg/integration/tests/undo/undo_checkout_and_drop.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,13 @@ var UndoCheckoutAndDrop = NewIntegrationTest(NewIntegrationTestArgs{
102102
Confirm()
103103

104104
t.Views().Branches().
105+
IsFocused().
105106
Lines(
106107
Contains("master").IsSelected(),
107108
Contains("other_branch"),
108109
)
109110
}).
111+
Focus().
110112
Lines(
111113
Contains("three").IsSelected(),
112114
Contains("two"),
@@ -135,11 +137,13 @@ var UndoCheckoutAndDrop = NewIntegrationTest(NewIntegrationTestArgs{
135137
Confirm()
136138

137139
t.Views().Branches().
140+
IsFocused().
138141
Lines(
139142
Contains("other_branch").IsSelected(),
140143
Contains("master"),
141144
)
142145
}).
146+
Focus().
143147
Press(keys.Universal.Redo).
144148
Tap(confirmRedoDrop).
145149
Lines(

0 commit comments

Comments
 (0)