Skip to content

Commit

Permalink
fix: return minimum version from module
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wfscheper committed Sep 17, 2023
1 parent 5d2f087 commit a351969
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
18 changes: 13 additions & 5 deletions gotagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,15 +358,19 @@ 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")
if err != nil {
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
Expand All @@ -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
Expand All @@ -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}")
Expand Down
15 changes: 14 additions & 1 deletion gotagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", ""},
Expand Down Expand Up @@ -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",
},
}

Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit a351969

Please sign in to comment.