2022-11-11 04:19:29 +02:00
|
|
|
package git_commands
|
2019-11-16 07:00:47 +02:00
|
|
|
|
|
|
|
import (
|
2019-11-17 01:23:06 +02:00
|
|
|
"strings"
|
2023-07-29 00:16:48 +02:00
|
|
|
"sync"
|
2020-09-29 10:41:06 +02:00
|
|
|
|
2022-03-19 06:36:46 +02:00
|
|
|
"github.com/jesseduffield/generics/slices"
|
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"
|
2023-07-29 00:16:48 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
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) {
|
2023-07-29 00:16:48 +02:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
var remoteBranchesByRemoteName map[string][]*models.RemoteBranch
|
|
|
|
var remoteBranchesErr error
|
|
|
|
go utils.Safe(func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
remoteBranchesByRemoteName, remoteBranchesErr = self.getRemoteBranchesByRemoteName()
|
|
|
|
})
|
2019-11-16 07:00:47 +02:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:16:48 +02:00
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if remoteBranchesErr != nil {
|
|
|
|
return nil, remoteBranchesErr
|
|
|
|
}
|
|
|
|
|
2022-03-19 10:51:48 +02:00
|
|
|
remotes := slices.Map(goGitRemotes, func(goGitRemote *gogit.Remote) *models.Remote {
|
2019-11-17 03:07:36 +02:00
|
|
|
remoteName := goGitRemote.Config().Name
|
2023-07-29 00:16:48 +02:00
|
|
|
branches := remoteBranchesByRemoteName[remoteName]
|
2019-11-16 07:00:47 +02:00
|
|
|
|
2022-03-19 06:36:46 +02:00
|
|
|
return &models.Remote{
|
2019-11-16 07:00:47 +02:00
|
|
|
Name: goGitRemote.Config().Name,
|
|
|
|
Urls: goGitRemote.Config().URLs,
|
|
|
|
Branches: branches,
|
|
|
|
}
|
2022-03-19 06:36:46 +02:00
|
|
|
})
|
2019-11-16 07:00:47 +02:00
|
|
|
|
2019-11-17 01:23:06 +02:00
|
|
|
// now lets sort our remotes by name alphabetically
|
2022-03-19 06:36:46 +02:00
|
|
|
slices.SortFunc(remotes, func(a, b *models.Remote) bool {
|
2019-11-17 01:23:06 +02:00
|
|
|
// we want origin at the top because we'll be most likely to want it
|
2022-03-19 06:36:46 +02:00
|
|
|
if a.Name == "origin" {
|
2019-11-17 01:23:06 +02:00
|
|
|
return true
|
|
|
|
}
|
2022-03-19 06:36:46 +02:00
|
|
|
if b.Name == "origin" {
|
2019-11-17 01:23:06 +02:00
|
|
|
return false
|
|
|
|
}
|
2022-03-19 06:36:46 +02:00
|
|
|
return strings.ToLower(a.Name) < strings.ToLower(b.Name)
|
2019-11-17 01:23:06 +02:00
|
|
|
})
|
|
|
|
|
2019-11-16 07:00:47 +02:00
|
|
|
return remotes, nil
|
|
|
|
}
|
2023-07-29 00:16:48 +02:00
|
|
|
|
|
|
|
func (self *RemoteLoader) getRemoteBranchesByRemoteName() (map[string][]*models.RemoteBranch, error) {
|
|
|
|
remoteBranchesByRemoteName := make(map[string][]*models.RemoteBranch)
|
|
|
|
|
|
|
|
cmdArgs := NewGitCmd("branch").Arg("-r").ToArgv()
|
|
|
|
err := self.cmd.New(cmdArgs).DontLog().RunAndProcessLines(func(line string) (bool, error) {
|
|
|
|
// excluding lines like 'origin/HEAD -> origin/master' (there will be a separate
|
|
|
|
// line for 'origin/master')
|
|
|
|
if strings.Contains(line, "->") {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
|
|
|
|
split := strings.SplitN(line, "/", 2)
|
|
|
|
if len(split) != 2 {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
remoteName := split[0]
|
|
|
|
name := split[1]
|
|
|
|
|
|
|
|
_, ok := remoteBranchesByRemoteName[remoteName]
|
|
|
|
if !ok {
|
|
|
|
remoteBranchesByRemoteName[remoteName] = []*models.RemoteBranch{}
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteBranchesByRemoteName[remoteName] = append(remoteBranchesByRemoteName[remoteName],
|
|
|
|
&models.RemoteBranch{
|
|
|
|
Name: name,
|
|
|
|
RemoteName: remoteName,
|
|
|
|
})
|
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return remoteBranchesByRemoteName, nil
|
|
|
|
}
|