1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-11-26 09:00:57 +02:00

get remote branches when getting remotes

This commit is contained in:
Jesse Duffield 2019-11-16 16:00:47 +11:00
parent eeb667954f
commit 325408d0e3
3 changed files with 71 additions and 37 deletions

View File

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

View File

@ -1061,40 +1061,3 @@ func (c *GitCommand) BeginInteractiveRebaseForCommit(commits []*Commit, commitIn
func (c *GitCommand) SetUpstreamBranch(upstream string) error {
return c.OSCommand.RunCommand(fmt.Sprintf("git branch -u %s", upstream))
}
func (c *GitCommand) GetRemotes() ([]*Remote, error) {
goGitRemotes, err := c.Repo.Remotes()
if err != nil {
return nil, err
}
return nil, nil
remotes := make([]*Remote, len(goGitRemotes))
// TODO: consider including the goGitRemote itself
for i, goGitRemote := range goGitRemotes {
goGitBranches, err := goGitRemote.List(&gogit.ListOptions{})
if err != nil {
c.Log.Warn(err)
continue
}
branches := []*Branch{}
for _, goGitBranch := range goGitBranches {
// for now we're only getting branch references, not tags/notes/etc
if goGitBranch.Name().IsBranch() {
branches = append(branches, &Branch{
Name: goGitBranch.Name().String(),
})
}
}
remotes[i] = &Remote{
Name: goGitRemote.Config().Name,
Urls: goGitRemote.Config().URLs,
Branches: branches,
}
}
return remotes, nil
}

View File

@ -0,0 +1,70 @@
package commands
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func (c *GitCommand) GetBranchesFromDir(dirPath string) ([]*Branch, error) {
branches := []*Branch{}
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
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.Error(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 {
return nil, err
}
return branches, nil
}
func (c *GitCommand) GetRemotes() ([]*Remote, error) {
goGitRemotes, err := c.Repo.Remotes()
if err != nil {
return nil, err
}
// first step is to get our remotes from go-git
remotes := make([]*Remote, len(goGitRemotes))
for i, goGitRemote := range goGitRemotes {
name := goGitRemote.Config().Name
c.Log.Warn(name)
// can't seem to easily get the branches of the remotes from go-git so we'll
// traverse the directory recursively
branches, err := c.GetBranchesFromDir(filepath.Join(".git", "refs", "remotes", name))
if err != nil {
return nil, err
}
remotes[i] = &Remote{
Name: goGitRemote.Config().Name,
Urls: goGitRemote.Config().URLs,
Branches: branches,
}
}
return remotes, nil
}