From a35196902f371107ffdb1694ce2704248656bab9 Mon Sep 17 00:00:00 2001 From: Walter Scheper Date: Wed, 13 Sep 2023 19:31:34 -0400 Subject: [PATCH] fix: return minimum version from module When versioning a go module with no matching major version tags, gotagger should return vX.0.0, where X is the major version of the module. Fixes #211 --- gotagger.go | 18 +++++++++++++----- gotagger_test.go | 15 ++++++++++++++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/gotagger.go b/gotagger.go index c43cd81..c688f94 100644 --- a/gotagger.go +++ b/gotagger.go @@ -358,7 +358,7 @@ func (g *Gotagger) latestModule(tags []string, m module) (*semver.Version, strin majorVersion := strings.TrimPrefix(versionRegex.FindString(m.name), goModSep) if majorVersion == "" { - majorVersion = "v1" + majorVersion = "v0" } moduleVersion, err := semver.NewVersion(majorVersion + ".0.0") @@ -366,7 +366,11 @@ func (g *Gotagger) latestModule(tags []string, m module) (*semver.Version, strin return nil, "", err } - maximumVersion := moduleVersion.IncMajor() + _maximumVersion := moduleVersion.IncMajor() + if majorVersion == "v0" { + _maximumVersion = _maximumVersion.IncMajor() + } + maximumVersion := &_maximumVersion logger.Info("ignoring modules greater than " + g.Config.VersionPrefix + maximumVersion.String()) var latestVersion *semver.Version @@ -375,7 +379,11 @@ func (g *Gotagger) latestModule(tags []string, m module) (*semver.Version, strin // strip the module prefix from the tag so we can parse it as a semver tagName := strings.TrimPrefix(tag, m.prefix) // we want the highest version that is less than the next major version - if tver, err := semver.NewVersion(tagName); err == nil && tver.LessThan(&maximumVersion) { + tver, err := semver.NewVersion(tagName) + if err != nil { + continue + } + if tver.Compare(maximumVersion) < 0 && tver.Compare(moduleVersion) >= 0 { if latestVersion == nil || latestVersion.LessThan(tver) { logger.Info("found newer tag", "tag", tag) latestVersion = tver @@ -384,9 +392,9 @@ func (g *Gotagger) latestModule(tags []string, m module) (*semver.Version, strin } } - // if there were no tags, then return 0.0.0 + // if there were no tags, then return the base module version if latestVersion == nil { - return semver.MustParse("0.0.0"), "", nil + return moduleVersion, "", nil } hash, err := g.repo.RevParse(latestTag + "^{commit}") diff --git a/gotagger_test.go b/gotagger_test.go index 87d3da7..dcaa2ff 100644 --- a/gotagger_test.go +++ b/gotagger_test.go @@ -44,6 +44,12 @@ func TestGotagger_latestModule(t *testing.T) { repoFunc: simpleGoRepo, want: "v0.1.0", }, + { + title: "new v2 module", + module: module{".", "foo/v2", ""}, + repoFunc: newV2Module, + want: "v2.0.0", + }, { title: "latest foo v1 directory", module: module{".", "foo", ""}, @@ -72,7 +78,7 @@ func TestGotagger_latestModule(t *testing.T) { title: "breaking change in v1 module", module: module{".", "foo/v2", ""}, repoFunc: untaggedV2Repo, - want: "v1.0.0", + want: "v2.0.0", }, } @@ -1703,6 +1709,13 @@ func mixedTagGoRepo(t testutils.T, repo *sgit.Repository, path string) { testutils.CommitFile(t, repo, path, "go.mod", "feat: add go.mod", []byte("module foo\n")) } +func newV2Module(t testutils.T, repo *sgit.Repository, path string) { + t.Helper() + + // create top-level go.mod with v2 module + testutils.CommitFile(t, repo, path, "go.mod", "feat: add go.mod", []byte("module foo/v2\n")) +} + func v2DirGitRepo(t testutils.T, repo *sgit.Repository, path string) { t.Helper()