Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ See the [docs](docs/Custom_Command_Keybindings.md)

### Git flow support

Lazygit supports [Gitflow](https://github.com/nvie/gitflow) if you have it installed. To understand how the Gitflow model works check out Vincent Driessen's original [post](https://nvie.com/posts/a-successful-git-branching-model/) explaining it. To view Gitflow options from within Lazygit, press `i` from within the branches view.
Lazygit supports [Gitflow](https://github.com/nvie/gitflow) (or [git-flow-next](https://github.com/gittower/git-flow-next)) if you have it installed. To understand how the Gitflow model works check out Vincent Driessen's original [post](https://nvie.com/posts/a-successful-git-branching-model/) explaining it. To view Gitflow options from within Lazygit, press `i` from within the branches view.

## Contributing

Expand Down
63 changes: 61 additions & 2 deletions pkg/commands/git_commands/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package git_commands

import (
"regexp"
"strings"

"github.com/jesseduffield/lazygit/pkg/commands/git_config"
Expand Down Expand Up @@ -112,8 +113,66 @@ func (self *ConfigCommands) Branches(cmd oscommands.ICmdObjBuilder) map[string]*
return result
}

func (self *ConfigCommands) GetGitFlowPrefixes() string {
return self.gitConfig.GetGeneral("--local --get-regexp gitflow.prefix")
// git-flow config key patterns: legacy uses gitflow.prefix.<type>, git-flow-next uses gitflow.branch.<type>.prefix
const (
gitFlowLegacyConfigArgs = "--local --get-regexp gitflow.prefix"
gitFlowNextConfigArgs = "--local --get-regexp gitflow\\.branch\\..*\\.prefix"
)

func (self *ConfigCommands) getGitFlowPrefixes() string {
return self.gitConfig.GetGeneral(gitFlowLegacyConfigArgs)
}

func (self *ConfigCommands) getGitFlowNextPrefixes() string {
return self.gitConfig.GetGeneral(gitFlowNextConfigArgs)
}

// parseGitFlowLines parses lines matching re (submatch 1 = branch type, 2 = prefix) into prefixToType.
// When overwrite is false, existing keys are left unchanged so legacy entries win over next.
func parseGitFlowLines(output string, re *regexp.Regexp, prefixToType map[string]string, overwrite bool) {
for line := range strings.SplitSeq(output, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
if m := re.FindStringSubmatch(line); len(m) == 3 {
prefix := normalizeGitFlowPrefix(m[2])
if prefix == "" {
continue
}
if overwrite || prefixToType[prefix] == "" {
prefixToType[prefix] = m[1]
}
}
}
}

// parseGitFlowPrefixMap parses legacy and git-flow-next config output into a unified prefix → branchType map.
// Legacy line format: "gitflow.prefix.<type> <prefix>"
// Next line format: "gitflow.branch.<type>.prefix <prefix>"
// Prefixes are normalized to end in "/". Legacy entries win on duplicate prefix.
func parseGitFlowPrefixMap(legacyOutput, nextOutput string) map[string]string {
legacyRegexp := regexp.MustCompile(`gitflow\.prefix\.(\S+)\s+(.*)`)
nextRegexp := regexp.MustCompile(`gitflow\.branch\.([^.]+)\.prefix\s+(.*)`)
prefixToType := make(map[string]string)
parseGitFlowLines(legacyOutput, legacyRegexp, prefixToType, true)
parseGitFlowLines(nextOutput, nextRegexp, prefixToType, false)
return prefixToType
}

func normalizeGitFlowPrefix(prefix string) string {
prefix = strings.TrimSpace(prefix)
if prefix == "" {
return ""
}
if !strings.HasSuffix(prefix, "/") {
return prefix + "/"
}
return prefix
}

func (self *ConfigCommands) GetGitFlowPrefixMap() map[string]string {
return parseGitFlowPrefixMap(self.getGitFlowPrefixes(), self.getGitFlowNextPrefixes())
}

func (self *ConfigCommands) GetCoreCommentChar() byte {
Expand Down
127 changes: 127 additions & 0 deletions pkg/commands/git_commands/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package git_commands

import (
"testing"

"github.com/jesseduffield/lazygit/pkg/commands/git_config"
"github.com/jesseduffield/lazygit/pkg/common"
"github.com/stretchr/testify/assert"
)

func TestParseGitFlowPrefixMap(t *testing.T) {
type scenario struct {
testName string
legacyOutput string
nextOutput string
expected map[string]string
}
scenarios := []scenario{
{
testName: "empty inputs",
legacyOutput: "",
nextOutput: "",
expected: map[string]string{},
},
{
testName: "legacy only",
legacyOutput: "gitflow.prefix.feature feature/\ngitflow.prefix.hotfix hotfix/",
nextOutput: "",
expected: map[string]string{"feature/": "feature", "hotfix/": "hotfix"},
},
{
testName: "next only",
legacyOutput: "",
nextOutput: "gitflow.branch.feature.prefix feature/\ngitflow.branch.release.prefix release/",
expected: map[string]string{"feature/": "feature", "release/": "release"},
},
{
testName: "legacy wins on duplicate prefix",
legacyOutput: "gitflow.prefix.foo feature/",
nextOutput: "gitflow.branch.bar.prefix feature/",
expected: map[string]string{"feature/": "foo"},
},
{
testName: "prefix normalized with trailing slash from legacy",
legacyOutput: "gitflow.prefix.feature feature",
nextOutput: "",
expected: map[string]string{"feature/": "feature"},
},
{
testName: "malformed legacy lines skipped",
legacyOutput: "gitflow.prefix.feature feature/\nnot-a-valid-line\ngitflow.prefix.hotfix hotfix/",
nextOutput: "",
expected: map[string]string{"feature/": "feature", "hotfix/": "hotfix"},
},
{
testName: "blank lines and whitespace ignored",
legacyOutput: " \n gitflow.prefix.feature feature/ \n \n ",
nextOutput: "",
expected: map[string]string{"feature/": "feature"},
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
got := parseGitFlowPrefixMap(s.legacyOutput, s.nextOutput)
assert.Equal(t, s.expected, got)
})
}
}

func TestGetGitFlowPrefixMap(t *testing.T) {
type scenario struct {
testName string
gitConfigMockResponses map[string]string
expected map[string]string
}
scenarios := []scenario{
{
testName: "empty when both queries empty",
gitConfigMockResponses: nil,
expected: map[string]string{},
},
{
testName: "correct map from legacy-only output",
gitConfigMockResponses: map[string]string{
"--local --get-regexp gitflow.prefix": "gitflow.prefix.feature feature/\ngitflow.prefix.hotfix hotfix/",
},
expected: map[string]string{"feature/": "feature", "hotfix/": "hotfix"},
},
{
testName: "correct map from git-flow-next-only output",
gitConfigMockResponses: map[string]string{
"--local --get-regexp gitflow\\.branch\\..*\\.prefix": "gitflow.branch.feature.prefix feature/\ngitflow.branch.release.prefix release/",
},
expected: map[string]string{"feature/": "feature", "release/": "release"},
},
{
testName: "merged map with legacy winning when both have same prefix",
gitConfigMockResponses: map[string]string{
"--local --get-regexp gitflow.prefix": "gitflow.prefix.foo feature/",
"--local --get-regexp gitflow\\.branch\\..*\\.prefix": "gitflow.branch.bar.prefix feature/",
},
expected: map[string]string{"feature/": "foo"},
},
{
testName: "prefix normalized with trailing slash",
gitConfigMockResponses: map[string]string{
"--local --get-regexp gitflow.prefix": "gitflow.prefix.feature feature",
},
expected: map[string]string{"feature/": "feature"},
},
{
testName: "malformed lines skipped",
gitConfigMockResponses: map[string]string{
"--local --get-regexp gitflow.prefix": "gitflow.prefix.feature feature/\nnot-a-valid-line\n",
},
expected: map[string]string{"feature/": "feature"},
},
}

for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
config := NewConfigCommands(common.NewDummyCommon(), git_config.NewFakeGitConfig(s.gitConfigMockResponses))
got := config.GetGitFlowPrefixMap()
assert.Equal(t, s.expected, got)
})
}
}
26 changes: 7 additions & 19 deletions pkg/commands/git_commands/flow.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package git_commands

import (
"regexp"
"strings"

"github.com/go-errors/errors"
Expand All @@ -21,30 +20,19 @@ func NewFlowCommands(
}

func (self *FlowCommands) GitFlowEnabled() bool {
return self.config.GetGitFlowPrefixes() != ""
return len(self.config.GetGitFlowPrefixMap()) > 0
}

func (self *FlowCommands) FinishCmdObj(branchName string) (*oscommands.CmdObj, error) {
prefixes := self.config.GetGitFlowPrefixes()
prefixMap := self.config.GetGitFlowPrefixMap()

// 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.SplitSeq(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
}
}
prefixPart, suffix, ok := strings.Cut(branchName, "/")
if !ok || prefixPart == "" || suffix == "" {
return nil, errors.New(self.Tr.NotAGitFlowBranch)
}
prefix := prefixPart + "/"

branchType := prefixMap[prefix]
if branchType == "" {
return nil, errors.New(self.Tr.NotAGitFlowBranch)
}
Expand Down
Loading
Loading