1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-10 04:07:18 +02:00
lazygit/pkg/gui/controllers/git_flow_controller.go

124 lines
3.0 KiB
Go
Raw Normal View History

2022-02-13 09:12:21 +02:00
package controllers
import (
"fmt"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
)
type GitFlowController struct {
baseController
2023-03-23 09:47:29 +02:00
c *ControllerCommon
2022-02-13 09:12:21 +02:00
}
var _ types.IController = &GitFlowController{}
func NewGitFlowController(
2023-03-23 09:47:29 +02:00
common *ControllerCommon,
2022-02-13 09:12:21 +02:00
) *GitFlowController {
return &GitFlowController{
2023-03-23 09:47:29 +02:00
baseController: baseController{},
c: common,
2022-02-13 09:12:21 +02:00
}
}
func (self *GitFlowController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{
{
Key: opts.GetKey(opts.Config.Branches.ViewGitFlowOptions),
Handler: self.checkSelected(self.handleCreateGitFlowMenu),
Description: self.c.Tr.GitFlowOptions,
2022-02-13 09:12:21 +02:00
OpensMenu: true,
},
}
return bindings
}
func (self *GitFlowController) handleCreateGitFlowMenu(branch *models.Branch) error {
2023-03-23 04:04:57 +02:00
if !self.c.Git().Flow.GitFlowEnabled() {
2022-02-13 09:12:21 +02:00
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(
2023-03-23 04:04:57 +02:00
self.c.Git().Flow.StartCmdObj(branchType, name),
2022-02-13 09:12:21 +02:00
)
},
})
}
}
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),
2022-02-13 09:12:21 +02:00
OnPress: func() error {
return self.gitFlowFinishBranch(branch.Name)
},
},
{
Label: "start feature",
OnPress: startHandler("feature"),
Key: 'f',
2022-02-13 09:12:21 +02:00
},
{
Label: "start hotfix",
OnPress: startHandler("hotfix"),
Key: 'h',
2022-02-13 09:12:21 +02:00
},
{
Label: "start bugfix",
OnPress: startHandler("bugfix"),
Key: 'b',
2022-02-13 09:12:21 +02:00
},
{
Label: "start release",
OnPress: startHandler("release"),
Key: 'r',
2022-02-13 09:12:21 +02:00
},
},
})
}
func (self *GitFlowController) gitFlowFinishBranch(branchName string) error {
2023-03-23 04:04:57 +02:00
cmdObj, err := self.c.Git().Flow.FinishCmdObj(branchName)
2022-02-13 09:12:21 +02:00
if err != nil {
return self.c.Error(err)
}
self.c.LogAction(self.c.Tr.Actions.GitFlowFinish)
return self.c.RunSubprocessAndRefresh(cmdObj)
}
func (self *GitFlowController) checkSelected(callback func(*models.Branch) error) func() error {
return func() error {
node := self.context().GetSelected()
if node == nil {
return nil
}
return callback(node)
}
}
func (self *GitFlowController) Context() types.Context {
return self.context()
}
func (self *GitFlowController) context() *context.BranchesContext {
2023-03-23 04:04:57 +02:00
return self.c.Contexts().Branches
2022-02-13 09:12:21 +02:00
}