Skip to content

Commit c002e67

Browse files
committed
fix: major version revving in go modules
`gotagger` was filtering out all tags that didn't match the major ersion of the module. This changes the logic to only filter out tags whose major version is greater than the module version. Fixes #17
1 parent 77dda1e commit c002e67

File tree

3 files changed

+96
-88
lines changed

3 files changed

+96
-88
lines changed

.stentor.d/17-fix-major-versioning.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Fixed a bug where `gotagger` calculated the wrong major version for go modules.
2+
3+
When finding the latest tag for a go module,
4+
`gotagger` was only considering tags that matched the major version of the module.
5+
This caused `gotagger` to essentially always calculate the version as `v1.0.0`.
6+
The filtering was changed to only filter out tags whose major version are greater than the module version.

gotagger.go

+24-14
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type Config struct {
4545
ExcludeModules []string
4646

4747
// IgnoreModules controls whether gotagger will ignore the existence of
48-
// go.mod files when determinging how to version a project.
48+
// go.mod files when determining how to version a project.
4949
IgnoreModules bool
5050

5151
// RemoteName represents the name of the remote repository. Defaults to origin.
@@ -118,7 +118,7 @@ func (g *Gotagger) ModuleVersions(names ...string) ([]string, error) {
118118
return g.versions(modules, nil)
119119
}
120120

121-
// TagRepo determines the curent version of the repository by parsing the commit
121+
// TagRepo determines the current version of the repository by parsing the commit
122122
// history since the previous release and returns that version. Depending
123123
// on the CreateTag and PushTag configuration options tags may be created and
124124
// pushed.
@@ -351,12 +351,25 @@ func (g *Gotagger) latest(tags []string) (latest *semver.Version, hash string, e
351351
return
352352
}
353353

354-
func (g *Gotagger) latestModule(m module, tags []string) (*semver.Version, string, error) {
355-
var hash string
356-
latest := &semver.Version{}
354+
// latestModule returns the latest version of m and the hash of the commit
355+
// tagged with that version.
356+
func (g *Gotagger) latestModule(m module, tags []string) (latest *semver.Version, hash string, err error) {
357+
majorVersion := strings.TrimPrefix(versionRegex.FindString(m.name), goModSep)
358+
if majorVersion == "" {
359+
majorVersion = "v1"
360+
}
361+
moduleVersion, err := semver.NewVersion(majorVersion + ".0.0")
362+
if err != nil {
363+
return nil, "", err
364+
}
365+
366+
maximumVersion := moduleVersion.IncMajor()
367+
latest = new(semver.Version)
357368
for _, tag := range tags {
369+
// strip the module prefix from the tag so we can parse it as a semver
358370
tagName := strings.TrimPrefix(tag, m.prefix)
359-
if tver, err := semver.NewVersion(tagName); err == nil && latest.LessThan(tver) {
371+
// we want the highest version that is less than the next major version
372+
if tver, err := semver.NewVersion(tagName); err == nil && tver.LessThan(&maximumVersion) && tver.GreaterThan(latest) {
360373
hash, err = g.repo.RevParse(tag + "^{commit}")
361374
if err != nil {
362375
return nil, "", err
@@ -435,16 +448,13 @@ func (g *Gotagger) versionsModules(modules []module, commitModules []module) ([]
435448
// version prefix, and the major version of this module.
436449
// the major version is the version part of the module name
437450
// (foo/v2, foo/v3) normalized to 'X.'
438-
var prefixes []string
439-
if major := strings.TrimPrefix(versionRegex.FindString(mod.name), goModSep); major == "" {
440-
// no major version in module name, so v0.x and v1.x are allowed
441-
prefixes = []string{mod.prefix + "v0.*", mod.prefix + "v1.*"}
442-
} else {
443-
prefixes = []string{mod.prefix + major + ".*"}
451+
prefix := g.Config.VersionPrefix
452+
if mod.prefix != "" {
453+
prefix = mod.prefix + prefix
444454
}
445455

446456
// get tags that match the prefixes
447-
tags, err := g.repo.Tags(head, prefixes...)
457+
tags, err := g.repo.Tags(head, prefix)
448458
if err != nil {
449459
return nil, err
450460
}
@@ -669,7 +679,7 @@ func validateCommitModules(commitModules, changedModules []module) (err error) {
669679
return
670680
}
671681

672-
// TagRepo determines what the curent version of the repository is by parsing the commit
682+
// TagRepo determines what the current version of the repository is by parsing the commit
673683
// history since previous release and returns that version. Depending on the state of
674684
// the Config passed it, it may also create the tag and push it.
675685
//

0 commit comments

Comments
 (0)