aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorᴜɴᴋɴᴡᴏɴ <u@gogs.io>2020-05-04 18:48:23 +0800
committerGitHub <noreply@github.com>2020-05-04 18:48:23 +0800
commit9fd4f5562d7a59abbef5e78d44fb85a0041e0733 (patch)
tree8b917bfbf8d2c7fb186e832f52f29b5942d43b12 /internal
parent9bb218734c53c98a13264c2f4f479e3633ccfe18 (diff)
all: use semver to compare versions (#6147)
Diffstat (limited to 'internal')
-rw-r--r--internal/cmd/restore.go6
-rw-r--r--internal/conf/conf.go4
-rw-r--r--internal/db/repo.go4
-rw-r--r--internal/semverutil/semverutil.go37
-rw-r--r--internal/semverutil/semverutil_test.go32
5 files changed, 76 insertions, 7 deletions
diff --git a/internal/cmd/restore.go b/internal/cmd/restore.go
index ba72c6da..1b6cdf2c 100644
--- a/internal/cmd/restore.go
+++ b/internal/cmd/restore.go
@@ -9,7 +9,6 @@ import (
"path"
"path/filepath"
- "github.com/mcuadros/go-version"
"github.com/pkg/errors"
"github.com/unknwon/cae/zip"
"github.com/unknwon/com"
@@ -19,6 +18,7 @@ import (
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/db"
+ "gogs.io/gogs/internal/semverutil"
)
var Restore = cli.Command{
@@ -58,7 +58,7 @@ func runRestore(c *cli.Context) error {
log.Fatal("Failed to extract backup archive: %v", err)
}
archivePath := path.Join(tmpDir, archiveRootDir)
- defer os.RemoveAll(archivePath)
+ defer func() { _ = os.RemoveAll(archivePath) }()
// Check backup version
metaFile := filepath.Join(archivePath, "metadata.ini")
@@ -70,7 +70,7 @@ func runRestore(c *cli.Context) error {
log.Fatal("Failed to load metadata '%s': %v", metaFile, err)
}
backupVersion := metadata.Section("").Key("GOGS_VERSION").MustString("999.0")
- if version.Compare(conf.App.Version, backupVersion, "<") {
+ if semverutil.Compare(conf.App.Version, "<", backupVersion) {
log.Fatal("Current Gogs version is lower than backup version: %s < %s", conf.App.Version, backupVersion)
}
formatVersion := metadata.Section("").Key("VERSION").MustInt()
diff --git a/internal/conf/conf.go b/internal/conf/conf.go
index 49bb2949..99e9a325 100644
--- a/internal/conf/conf.go
+++ b/internal/conf/conf.go
@@ -18,13 +18,13 @@ import (
_ "github.com/go-macaron/cache/redis"
_ "github.com/go-macaron/session/redis"
"github.com/gogs/go-libravatar"
- "github.com/mcuadros/go-version"
"github.com/pkg/errors"
"gopkg.in/ini.v1"
log "unknwon.dev/clog/v2"
"gogs.io/gogs/internal/assets/conf"
"gogs.io/gogs/internal/osutil"
+ "gogs.io/gogs/internal/semverutil"
)
func init() {
@@ -156,7 +156,7 @@ func Init(customConf string) error {
return errors.Wrap(err, "get OpenSSH version")
}
- if IsWindowsRuntime() || version.Compare(sshVersion, "5.1", "<") {
+ if IsWindowsRuntime() || semverutil.Compare(sshVersion, "<", "5.1") {
log.Warn(`SSH minimum key size check is forced to be disabled because server is not eligible:
1. Windows server
2. OpenSSH version is lower than 5.1`)
diff --git a/internal/db/repo.go b/internal/db/repo.go
index 66cecdd3..9c91d90f 100644
--- a/internal/db/repo.go
+++ b/internal/db/repo.go
@@ -19,7 +19,6 @@ import (
"strings"
"time"
- "github.com/mcuadros/go-version"
"github.com/nfnt/resize"
"github.com/unknwon/cae/zip"
"github.com/unknwon/com"
@@ -37,6 +36,7 @@ import (
"gogs.io/gogs/internal/markup"
"gogs.io/gogs/internal/osutil"
"gogs.io/gogs/internal/process"
+ "gogs.io/gogs/internal/semverutil"
"gogs.io/gogs/internal/sync"
)
@@ -117,7 +117,7 @@ func NewRepoContext() {
}
log.Trace("Git version: %s", conf.Git.Version)
- if version.Compare("1.8.3", conf.Git.Version, ">") {
+ if semverutil.Compare(conf.Git.Version, "<", "1.8.3") {
log.Fatal("Gogs requires Git version greater or equal to 1.8.3")
}
diff --git a/internal/semverutil/semverutil.go b/internal/semverutil/semverutil.go
new file mode 100644
index 00000000..d66dbfb8
--- /dev/null
+++ b/internal/semverutil/semverutil.go
@@ -0,0 +1,37 @@
+// Copyright 2020 The Gogs Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package semverutil
+
+import (
+ "strings"
+
+ "github.com/Masterminds/semver/v3"
+)
+
+// Compare returns true if the comparison is true for given versions. It returns false if
+// comparison is false, or failed to parse one or both versions as Semantic Versions.
+//
+// See https://github.com/Masterminds/semver#basic-comparisons for supported comparisons.
+func Compare(version1, comparison, version2 string) bool {
+ clean := func(v string) string {
+ if strings.Count(v, ".") > 2 {
+ fields := strings.SplitN(v, ".", 4)
+ v = strings.Join(fields[:3], ".")
+ }
+ return v
+ }
+
+ v, err := semver.NewVersion(clean(version1))
+ if err != nil {
+ return false
+ }
+
+ c, err := semver.NewConstraint(comparison + " " + clean(version2))
+ if err != nil {
+ return false
+ }
+
+ return c.Check(v)
+}
diff --git a/internal/semverutil/semverutil_test.go b/internal/semverutil/semverutil_test.go
new file mode 100644
index 00000000..d11421b8
--- /dev/null
+++ b/internal/semverutil/semverutil_test.go
@@ -0,0 +1,32 @@
+// Copyright 2020 The Gogs Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package semverutil
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestCheck(t *testing.T) {
+ tests := []struct {
+ version1 string
+ comparison string
+ version2 string
+ expVal bool
+ }{
+ {version1: "1.0.c", comparison: ">", version2: "0.9", expVal: false},
+ {version1: "1.0.1", comparison: ">", version2: "0.9.a", expVal: false},
+
+ {version1: "7.2", comparison: ">=", version2: "5.1", expVal: true},
+ {version1: "1.8.3.1", comparison: ">=", version2: "1.8.3", expVal: true},
+ {version1: "0.12.0+dev", comparison: ">=", version2: "0.11.68.1023", expVal: true},
+ }
+ for _, test := range tests {
+ t.Run(test.version1+" "+test.comparison+" "+test.version2, func(t *testing.T) {
+ assert.Equal(t, test.expVal, Compare(test.version1, test.comparison, test.version2))
+ })
+ }
+}