1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2026-06-20 01:19:23 +02:00

Don't use go-git to parse branches

As with the previous commit, the code to parse `git config --get-regex` output
on our end isn't so bad.
This commit is contained in:
Stefan Haller
2026-03-26 10:15:44 +01:00
parent 23f973496f
commit 1da87b9a7c
2 changed files with 42 additions and 12 deletions
+3 -7
View File
@@ -9,7 +9,6 @@ import (
"time"
"github.com/jesseduffield/generics/set"
"github.com/jesseduffield/go-git/v5/config"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/common"
@@ -30,7 +29,7 @@ import (
// can just pull them out of here and put them there and then call them from in here
type BranchLoaderConfigCommands interface {
Branches() (map[string]*config.Branch, error)
Branches(cmd oscommands.ICmdObjBuilder) map[string]*BranchConfig
}
type BranchInfo struct {
@@ -119,16 +118,13 @@ func (self *BranchLoader) Load(reflogCommits []*models.Commit,
branches = utils.Prepend(branches, &models.Branch{Name: info.RefName, DisplayName: info.DisplayName, Head: true, DetachedHead: info.DetachedHead, Recency: " *"})
}
configBranches, err := self.config.Branches()
if err != nil {
return nil, err
}
configBranches := self.config.Branches(self.cmd)
for _, branch := range branches {
match := configBranches[branch.Name]
if match != nil {
branch.UpstreamRemote = match.Remote
branch.UpstreamBranch = match.Merge.Short()
branch.UpstreamBranch = match.Merge
}
// If the branch already existed, take over its BehindBaseBranch value
+39 -5
View File
@@ -1,12 +1,20 @@
package git_commands
import (
"strings"
gogit "github.com/jesseduffield/go-git/v5"
"github.com/jesseduffield/go-git/v5/config"
"github.com/jesseduffield/lazygit/pkg/commands/git_config"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/common"
)
// BranchConfig holds the tracking configuration for a branch.
type BranchConfig struct {
Remote string
Merge string // short ref name of upstream branch
}
type ConfigCommands struct {
*common.Common
@@ -72,13 +80,39 @@ func (self *ConfigCommands) GetPushToCurrent() bool {
}
// returns the repo's branches as specified in the git config
func (self *ConfigCommands) Branches() (map[string]*config.Branch, error) {
conf, err := self.repo.Config()
func (self *ConfigCommands) Branches(cmd oscommands.ICmdObjBuilder) map[string]*BranchConfig {
cmdArgs := NewGitCmd("config").
Arg("--local", "--get-regexp", `^branch\.`).ToArgv()
output, err := cmd.New(cmdArgs).DontLog().RunWithOutput()
if err != nil {
return nil, err
// exit code 1 means no matching keys (no branches with config)
return nil
}
return conf.Branches, nil
result := make(map[string]*BranchConfig)
for _, line := range strings.Split(output, "\n") {
key, value, found := strings.Cut(strings.TrimSpace(line), " ")
if !found {
continue
}
// key is like "branch.<name>.remote" or "branch.<name>.merge"
lastDot := strings.LastIndex(key, ".")
if lastDot < 0 {
continue
}
configKey := key[lastDot+1:]
branchName := key[len("branch."):lastDot]
if _, ok := result[branchName]; !ok {
result[branchName] = &BranchConfig{}
}
switch configKey {
case "remote":
result[branchName].Remote = value
case "merge":
result[branchName].Merge = strings.TrimPrefix(value, "refs/heads/")
}
}
return result
}
func (self *ConfigCommands) GetGitFlowPrefixes() string {