Skip to content

Commit 5b12920

Browse files
kyu08stefanhaller
authored andcommitted
Add new command "Checkout previous branch"
1 parent 7d92260 commit 5b12920

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

pkg/config/user_config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ type KeybindingBranchesConfig struct {
497497
CopyPullRequestURL string `yaml:"copyPullRequestURL"`
498498
CheckoutBranchByName string `yaml:"checkoutBranchByName"`
499499
ForceCheckoutBranch string `yaml:"forceCheckoutBranch"`
500+
CheckoutPreviousBranch string `yaml:"checkoutPreviousBranch"`
500501
RebaseBranch string `yaml:"rebaseBranch"`
501502
RenameBranch string `yaml:"renameBranch"`
502503
MergeIntoCurrentBranch string `yaml:"mergeIntoCurrentBranch"`
@@ -957,6 +958,7 @@ func GetDefaultConfig() *UserConfig {
957958
ViewPullRequestOptions: "O",
958959
CheckoutBranchByName: "c",
959960
ForceCheckoutBranch: "F",
961+
CheckoutPreviousBranch: "-",
960962
RebaseBranch: "r",
961963
RenameBranch: "R",
962964
MergeIntoCurrentBranch: "M",

pkg/gui/controllers/branches_controller.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ func (self *BranchesController) GetKeybindings(opts types.KeybindingsOpts) []*ty
8989
Description: self.c.Tr.CheckoutByName,
9090
Tooltip: self.c.Tr.CheckoutByNameTooltip,
9191
},
92+
{
93+
Key: opts.GetKey(opts.Config.Branches.CheckoutPreviousBranch),
94+
Handler: self.checkoutPreviousBranch,
95+
Description: self.c.Tr.CheckoutPreviousBranch,
96+
},
9297
{
9398
Key: opts.GetKey(opts.Config.Branches.ForceCheckoutBranch),
9499
Handler: self.forceCheckout,
@@ -483,6 +488,11 @@ func (self *BranchesController) forceCheckout() error {
483488
return nil
484489
}
485490

491+
func (self *BranchesController) checkoutPreviousBranch() error {
492+
self.c.LogAction(self.c.Tr.Actions.CheckoutBranch)
493+
return self.c.Helpers().Refs.CheckoutRef("-", types.CheckoutRefOptions{})
494+
}
495+
486496
func (self *BranchesController) checkoutByName() error {
487497
self.c.Prompt(types.PromptOpts{
488498
Title: self.c.Tr.BranchName + ":",

pkg/i18n/english.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ type TranslationSet struct {
139139
ForceCheckoutTooltip string
140140
CheckoutByName string
141141
CheckoutByNameTooltip string
142+
CheckoutPreviousBranch string
142143
RemoteBranchCheckoutTitle string
143144
RemoteBranchCheckoutPrompt string
144145
CheckoutTypeNewBranch string
@@ -1182,6 +1183,7 @@ func EnglishTranslationSet() *TranslationSet {
11821183
ForceCheckoutTooltip: "Force checkout selected branch. This will discard all local changes in your working directory before checking out the selected branch.",
11831184
CheckoutByName: "Checkout by name",
11841185
CheckoutByNameTooltip: "Checkout by name. In the input box you can enter '-' to switch to the last branch.",
1186+
CheckoutPreviousBranch: "Checkout previous branch",
11851187
RemoteBranchCheckoutTitle: "Checkout {{.branchName}}",
11861188
RemoteBranchCheckoutPrompt: "How would you like to check out this branch?",
11871189
CheckoutTypeNewBranch: "New local branch",
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package branch
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var CheckoutPreviousBranch = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Checkout to the previous branch using the checkout previous branch functionality",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {},
13+
SetupRepo: func(shell *Shell) {
14+
shell.
15+
CreateNCommits(3).
16+
NewBranch("previous-branch").
17+
EmptyCommit("previous commit").
18+
Checkout("master").
19+
EmptyCommit("master commit")
20+
},
21+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
22+
t.Views().Branches().
23+
Focus().
24+
Lines(
25+
Contains("master").IsSelected(),
26+
Contains("previous-branch"),
27+
)
28+
29+
// Press the checkout previous branch key (should checkout previous-branch)
30+
t.Views().Branches().
31+
Press(keys.Branches.CheckoutPreviousBranch).
32+
Lines(
33+
Contains("previous-branch").IsSelected(),
34+
Contains("master"),
35+
)
36+
37+
// Verify we're on previous-branch
38+
t.Git().CurrentBranchName("previous-branch")
39+
40+
// Press again to go back to master
41+
t.Views().Branches().
42+
Press(keys.Branches.CheckoutPreviousBranch).
43+
Lines(
44+
Contains("master").IsSelected(),
45+
Contains("previous-branch"),
46+
)
47+
48+
// Verify we're back on master
49+
t.Git().CurrentBranchName("master")
50+
},
51+
})

pkg/integration/tests/test_list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ var tests = []*components.IntegrationTest{
4040
bisect.Skip,
4141
branch.CheckoutAutostash,
4242
branch.CheckoutByName,
43+
branch.CheckoutPreviousBranch,
4344
branch.CreateTag,
4445
branch.Delete,
4546
branch.DeleteMultiple,

0 commit comments

Comments
 (0)