From 5b1292006d81571b5e2721993f0f28539a511c92 Mon Sep 17 00:00:00 2001 From: kyu08 <49891479+kyu08@users.noreply.github.com> Date: Sun, 13 Jul 2025 00:42:01 +0900 Subject: [PATCH] Add new command "Checkout previous branch" --- pkg/config/user_config.go | 2 + pkg/gui/controllers/branches_controller.go | 10 ++++ pkg/i18n/english.go | 2 + .../tests/branch/checkout_previous_branch.go | 51 +++++++++++++++++++ pkg/integration/tests/test_list.go | 1 + 5 files changed, 66 insertions(+) create mode 100644 pkg/integration/tests/branch/checkout_previous_branch.go diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index 5a7838601..5465a376c 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -497,6 +497,7 @@ type KeybindingBranchesConfig struct { CopyPullRequestURL string `yaml:"copyPullRequestURL"` CheckoutBranchByName string `yaml:"checkoutBranchByName"` ForceCheckoutBranch string `yaml:"forceCheckoutBranch"` + CheckoutPreviousBranch string `yaml:"checkoutPreviousBranch"` RebaseBranch string `yaml:"rebaseBranch"` RenameBranch string `yaml:"renameBranch"` MergeIntoCurrentBranch string `yaml:"mergeIntoCurrentBranch"` @@ -957,6 +958,7 @@ func GetDefaultConfig() *UserConfig { ViewPullRequestOptions: "O", CheckoutBranchByName: "c", ForceCheckoutBranch: "F", + CheckoutPreviousBranch: "-", RebaseBranch: "r", RenameBranch: "R", MergeIntoCurrentBranch: "M", diff --git a/pkg/gui/controllers/branches_controller.go b/pkg/gui/controllers/branches_controller.go index ef27ba01d..ed2b01509 100644 --- a/pkg/gui/controllers/branches_controller.go +++ b/pkg/gui/controllers/branches_controller.go @@ -89,6 +89,11 @@ func (self *BranchesController) GetKeybindings(opts types.KeybindingsOpts) []*ty Description: self.c.Tr.CheckoutByName, Tooltip: self.c.Tr.CheckoutByNameTooltip, }, + { + Key: opts.GetKey(opts.Config.Branches.CheckoutPreviousBranch), + Handler: self.checkoutPreviousBranch, + Description: self.c.Tr.CheckoutPreviousBranch, + }, { Key: opts.GetKey(opts.Config.Branches.ForceCheckoutBranch), Handler: self.forceCheckout, @@ -483,6 +488,11 @@ func (self *BranchesController) forceCheckout() error { return nil } +func (self *BranchesController) checkoutPreviousBranch() error { + self.c.LogAction(self.c.Tr.Actions.CheckoutBranch) + return self.c.Helpers().Refs.CheckoutRef("-", types.CheckoutRefOptions{}) +} + func (self *BranchesController) checkoutByName() error { self.c.Prompt(types.PromptOpts{ Title: self.c.Tr.BranchName + ":", diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index bfac1fc20..7259ae372 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -139,6 +139,7 @@ type TranslationSet struct { ForceCheckoutTooltip string CheckoutByName string CheckoutByNameTooltip string + CheckoutPreviousBranch string RemoteBranchCheckoutTitle string RemoteBranchCheckoutPrompt string CheckoutTypeNewBranch string @@ -1182,6 +1183,7 @@ func EnglishTranslationSet() *TranslationSet { ForceCheckoutTooltip: "Force checkout selected branch. This will discard all local changes in your working directory before checking out the selected branch.", CheckoutByName: "Checkout by name", CheckoutByNameTooltip: "Checkout by name. In the input box you can enter '-' to switch to the last branch.", + CheckoutPreviousBranch: "Checkout previous branch", RemoteBranchCheckoutTitle: "Checkout {{.branchName}}", RemoteBranchCheckoutPrompt: "How would you like to check out this branch?", CheckoutTypeNewBranch: "New local branch", diff --git a/pkg/integration/tests/branch/checkout_previous_branch.go b/pkg/integration/tests/branch/checkout_previous_branch.go new file mode 100644 index 000000000..8a8698d85 --- /dev/null +++ b/pkg/integration/tests/branch/checkout_previous_branch.go @@ -0,0 +1,51 @@ +package branch + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var CheckoutPreviousBranch = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Checkout to the previous branch using the checkout previous branch functionality", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(config *config.AppConfig) {}, + SetupRepo: func(shell *Shell) { + shell. + CreateNCommits(3). + NewBranch("previous-branch"). + EmptyCommit("previous commit"). + Checkout("master"). + EmptyCommit("master commit") + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.Views().Branches(). + Focus(). + Lines( + Contains("master").IsSelected(), + Contains("previous-branch"), + ) + + // Press the checkout previous branch key (should checkout previous-branch) + t.Views().Branches(). + Press(keys.Branches.CheckoutPreviousBranch). + Lines( + Contains("previous-branch").IsSelected(), + Contains("master"), + ) + + // Verify we're on previous-branch + t.Git().CurrentBranchName("previous-branch") + + // Press again to go back to master + t.Views().Branches(). + Press(keys.Branches.CheckoutPreviousBranch). + Lines( + Contains("master").IsSelected(), + Contains("previous-branch"), + ) + + // Verify we're back on master + t.Git().CurrentBranchName("master") + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index 9de69d3b0..d5f4f5c21 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -40,6 +40,7 @@ var tests = []*components.IntegrationTest{ bisect.Skip, branch.CheckoutAutostash, branch.CheckoutByName, + branch.CheckoutPreviousBranch, branch.CreateTag, branch.Delete, branch.DeleteMultiple,