common/hugo: Fix version logic

[ci skip]
This commit is contained in:
hugoreleaser 2022-05-16 08:50:17 +00:00 committed by Bjørn Erik Pedersen
parent 1de333e7a3
commit 7bc3401eb5
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
7 changed files with 116 additions and 133 deletions

View file

@ -16,7 +16,9 @@ package hugo
import ( import (
"fmt" "fmt"
"io" "io"
"math"
"runtime" "runtime"
"strconv"
"strings" "strings"
"github.com/gohugoio/hugo/compare" "github.com/gohugoio/hugo/compare"
@ -25,8 +27,9 @@ import (
// Version represents the Hugo build version. // Version represents the Hugo build version.
type Version struct { type Version struct {
// Major and minor version. Major int
Number float32
Minor int
// Increment this for bug releases // Increment this for bug releases
PatchLevel int PatchLevel int
@ -42,7 +45,7 @@ var (
) )
func (v Version) String() string { 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. // Version returns the Hugo version.
@ -50,6 +53,11 @@ func (v Version) Version() VersionString {
return VersionString(v.String()) 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. // VersionString represents a Hugo version string.
type VersionString string type VersionString string
@ -60,7 +68,7 @@ func (h VersionString) String() string {
// Compare implements the compare.Comparer interface. // Compare implements the compare.Comparer interface.
func (h VersionString) Compare(other any) int { func (h VersionString) Compare(other any) int {
v := MustParseVersion(h.String()) v := MustParseVersion(h.String())
return compareVersionsWithSuffix(v.Number, v.PatchLevel, v.Suffix, other) return compareVersions(v, other)
} }
// Eq implements the compare.Eqer interface. // Eq implements the compare.Eqer interface.
@ -84,10 +92,7 @@ func ParseVersion(s string) (Version, error) {
} }
} }
v, p := parseVersion(s) vv.Major, vv.Minor, vv.PatchLevel = parseVersion(s)
vv.Number = v
vv.PatchLevel = p
return vv, nil return vv, nil
} }
@ -110,18 +115,20 @@ func (v Version) ReleaseVersion() Version {
// Next returns the next Hugo release version. // Next returns the next Hugo release version.
func (v Version) Next() 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. // Prev returns the previous Hugo release version.
func (v Version) Prev() 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. // NextPatchLevel returns the next patch/bugfix Hugo version.
// This will be a patch increment on the previous Hugo version. // This will be a patch increment on the previous Hugo version.
func (v Version) NextPatchLevel(level int) 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 // BuildVersionString creates a version string. This is what you see when
@ -160,11 +167,11 @@ func BuildVersionString() string {
return versionString return versionString
} }
func version(version float32, patchVersion int, suffix string) string { func version(major, minor, patch int, suffix string) string {
if patchVersion > 0 || version > 0.53 { if patch > 0 || minor > 53 {
return fmt.Sprintf("%.2f.%d%s", version, patchVersion, suffix) 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 // CompareVersion compares the given version string or number against the
@ -172,26 +179,41 @@ func version(version float32, patchVersion int, suffix string) string {
// It returns -1 if the given version is less than, 0 if equal and 1 if greater than // It returns -1 if the given version is less than, 0 if equal and 1 if greater than
// the running version. // the running version.
func CompareVersion(version any) int { 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 { func compareVersions(inVersion Version, in any) int {
return compareVersionsWithSuffix(inVersion, inPatchVersion, "", in)
}
func compareVersionsWithSuffix(inVersion float32, inPatchVersion int, suffix string, in any) int {
var c int var c int
switch d := in.(type) { switch d := in.(type) {
case float64: case float64:
c = compareFloatVersions(inVersion, float32(d)) c = compareFloatWithVersion(d, inVersion)
case float32: case float32:
c = compareFloatVersions(inVersion, d) c = compareFloatWithVersion(float64(d), inVersion)
case int: case int:
c = compareFloatVersions(inVersion, float32(d)) c = compareFloatWithVersion(float64(d), inVersion)
case int32: case int32:
c = compareFloatVersions(inVersion, float32(d)) c = compareFloatWithVersion(float64(d), inVersion)
case int64: 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: default:
s, err := cast.ToStringE(in) s, err := cast.ToStringE(in)
if err != nil { if err != nil {
@ -202,50 +224,55 @@ func compareVersionsWithSuffix(inVersion float32, inPatchVersion int, suffix str
if err != nil { if err != nil {
return -1 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 1
}
if c == 0 && suffix != "" {
return 1
} }
return c return c
} }
func parseVersion(s string) (float32, int) { func parseVersion(s string) (int, int, int) {
var ( var major, minor, patch int
v float32 parts := strings.Split(s, ".")
p int if len(parts) > 0 {
) major, _ = strconv.Atoi(parts[0])
}
if strings.Count(s, ".") == 2 { if len(parts) > 1 {
li := strings.LastIndex(s, ".") minor, _ = strconv.Atoi(parts[1])
p = cast.ToInt(s[li+1:]) }
s = s[:li] if len(parts) > 2 {
patch, _ = strconv.Atoi(parts[2])
} }
v = float32(cast.ToFloat64(s)) return major, minor, patch
return v, p
} }
func compareFloatVersions(version float32, v float32) int { // compareFloatWithVersion compares v1 with v2.
if v == version { // 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 v2.Major == v1maj && v2.Minor == v1min {
return 0 return 0
} }
if v < version {
if v1maj > v2.Major {
return 1
}
if v1maj < v2.Major {
return -1 return -1
} }
if v1min > v2.Minor {
return 1 return 1
}
return -1
} }
func GoMinorVersion() int { func GoMinorVersion() int {

View file

@ -16,7 +16,8 @@ package hugo
// CurrentVersion represents the current build version. // CurrentVersion represents the current build version.
// This should be the only one. // This should be the only one.
var CurrentVersion = Version{ var CurrentVersion = Version{
Number: 0.99, Major: 0,
Minor: 100,
PatchLevel: 0, PatchLevel: 0,
Suffix: "", Suffix: "-DEV",
} }

View file

@ -22,10 +22,10 @@ import (
func TestHugoVersion(t *testing.T) { func TestHugoVersion(t *testing.T) {
c := qt.New(t) c := qt.New(t)
c.Assert(version(0.15, 0, "-DEV"), qt.Equals, "0.15-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") 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.ReleaseVersion().String(), qt.Equals, "0.21")
c.Assert(v.String(), qt.Equals, "0.21-DEV") c.Assert(v.String(), qt.Equals, "0.21-DEV")
@ -39,37 +39,36 @@ func TestHugoVersion(t *testing.T) {
// We started to use full semver versions even for main // We started to use full semver versions even for main
// releases in v0.54.0 // 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.String(), qt.Equals, "0.53")
c.Assert(v.Next().String(), qt.Equals, "0.54.0") c.Assert(v.Next().String(), qt.Equals, "0.54.0")
c.Assert(v.Next().Next().String(), qt.Equals, "0.55.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") c.Assert(v.String(), qt.Equals, "0.54.0-DEV")
} }
func TestCompareVersions(t *testing.T) { func TestCompareVersions(t *testing.T) {
c := qt.New(t) c := qt.New(t)
c.Assert(compareVersions(0.20, 0, 0.20), qt.Equals, 0) c.Assert(compareVersions(MustParseVersion("0.20.0"), 0.20), qt.Equals, 0)
c.Assert(compareVersions(0.20, 0, float32(0.20)), qt.Equals, 0) c.Assert(compareVersions(MustParseVersion("0.20.0"), float32(0.20)), qt.Equals, 0)
c.Assert(compareVersions(0.20, 0, float64(0.20)), qt.Equals, 0) c.Assert(compareVersions(MustParseVersion("0.20.0"), float64(0.20)), qt.Equals, 0)
c.Assert(compareVersions(0.19, 1, 0.20), qt.Equals, 1) c.Assert(compareVersions(MustParseVersion("0.19.1"), 0.20), qt.Equals, 1)
c.Assert(compareVersions(0.19, 3, "0.20.2"), qt.Equals, 1) c.Assert(compareVersions(MustParseVersion("0.19.3"), "0.20.2"), qt.Equals, 1)
c.Assert(compareVersions(0.19, 1, 0.01), qt.Equals, -1) c.Assert(compareVersions(MustParseVersion("0.1"), 3), qt.Equals, 1)
c.Assert(compareVersions(0, 1, 3), qt.Equals, 1) c.Assert(compareVersions(MustParseVersion("0.1"), int32(3)), qt.Equals, 1)
c.Assert(compareVersions(0, 1, int32(3)), qt.Equals, 1) c.Assert(compareVersions(MustParseVersion("0.1"), int64(3)), qt.Equals, 1)
c.Assert(compareVersions(0, 1, int64(3)), qt.Equals, 1) c.Assert(compareVersions(MustParseVersion("0.20"), "0.20"), qt.Equals, 0)
c.Assert(compareVersions(0.20, 0, "0.20"), qt.Equals, 0) c.Assert(compareVersions(MustParseVersion("0.20.1"), "0.20.1"), qt.Equals, 0)
c.Assert(compareVersions(0.20, 1, "0.20.1"), qt.Equals, 0) c.Assert(compareVersions(MustParseVersion("0.20.1"), "0.20"), qt.Equals, -1)
c.Assert(compareVersions(0.20, 1, "0.20"), qt.Equals, -1) c.Assert(compareVersions(MustParseVersion("0.20.0"), "0.20.1"), qt.Equals, 1)
c.Assert(compareVersions(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(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(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(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(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(compareVersionsWithSuffix(0.22, 0, "-DEV", "0.22"), qt.Equals, 1) c.Assert(compareVersions(MustParseVersion("0.22.1-DEV"), "0.22"), qt.Equals, -1)
c.Assert(compareVersionsWithSuffix(0.22, 1, "-DEV", "0.22"), qt.Equals, -1) c.Assert(compareVersions(MustParseVersion("0.22.1-DEV"), "0.22.1-DEV"), qt.Equals, 0)
c.Assert(compareVersionsWithSuffix(0.22, 1, "-DEV", "0.22.1-DEV"), qt.Equals, 0)
} }
func TestParseHugoVersion(t *testing.T) { func TestParseHugoVersion(t *testing.T) {

View file

@ -33,9 +33,9 @@ func TestConfigHugoVersionIsValid(t *testing.T) {
{HugoVersion{Min: "0.33.0"}, true}, {HugoVersion{Min: "0.33.0"}, true},
{HugoVersion{Min: "0.56.0-DEV"}, 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.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))
} }
} }

View file

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

View file

@ -8,7 +8,7 @@ description: |
license: "Apache-2.0" license: "Apache-2.0"
base: core20 base: core20
confinement: strict confinement: strict
grade: stable # "devel" or "stable" grade: devel # "devel" or "stable"
package-repositories: package-repositories:
- type: apt - type: apt

View file

@ -1,44 +0,0 @@
This release represents **24 contributions by 4 contributors** to the main Hugo code base.[@bep](https://github.com/bep) leads the Hugo development with a significant amount of contributions, but also a big shoutout to [@dependabot[bot]](https://github.com/apps/dependabot), [@satotake](https://github.com/satotake), and [@nathannaveen](https://github.com/nathannaveen) for their ongoing contributions.
And thanks to [@digitalcraftsman](https://github.com/digitalcraftsman) for his ongoing work on keeping the themes site in pristine condition.
Many have also been busy writing and fixing the documentation in [hugoDocs](https://github.com/gohugoio/hugoDocs),
which has received **3 contributions by 2 contributors**.
Hugo now has:
* 58934+ [stars](https://github.com/gohugoio/hugo/stargazers)
* 428+ [contributors](https://github.com/gohugoio/hugo/graphs/contributors)
* 399+ [themes](http://themes.gohugo.io/)
## Changes
* server: Refresh the error template 657d1a2d @bep
* server: Fix SIGINT handling after loading bad configuration 87a22eb6 @bep #9664
* Improve SASS errors fc9f315d @bep #9897
* postcss: Fix import error handling 4b189d8f @bep #9895
* build(deps): bump github.com/fsnotify/fsnotify from 1.5.3 to 1.5.4 c2fa0a33 @dependabot[bot]
* common/herrors: Remove unused struct 48ea24f8 @bep
* build(deps): bump github.com/evanw/esbuild from 0.14.38 to 0.14.39 9f563856 @dependabot[bot]
* errors: Misc improvements 5c96bda7 @bep #9892 #9891 #9893
* server: Always rebuild the files involved in an error 4a96df96 @bep #9884
* postcss: Fix line numbers in error messages e8537e6d @bep #9880
* Update CONTRIBUTING.md 2fbdee72 @bep
* js: Bump test dependency 91fe1b6c @bep
* deps: Update github.com/spf13/cast v1.4.1 => v1.5.0 7de62912 @bep
* hugolib: Check for nil in shouldRender 9d7f1662 @bep
* Revise the use of htime.Since/htime.Now 51f08b0b @bep #9868
* tpl/collections: Make sort stable 860c51c3 @bep #9865
* docs: Regen CLI docs 855e5869 @bep
* Use configured timeZone for the clock 35c88a7f @bep #8787
* Add `clock` cli flag e77ca3c1 @satotake #8787
* Improve error messages, esp. when the server is running f2946da9 @bep #9852 #9857 #9863
* tpl: Improve godoc 6eea32bd @bep
* github: Add permissions to test action a6d54585 @nathannaveen
* tpl/crypto: Add example for FNV32a e5f21731 @bep
* releaser: Prepare repository for 0.99.0-DEV 89c1655e @bep