From 610e503296b36ef1d0551c957a6c593fae4cd60c Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Fri, 7 Jan 2022 19:56:33 +1100 Subject: refactor git flow --- pkg/commands/config.go | 4 +++ pkg/commands/flow.go | 64 ++++++++++++++++++++++++++++++++++++++ pkg/commands/git.go | 3 ++ pkg/commands/git_config/get_key.go | 5 +-- pkg/gui/git_flow.go | 39 +++++------------------ 5 files changed, 82 insertions(+), 33 deletions(-) create mode 100644 pkg/commands/flow.go (limited to 'pkg') diff --git a/pkg/commands/config.go b/pkg/commands/config.go index 52eb82ac3..c5f56c7b4 100644 --- a/pkg/commands/config.go +++ b/pkg/commands/config.go @@ -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.") +} diff --git a/pkg/commands/flow.go b/pkg/commands/flow.go new file mode 100644 index 000000000..9506a7e85 --- /dev/null +++ b/pkg/commands/flow.go @@ -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) +} diff --git a/pkg/commands/git.go b/pkg/commands/git.go index f9dd0c8a4..4ca2631cf 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -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), diff --git a/pkg/commands/git_config/get_key.go b/pkg/commands/git_config/get_key.go index 85892ea2d..4ad5ae63e 100644 --- a/pkg/commands/git_config/get_key.go +++ b/pkg/commands/git_config/get_key.go @@ -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 diff --git a/pkg/gui/git_flow.go b/pkg/gui/git_flow.go index 61acffbb0..959f22788 100644 --- a/pkg/gui/git_flow.go +++ b/pkg/gui/git_flow.go @@ -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) }, }, { -- cgit v1.2.3