1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-05-15 22:26:40 +02:00

refactor git flow

This commit is contained in:
Jesse Duffield 2022-01-07 19:56:33 +11:00
parent e92076d2c2
commit 610e503296
5 changed files with 82 additions and 33 deletions

View File

@ -93,3 +93,7 @@ func (self *ConfigCommands) Branches() (map[string]*config.Branch, error) {
return conf.Branches, nil
}
func (self *ConfigCommands) GetGitFlowPrefixes() string {
return self.gitConfig.Get("--local --get-regexp gitflow.prefix.")
}

64
pkg/commands/flow.go Normal file
View File

@ -0,0 +1,64 @@
package commands
import (
"regexp"
"strings"
"github.com/go-errors/errors"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/common"
)
type FlowCommands struct {
*common.Common
config *ConfigCommands
cmd oscommands.ICmdObjBuilder
}
func NewFlowCommands(
common *common.Common,
cmd oscommands.ICmdObjBuilder,
config *ConfigCommands,
) *FlowCommands {
return &FlowCommands{
Common: common,
cmd: cmd,
config: config,
}
}
func (self *FlowCommands) GitFlowEnabled() bool {
return self.config.GetGitFlowPrefixes() == ""
}
func (self *FlowCommands) FinishCmdObj(branchName string) (oscommands.ICmdObj, error) {
prefixes := self.config.GetGitFlowPrefixes()
// 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(prefixes), "\n") {
if strings.HasPrefix(line, "gitflow.prefix.") && strings.HasSuffix(line, prefix) {
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 nil, errors.New(self.Tr.NotAGitFlowBranch)
}
return self.cmd.New("git flow " + branchType + " finish " + suffix), nil
}
func (self *FlowCommands) StartCmdObj(branchType string, name string) oscommands.ICmdObj {
return self.cmd.New("git flow " + branchType + " start " + name)
}

View File

@ -37,6 +37,7 @@ type GitCommand struct {
Patch *PatchCommands
Remote *RemoteCommands
Sync *SyncCommands
Flow *FlowCommands
}
type Loaders struct {
@ -94,6 +95,7 @@ func NewGitCommandAux(
configCommands := NewConfigCommands(cmn, gitConfig)
statusCommands := NewStatusCommands(cmn, osCommand, repo, dotGitDir)
fileLoader := loaders.NewFileLoader(cmn, cmd, configCommands)
flowCommands := NewFlowCommands(cmn, cmd, configCommands)
remoteCommands := NewRemoteCommands(cmn, cmd)
branchCommands := NewBranchCommands(cmn, cmd)
syncCommands := NewSyncCommands(cmn, cmd)
@ -132,6 +134,7 @@ func NewGitCommandAux(
Patch: patchCommands,
Remote: remoteCommands,
Sync: syncCommands,
Flow: flowCommands,
Loaders: Loaders{
Commits: loaders.NewCommitLoader(cmn, cmd, dotGitDir, branchCommands.CurrentBranchName, statusCommands.RebaseMode),
Branches: loaders.NewBranchLoader(cmn, branchCommands.GetRawBranches, branchCommands.CurrentBranchName),

View File

@ -36,7 +36,8 @@ import (
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
func getGitConfigValue(key string) (string, error) {
gitArgs := []string{"config", "--get", "--null", key}
// allowing caller to say that key is '--local mykey' so that they can add extra flags.
gitArgs := append([]string{"config", "--get", "--null"}, strings.Split(key, " ")...)
var stdout bytes.Buffer
cmd := secureexec.Command("git", gitArgs...)
cmd.Stdout = &stdout
@ -46,7 +47,7 @@ func getGitConfigValue(key string) (string, error) {
if exitError, ok := err.(*exec.ExitError); ok {
if waitStatus, ok := exitError.Sys().(syscall.WaitStatus); ok {
if waitStatus.ExitStatus() == 1 {
return "", fmt.Errorf("the key `%s` is not found", key)
return "", fmt.Errorf("the key is not found for %s", cmd.Args)
}
}
return "", err

View File

@ -2,39 +2,18 @@ package gui
import (
"fmt"
"regexp"
"strings"
"github.com/jesseduffield/lazygit/pkg/utils"
)
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.Tr.NotAGitFlowBranch)
func (gui *Gui) gitFlowFinishBranch(branchName string) error {
cmdObj, err := gui.GitCommand.Flow.FinishCmdObj(branchName)
if err != nil {
return gui.surfaceError(err)
}
gui.logAction(gui.Tr.Actions.GitFlowFinish)
return gui.runSubprocessWithSuspenseAndRefresh(
gui.GitCommand.Cmd.New("git flow " + branchType + " finish " + suffix),
)
return gui.runSubprocessWithSuspenseAndRefresh(cmdObj)
}
func (gui *Gui) handleCreateGitFlowMenu() error {
@ -43,9 +22,7 @@ func (gui *Gui) handleCreateGitFlowMenu() error {
return nil
}
// get config
gitFlowConfig, err := gui.GitCommand.Cmd.New("git config --local --get-regexp gitflow").RunWithOutput()
if err != nil {
if !gui.GitCommand.Flow.GitFlowEnabled() {
return gui.createErrorPanel("You need to install git-flow and enable it in this repo to use git-flow features")
}
@ -58,7 +35,7 @@ func (gui *Gui) handleCreateGitFlowMenu() error {
handleConfirm: func(name string) error {
gui.logAction(gui.Tr.Actions.GitFlowStart)
return gui.runSubprocessWithSuspenseAndRefresh(
gui.GitCommand.Cmd.New("git flow " + branchType + " start " + name),
gui.GitCommand.Flow.StartCmdObj(branchType, name),
)
},
})
@ -70,7 +47,7 @@ func (gui *Gui) handleCreateGitFlowMenu() error {
// not localising here because it's one to one with the actual git flow commands
displayString: fmt.Sprintf("finish branch '%s'", branch.Name),
onPress: func() error {
return gui.gitFlowFinishBranch(gitFlowConfig, branch.Name)
return gui.gitFlowFinishBranch(branch.Name)
},
},
{