summaryrefslogtreecommitdiffstats
path: root/pkg/gui/controllers/git_flow_controller.go
blob: 7c3e45603cf9e9223abb1cc791aa9c8bb7c4e62c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package controllers

import (
	"errors"
	"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 errors.New("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 err
	}

	self.c.LogAction(self.c.Tr.Actions.GitFlowFinish)
	return self.c.RunSubprocessAndRefresh(cmdObj)
}