semver

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package semver resolves SemVer versions from conventional commits: the bump level, SemVer 2.0.0 ordering, pre-releases, maintenance lines and the stay_at_v0 and confirm_major gates.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrOutOfRange       = errors.New("next version is outside the maintenance range")
	ErrTagExists        = errors.New("tag already exists")
	ErrNoInRangeRelease = errors.New("no release in the maintenance range is reachable from HEAD")
)
View Source
var ErrInvalidVersion = errors.New("invalid SemVer version")

ErrInvalidVersion is returned (wrapped) by Parse for any string that is not a SemVer 2.0.0 version.

View Source
var ErrMajorEscalation = errors.New("pre-release series major escalation")

ErrMajorEscalation is matched (errors.Is) by the error returned when a pre-release run would move an open series to a new major without --allow-major.

View Source
var ErrPreReleaseRegression = errors.New("pre-release would not sort above existing tags of its core")

ErrPreReleaseRegression is matched (errors.Is) by the error returned when the candidate pre-release would not sort above every existing tag of its core.

Functions

func BumpVersion

func BumpVersion(current string, bump versioning.BumpType) (string, error)

BumpVersion increments the appropriate SemVer component. current must be a bare version string without prefix (e.g. "1.2.3").

func Compare added in v0.72.0

func Compare(a, b Version) int

Compare orders a and b by SemVer §11 precedence: -1 if a < b, 0 if equal, +1 if a > b. Build metadata is ignored, so versions differing only in it compare equal.

func DetermineBump

func DetermineBump(commits []string, overrides []config.BumpRule) versioning.BumpType

DetermineBump scans commits and returns the highest bump level any of them contributes (T261). Per commit, in order: the first matching rule in overrides wins (a rule matches when every condition it sets — type, regex against the subject, breaking — holds); otherwise the built-in defaults apply (breaking → major, feat → minor, any other conventional commit → patch); a non-conventional commit matched by nothing contributes nothing. A release with nothing to contribute (everything ignored or explicitly "none") resolves to BumpNone — callers decide whether that's an error.

func ExistingRelease added in v0.75.0

func ExistingRelease(runner port.Runner, tag string) (string, error)

ExistingRelease returns a tag that already releases tag's version, or "" when there is none. Without build metadata in tag, a <tag>+<build> tag counts too: build metadata does not make a different release (ADR-0064), so v1.3.2+7 cut elsewhere is the release of 1.3.2. Pre-releases such as v1.3.2-rc.1 match neither pattern.

func IsBareVersion added in v0.32.0

func IsBareVersion(s string) bool

IsBareVersion reports whether s is a dotted MAJOR.MINOR.PATCH of integers with no pre-release or build suffix. It is deliberately lenient (it accepts leading zeros), because calver-per-env's zero-padded versions (2026.05.3) go through it — SemVer strategies use Parse instead (ADR-0064).

func MajorMinor added in v0.59.0

func MajorMinor(version string) (major, minor int, err error)

MajorMinor extracts the MAJOR and MINOR components from a bare SemVer version string, for substitution into a rotating changelog output pattern's {MAJOR}/{MINOR} tokens. version must have at least MAJOR.MINOR.PATCH shape; PATCH may carry a pre-release/build-metadata suffix (e.g. "1.4.2-rc.1", "1.4.2+build.5") since heraut's --set-version override is strategy-agnostic about shape (see tagfmt.ValidateVersionOverride) — MAJOR and MINOR are unaffected either way.

func RotationPattern added in v0.59.0

func RotationPattern(tokens []string, major, minor int) (string, error)

