From 816d0c0820c3d3e45070621ac0664519ac0e6413 Mon Sep 17 00:00:00 2001 From: Elias Assaf Date: Thu, 29 May 2025 00:38:29 +0300 Subject: [PATCH 1/2] Add a function to suggest a branch name based on branchPrefix Moving the getter of the suggested branch name to a separate function allows us to reuse it in situations where we are not calling the regular create new branch function, such as move commits to a new branch Signed-off-by: Elias Assaf --- pkg/gui/controllers/helpers/refs_helper.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/pkg/gui/controllers/helpers/refs_helper.go b/pkg/gui/controllers/helpers/refs_helper.go index 808b7866f..2a73d30b1 100644 --- a/pkg/gui/controllers/helpers/refs_helper.go +++ b/pkg/gui/controllers/helpers/refs_helper.go @@ -325,13 +325,10 @@ func (self *RefsHelper) NewBranch(from string, fromFormattedName string, suggest if suggestedBranchName == "" { var err error - suggestedBranchName, err = utils.ResolveTemplate(self.c.UserConfig().Git.BranchPrefix, nil, template.FuncMap{ - "runCommand": self.c.Git().Custom.TemplateFunctionRunCommand, - }) + suggestedBranchName, err = self.getSuggestedBranchName() if err != nil { return err } - suggestedBranchName = strings.ReplaceAll(suggestedBranchName, "\t", " ") } refresh := func() error { @@ -587,3 +584,14 @@ func (self *RefsHelper) ParseRemoteBranchName(fullBranchName string) (string, st func IsSwitchBranchUncommittedChangesError(err error) bool { return strings.Contains(err.Error(), "Please commit your changes or stash them before you switch branch") } + +func (self *RefsHelper) getSuggestedBranchName() (string, error) { + suggestedBranchName, err := utils.ResolveTemplate(self.c.UserConfig().Git.BranchPrefix, nil, template.FuncMap{ + "runCommand": self.c.Git().Custom.TemplateFunctionRunCommand, + }) + if err != nil { + return suggestedBranchName, err + } + suggestedBranchName = strings.ReplaceAll(suggestedBranchName, "\t", " ") + return suggestedBranchName, nil +} From fdf9726c3752c2a3170aba45aade5a5caf08d9cf Mon Sep 17 00:00:00 2001 From: Elias Assaf Date: Wed, 28 May 2025 23:53:55 +0300 Subject: [PATCH 2/2] Use branchPrefix on moving commits to a new branch Signed-off-by: Elias Assaf --- pkg/gui/controllers/helpers/refs_helper.go | 19 +++++++++++-------- ...move_commits_to_new_branch_keep_stacked.go | 7 +++++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/pkg/gui/controllers/helpers/refs_helper.go b/pkg/gui/controllers/helpers/refs_helper.go index 2a73d30b1..b69a68c79 100644 --- a/pkg/gui/controllers/helpers/refs_helper.go +++ b/pkg/gui/controllers/helpers/refs_helper.go @@ -396,16 +396,21 @@ func (self *RefsHelper) MoveCommitsToNewBranch() error { return err } - withNewBranchNamePrompt := func(baseBranchName string, f func(string, string) error) { + withNewBranchNamePrompt := func(baseBranchName string, f func(string, string) error) error { prompt := utils.ResolvePlaceholderString( self.c.Tr.NewBranchNameBranchOff, map[string]string{ "branchName": baseBranchName, }, ) + suggestedBranchName, err := self.getSuggestedBranchName() + if err != nil { + return err + } self.c.Prompt(types.PromptOpts{ - Title: prompt, + Title: prompt, + InitialContent: suggestedBranchName, HandleConfirm: func(response string) error { self.c.LogAction(self.c.Tr.MoveCommitsToNewBranch) newBranchName := SanitizedBranchName(response) @@ -414,6 +419,7 @@ func (self *RefsHelper) MoveCommitsToNewBranch() error { }) }, }) + return nil } isMainBranch := lo.Contains(self.c.UserConfig().Git.MainBranches, currentBranch.Name) @@ -428,8 +434,7 @@ func (self *RefsHelper) MoveCommitsToNewBranch() error { Title: self.c.Tr.MoveCommitsToNewBranch, Prompt: prompt, HandleConfirm: func() error { - withNewBranchNamePrompt(currentBranch.Name, self.moveCommitsToNewBranchStackedOnCurrentBranch) - return nil + return withNewBranchNamePrompt(currentBranch.Name, self.moveCommitsToNewBranchStackedOnCurrentBranch) }, }) return nil @@ -449,17 +454,15 @@ func (self *RefsHelper) MoveCommitsToNewBranch() error { { Label: fmt.Sprintf(self.c.Tr.MoveCommitsToNewBranchFromBaseItem, shortBaseBranchName), OnPress: func() error { - withNewBranchNamePrompt(shortBaseBranchName, func(currentBranch string, newBranchName string) error { + return withNewBranchNamePrompt(shortBaseBranchName, func(currentBranch string, newBranchName string) error { return self.moveCommitsToNewBranchOffOfMainBranch(currentBranch, newBranchName, baseBranchRef) }) - return nil }, }, { Label: fmt.Sprintf(self.c.Tr.MoveCommitsToNewBranchStackedItem, currentBranch.Name), OnPress: func() error { - withNewBranchNamePrompt(currentBranch.Name, self.moveCommitsToNewBranchStackedOnCurrentBranch) - return nil + return withNewBranchNamePrompt(currentBranch.Name, self.moveCommitsToNewBranchStackedOnCurrentBranch) }, }, }, diff --git a/pkg/integration/tests/branch/move_commits_to_new_branch_keep_stacked.go b/pkg/integration/tests/branch/move_commits_to_new_branch_keep_stacked.go index 0b9828ef1..143adad0b 100644 --- a/pkg/integration/tests/branch/move_commits_to_new_branch_keep_stacked.go +++ b/pkg/integration/tests/branch/move_commits_to_new_branch_keep_stacked.go @@ -9,7 +9,9 @@ var MoveCommitsToNewBranchKeepStacked = NewIntegrationTest(NewIntegrationTestArg Description: "Create a new branch from the commits that you accidentally made on the wrong branch; choosing stacked on current branch", ExtraCmdArgs: []string{}, Skip: false, - SetupConfig: func(config *config.AppConfig) {}, + SetupConfig: func(config *config.AppConfig) { + config.GetUserConfig().Git.BranchPrefix = "myprefix/" + }, SetupRepo: func(shell *Shell) { shell.EmptyCommit("initial commit") shell.CloneIntoRemote("origin") @@ -42,12 +44,13 @@ var MoveCommitsToNewBranchKeepStacked = NewIntegrationTest(NewIntegrationTestArg t.ExpectPopup().Prompt(). Title(Equals("New branch name (branch is off of 'feature')")). + InitialText(Equals("myprefix/")). Type("new branch"). Confirm() t.Views().Branches(). Lines( - Contains("new-branch").DoesNotContain("↑").IsSelected(), + Contains("myprefix/new-branch").DoesNotContain("↑").IsSelected(), Contains("feature ✓"), Contains("master ✓"), )