version

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2024 License: MIT Imports: 9 Imported by: 0

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

Examples

Constants

View Source
const (
	// DefaultMajorPattern defines default regular expression to match a commit message with a major change.
	DefaultMajorPattern = `(?:^.+\!:.+|(?m)^BREAKING CHANGE:.+$)`
	// DefaultMinorPattern defines default regular expression to match a commit message with a minor change.
	DefaultMinorPattern = `^(?:feat|chore|build|ci|refactor|perf)(?:\(.+\))?:.+`
	// DefaultReleaseBranchesPattern defines default regular expression to match release branches
	DefaultReleaseBranchesPattern = `^(main|master|release/.*)$`
	// DefaultPreRelease defines default pre-release activation for non release branches
	DefaultPreRelease = false
	// DefaultPreReleaseTemplate defines default pre-release go template for non release branches
	DefaultPreReleaseTemplate = ""
	// DefaultPreReleaseOverwrite defines default pre-release overwrite activation for non release branches
	DefaultPreReleaseOverwrite = false
	// DefaultBuildMetadataTemplate defines default go template used for non release branches strategy
	DefaultBuildMetadataTemplate = `{{.Commits | len}}.{{(.Commits | first).Hash.Short}}`
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BumpBranchesStrategy added in v0.3.0

type BumpBranchesStrategy struct {
	// Strategy defines the strategy to use to bump the version.
	// It can be automatic (AUTO) or manual (MAJOR, MINOR, PATCH)
	Strategy BumpStrategyType `json:"strategy"`
	// BranchesPattern is the regex used to match against the current branch
	BranchesPattern *regexp.Regexp `json:"branchesPattern,omitempty"`
	// PreRelease defines if the bump strategy should generate a pre-release version
	PreRelease bool `json:"preRelease"`
	// PreReleaseTemplate defines the pre-release template for the next version
	// It can be alpha, beta, or a go-template expression
	PreReleaseTemplate *template.Template `json:"preReleaseTemplate,omitempty"`
	// 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 `json:"preReleaseOverwrite"`
	// BuildMetadataTemplate defines the build metadata for the next version.
	// It can be a static value but it will usually be a go-template expression to guarantee uniqueness of each built version.
	BuildMetadataTemplate *template.Template `json:"buildMetadataTemplate,omitempty"`
}

BumpBranchesStrategy allows you to configure the bump strategy option for a matching set of branches.

func NewBuildBumpBranchesStrategy added in v0.4.0

func NewBuildBumpBranchesStrategy(pattern string, buildMetadataTemplate string) *BumpBranchesStrategy

NewBuildBumpBranchesStrategy creates a new BumpBranchesStrategy for build version strategy.

func NewBumpAllBranchesStrategy added in v0.5.0

func NewBumpAllBranchesStrategy(strategy BumpStrategyType, preRelease bool, preReleaseTemplate string, preReleaseOverwrite bool, buildMetadataTemplate string) *BumpBranchesStrategy

NewBumpAllBranchesStrategy creates a new BumpBranchesStrategy that matches all branches.

func NewBumpBranchesStrategy added in v0.3.0

func NewBumpBranchesStrategy(strategy BumpStrategyType, pattern string, preRelease bool, preReleaseTemplate string, preReleaseOverwrite bool, buildMetadataTemplate string) *BumpBranchesStrategy

NewBumpBranchesStrategy creates a new BumpBranchesStrategy

func NewDefaultBumpBranchesStrategy added in v0.3.0

func NewDefaultBumpBranchesStrategy(pattern string) *BumpBranchesStrategy

NewDefaultBumpBranchesStrategy creates a new BumpBranchesStrategy for pre-release version strategy.

func NewPreReleaseBumpBranchesStrategy added in v0.4.0

func NewPreReleaseBumpBranchesStrategy(pattern string, preReleaseTemplate string, preReleaseOverwrite bool) *BumpBranchesStrategy

NewPreReleaseBumpBranchesStrategy creates a new BumpBranchesStrategy for pre-release version strategy.

func (BumpBranchesStrategy) GoString added in v0.4.0

func (s BumpBranchesStrategy) GoString() string

GoString makes BumpBranchesStrategy satisfy the GoStringer interface.

Example
s := NewBumpBranchesStrategy(AUTO, ".*", true, "foo", true, "bar")
fmt.Printf("%#v\n", s)
Output:

version.BumpBranchesStrategy{Strategy: AUTO, BranchesPattern: &regexp.Regexp{expr: ".*"}, PreRelease: true, PreReleaseTemplate: &template.Template{text: "foo"}, PreReleaseOverwrite: true, BuildMetadataTemplate: &template.Template{text: "bar"}}

func (*BumpBranchesStrategy) MarshalJSON added in v0.3.0

func (s *BumpBranchesStrategy) MarshalJSON() ([]byte, error)

MarshalJSON implements json encoding

func (*BumpBranchesStrategy) UnmarshalJSON added in v0.3.0

func (s *BumpBranchesStrategy) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json decoding

type BumpStrategy

type BumpStrategy struct {
	// MajorPattern is the regex used to detect if a commit contains a breaking/major change
	// See RegexMinor for more details
	MajorPattern *regexp.Regexp `json:"majorPattern,omitempty"`
	// MinorPattern 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
	MinorPattern *regexp.Regexp `json:"minorPattern,omitempty"`
	// BumpStrategies is a list of bump strategies for matching branches
	BumpStrategies []BumpBranchesStrategy `json:"bumpStrategies,omitempty"`
	// contains filtered or unexported fields
}

BumpStrategy allows you to configure the bump strategy

func NewConventionalCommitBumpStrategy added in v0.3.0

func NewConventionalCommitBumpStrategy(gitRepo GitRepo) *BumpStrategy

NewConventionalCommitBumpStrategy create a BumpStrategy following https://www.conventionalcommits.org

The strategy configuration is:

MajorPattern: (?:^.+\!:.+|(?m)^BREAKING CHANGE:.+$)
MinorPattern: ^(?:feat|chore|build|ci|refactor|perf)(?:\(.+\))?:.+
BumpBranchesStrategies: [
	{
		Strategy: AUTO
		BranchesPattern: ^(main|master|release/.*)$
		PreRelease:             false
		PreReleaseTemplate:     ""
		PreReleaseOverwrite:    false
		BuildMetadataTemplate:  ""
	},
	{
		Strategy: AUTO
		BranchesPattern: .*
		PreRelease:    		    false
		PreReleaseTemplate:     ""
		PreReleaseOverwrite:    false
		BuildMetadataTemplate:  "{{.Commits | len}}.{{(.Commits | first).Hash.Short}}"
	}
]

func (*BumpStrategy) Bump added in v0.3.0

func (o *BumpStrategy) 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.NewConventionalCommitBumpStrategy(gitRepo)
	v, err := bumpStrategy.Bump()
	if err != nil {
		panic(err)
	}
	fmt.Println(v.String())
	// Use v like you want
}
Output:

