version

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2019 License: MIT Imports: 6 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

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

func (Error) Error

func (e Error) Error() string

Error formats VersionError into a string

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 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

func (v Version) HasSamePreReleaseIdentifiers(identifiers string) bool

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

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) 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

Directories

Path Synopsis
Package mock_version is a generated GoMock package.
Package mock_version is a generated GoMock package.

Jump to

Keyboard shortcuts

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