1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/git_flow.go

87 lines
2.5 KiB
Go
Raw Normal View History

2020-01-07 12:42:33 +02:00
package gui
import (
"fmt"
"regexp"
"strings"
"github.com/jesseduffield/gocui"
)
func (gui *Gui) gitFlowFinishBranch(gitFlowConfig string, branchName string) error {
// need to find out what kind of branch this is
prefix := strings.SplitAfterN(branchName, "/", 2)[0]
suffix := strings.Replace(branchName, prefix, "", 1)
branchType := ""
for _, line := range strings.Split(strings.TrimSpace(gitFlowConfig), "\n") {
if strings.HasPrefix(line, "gitflow.prefix.") && strings.HasSuffix(line, prefix) {
// now I just need to how do you say
regex := regexp.MustCompile("gitflow.prefix.([^ ]*) .*")
matches := regex.FindAllStringSubmatch(line, 1)
if len(matches) > 0 && len(matches[0]) > 1 {
branchType = matches[0][1]
break
}
}
}
if branchType == "" {
return gui.createErrorPanel(gui.g, gui.Tr.SLocalize("NotAGitFlowBranch"))
}
subProcess := gui.OSCommand.PrepareSubProcess("git", "flow", branchType, "finish", suffix)
gui.SubProcess = subProcess
return gui.Errors.ErrSubProcess
}
func (gui *Gui) handleCreateGitFlowMenu(g *gocui.Gui, v *gocui.View) error {
branch := gui.getSelectedBranch()
if branch == nil {
return nil
}
// get config
gitFlowConfig, err := gui.OSCommand.RunCommandWithOutput("git config --local --get-regexp gitflow")
if err != nil {
return gui.createErrorPanel(gui.g, "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 := gui.Tr.TemplateLocalize("NewBranchNamePrompt", map[string]interface{}{"branchType": branchType})
return gui.createPromptPanel(gui.g, gui.getMenuView(), title, "", func(g *gocui.Gui, v *gocui.View) error {
name := gui.trimmedContent(v)
subProcess := gui.OSCommand.PrepareSubProcess("git", "flow", branchType, "start", name)
gui.SubProcess = subProcess
return gui.Errors.ErrSubProcess
})
}
}
2020-02-14 14:35:01 +02:00
menuItems := []*menuItem{
2020-01-07 12:42:33 +02:00
{
// not localising here because it's one to one with the actual git flow commands
2020-02-14 14:35:01 +02:00
displayString: fmt.Sprintf("finish branch '%s'", branch.Name),
onPress: func() error {
2020-01-07 12:42:33 +02:00
return gui.gitFlowFinishBranch(gitFlowConfig, branch.Name)
},
},
{
2020-02-14 14:35:01 +02:00
displayString: "start feature",
onPress: startHandler("feature"),
2020-01-07 12:42:33 +02:00
},
{
2020-02-14 14:35:01 +02:00
displayString: "start hotfix",
onPress: startHandler("hotfix"),
2020-01-07 12:42:33 +02:00
},
{
2020-02-14 14:35:01 +02:00
displayString: "start release",
onPress: startHandler("release"),
2020-01-07 12:42:33 +02:00
},
}
2020-02-14 14:35:01 +02:00
return gui.createMenuNew("git flow", menuItems, createMenuOptions{})
2020-01-07 12:42:33 +02:00
}