func (BumpStrategy) GoString added in v0.3.0

func (o BumpStrategy) GoString() string

GoString makes BumpStrategy satisfy the GoStringer interface.

Example
gitRepo := mock_version.NewMockGitRepo(nil)
s := NewConventionalCommitBumpStrategy(gitRepo)
fmt.Printf("%#v\n", s)
Output:

version.BumpStrategy{MajorPattern: &regexp.Regexp{expr: "(?:^.+\\!:.+|(?m)^BREAKING CHANGE:.+$)"}, MinorPattern: &regexp.Regexp{expr: "^(?:feat|chore|build|ci|refactor|perf)(?:\\(.+\\))?:.+"}, BumpBranchesStrategies: []version.BumpBranchesStrategy{version.BumpBranchesStrategy{Strategy: AUTO, BranchesPattern: &regexp.Regexp{expr: "^(main|master|release/.*)$"}, PreRelease: false, PreReleaseTemplate: &template.Template{text: ""}, PreReleaseOverwrite: false, BuildMetadataTemplate: &template.Template{text: ""}}, version.BumpBranchesStrategy{Strategy: AUTO, BranchesPattern: &regexp.Regexp{expr: ".*"}, PreRelease: false, PreReleaseTemplate: &template.Template{text: ""}, PreReleaseOverwrite: false, BuildMetadataTemplate: &template.Template{text: "{{.Commits | len}}.{{(.Commits | first).Hash.Short}}"}}}}

