mirror of
https://github.com/TECHNOFAB11/powerproto.git
synced 2026-02-02 09:25:07 +01:00
ref(git): replace git's native -sort with a thrid-party function
Signed-off-by: storyicon <storyicon@foxmail.com>
This commit is contained in:
parent
3953017597
commit
7b2c0762d4
7 changed files with 95 additions and 62 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
|
|
|
|||
|
|
@ -20,11 +20,41 @@ import (
|
|||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/coreos/go-semver/semver"
|
||||
)
|
||||
|
||||
// DeduplicateSlice is used to deduplicate slice items stably
|
||||
// SortSemanticVersion is used to sort semantic version
|
||||
func SortSemanticVersion(items []string) ([]string, []string) {
|
||||
versionMap := make(map[*semver.Version]string, len(items))
|
||||
versions := make(semver.Versions, 0, len(items))
|
||||
var malformed []string
|
||||
for _, item := range items {
|
||||
s := item
|
||||
if strings.HasPrefix(s, "v") {
|
||||
s = item[1:]
|
||||
}
|
||||
version, err := semver.NewVersion(s)
|
||||
if err != nil {
|
||||
malformed = append(malformed, item)
|
||||
continue
|
||||
}
|
||||
versionMap[version] = item
|
||||
versions = append(versions, version)
|
||||
}
|
||||
sort.Sort(versions)
|
||||
var data []string
|
||||
for _, version := range versions {
|
||||
data = append(data, versionMap[version])
|
||||
}
|
||||
sort.Strings(malformed)
|
||||
return malformed, data
|
||||
}
|
||||
|
||||
// DeduplicateSliceStably is used to deduplicate slice items stably
|
||||
func DeduplicateSliceStably(items []string) []string {
|
||||
data := make([]string, 0, len(items))
|
||||
deduplicate := map[string]struct{}{}
|
||||
|
|
|
|||
|
|
@ -84,4 +84,39 @@ func TestDeduplicateSliceStably(t *testing.T) {
|
|||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSortSemanticVersion(t *testing.T) {
|
||||
type args struct {
|
||||
items []string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want []string
|
||||
want1 []string
|
||||
}{
|
||||
{
|
||||
args: args{
|
||||
items: []string{
|
||||
"v3.0.0-beta-3.1",
|
||||
"v3.0.0-alpha-2",
|
||||
"3.15.0-rc1", "conformance-build-tag", "v2.4.1",
|
||||
},
|
||||
},
|
||||
want: []string{"conformance-build-tag"},
|
||||
want1: []string{"v2.4.1", "v3.0.0-alpha-2", "v3.0.0-beta-3.1", "3.15.0-rc1"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, got1 := SortSemanticVersion(tt.args.items)
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("SortSemanticVersion() got = %v, want %v", got, tt.want)
|
||||
}
|
||||
if !reflect.DeepEqual(got1, tt.want1) {
|
||||
t.Errorf("SortSemanticVersion() got1 = %v, want %v", got1, tt.want1)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue