mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-05-17 22:32:58 +02:00
refactor git flow
This commit is contained in:
parent
e92076d2c2
commit
610e503296
@ -93,3 +93,7 @@ func (self *ConfigCommands) Branches() (map[string]*config.Branch, error) {
|
|||||||
|
|
||||||
return conf.Branches, nil
|
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
64
pkg/commands/flow.go
Normal 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)
|
||||||
|
}
|
@ -37,6 +37,7 @@ type GitCommand struct {
|
|||||||
Patch *PatchCommands
|
Patch *PatchCommands
|
||||||
Remote *RemoteCommands
|
Remote *RemoteCommands
|
||||||
Sync *SyncCommands
|
Sync *SyncCommands
|
||||||
|
Flow *FlowCommands
|
||||||
}
|
}
|
||||||
|
|
||||||
type Loaders struct {
|
type Loaders struct {
|
||||||
@ -94,6 +95,7 @@ func NewGitCommandAux(
|
|||||||
configCommands := NewConfigCommands(cmn, gitConfig)
|
configCommands := NewConfigCommands(cmn, gitConfig)
|
||||||
statusCommands := NewStatusCommands(cmn, osCommand, repo, dotGitDir)
|
statusCommands := NewStatusCommands(cmn, osCommand, repo, dotGitDir)
|
||||||
fileLoader := loaders.NewFileLoader(cmn, cmd, configCommands)
|
fileLoader := loaders.NewFileLoader(cmn, cmd, configCommands)
|
||||||
|
flowCommands := NewFlowCommands(cmn, cmd, configCommands)
|
||||||
remoteCommands := NewRemoteCommands(cmn, cmd)
|
remoteCommands := NewRemoteCommands(cmn, cmd)
|
||||||
branchCommands := NewBranchCommands(cmn, cmd)
|
branchCommands := NewBranchCommands(cmn, cmd)
|
||||||
syncCommands := NewSyncCommands(cmn, cmd)
|
syncCommands := NewSyncCommands(cmn, cmd)
|
||||||
@ -132,6 +134,7 @@ func NewGitCommandAux(
|
|||||||
Patch: patchCommands,
|
Patch: patchCommands,
|
||||||
Remote: remoteCommands,
|
Remote: remoteCommands,
|
||||||
Sync: syncCommands,
|
Sync: syncCommands,
|
||||||
|
Flow: flowCommands,
|
||||||
Loaders: Loaders{
|
Loaders: Loaders{
|
||||||
Commits: loaders.NewCommitLoader(cmn, cmd, dotGitDir, branchCommands.CurrentBranchName, statusCommands.RebaseMode),
|
Commits: loaders.NewCommitLoader(cmn, cmd, dotGitDir, branchCommands.CurrentBranchName, statusCommands.RebaseMode),
|
||||||
Branches: loaders.NewBranchLoader(cmn, branchCommands.GetRawBranches, branchCommands.CurrentBranchName),
|
Branches: loaders.NewBranchLoader(cmn, branchCommands.GetRawBranches, branchCommands.CurrentBranchName),
|
||||||
|
@ -36,7 +36,8 @@ import (
|
|||||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
func getGitConfigValue(key string) (string, error) {
|
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
|
var stdout bytes.Buffer
|
||||||
cmd := secureexec.Command("git", gitArgs...)
|
cmd := secureexec.Command("git", gitArgs...)
|
||||||
cmd.Stdout = &stdout
|
cmd.Stdout = &stdout
|
||||||
@ -46,7 +47,7 @@ func getGitConfigValue(key string) (string, error) {
|
|||||||
if exitError, ok := err.(*exec.ExitError); ok {
|
if exitError, ok := err.(*exec.ExitError); ok {
|
||||||
if waitStatus, ok := exitError.Sys().(syscall.WaitStatus); ok {
|
if waitStatus, ok := exitError.Sys().(syscall.WaitStatus); ok {
|
||||||
if waitStatus.ExitStatus() == 1 {
|
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
|
return "", err
|
||||||
|
@ -2,39 +2,18 @@ package gui
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/jesseduffield/lazygit/pkg/utils"
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (gui *Gui) gitFlowFinishBranch(gitFlowConfig string, branchName string) error {
|
func (gui *Gui) gitFlowFinishBranch(branchName string) error {
|
||||||
// need to find out what kind of branch this is
|
cmdObj, err := gui.GitCommand.Flow.FinishCmdObj(branchName)
|
||||||
prefix := strings.SplitAfterN(branchName, "/", 2)[0]
|
if err != nil {
|
||||||
suffix := strings.Replace(branchName, prefix, "", 1)
|
return gui.surfaceError(err)
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gui.logAction(gui.Tr.Actions.GitFlowFinish)
|
gui.logAction(gui.Tr.Actions.GitFlowFinish)
|
||||||
return gui.runSubprocessWithSuspenseAndRefresh(
|
return gui.runSubprocessWithSuspenseAndRefresh(cmdObj)
|
||||||
gui.GitCommand.Cmd.New("git flow " + branchType + " finish " + suffix),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) handleCreateGitFlowMenu() error {
|
func (gui *Gui) handleCreateGitFlowMenu() error {
|
||||||
@ -43,9 +22,7 @@ func (gui *Gui) handleCreateGitFlowMenu() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// get config
|
if !gui.GitCommand.Flow.GitFlowEnabled() {
|
||||||
gitFlowConfig, err := gui.GitCommand.Cmd.New("git config --local --get-regexp gitflow").RunWithOutput()
|
|
||||||
if err != nil {
|
|
||||||
return gui.createErrorPanel("You need to install git-flow and enable it in this repo to use git-flow features")
|
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 {
|
handleConfirm: func(name string) error {
|
||||||
gui.logAction(gui.Tr.Actions.GitFlowStart)
|
gui.logAction(gui.Tr.Actions.GitFlowStart)
|
||||||
return gui.runSubprocessWithSuspenseAndRefresh(
|
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
|
// not localising here because it's one to one with the actual git flow commands
|
||||||
displayString: fmt.Sprintf("finish branch '%s'", branch.Name),
|
displayString: fmt.Sprintf("finish branch '%s'", branch.Name),
|
||||||
onPress: func() error {
|
onPress: func() error {
|
||||||
return gui.gitFlowFinishBranch(gitFlowConfig, branch.Name)
|
return gui.gitFlowFinishBranch(branch.Name)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user