1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-19 00:28:03 +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