Skip to content

Commit 8280fde

Browse files
authored
Use branchPrefix when moving commits to new branch (jesseduffield#4604)
- **PR Description** When pressing N to move new commits to a new branch we get greeted with an empty prompt, this PR makes it so we fill the empty prompt with a suggestion taken from branchPrefix, similar to the good old create a new branch.
2 parents 5b4d009 + fdf9726 commit 8280fde

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

pkg/gui/controllers/helpers/refs_helper.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,10 @@ func (self *RefsHelper) NewBranch(from string, fromFormattedName string, suggest
325325
if suggestedBranchName == "" {
326326
var err error
327327

328-
suggestedBranchName, err = utils.ResolveTemplate(self.c.UserConfig().Git.BranchPrefix, nil, template.FuncMap{
329-
"runCommand": self.c.Git().Custom.TemplateFunctionRunCommand,
330-
})
328+
suggestedBranchName, err = self.getSuggestedBranchName()
331329
if err != nil {
332330
return err
333331
}
334-
suggestedBranchName = strings.ReplaceAll(suggestedBranchName, "\t", " ")
335332
}
336333

337334
refresh := func() error {
@@ -399,16 +396,21 @@ func (self *RefsHelper) MoveCommitsToNewBranch() error {
399396
return err
400397
}
401398

402-
withNewBranchNamePrompt := func(baseBranchName string, f func(string, string) error) {
399+
withNewBranchNamePrompt := func(baseBranchName string, f func(string, string) error) error {
403400
prompt := utils.ResolvePlaceholderString(
404401
self.c.Tr.NewBranchNameBranchOff,
405402
map[string]string{
406403
"branchName": baseBranchName,
407404
},
408405
)
406+
suggestedBranchName, err := self.getSuggestedBranchName()
407+
if err != nil {
408+
return err
409+
}
409410

410411
self.c.Prompt(types.PromptOpts{
411-
Title: prompt,
412+
Title: prompt,
413+
InitialContent: suggestedBranchName,
412414
HandleConfirm: func(response string) error {
413415
self.c.LogAction(self.c.Tr.MoveCommitsToNewBranch)
414416
newBranchName := SanitizedBranchName(response)
@@ -417,6 +419,7 @@ func (self *RefsHelper) MoveCommitsToNewBranch() error {
417419
})
418420
},
419421
})
422+
return nil
420423
}
421424

422425
isMainBranch := lo.Contains(self.c.UserConfig().Git.MainBranches, currentBranch.Name)
@@ -431,8 +434,7 @@ func (self *RefsHelper) MoveCommitsToNewBranch() error {
431434
Title: self.c.Tr.MoveCommitsToNewBranch,
432435
Prompt: prompt,
433436
HandleConfirm: func() error {
434-
withNewBranchNamePrompt(currentBranch.Name, self.moveCommitsToNewBranchStackedOnCurrentBranch)
435-
return nil
437+
return withNewBranchNamePrompt(currentBranch.Name, self.moveCommitsToNewBranchStackedOnCurrentBranch)
436438
},
437439
})
438440
return nil
@@ -452,17 +454,15 @@ func (self *RefsHelper) MoveCommitsToNewBranch() error {
452454
{
453455
Label: fmt.Sprintf(self.c.Tr.MoveCommitsToNewBranchFromBaseItem, shortBaseBranchName),
454456
OnPress: func() error {
455-
withNewBranchNamePrompt(shortBaseBranchName, func(currentBranch string, newBranchName string) error {
457+
return withNewBranchNamePrompt(shortBaseBranchName, func(currentBranch string, newBranchName string) error {
456458
return self.moveCommitsToNewBranchOffOfMainBranch(currentBranch, newBranchName, baseBranchRef)
457459
})
458-
return nil
459460
},
460461
},
461462
{
462463
Label: fmt.Sprintf(self.c.Tr.MoveCommitsToNewBranchStackedItem, currentBranch.Name),
463464
OnPress: func() error {
464-
withNewBranchNamePrompt(currentBranch.Name, self.moveCommitsToNewBranchStackedOnCurrentBranch)
465-
return nil
465+
return withNewBranchNamePrompt(currentBranch.Name, self.moveCommitsToNewBranchStackedOnCurrentBranch)
466466
},
467467
},
468468
},
@@ -587,3 +587,14 @@ func (self *RefsHelper) ParseRemoteBranchName(fullBranchName string) (string, st
587587
func IsSwitchBranchUncommittedChangesError(err error) bool {
588588
return strings.Contains(err.Error(), "Please commit your changes or stash them before you switch branch")
589589
}
590+
591+
func (self *RefsHelper) getSuggestedBranchName() (string, error) {
592+
suggestedBranchName, err := utils.ResolveTemplate(self.c.UserConfig().Git.BranchPrefix, nil, template.FuncMap{
593+
"runCommand": self.c.Git().Custom.TemplateFunctionRunCommand,
594+
})
595+
if err != nil {
596+
return suggestedBranchName, err
597+
}
598+
suggestedBranchName = strings.ReplaceAll(suggestedBranchName, "\t", " ")
599+
return suggestedBranchName, nil
600+
}

pkg/integration/tests/branch/move_commits_to_new_branch_keep_stacked.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ var MoveCommitsToNewBranchKeepStacked = NewIntegrationTest(NewIntegrationTestArg
99
Description: "Create a new branch from the commits that you accidentally made on the wrong branch; choosing stacked on current branch",
1010
ExtraCmdArgs: []string{},
1111
Skip: false,
12-
SetupConfig: func(config *config.AppConfig) {},
12+
SetupConfig: func(config *config.AppConfig) {
13+
config.GetUserConfig().Git.BranchPrefix = "myprefix/"
14+
},
1315
SetupRepo: func(shell *Shell) {
1416
shell.EmptyCommit("initial commit")
1517
shell.CloneIntoRemote("origin")
@@ -42,12 +44,13 @@ var MoveCommitsToNewBranchKeepStacked = NewIntegrationTest(NewIntegrationTestArg
4244

4345
t.ExpectPopup().Prompt().
4446
Title(Equals("New branch name (branch is off of 'feature')")).
47+
InitialText(Equals("myprefix/")).
4548
Type("new branch").
4649
Confirm()
4750

4851
t.Views().Branches().
4952
Lines(
50-
Contains("new-branch").DoesNotContain("↑").IsSelected(),
53+
Contains("myprefix/new-branch").DoesNotContain("↑").IsSelected(),
5154
Contains("feature ✓"),
5255
Contains("master ✓"),
5356
)

0 commit comments

Comments
 (0)