Skip to content

Commit

Permalink
common/hugo: Fix version logic
Browse files Browse the repository at this point in the history
[ci skip]
  • Loading branch information
bep committed May 16, 2022
1 parent 1de333e commit 7bc3401
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 132 deletions.
137 changes: 82 additions & 55 deletions common/hugo/version.go
Expand Up @@ -16,7 +16,9 @@ package hugo
import (
"fmt"
"io"
"math"
"runtime"
"strconv"
"strings"

"github.com/gohugoio/hugo/compare"
Expand All @@ -25,8 +27,9 @@ import (

// Version represents the Hugo build version.
type Version struct {
// Major and minor version.
Number float32
Major int

Minor int

// Increment this for bug releases
PatchLevel int
Expand All @@ -42,14 +45,19 @@ var (
)

func (v Version) String() string {
return version(v.Number, v.PatchLevel, v.Suffix)
return version(v.Major, v.Minor, v.PatchLevel, v.Suffix)
}

// Version returns the Hugo version.
func (v Version) Version() VersionString {
return VersionString(v.String())
}

// Compare implements the compare.Comparer interface.
func (h Version) Compare(other any) int {
return compareVersions(h, other)
}

// VersionString represents a Hugo version string.
type VersionString string

Expand All @@ -60,7 +68,7 @@ func (h VersionString) String() string {
// Compare implements the compare.Comparer interface.
func (h VersionString) Compare(other any) int {
v := MustParseVersion(h.String())
return compareVersionsWithSuffix(v.Number, v.PatchLevel, v.Suffix, other)
return compareVersions(v, other)
}

// Eq implements the compare.Eqer interface.
Expand All @@ -84,10 +92,7 @@ func ParseVersion(s string) (Version, error) {
}
}

v, p := parseVersion(s)

vv.Number = v
vv.PatchLevel = p
vv.Major, vv.Minor, vv.PatchLevel = parseVersion(s)

return vv, nil
}
Expand All @@ -110,18 +115,20 @@ func (v Version) ReleaseVersion() Version {

// Next returns the next Hugo release version.
func (v Version) Next() Version {
return Version{Number: v.Number + 0.01}
return Version{Major: v.Major, Minor: v.Minor + 1}
}

// Prev returns the previous Hugo release version.
func (v Version) Prev() Version {
return Version{Number: v.Number - 0.01}
return Version{Major: v.Major, Minor: v.Minor - 1}
}

// NextPatchLevel returns the next patch/bugfix Hugo version.
// This will be a patch increment on the previous Hugo version.
func (v Version) NextPatchLevel(level int) Version {
return Version{Number: v.Number - 0.01, PatchLevel: level}
prev := v.Prev()
prev.PatchLevel = level
return prev
}

// BuildVersionString creates a version string. This is what you see when
Expand Down Expand Up @@ -160,38 +167,53 @@ func BuildVersionString() string {
return versionString
}

func version(version float32, patchVersion int, suffix string) string {
if patchVersion > 0 || version > 0.53 {
return fmt.Sprintf("%.2f.%d%s", version, patchVersion, suffix)
func version(major, minor, patch int, suffix string) string {
if patch > 0 || minor > 53 {
return fmt.Sprintf("%d.%d.%d%s", major, minor, patch, suffix)
}
return fmt.Sprintf("%.2f%s", version, suffix)
return fmt.Sprintf("%d.%d%s", major, minor, suffix)
}

// CompareVersion compares the given version string or number against the
// running Hugo version.
// It returns -1 if the given version is less than, 0 if equal and 1 if greater than
// the running version.
func CompareVersion(version any) int {
return compareVersionsWithSuffix(CurrentVersion.Number, CurrentVersion.PatchLevel, CurrentVersion.Suffix, version)
return compareVersions(CurrentVersion, version)
}

func compareVersions(inVersion float32, inPatchVersion int, in any) int {
return compareVersionsWithSuffix(inVersion, inPatchVersion, "", in)
}

func compareVersionsWithSuffix(inVersion float32, inPatchVersion int, suffix string, in any) int {
func compareVersions(inVersion Version, in any) int {
var c int
switch d := in.(type) {
case float64:
c = compareFloatVersions(inVersion, float32(d))
c = compareFloatWithVersion(d, inVersion)
case float32:
c = compareFloatVersions(inVersion, d)
c = compareFloatWithVersion(float64(d), inVersion)
case int:
c = compareFloatVersions(inVersion, float32(d))
c = compareFloatWithVersion(float64(d), inVersion)
case int32:
c = compareFloatVersions(inVersion, float32(d))
c = compareFloatWithVersion(float64(d), inVersion)
case int64:
c = compareFloatVersions(inVersion, float32(d))
c = compareFloatWithVersion(float64(d), inVersion)
case Version:
if d.Major == inVersion.Major && d.Minor == inVersion.Minor && d.PatchLevel == inVersion.PatchLevel {
return strings.Compare(inVersion.Suffix, d.Suffix)
}
if d.Major > inVersion.Major {
return 1
} else if d.Major < inVersion.Major {
return -1
}
if d.Minor > inVersion.Minor {
return 1
} else if d.Minor < inVersion.Minor {
return -1
}
if d.PatchLevel > inVersion.PatchLevel {
return 1
} else if d.PatchLevel < inVersion.PatchLevel {
return -1
}
default:
s, err := cast.ToStringE(in)
if err != nil {
Expand All @@ -202,50 +224,55 @@ func compareVersionsWithSuffix(inVersion float32, inPatchVersion int, suffix str
if err != nil {
return -1
}
return inVersion.Compare(v)

if v.Number == inVersion && v.PatchLevel == inPatchVersion {
return strings.Compare(suffix, v.Suffix)
}
}

if v.Number < inVersion || (v.Number == inVersion && v.PatchLevel < inPatchVersion) {
return -1
}
return c
}

return 1
func parseVersion(s string) (int, int, int) {
var major, minor, patch int
parts := strings.Split(s, ".")
if len(parts) > 0 {
major, _ = strconv.Atoi(parts[0])
}

if c == 0 && suffix != "" {
return 1
if len(parts) > 1 {
minor, _ = strconv.Atoi(parts[1])
}
if len(parts) > 2 {
patch, _ = strconv.Atoi(parts[2])
}

return c
return major, minor, patch
}

func parseVersion(s string) (float32, int) {
var (
v float32
p int
)
// compareFloatWithVersion compares v1 with v2.
// It returns -1 if v1 is less than v2, 0 if v1 is equal to v2 and 1 if v1 is greater than v2.
func compareFloatWithVersion(v1 float64, v2 Version) int {
mf, minf := math.Modf(v1)
v1maj := int(mf)
v1min := int(minf * 100)

if strings.Count(s, ".") == 2 {
li := strings.LastIndex(s, ".")
p = cast.ToInt(s[li+1:])
s = s[:li]
if v2.Major == v1maj && v2.Minor == v1min {
return 0
}

v = float32(cast.ToFloat64(s))

return v, p
}
if v1maj > v2.Major {
return 1

func compareFloatVersions(version float32, v float32) int {
if v == version {
return 0
}
if v < version {

if v1maj < v2.Major {
return -1
}
return 1

if v1min > v2.Minor {
return 1
}

return -1

}

func GoMinorVersion() int {
Expand Down
5 changes: 3 additions & 2 deletions common/hugo/version_current.go
Expand Up @@ -16,7 +16,8 @@ package hugo
// CurrentVersion represents the current build version.
// This should be the only one.
var CurrentVersion = Version{
Number: 0.99,
Major: 0,
Minor: 100,
PatchLevel: 0,
Suffix: "",
Suffix: "-DEV",
}
49 changes: 24 additions & 25 deletions common/hugo/version_test.go
Expand Up @@ -22,10 +22,10 @@ import (
func TestHugoVersion(t *testing.T) {
c := qt.New(t)

c.Assert(version(0.15, 0, "-DEV"), qt.Equals, "0.15-DEV")
c.Assert(version(0.15, 2, "-DEV"), qt.Equals, "0.15.2-DEV")
c.Assert(version(0, 15, 0, "-DEV"), qt.Equals, "0.15-DEV")
c.Assert(version(0, 15, 2, "-DEV"), qt.Equals, "0.15.2-DEV")

v := Version{Number: 0.21, PatchLevel: 0, Suffix: "-DEV"}
v := Version{Minor: 21, Suffix: "-DEV"}

c.Assert(v.ReleaseVersion().String(), qt.Equals, "0.21")
c.Assert(v.String(), qt.Equals, "0.21-DEV")
Expand All @@ -39,37 +39,36 @@ func TestHugoVersion(t *testing.T) {

// We started to use full semver versions even for main
// releases in v0.54.0
v = Version{Number: 0.53, PatchLevel: 0}
v = Version{Minor: 53, PatchLevel: 0}
c.Assert(v.String(), qt.Equals, "0.53")
c.Assert(v.Next().String(), qt.Equals, "0.54.0")
c.Assert(v.Next().Next().String(), qt.Equals, "0.55.0")
v = Version{Number: 0.54, PatchLevel: 0, Suffix: "-DEV"}
v = Version{Minor: 54, PatchLevel: 0, Suffix: "-DEV"}
c.Assert(v.String(), qt.Equals, "0.54.0-DEV")
}

func TestCompareVersions(t *testing.T) {
c := qt.New(t)

c.Assert(compareVersions(0.20, 0, 0.20), qt.Equals, 0)
c.Assert(compareVersions(0.20, 0, float32(0.20)), qt.Equals, 0)
c.Assert(compareVersions(0.20, 0, float64(0.20)), qt.Equals, 0)
c.Assert(compareVersions(0.19, 1, 0.20), qt.Equals, 1)
c.Assert(compareVersions(0.19, 3, "0.20.2"), qt.Equals, 1)
c.Assert(compareVersions(0.19, 1, 0.01), qt.Equals, -1)
c.Assert(compareVersions(0, 1, 3), qt.Equals, 1)
c.Assert(compareVersions(0, 1, int32(3)), qt.Equals, 1)
c.Assert(compareVersions(0, 1, int64(3)), qt.Equals, 1)
c.Assert(compareVersions(0.20, 0, "0.20"), qt.Equals, 0)
c.Assert(compareVersions(0.20, 1, "0.20.1"), qt.Equals, 0)
c.Assert(compareVersions(0.20, 1, "0.20"), qt.Equals, -1)
c.Assert(compareVersions(0.20, 0, "0.20.1"), qt.Equals, 1)
c.Assert(compareVersions(0.20, 1, "0.20.2"), qt.Equals, 1)
c.Assert(compareVersions(0.21, 1, "0.22.1"), qt.Equals, 1)
c.Assert(compareVersions(0.22, 0, "0.22-DEV"), qt.Equals, -1)
c.Assert(compareVersions(0.22, 0, "0.22.1-DEV"), qt.Equals, 1)
c.Assert(compareVersionsWithSuffix(0.22, 0, "-DEV", "0.22"), qt.Equals, 1)
c.Assert(compareVersionsWithSuffix(0.22, 1, "-DEV", "0.22"), qt.Equals, -1)
c.Assert(compareVersionsWithSuffix(0.22, 1, "-DEV", "0.22.1-DEV"), qt.Equals, 0)
c.Assert(compareVersions(MustParseVersion("0.20.0"), 0.20), qt.Equals, 0)
c.Assert(compareVersions(MustParseVersion("0.20.0"), float32(0.20)), qt.Equals, 0)
c.Assert(compareVersions(MustParseVersion("0.20.0"), float64(0.20)), qt.Equals, 0)
c.Assert(compareVersions(MustParseVersion("0.19.1"), 0.20), qt.Equals, 1)
c.Assert(compareVersions(MustParseVersion("0.19.3"), "0.20.2"), qt.Equals, 1)
c.Assert(compareVersions(MustParseVersion("0.1"), 3), qt.Equals, 1)
c.Assert(compareVersions(MustParseVersion("0.1"), int32(3)), qt.Equals, 1)
c.Assert(compareVersions(MustParseVersion("0.1"), int64(3)), qt.Equals, 1)
c.Assert(compareVersions(MustParseVersion("0.20"), "0.20"), qt.Equals, 0)
c.Assert(compareVersions(MustParseVersion("0.20.1"), "0.20.1"), qt.Equals, 0)
c.Assert(compareVersions(MustParseVersion("0.20.1"), "0.20"), qt.Equals, -1)
c.Assert(compareVersions(MustParseVersion("0.20.0"), "0.20.1"), qt.Equals, 1)
c.Assert(compareVersions(MustParseVersion("0.20.1"), "0.20.2"), qt.Equals, 1)
c.Assert(compareVersions(MustParseVersion("0.21.1"), "0.22.1"), qt.Equals, 1)
c.Assert(compareVersions(MustParseVersion("0.22.0"), "0.22-DEV"), qt.Equals, -1)
c.Assert(compareVersions(MustParseVersion("0.22.0"), "0.22.1-DEV"), qt.Equals, 1)
c.Assert(compareVersions(MustParseVersion("0.22.0-DEV"), "0.22"), qt.Equals, 1)
c.Assert(compareVersions(MustParseVersion("0.22.1-DEV"), "0.22"), qt.Equals, -1)
c.Assert(compareVersions(MustParseVersion("0.22.1-DEV"), "0.22.1-DEV"), qt.Equals, 0)
}

func TestParseHugoVersion(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions modules/config_test.go
Expand Up @@ -33,9 +33,9 @@ func TestConfigHugoVersionIsValid(t *testing.T) {
{HugoVersion{Min: "0.33.0"}, true},
{HugoVersion{Min: "0.56.0-DEV"}, true},
{HugoVersion{Min: "0.33.0", Max: "0.55.0"}, false},
{HugoVersion{Min: "0.33.0", Max: "0.99.0"}, true},
{HugoVersion{Min: "0.33.0", Max: "0.199.0"}, true},
} {
c.Assert(test.in.IsValid(), qt.Equals, test.expect)
c.Assert(test.in.IsValid(), qt.Equals, test.expect, qt.Commentf("%#v", test.in))
}
}

Expand Down
6 changes: 3 additions & 3 deletions releaser/releaser.go
Expand Up @@ -231,9 +231,9 @@ func (r *ReleaseHandler) bumpVersions(ver hugo.Version) error {
}

if err := r.replaceInFile("common/hugo/version_current.go",
`Number:(\s{4,})(.*),`, fmt.Sprintf(`Number:${1}%.2f,`, ver.Number),
`PatchLevel:(\s*)(.*),`, fmt.Sprintf(`PatchLevel:${1}%d,`, ver.PatchLevel),
`Suffix:(\s{4,})".*",`, fmt.Sprintf(`Suffix:${1}"%s",`, toDev)); err != nil {
`Minor:(\s*)(\d*),`, fmt.Sprintf(`Number:${1}%d,`, ver.Minor),
`PatchLevel:(\s*)(\d*),`, fmt.Sprintf(`PatchLevel:${1}%d,`, ver.PatchLevel),
`Suffix:(\s*)".*",`, fmt.Sprintf(`Suffix:${1}"%s",`, toDev)); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Expand Up @@ -8,7 +8,7 @@ description: |
license: "Apache-2.0"
base: core20
confinement: strict
grade: stable # "devel" or "stable"
grade: devel # "devel" or "stable"

package-repositories:
- type: apt
Expand Down

0 comments on commit 7bc3401

Please sign in to comment.