1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-04 23:37:41 +02:00

get branches with git for-each-ref

This commit is contained in:
Jesse Duffield 2019-11-17 10:04:06 +11:00
parent 6bd0979b4a
commit b7f2d0366b
2 changed files with 12 additions and 40 deletions

View File

@ -17,7 +17,6 @@ type Branch struct {
Recency string Recency string
Pushables string Pushables string
Pullables string Pullables string
Hash string
Selected bool Selected bool
} }

View File

@ -1,46 +1,17 @@
package commands package commands
import ( import (
"io/ioutil" "fmt"
"os" "regexp"
"path/filepath"
"strings"
) )
func (c *GitCommand) GetBranchesFromDir(dirPath string) ([]*Branch, error) { func (c *GitCommand) GetRemotes() ([]*Remote, error) {
branches := []*Branch{} // get remote branches
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error { remoteBranchesStr, err := c.OSCommand.RunCommandWithOutput("git for-each-ref --format='%(refname:strip=2)' refs/remotes")
if err != nil {
// it's possible that go-git is referencing a remote we don't have locally
// in which case we'll just swallow this error
c.Log.Warn(err)
return nil
}
if info.IsDir() {
return nil
}
// it's a file: we need to get the path and work out the branch name from that
fileContents, err := ioutil.ReadFile(path)
if err != nil {
return err
}
branches = append(branches, &Branch{
Name: strings.TrimPrefix(path, dirPath)[1:], // stripping prefix slash
Hash: strings.TrimSpace(string(fileContents)),
})
return nil
})
if err != nil { if err != nil {
return nil, err return nil, err
} }
return branches, nil
}
func (c *GitCommand) GetRemotes() ([]*Remote, error) {
goGitRemotes, err := c.Repo.Remotes() goGitRemotes, err := c.Repo.Remotes()
if err != nil { if err != nil {
return nil, err return nil, err
@ -51,11 +22,13 @@ func (c *GitCommand) GetRemotes() ([]*Remote, error) {
for i, goGitRemote := range goGitRemotes { for i, goGitRemote := range goGitRemotes {
name := goGitRemote.Config().Name name := goGitRemote.Config().Name
// can't seem to easily get the branches of the remotes from go-git so we'll re := regexp.MustCompile(fmt.Sprintf("%s\\/(.*)", name))
// traverse the directory recursively matches := re.FindAllStringSubmatch(remoteBranchesStr, -1)
branches, err := c.GetBranchesFromDir(filepath.Join(".git", "refs", "remotes", name)) branches := make([]*Branch, len(matches))
if err != nil { for j, match := range matches {
return nil, err branches[j] = &Branch{
Name: match[1],
}
} }
remotes[i] = &Remote{ remotes[i] = &Remote{