ref(git): replace git's native -sort with a thrid-party function

Signed-off-by: storyicon <storyicon@foxmail.com>
This commit is contained in:
storyicon 2021-08-01 17:11:54 +08:00
parent 3953017597
commit 7b2c0762d4
7 changed files with 95 additions and 62 deletions

View file

@ -24,6 +24,7 @@ import (
"strings"
"github.com/mholt/archiver"
"github.com/storyicon/powerproto/pkg/util"
"github.com/storyicon/powerproto/pkg/util/command"
"github.com/storyicon/powerproto/pkg/util/logger"
@ -53,31 +54,10 @@ func GetGitLatestCommitId(ctx context.Context, log logger.Logger, repo string) (
return f[0], nil
}
// ListGitCommitIds is used to list git commit ids
func ListGitCommitIds(ctx context.Context, log logger.Logger, repo string) ([]string, error) {
data, err := command.Execute(ctx, log, "", "git", []string{
"ls-remote", repo,
}, nil)
if err != nil {
return nil, &ErrGitList{
ErrCommandExec: err.(*command.ErrCommandExec),
}
}
var commitIds []string
for _, line := range strings.Split(string(data), "\n") {
f := strings.Fields(line)
if len(f) != 2 {
continue
}
commitIds = append(commitIds, f[0])
}
return commitIds, nil
}
// ListGitTags is used to list the git tags of specified repository
func ListGitTags(ctx context.Context, log logger.Logger, repo string) ([]string, error) {
data, err := command.Execute(ctx, log, "", "git", []string{
"ls-remote", "--tags", "--refs", "--sort", "version:refname", repo,
"ls-remote", "--tags", "--refs", repo,
}, nil)
if err != nil {
return nil, &ErrGitList{
@ -94,7 +74,8 @@ func ListGitTags(ctx context.Context, log logger.Logger, repo string) ([]string,
tags = append(tags, strings.TrimPrefix(f[1], "refs/tags/"))
}
}
return tags, nil
malformed, wellFormed := util.SortSemanticVersion(tags)
return append(malformed, wellFormed...), nil
}
// GithubArchive is github archive

View file

@ -45,8 +45,6 @@ type PluginManager interface {
GetGitRepoLatestVersion(ctx context.Context, uri string) (string, error)
// InstallGitRepo is used to install google apis
InstallGitRepo(ctx context.Context, uri string, commitId string) (local string, err error)
// ListGitRepoVersions is used to list protoc version
ListGitRepoVersions(ctx context.Context, uri string) ([]string, error)
// IsGitRepoInstalled is used to check whether the protoc is installed
IsGitRepoInstalled(ctx context.Context, uri string, commitId string) (bool, string, error)
// GitRepoPath returns the googleapis path
@ -143,14 +141,7 @@ func (b *BasicPluginManager) InstallPlugin(ctx context.Context, path string, ver
// GetGitRepoLatestVersion is used to get the latest version of google apis
func (b *BasicPluginManager) GetGitRepoLatestVersion(ctx context.Context, url string) (string, error) {
versions, err := b.ListGitRepoVersions(ctx, url)
if err != nil {
return "", err
}
if len(versions) == 0 {
return "", errors.New("no version list")
}
return versions[len(versions)-1], nil
return GetGitLatestCommitId(ctx, b.Logger, url)
}
// InstallGitRepo is used to install google apis
@ -180,27 +171,6 @@ func (b *BasicPluginManager) InstallGitRepo(ctx context.Context, uri string, com
return local, nil
}
// ListGitRepoVersions is used to list protoc version
func (b *BasicPluginManager) ListGitRepoVersions(ctx context.Context, uri string) ([]string, error) {
ctx, cancel := context.WithTimeout(ctx, defaultExecuteTimeout)
defer cancel()
b.versionsLock.RLock()
versions, ok := b.versions[uri]
b.versionsLock.RUnlock()
if ok {
return versions, nil
}
versions, err := ListGitCommitIds(ctx, b.Logger, uri)
if err != nil {
return nil, err
}
b.versionsLock.Lock()
b.versions[uri] = versions
b.versionsLock.Unlock()
return versions, nil
}
// IsGitRepoInstalled is used to check whether the protoc is installed
func (b *BasicPluginManager) IsGitRepoInstalled(ctx context.Context, uri string, commitId string) (bool, string, error) {
codePath, err := PathForGitReposCode(b.storageDir, uri, commitId)

View file

@ -25,7 +25,7 @@ import (
"github.com/storyicon/powerproto/pkg/util/logger"
)
var _ = Describe("Pluginmanager", func() {
var _ = Describe("PluginManager", func() {
cfg := pluginmanager.NewConfig()
cfg.StorageDir, _ = filepath.Abs("./tests")
@ -72,12 +72,9 @@ var _ = Describe("Pluginmanager", func() {
})
It("should able to install git repos", func() {
const uri = "https://github.com/gogo/protobuf"
versions, err := manager.ListGitRepoVersions(context.TODO(), uri)
Expect(err).To(BeNil())
Expect(len(versions) > 0).To(BeTrue())
latestVersion, err := manager.GetGitRepoLatestVersion(context.TODO(), uri)
Expect(err).To(BeNil())
Expect(latestVersion).To(Equal(versions[len(versions)-1]))
Expect(len(latestVersion) > 0).To(Equal(true))
local, err := manager.InstallGitRepo(context.TODO(), uri, latestVersion)
Expect(err).To(BeNil())