1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-03-03 15:02:52 +02:00
lazygit/pkg/commands/loading_remotes.go

43 lines
955 B
Go
Raw Normal View History

package commands
import (
2019-11-17 10:04:06 +11:00
"fmt"
"regexp"
)
2019-11-17 10:04:06 +11:00
func (c *GitCommand) GetRemotes() ([]*Remote, error) {
// get remote branches
remoteBranchesStr, err := c.OSCommand.RunCommandWithOutput("git for-each-ref --format='%(refname:strip=2)' refs/remotes")
if err != nil {
return nil, err
}
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
2019-11-17 10:04:06 +11:00
re := regexp.MustCompile(fmt.Sprintf("%s\\/(.*)", name))
matches := re.FindAllStringSubmatch(remoteBranchesStr, -1)
branches := make([]*Branch, len(matches))
for j, match := range matches {
branches[j] = &Branch{
Name: match[1],
}
}
remotes[i] = &Remote{
Name: goGitRemote.Config().Name,
Urls: goGitRemote.Config().URLs,
Branches: branches,
}
}
return remotes, nil
}