diff --git a/pkg/commands/git_commands/branch_loader.go b/pkg/commands/git_commands/branch_loader.go index a4e41b756..c01b204e0 100644 --- a/pkg/commands/git_commands/branch_loader.go +++ b/pkg/commands/git_commands/branch_loader.go @@ -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 diff --git a/pkg/commands/git_commands/config.go b/pkg/commands/git_commands/config.go index a9fd4e147..d42d060c8 100644 --- a/pkg/commands/git_commands/config.go +++ b/pkg/commands/git_commands/config.go @@ -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..remote" or "branch..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 {