1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-24 05:36:19 +02:00
lazygit/pkg/gui/controllers/git_flow_controller.go
Jesse Duffield a5f3515ad8 Set groundwork for better disabled reasons with range select
Something dumb that we're currently doing is expecting list items
to define an ID method which returns a string. We use that when copying
items to clipboard with ctrl+o and when getting a ref name for diffing.

This commit gets us a little deeper into that hole by explicitly requiring
list items to implement that method so that we can easily use the new
helper functions in list_controller_trait.go.

In future we need to just remove the whole ID thing entirely but I'm too
lazy to do that right now.
2024-01-23 13:03:37 +11:00

112 lines
2.8 KiB
Go

package controllers
import (
"fmt"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
)
type GitFlowController struct {
baseController
*ListControllerTrait[*models.Branch]
c *ControllerCommon
}
var _ types.IController = &GitFlowController{}
func NewGitFlowController(
c *ControllerCommon,
) *GitFlowController {
return &GitFlowController{
baseController: baseController{},
ListControllerTrait: NewListControllerTrait[*models.Branch](
c,
c.Contexts().Branches,
c.Contexts().Branches.GetSelected,
c.Contexts().Branches.GetSelectedItems,
),
c: c,
}
}
func (self *GitFlowController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{
{
Key: opts.GetKey(opts.Config.Branches.ViewGitFlowOptions),
Handler: self.withItem(self.handleCreateGitFlowMenu),
Description: self.c.Tr.GitFlowOptions,
OpensMenu: true,
},
}
return bindings
}
func (self *GitFlowController) handleCreateGitFlowMenu(branch *models.Branch) error {
if !self.c.Git().Flow.GitFlowEnabled() {
return self.c.ErrorMsg("You need to install git-flow and enable it in this repo to use git-flow features")
}
startHandler := func(branchType string) func() error {
return func() error {
title := utils.ResolvePlaceholderString(self.c.Tr.NewGitFlowBranchPrompt, map[string]string{"branchType": branchType})
return self.c.Prompt(types.PromptOpts{
Title: title,
HandleConfirm: func(name string) error {
self.c.LogAction(self.c.Tr.Actions.GitFlowStart)
return self.c.RunSubprocessAndRefresh(
self.c.Git().Flow.StartCmdObj(branchType, name),
)
},
})
}
}
return self.c.Menu(types.CreateMenuOptions{
Title: "git flow",
Items: []*types.MenuItem{
{
// not localising here because it's one to one with the actual git flow commands
Label: fmt.Sprintf("finish branch '%s'", branch.Name),
OnPress: func() error {
return self.gitFlowFinishBranch(branch.Name)
},
DisabledReason: self.require(self.singleItemSelected())(),
},
{
Label: "start feature",
OnPress: startHandler("feature"),
Key: 'f',
},
{
Label: "start hotfix",
OnPress: startHandler("hotfix"),
Key: 'h',
},
{
Label: "start bugfix",
OnPress: startHandler("bugfix"),
Key: 'b',
},
{
Label: "start release",
OnPress: startHandler("release"),
Key: 'r',
},
},
})
}
func (self *GitFlowController) gitFlowFinishBranch(branchName string) error {
cmdObj, err := self.c.Git().Flow.FinishCmdObj(branchName)
if err != nil {
return self.c.Error(err)
}
self.c.LogAction(self.c.Tr.Actions.GitFlowFinish)
return self.c.RunSubprocessAndRefresh(cmdObj)
}