semver

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package semver provides functionality for parsing, comparing, and manipulating semantic version strings according to the SemVer 1.0.0 and 2.0.0 spec.

Index

Constants

View Source
const (
	// SpecV1 is the string for Semantic Versioning 1.0.0.
	// See https://semver.org/spec/v1.0.0.html.
	SpecV1 = "1.0.0"

	// SpecV2 is the string for Semantic Versioning 2.0.0.
	// See https://semver.org/spec/v2.0.0.html.
	SpecV2 = "2.0.0"
)

Variables

This section is empty.

Functions

func DeriveNext

func DeriveNext(changeLevel ChangeLevel, currentVersion string, opts DeriveNextOptions) (string, error)

DeriveNext determines the appropriate SemVer version bump based on the provided ChangeLevel and the provided DeriveNextOptions.

func DeriveNextPreview added in v0.8.0

func DeriveNextPreview(previewVersion, stableVersion string, opts DeriveNextOptions) (string, error)

DeriveNextPreview determines the next appropriate SemVer version bump for the preview version relative to the provided stable version. Previews always lead the stable version, so when the preview is equal with or behind the stable version, it must be caught up. When the preview version is ahead, a prerelease number bump is all that is necessary. Every change is treated as a Minor change. The provided preview version must have a prerelease segment.

func MaxVersion

func MaxVersion(versionStrings ...string) string

MaxVersion returns the largest semantic version string among the provided version strings.

func ValidateNext added in v0.8.0

func ValidateNext(currentVersion, nextVersion string) error

ValidateNext checks that nextVersion is a valid version to follow after currentVersion. The nextVersion must always be valid, and if currentVersion is not empty, then nextVersion must be a later version than currentVersion. ValidateNext returns nil if nextVersion is a valid version to follow after currentVersion, or a descriptive error otherwise.

Types

type ChangeLevel

type ChangeLevel int

ChangeLevel represents the level of change, corresponding to semantic versioning.

const (
	// None indicates no change.
	None ChangeLevel = iota
	// Patch is for backward-compatible bug fixes.
	Patch
	// Minor is for backward-compatible new features.
	Minor
	// Major is for incompatible API changes.
	Major
)

func (ChangeLevel) String

func (c ChangeLevel) String() string

String converts a ChangeLevel to its string representation.

type DeriveNextOptions added in v0.8.0

type DeriveNextOptions struct {
	// BumpVersionCore forces the version bump to occur in the version core,
	// as opposed to the prerelease number, if one was present. If true, and
	// the version has a prerelease number, that number will be reset to 1.
	//
	// Default behavior is to prefer bumping the prerelease number or adding one
	// when the version is a prerelease without a number.
	BumpVersionCore bool

	// DowngradePreGAChanges specifically forces [Minor] changes to be treated
	// as [Patch] changes when the current version is pre-1.0.0. [Major] changes
	// are always downgraded to [Minor] changes when the current version is
	// pre-1.0.0 regardless of if this is enabled. This is primarily for Rust.
	//
	// This has no effect on prerelease versions unless BumpVersionCore is also
	// enabled.
	DowngradePreGAChanges bool
}

DeriveNextOptions contains options for controlling SemVer version derivation.

type Version

type Version struct {
	Major, Minor, Patch int
	// Prerelease is the non-numeric part of the prerelease string (e.g., "alpha", "beta").
	Prerelease string
	// PrereleaseSeparator is the separator between the pre-release string and
	// its version (e.g., "."). SemVer 1.0.0 versions do not have a prerelease
	// separator.
	PrereleaseSeparator string
	// PrereleaseNumber is the numeric part of the pre-release segment of the
	// version string (e.g., the 1 in "alpha.1"). Zero is a valid pre-release
	// number. If there is no numeric part in the pre-release segment, this
	// field is nil.
	PrereleaseNumber *int
	// SpecVersion is the SemVer spec version detected in the version string.
	// Currently, this only impacts stringifying of prerelease numbers.
	SpecVersion string
}

Version represents a semantic version.

func Parse

func Parse(versionString string) (Version, error)

Parse deconstructs the SemVer 1.0.0 or 2.0.0 version string into a Version struct.

func (Version) String

func (v Version) String() string

String formats a Version struct into a string.

Jump to

Keyboard shortcuts

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