2021-12-30 08:36:21 +02:00
|
|
|
package loaders
|
2019-11-16 07:00:47 +02:00
|
|
|
|
|
|
|
import (
|
2019-11-17 01:04:06 +02:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2019-11-17 01:23:06 +02:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2020-09-29 10:41:06 +02:00
|
|
|
|
2021-12-30 08:36:21 +02:00
|
|
|
gogit "github.com/jesseduffield/go-git/v5"
|
2020-09-29 12:28:39 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
2021-12-30 08:36:21 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/common"
|
2019-11-16 07:00:47 +02:00
|
|
|
)
|
|
|
|
|
2021-12-30 08:36:21 +02:00
|
|
|
type RemoteLoader struct {
|
|
|
|
*common.Common
|
|
|
|
cmd oscommands.ICmdObjBuilder
|
|
|
|
getGoGitRemotes func() ([]*gogit.Remote, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRemoteLoader(
|
|
|
|
common *common.Common,
|
|
|
|
cmd oscommands.ICmdObjBuilder,
|
|
|
|
getGoGitRemotes func() ([]*gogit.Remote, error),
|
|
|
|
) *RemoteLoader {
|
|
|
|
return &RemoteLoader{
|
|
|
|
Common: common,
|
|
|
|
cmd: cmd,
|
|
|
|
getGoGitRemotes: getGoGitRemotes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *RemoteLoader) GetRemotes() ([]*models.Remote, error) {
|
2022-01-05 02:57:32 +02:00
|
|
|
remoteBranchesStr, err := self.cmd.New("git branch -r").DontLog().RunWithOutput()
|
2019-11-16 07:00:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-30 08:36:21 +02:00
|
|
|
goGitRemotes, err := self.getGoGitRemotes()
|
2019-11-16 07:00:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// first step is to get our remotes from go-git
|
2020-09-29 10:41:06 +02:00
|
|
|
remotes := make([]*models.Remote, len(goGitRemotes))
|
2019-11-16 07:00:47 +02:00
|
|
|
for i, goGitRemote := range goGitRemotes {
|
2019-11-17 03:07:36 +02:00
|
|
|
remoteName := goGitRemote.Config().Name
|
2019-11-16 07:00:47 +02:00
|
|
|
|
2021-12-23 09:35:29 +02:00
|
|
|
re := regexp.MustCompile(fmt.Sprintf(`(?m)^\s*%s\/([\S]+)`, remoteName))
|
2019-11-17 01:04:06 +02:00
|
|
|
matches := re.FindAllStringSubmatch(remoteBranchesStr, -1)
|
2020-09-29 10:41:06 +02:00
|
|
|
branches := make([]*models.RemoteBranch, len(matches))
|
2019-11-17 01:04:06 +02:00
|
|
|
for j, match := range matches {
|
2020-09-29 10:41:06 +02:00
|
|
|
branches[j] = &models.RemoteBranch{
|
2019-11-17 03:07:36 +02:00
|
|
|
Name: match[1],
|
|
|
|
RemoteName: remoteName,
|
2019-11-17 01:04:06 +02:00
|
|
|
}
|
2019-11-16 07:00:47 +02:00
|
|
|
}
|
|
|
|
|
2020-09-29 10:41:06 +02:00
|
|
|
remotes[i] = &models.Remote{
|
2019-11-16 07:00:47 +02:00
|
|
|
Name: goGitRemote.Config().Name,
|
|
|
|
Urls: goGitRemote.Config().URLs,
|
|
|
|
Branches: branches,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-17 01:23:06 +02:00
|
|
|
// now lets sort our remotes by name alphabetically
|
|
|
|
sort.Slice(remotes, func(i, j int) bool {
|
|
|
|
// we want origin at the top because we'll be most likely to want it
|
|
|
|
if remotes[i].Name == "origin" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if remotes[j].Name == "origin" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return strings.ToLower(remotes[i].Name) < strings.ToLower(remotes[j].Name)
|
|
|
|
})
|
|
|
|
|
2019-11-16 07:00:47 +02:00
|
|
|
return remotes, nil
|
|
|
|
}
|