func (*BumpStrategy) SetGitRepository added in v0.5.0

func (o *BumpStrategy) SetGitRepository(gitRepo GitRepo)

SetGitRepository configures the git repository to use for the strategy

type BumpStrategyType added in v0.3.0

type BumpStrategyType int

BumpStrategyType represents the bump SemVer strategy to use to bump the version

const (
	// PATCH means to bump the patch number
	PATCH BumpStrategyType = 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 ParseBumpStrategyType added in v0.3.0

func ParseBumpStrategyType(value string) BumpStrategyType

ParseBumpStrategyType converts string value to BumpStrategy

func (BumpStrategyType) MarshalJSON added in v0.3.0

func (b BumpStrategyType) MarshalJSON() ([]byte, error)

MarshalJSON implements marshall for encoding/json

func (BumpStrategyType) String added in v0.3.0

func (b BumpStrategyType) String() string

func (*BumpStrategyType) UnmarshalJSON added in v0.3.0

func (b *BumpStrategyType) UnmarshalJSON(bs []byte) error

UnmarshalJSON implements unmarshall for encoding/json

type Context added in v0.3.0

type Context struct {
	// Branch is the current branch name
	Branch string
	// LastVersion is a semver version representation of the last git tag
	LastVersion *Version
	// LastTag is the last git tag
	LastTag *git.Tag
	// Commits is the list of commits from the previous tag until now
	Commits []git.Commit
}

Context represents the context data used to compute the next version. This context is also used as template data.

func NewContext added in v0.3.0

func NewContext(branch string, lastVersion *Version, lastTag *git.Tag, commits []git.Commit) *Context

NewContext returns a new Context

func (*Context) EvalTemplate added in v0.3.0

func (c *Context) EvalTemplate(template *template.Template) string

EvalTemplate evaluates the given template against the current context

type Error

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

Error is a typical error representation that can happen during the version bump process

func (Error) Error

func (e Error) Error() string

Error formats VersionError into a string

Example
err := newError("Error 1 occurred")
fmt.Println(err)
err = newErrorC(newError("Error 3"), "Error 2 occurred")
fmt.Println(err)
Output:

Error 1 occurred
Error 2 occurred caused by: Error 3

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

func NewVersion(value string) (Version, error)

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

func (v Version) BumpMajor() Version

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

func (v Version) BumpMinor() Version

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

func (v Version) BumpPatch() Version

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 func(Version) Version) 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) GetPreReleaseIncrement added in v0.4.0

func (v Version) GetPreReleaseIncrement() (int, error)

GetPreReleaseIncrement returns the current pre-release increment or an error if there is not.

func (Version) IsPreRelease

func (v Version) IsPreRelease() bool

IsPreRelease returns true if it's a pre-release version. eg 1.1.0-alpha.1

func (*Version) IsUnstable

func (v *Version) IsUnstable() bool

IsUnstable returns true if the version is an early stage version. eg. 0.Y.Z

func (Version) PreReleaseIdentifiersEqual added in v0.4.0

func (v Version) PreReleaseIdentifiersEqual(identifiers string) bool

PreReleaseIdentifiersEqual returns true if the version has the same pre-release identifiers. The parameter identifiers is a string where identifiers are separated by .

func (Version) String

func (v Version) String() string

String returns a string representation of a Version object. The format is: major.minor.patch[-pre_release_identifiers][+build_metadata]

func (Version) WithBuildMetadata

func (v Version) WithBuildMetadata(metadata string) Version

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

Jump to

Keyboard shortcuts

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