RotationPattern returns a regular expression matching any bare version string in the same rotation bucket as major/minor, given the tokens a rotating changelog output/tag pattern groups by ("MAJOR" alone, or "MAJOR" and "MINOR" together — order doesn't matter). "MINOR" alone is rejected: MAJOR is always the leading, required dimension, mirroring calver.BucketPattern's prefix-order rule for the CalVer strategy family. The pattern is anchored at the start (^) only — callers compose their own tag_prefix quoting.

func ValidatePreReleaseLabel added in v0.74.0

func ValidatePreReleaseLabel(label string) error

ValidatePreReleaseLabel checks that label is a single SemVer pre-release identifier that is not purely numeric: [0-9A-Za-z-]+, no dots. heraut appends ".N", so a numeric label would collide with the counter's own identifier type.

Types

type Range added in v0.75.0

type Range struct {
	Lo, Hi Version
	Label  string // "1.3.x"
	Branch string // "release/1.3"
}

Range is a maintenance line [Lo, Hi) (ADR-0065).

func RangeFrom added in v0.75.0

func RangeFrom(r config.BranchRange, branch string) Range

RangeFrom turns a configured branch range into the [Lo, Hi) interval it covers: N.x is [N.0.0, N+1.0.0), N.M.x is [N.M.0, N.M+1.0).

func (Range) Contains added in v0.75.0

func (r Range) Contains(v Version) bool

Contains reports whether v's core lies in [Lo, Hi). Pre-release and build metadata are ignored: a line owns every pre-release of the cores it owns.

type Resolver

type Resolver struct {
	// contains filtered or unexported fields
}

Resolver resolves the next SemVer version.

func New

func New(runner port.Runner, cfg *config.Config) *Resolver

New constructs a SemVer Resolver.

func (*Resolver) BumpAuto added in v0.4.0

func (r *Resolver) BumpAuto(tags []string, commits []string) (string, error)

BumpAuto computes the next SemVer from pre-fetched bare versions and commits. tags is sorted newest-first with the prefix already stripped; commits are the full commit messages since the latest tag. Implements the VersionCalculator interface consumed by internal/versioning/perenv.

func (*Resolver) BumpFromDate added in v0.4.0

func (r *Resolver) BumpFromDate(_ []string) (string, error)

BumpFromDate is not used by the SemVer calculator; it satisfies the VersionCalculator interface for internal/versioning/perenv.

func (*Resolver) Resolve

func (r *Resolver) Resolve() (versioning.Result, error)

Resolve returns the next version result. --set-version never reaches it: app.NewResolver returns a static resolver instead, so bump: manual here means the flag is missing.

func (*Resolver) SetAllowMajor added in v0.69.0

func (r *Resolver) SetAllowMajor(allow bool)

SetAllowMajor lifts versioning.bump.stay_at_v0's hold-back for this resolver (--allow-major).

func (*Resolver) SetAllowReleased added in v0.78.0

func (r *Resolver) SetAllowReleased(allow bool)

SetAllowReleased lets a final whose version is already tagged resolve to that version again, for runs that never create the tag (changelog without --tag).

func (*Resolver) SetFixedVersion added in v0.78.0

func (r *Resolver) SetFixedVersion(core string)

SetFixedVersion pins resolution to core (MAJOR.MINOR.PATCH), the version a version branch carries in its name (ADR-0065): commits never bump it, only the pre-release label or the final decides the tag. "" restores normal resolution. --set-version and manual bump mode still win; Resolve never reaches this path for them.

func (*Resolver) SetMaintenanceRange added in v0.75.0

func (r *Resolver) SetMaintenanceRange(rg *Range)

SetMaintenanceRange confines auto resolution to rg's line; nil restores global resolution.

func (*Resolver) SetPreRelease added in v0.74.0

func (r *Resolver) SetPreRelease(label string)

SetPreRelease switches Resolve into pre-release mode for label; "" restores final-release resolution. The label is not validated here — see ValidatePreReleaseLabel.

func (*Resolver) UnconfirmedMajor added in v0.83.0

func (r *Resolver) UnconfirmedMajor() []string

UnconfirmedMajor returns the subject of every commit that forced the major bump of the most recent Resolve or BumpAuto call when versioning.bump.confirm_major asks for --allow-major before it (ADR-0078) — nil when there is nothing to confirm. The resolver still returns the major; the commands decide whether to refuse it.

func (*Resolver) Warnings added in v0.69.0

func (r *Resolver) Warnings() []string

Warnings returns the warnings produced by the most recent Resolve or BumpAuto call — nil when nothing was held back. The resolver never prints them; the caller decides how to surface them.

func (*Resolver) WouldBeVersions added in v0.69.0

func (r *Resolver) WouldBeVersions() []string

WouldBeVersions returns the bare "would-be" major version for each entry in Warnings, in the same order — nil when Warnings is empty. See holdMajorAtZero's doc comment for why this exists.

type TagVersion added in v0.72.0

type TagVersion struct {
	Tag     string
	Version Version
}

TagVersion pairs a git tag with the SemVer version parsed from it.

func Latest added in v0.72.0

func Latest(sorted []TagVersion, includePreRelease bool) (TagVersion, bool)

Latest returns the first entry of sorted (as returned by SortTags) that is a release — or, with includePreRelease, the first entry of any kind. ok is false when none qualifies.

func SortTags added in v0.72.0

func SortTags(tags []string, extract func(tag string) (string, bool)) []TagVersion

SortTags parses every tag through extract — which returns the tag's bare version string and whether the tag belongs to this scheme at all — and returns the valid SemVer ones, highest §11 precedence first. Tags that don't extract or don't parse are dropped. The sort is stable, so tags of equal precedence (differing only in build metadata) keep their input order.

type Version added in v0.72.0

type Version struct {
	Major, Minor, Patch uint64
	// Pre holds the pre-release identifiers (§9); empty for a release.
	Pre []string
	// Build holds the build-metadata identifiers (§10); Compare ignores it.
	Build []string
}

Version is a parsed SemVer 2.0.0 version (https://semver.org/spec/v2.0.0.html).

func CoreOf added in v0.78.0

func CoreOf(v Version) Version

CoreOf is v without its pre-release and build metadata.

func Parse added in v0.72.0

func Parse(s string) (Version, error)

Parse parses s strictly per the SemVer 2.0.0 grammar. s must not carry a tag prefix.

func (Version) Core added in v0.72.0

func (v Version) Core() string

Core returns MAJOR.MINOR.PATCH without pre-release or build metadata.

func (Version) IsPreRelease added in v0.72.0

func (v Version) IsPreRelease() bool

IsPreRelease reports whether v carries pre-release identifiers. Build metadata alone does not make a pre-release: 1.4.0+5 is the 1.4.0 release.

func (Version) String added in v0.72.0

func (v Version) String() string

String renders v in canonical SemVer form, including pre-release and build metadata.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL