1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-11-29 16:03:07 +03:00

Switch to branches view when checking out a commit

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.
This commit is contained in:
Stefan Haller
2025-11-16 18:45:23 +01:00
parent e3a09eacd1
commit 221e025ee6
4 changed files with 13 additions and 8 deletions

View File

@@ -61,6 +61,10 @@ func (self *RefsHelper) CheckoutRef(ref string, options types.CheckoutRefOptions
return self.c.WithWaitingStatus(waitingStatus, f) return self.c.WithWaitingStatus(waitingStatus, f)
} }
// Switch to the branches context _before_ starting to check out the branch, so that we see the
// inline status. This is a no-op if the branches panel is already focused.
self.c.Context().Push(self.c.Contexts().Branches, types.OnFocusOpts{})
return withCheckoutStatus(func(gocui.Task) error { return withCheckoutStatus(func(gocui.Task) error {
if err := self.c.Git().Branch.Checkout(ref, cmdOptions); err != nil { if err := self.c.Git().Branch.Checkout(ref, cmdOptions); err != nil {
// 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 // 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
// Shows a prompt to choose between creating a new branch or checking out a detached head // Shows a prompt to choose between creating a new branch or checking out a detached head
func (self *RefsHelper) CheckoutRemoteBranch(fullBranchName string, localBranchName string) error { func (self *RefsHelper) CheckoutRemoteBranch(fullBranchName string, localBranchName string) error {
checkout := func(branchName string) error { checkout := func(branchName string) error {
// Switch to the branches context _before_ starting to check out the
// branch, so that we see the inline status
if self.c.Context().Current() != self.c.Contexts().Branches {
self.c.Context().Push(self.c.Contexts().Branches, types.OnFocusOpts{})
}
return self.CheckoutRef(branchName, types.CheckoutRefOptions{}) return self.CheckoutRef(branchName, types.CheckoutRefOptions{})
} }

View File

@@ -155,7 +155,6 @@ func (self *TagsController) checkout(tag *models.Tag) error {
if err := self.c.Helpers().Refs.CheckoutRef(tag.FullRefName(), types.CheckoutRefOptions{}); err != nil { if err := self.c.Helpers().Refs.CheckoutRef(tag.FullRefName(), types.CheckoutRefOptions{}); err != nil {
return err return err
} }
self.c.Context().Push(self.c.Contexts().Branches, types.OnFocusOpts{})
return nil return nil
} }

View File

@@ -43,14 +43,16 @@ var Checkout = NewIntegrationTest(NewIntegrationTestArgs{
Select(MatchesRegexp("Checkout commit [a-f0-9]+ as detached head")). Select(MatchesRegexp("Checkout commit [a-f0-9]+ as detached head")).
Confirm() Confirm()
t.Views().Branches(). t.Views().Branches().
IsFocused().
Lines( Lines(
Contains("* (HEAD detached at"), Contains("* (HEAD detached at").IsSelected(),
Contains("branch1"), Contains("branch1"),
Contains("branch2"), Contains("branch2"),
Contains("master"), Contains("master"),
) )
t.Views().Commits(). t.Views().Commits().
Focus().
NavigateToLine(Contains("two")). NavigateToLine(Contains("two")).
PressPrimaryAction() PressPrimaryAction()
@@ -65,8 +67,9 @@ var Checkout = NewIntegrationTest(NewIntegrationTestArgs{
Select(Contains("Checkout branch 'master'")). Select(Contains("Checkout branch 'master'")).
Confirm() Confirm()
t.Views().Branches(). t.Views().Branches().
IsFocused().
Lines( Lines(
Contains("master"), Contains("master").IsSelected(),
Contains("branch1"), Contains("branch1"),
Contains("branch2"), Contains("branch2"),
) )

View File

@@ -102,11 +102,13 @@ var UndoCheckoutAndDrop = NewIntegrationTest(NewIntegrationTestArgs{
Confirm() Confirm()
t.Views().Branches(). t.Views().Branches().
IsFocused().
Lines( Lines(
Contains("master").IsSelected(), Contains("master").IsSelected(),
Contains("other_branch"), Contains("other_branch"),
) )
}). }).
Focus().
Lines( Lines(
Contains("three").IsSelected(), Contains("three").IsSelected(),
Contains("two"), Contains("two"),
@@ -135,11 +137,13 @@ var UndoCheckoutAndDrop = NewIntegrationTest(NewIntegrationTestArgs{
Confirm() Confirm()
t.Views().Branches(). t.Views().Branches().
IsFocused().
Lines( Lines(
Contains("other_branch").IsSelected(), Contains("other_branch").IsSelected(),
Contains("master"), Contains("master"),
) )
}). }).
Focus().
Press(keys.Universal.Redo). Press(keys.Universal.Redo).
Tap(confirmRedoDrop). Tap(confirmRedoDrop).
Lines( Lines(