1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-05 03:23:49 +08:00

fix(global): version compare for semver.

This commit is contained in:
wdvxdr 2021-05-05 17:25:59 +08:00
parent 459f9e0f44
commit 3c6dfa8e48
No known key found for this signature in database
GPG Key ID: 55FF1414A69CEBA6
2 changed files with 29 additions and 7 deletions

View File

@ -13,10 +13,19 @@ func TestVersionNameCompare(t *testing.T) {
remote string remote string
expected bool expected bool
}{ }{
// Normal Tests:
{"v0.9.29-fix2", "v0.9.29-fix2", false}, {"v0.9.29-fix2", "v0.9.29-fix2", false},
{"v0.9.29-fix1", "v0.9.29-fix2", true}, {"v0.9.29-fix1", "v0.9.29-fix2", true},
{"v0.9.29-fix2", "v0.9.29-fix1", false}, {"v0.9.29-fix2", "v0.9.29-fix1", false},
{"v0.9.29-fix2", "v0.9.30", true}, {"v0.9.29-fix2", "v0.9.30", true},
{"v1.0.0-alpha", "v1.0.0-alpha2", true},
{"v1.0.0-alpha2", "v1.0.0-beta1", true},
{"v1.0.0", "v1.0.0-beta1", false},
{"v1.0.0-alpha", "v1.0.0", true},
{"v1.0.0", "v1.0.0", false},
{"v1.0.0-alpha", "v1.0.0-rc1", true},
// Issue Fixes:
{"v1.0.0-beta1", "v0.9.40-fix5", false}, // issue #877 {"v1.0.0-beta1", "v0.9.40-fix5", false}, // issue #877
} }
for i := 0; i < len(tests); i++ { for i := 0; i < len(tests); i++ {

View File

@ -7,6 +7,7 @@ import (
"strings" "strings"
"sync" "sync"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
) )
@ -72,18 +73,30 @@ func EnsureBool(p interface{}, defaultVal bool) bool {
// v0.9.29-fix2 > v0.9.29-fix1 -> false // v0.9.29-fix2 > v0.9.29-fix1 -> false
// //
// v0.9.29-fix2 < v0.9.30 -> true // v0.9.29-fix2 < v0.9.30 -> true
//
// v1.0.0-alpha2 < v1.0.0-beta1 -> true
//
// v1.0.0 > v1.0.0-beta1 -> false
func VersionNameCompare(current, remote string) bool { func VersionNameCompare(current, remote string) bool {
sp := regexp.MustCompile(`[0-9]\d*`) defer func() { // 应该不会panic 为了保险还是加个
cur := sp.FindAllStringSubmatch(current, -1) if err := recover(); err != nil {
re := sp.FindAllStringSubmatch(remote, -1) log.Warn("检查更新失败!")
for i := 0; i < int(math.Min(float64(len(cur)), float64(len(re)))); i++ { }
curSub, _ := strconv.Atoi(cur[i][0]) }()
reSub, _ := strconv.Atoi(re[i][0]) sp := regexp.MustCompile(`v(\d+)\.(\d+)\.(\d+)-?(.+)?`)
cur := sp.FindStringSubmatch(current)
re := sp.FindStringSubmatch(remote)
for i := 1; i <= 3; i++ {
curSub, _ := strconv.Atoi(cur[i])
reSub, _ := strconv.Atoi(re[i])
if curSub != reSub { if curSub != reSub {
return curSub < reSub return curSub < reSub
} }
} }
return len(cur) < len(re) if cur[4] == "" || re[4] == "" {
return re[4] == "" && cur[4] != re[4]
}
return cur[4] < re[4]
} }
var ( var (