Documentation ¶
Overview ¶
Package version contains SemVer compatible Version implementation and the bump strategies
See the spec: https://semver.org/spec/v2.0.0.html
Index ¶
- type BumpStrategy
- type BumpStrategyOptions
- type Error
- type GitRepo
- type Version
- func (v Version) BumpMajor() Version
- func (v Version) BumpMinor() Version
- func (v Version) BumpPatch() Version
- func (v Version) BumpPreRelease(preRelease string, overwrite bool, semverBumper versionBumper) Version
- func (v Version) HasSamePreReleaseIdentifiers(identifiers string) bool
- func (v Version) IsPreRelease() bool
- func (v *Version) IsUnstable() bool
- func (v Version) String() string
- func (v Version) WithBuildMetadata(metadata string) Version
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BumpStrategy ¶
type BumpStrategy int
BumpStrategy represents the SemVer number that needs to be bumped
const ( // PATCH means to bump the patch number PATCH BumpStrategy = iota // MINOR means to bump the minor number MINOR // MAJOR means to bump the patch number MAJOR // AUTO means to apply the automatic strategy based on commit history AUTO )
func ParseBumpStrategy ¶
func ParseBumpStrategy(value string) BumpStrategy
ParseBumpStrategy converts string value to BumpStrategy
func (BumpStrategy) String ¶
func (b BumpStrategy) String() string
type BumpStrategyOptions ¶
type BumpStrategyOptions struct { // Strategy defines the strategy to use to bump the version. // It can be automatic (AUTO) or manual (MAJOR, MINOR, PATCH) Strategy BumpStrategy // PreRelease defines the pre-release class (alpha, beta, etc.) for the next version PreRelease string // PreReleaseOverwrite defines if a pre-release can be overwritten // If true, it will not append an index to the next version // If false, it will append an incremented index based on the previous same version of same class if any and 0 otherwise PreReleaseOverwrite bool // BuildMetadata defines the build metadata for the next version BuildMetadata string // RegexReleaseBranches is the regex used to detect if the current branch is a release branch RegexReleaseBranches *regexp.Regexp // RegexMajor is the regex used to detect if a commit contains a breaking/major change // See RegexMinor for more details RegexMajor *regexp.Regexp // RegexMinor is the regex used to detect if a commit contains a minor change // If no commit match RegexMajor or RegexMinor, the change is considered as a patch RegexMinor *regexp.Regexp // contains filtered or unexported fields }
BumpStrategyOptions allows you to configure the bump strategy
func NewConventionalCommitBumpStrategyOptions ¶
func NewConventionalCommitBumpStrategyOptions(gitRepo GitRepo) *BumpStrategyOptions
NewConventionalCommitBumpStrategyOptions create a BumpStrategyOptions following https://www.conventionalcommits.org
The strategy configuration is:
Strategy: AUTO PreRelease: "" PreReleaseOverwrite: false BuildMetadata: "" RegexReleaseBranches: ^(master|release/.*)$ RegexMajor: (?m)^BREAKING CHANGE:.*$ RegexMinor: ^feat(?:\(.+\))?:.*
func (*BumpStrategyOptions) Bump ¶
func (o *BumpStrategyOptions) Bump() (Version, error)
Bump performs the version bumping based on the strategy
Example ¶
package main import ( "fmt" "github.com/arnaud-deprez/gsemver/internal/git" "github.com/arnaud-deprez/gsemver/pkg/version" ) func main() { gitRepo := git.NewVersionGitRepo("dir") bumpStrategy := version.NewConventionalCommitBumpStrategyOptions(gitRepo) v, err := bumpStrategy.Bump() if err != nil { panic(err) } fmt.Println(v.String()) // Use v like you want }
Output:
func (BumpStrategyOptions) GoString ¶ added in v0.2.0
func (o BumpStrategyOptions) GoString() string
GoString makes BumpStrategyOptions satisfy the GoStringer interface.
func (*BumpStrategyOptions) WithPreRelease ¶
func (o *BumpStrategyOptions) WithPreRelease(value string, override bool) *BumpStrategyOptions
WithPreRelease sets the pre-release name
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error is a typical error representation that can happen during the version bump process
type GitRepo ¶
type GitRepo interface { // FetchTags fetches the tags from remote FetchTags() error // GetCommits return the list of commits between 2 revisions. // If no revision is provided, it does from beginning to HEAD GetCommits(from string, to string) ([]git.Commit, error) // CountCommits counts the number of commits between 2 revisions. CountCommits(from string, to string) (int, error) // GetLastRelativeTag gives the last ancestor tag from HEAD GetLastRelativeTag(rev string) (git.Tag, error) // GetCurrentBranch gives the current branch from HEAD GetCurrentBranch() (string, error) }
GitRepo defines common git actions used by gsemver
type Version ¶
type Version struct { // Major represents the major (aka X) number in a semver version Major int `json:"major"` // Minor represents the minor (aka Y) number in a semver version Minor int `json:"minor"` // Patch represents the patch (aka Z) number in a semver version Patch int `json:"patch"` // PreRelease represents the optional pre-release information in a semver version PreRelease string `json:"preRelease,omitempty"` // BuildMetadata represents the optional build metadata in a semver version BuildMetadata string `json:"buildMetadata,omitempty"` }
Version object to represent a SemVer version
func NewVersion ¶
NewVersion creates a new Version from a string representation
Example ¶
NewVersion("1.2.3") NewVersion("v1.2.3") // with v prefix NewVersion("2.3.5-beta") // pre-release overwritable NewVersion("2.3.5-beta.5") // pre-release with index NewVersion("2.3.5+metadata") // build-metadata
Output:
func (Version) BumpMajor ¶
BumpMajor bump the major number of the version
Example ¶
v := Version{Major: 1} // 1.0.0 v2 := v.BumpMajor() fmt.Println(v2.String())
Output: 2.0.0
func (Version) BumpMinor ¶
BumpMinor bumps the minor number of the version
Example ¶
v := Version{Major: 1} // 1.0.0 v2 := v.BumpMinor() fmt.Println(v2.String())
Output: 1.1.0
func (Version) BumpPatch ¶
BumpPatch bumps the patch number of the version
Example ¶
v := Version{Major: 1} // 1.0.0 v2 := v.BumpPatch() fmt.Println(v2.String())
Output: 1.0.1
func (Version) BumpPreRelease ¶
func (v Version) BumpPreRelease(preRelease string, overwrite bool, semverBumper versionBumper) Version
BumpPreRelease bumps the pre-release identifiers
Example ¶
v1 := Version{Major: 1} // 1.0.0 // Parameters: pre-release, pre-release overwrite, versionBumper (default to Version.BumpMinor) v2 := v1.BumpPreRelease("alpha", false, nil) // The current version is not a pre-release, so it will use the versionBumper to first bump the minor (default) and then set the pre-release to alpha.0 fmt.Println(v2.String()) // However if the current is already a pre-release of the same class (alpha here), then it just increments the pre-release id v3 := v2.BumpPreRelease("alpha", false, nil) fmt.Println(v3.String())
Output: 1.1.0-alpha.0 1.1.0-alpha.1
Example (Overwrite) ¶
v1 := Version{Major: 1} // 1.0.0 // If you don't want to have pre-release index, you can pass true for pre-release overwrite as parameter v2 := v1.BumpPreRelease("alpha", true, nil) fmt.Println(v2.String()) // But then it means your version can be overwritten if you perform again the same operation v3 := v2.BumpPreRelease("alpha", true, nil) fmt.Println(v3.String())
Output: 1.1.0-alpha 1.1.0-alpha
Example (VersionBumper) ¶
v1 := Version{Major: 1} // 1.0.0 // It is also possible to overwrite the default version bumper v2 := v1.BumpPreRelease("alpha", false, Version.BumpMajor) fmt.Println(v2.String()) // But if the previous is already a pre-release of the same class (alpha here), then it will not be used v3 := v2.BumpPreRelease("alpha", false, Version.BumpMajor) fmt.Println(v3.String())
Output: 2.0.0-alpha.0 2.0.0-alpha.1
func (Version) HasSamePreReleaseIdentifiers ¶
HasSamePreReleaseIdentifiers returns true if the version has the same pre-release identifiers. The parameter identifiers is a string where identifiers are separated by .
func (Version) IsPreRelease ¶
IsPreRelease returns true if it's a pre-release version. eg 1.1.0-alpha.1
func (*Version) IsUnstable ¶
IsUnstable returns true if the version is an early stage version. eg. 0.Y.Z
func (Version) String ¶
String returns a string representation of a Version object. The format is: major.minor.patch[-pre_release_identifiers][+build_metadata]
func (Version) WithBuildMetadata ¶
WithBuildMetadata return a new Version with build metadata
Example ¶
v := Version{Major: 1} // 1.0.0 v2 := v.WithBuildMetadata("build.1") // this simply set the build metadata to the version fmt.Println(v2.String())
Output: 1.0.0+build.1