diff --git a/README.md b/README.md index 89cec52..3555819 100644 --- a/README.md +++ b/README.md @@ -4,20 +4,36 @@ Replacement for `git describe --tags` that produces [semver](https://semver.org/ ## Comparison -Previous git tag | git describe --tags | git-describe-semver --fallback v0.0.0 ---- | --- | --- -`v1.2.3` | `v1.2.3` | `v1.2.3` -`v1.2.3` | `v1.2.3-23-gabc1234` | `v1.2.4-dev.23.gabc1234` -`v1.3.0-rc.1` | `v1.3.0-rc.1-23-gabc1234` | `v1.3.0-rc.1.dev.23.gabc1234` -`v1.3.0-rc.1+info` | `v1.3.0-rc.1+info-23-gabc1234` | `v1.3.0-rc.1.dev.23.gabc1234+info` -none | fail | `v0.0.0-dev.23.gabc1234` +| Previous git tag | git describe --tags | git-describe-semver --fallback v0.0.0 | +|--------------------|--------------------------------|---------------------------------------| +| `v1.2.3` | `v1.2.3` | `v1.2.3` | +| `v1.2.3` | `v1.2.3-23-gabc1234` | `v1.2.4-dev.23.gabc1234` | +| `v1.3.0-rc.1` | `v1.3.0-rc.1-23-gabc1234` | `v1.3.0-rc.1.dev.23.gabc1234` | +| `v1.3.0-rc.1+info` | `v1.3.0-rc.1+info-23-gabc1234` | `v1.3.0-rc.1.dev.23.gabc1234+info` | +| none | fail | `v0.0.0-dev.23.gabc1234` | + +## Next Release + +| Previous git tag | git describe --tags | git-describe-semver --fallback v0.0.0 | --next-release | +|------------------|---------------------------|---------------------------------------|-----------------------| +| `v1.2.3` | `v1.2.3` | `v1.2.4` | patch | +| `v1.2.3` | `v1.2.3-23-gabc1234` | `v1.2.4` | patch | +| `v1.3.0-rc.1` | `v1.3.0-rc.1-23-gabc1234` | `v1.3.0` | patch / minor | +| `v1.3.1-rc.1` | `v1.3.1-rc.1-23-gabc1234` | `v1.4.0` | minor | +| `v1.0.0-rc.1` | `v1.0.0-rc.1-23-gabc1234` | `v1.0.0` | major | +| `v1.0.0` | `v1.0.0` | `v2.0.0` | major | +| `v1.0.1-rc.1` | `v1.0.1-rc.1-23-gabc1234` | `v2.0.0` | major | +| none | fail | `v0.0.0` | patch / minor / major | ## Usage -* Flag `--dir /some/git/worktree`: Git worktree directory (defaults to current directory) +* Flag `--dir /some/git/worktree`: Git worktree directory (defaults to current directory `.`) * Flag `--fallback v0.0.0`: Fallback to given tag name if no tag is available * Flag `--drop-prefix`: Drop any present prefix (like `v`) from the output * Flag `--prerelease-suffix`: Adds a dash-separated suffix to the prerelease part +* Flag `--prerelease-prefix`: Adds a dash-separated prefix to the prerelease part (defaults to `dev`) +* Flag `--prerelease-timestamped`: Use timestamp instead of commit count for prerelease +* Flag `--next-release`: Bump current version to next release (choices: `major`, `minor`, `patch`) * Flag `--format`: Changes output (use `` as placeholder) ### Docker @@ -50,5 +66,6 @@ jobs: prerelease-prefix: dev prerelease-suffix: SNAPSHOT prerelease-timestamped: true + next-release: '' - run: echo This is the version ${{ steps.git-describe-semver.outputs.version }} ``` diff --git a/action.yaml b/action.yaml index 984c1ed..b4f6d04 100644 --- a/action.yaml +++ b/action.yaml @@ -2,29 +2,32 @@ name: 'Git describe semver' description: '' inputs: version: - description: 'version' + description: 'Which git-describe-semver release should be used?' default: 'latest' dir: - description: 'dir' + description: 'Git worktree directory (defaults to current directory)' default: '.' fallback: - description: 'fallback' + description: 'Fallback to given tag name if no tag is available' default: 'v0.0.0' drop-prefix: - description: 'drop-prefix' + description: 'Drop any present prefix (like "v") from the output' default: 'false' prerelease-prefix: - description: 'prerelease-suffix' + description: 'Adds a dash-separated prefix to the prerelease part' default: 'dev' prerelease-suffix: - description: 'prerelease-suffix' + description: 'Adds a dash-separated suffix to the prerelease part' default: '' prerelease-timestamped: - description: 'prerelease-timestamped' + description: 'Use timestamp instead of commit count for prerelease' default: 'false' + next-release: + description: 'Bump current version to next release (choices: "major", "minor", "patch")' + default: '' outputs: version: - description: 'version' + description: 'Version output from git-describe-semver' value: ${{ steps.git-describe-semver.outputs.version }} runs: using: 'composite' @@ -53,6 +56,7 @@ runs: --prerelease-prefix="${{ inputs.prerelease-prefix }}" \ --prerelease-suffix="${{ inputs.prerelease-suffix }}" \ --prerelease-timestamped="${{ inputs.prerelease-timestamped }}" \ + --next-release="${{ inputs.next-release }}" \ --format="version=" \ $GITHUB_OUTPUT shell: bash diff --git a/cmd/root.go b/cmd/root.go index 3d76c08..c55ded4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,7 +1,6 @@ package cmd import ( - "flag" "fmt" "io" "os" @@ -10,6 +9,7 @@ import ( "time" "github.com/choffmeister/git-describe-semver/internal" + "github.com/jessevdk/go-flags" ) func run(dir string, opts internal.GenerateVersionOptions) (*string, error) { @@ -35,32 +35,50 @@ func openStdoutOrFile(file string) (io.WriteCloser, error) { return os.OpenFile(file, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644) } +type ParserOptions struct { + Dir string `long:"dir" default:"." description:"The git worktree directory"` + Fallback string `long:"fallback" description:"The first version to fallback to should there be no tag"` + DropPrefix bool `long:"drop-prefix" description:"Drop prefix from output"` + PrereleaseSuffix string `long:"prerelease-suffix" description:"Suffix to add to prereleases"` + PrereleasePrefix string `long:"prerelease-prefix" default:"dev" description:"Prefix to use as start of prerelease"` + PrereleaseTimestamped bool `long:"prerelease-timestamped" description:"Use timestamp instead of commit count for prerelease"` + NextRelease string `long:"next-release" description:"Bump current version to next release" choice:"major" choice:"minor" choice:"patch"` + Format string `long:"format" description:"Format of output (use as placeholder)"` +} + func Execute(version FullVersion) error { - dirFlag := flag.String("dir", ".", "The git worktree directory") - fallbackFlag := flag.String("fallback", "", "The first version to fallback to should there be no tag") - dropPrefixFlag := flag.Bool("drop-prefix", false, "Drop prefix from output") - prereleaseSuffixFlag := flag.String("prerelease-suffix", "", "Suffix to add to prereleases") - prereleasePrefixFlag := flag.String("prerelease-prefix", "dev", "Prefix to use as start of prerelease (default to \"dev\"))") - prereleaseTimestampedFlag := flag.Bool("prerelease-timestamped", false, "Use timestamp instead of commit count for prerelease") - formatFlag := flag.String("format", "", "Format of output") - flag.Parse() + var options ParserOptions + parser := flags.NewParser(&options, flags.Default) + args, err := parser.Parse() + if err != nil { + switch flagsErr := err.(type) { + case flags.ErrorType: + if flagsErr == flags.ErrHelp { + os.Exit(0) + } + os.Exit(1) + default: + os.Exit(1) + } + } opts := internal.GenerateVersionOptions{ - FallbackTagName: *fallbackFlag, - DropTagNamePrefix: *dropPrefixFlag, - PrereleaseSuffix: *prereleaseSuffixFlag, - PrereleasePrefix: *prereleasePrefixFlag, - PrereleaseTimestamped: *prereleaseTimestampedFlag, - Format: *formatFlag, + FallbackTagName: options.Fallback, + DropTagNamePrefix: options.DropPrefix, + PrereleaseSuffix: options.PrereleaseSuffix, + PrereleasePrefix: options.PrereleasePrefix, + PrereleaseTimestamped: options.PrereleaseTimestamped, + NextRelease: options.NextRelease, + Format: options.Format, } - result, err := run(*dirFlag, opts) + result, err := run(options.Dir, opts) if err != nil { return err } file := "-" - if len(flag.Args()) == 1 { - arg := flag.Args()[0] + if len(args) == 1 { + arg := args[0] if strings.HasPrefix(arg, "$") { file = os.Getenv(strings.TrimPrefix(arg, "$")) } else { diff --git a/go.mod b/go.mod index 3137fdb..e03df13 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/Microsoft/go-winio v0.6.0 // indirect github.com/ProtonMail/go-crypto v0.0.0-20221026131551-cf6655e29de4 // indirect github.com/go-git/go-git/v5 v5.4.2 + github.com/jessevdk/go-flags v1.5.0 github.com/kevinburke/ssh_config v1.2.0 // indirect github.com/sergi/go-diff v1.2.0 // indirect github.com/stretchr/testify v1.7.0 diff --git a/go.sum b/go.sum index 95c4016..bc15fda 100644 --- a/go.sum +++ b/go.sum @@ -13,7 +13,6 @@ github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYU github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= -github.com/bwesterb/go-ristretto v1.2.2/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= github.com/cloudflare/circl v1.1.0/go.mod h1:prBCrKB9DV4poKZY1l9zBXg2QJY7mvgRvtMxxK7fi4I= github.com/cloudflare/circl v1.3.0 h1:Anq00jxDtoyX3+aCaYUZ0vXC5r4k4epberfWGDXV1zE= github.com/cloudflare/circl v1.3.0/go.mod h1:+CauBF6R70Jqcyl8N2hC8pAXYbWkGIezuSbuGLtRhnw= @@ -43,6 +42,7 @@ github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk= github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= +github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc= github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4= @@ -79,32 +79,22 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0= github.com/xanzy/ssh-agent v0.3.2 h1:eKj4SX2Fe7mui28ZgnFW5fmTz1EIr7ugo5s6wDxdHBM= github.com/xanzy/ssh-agent v0.3.2/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw= -github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.3.0 h1:a06MkbcxBrEFc0w0QIZWXrH/9cCX6KJyWbBOIwAn+7A= golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA= golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210326060303-6b1517762897/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.2.0 h1:sZfSu1wtKLGlWI4ZZayP0ck9Y73K1ynO6gqzTdBVdPU= golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -117,28 +107,17 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A= golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.2.0 h1:z85xZCsEl7bi/KwbNADeBYoOP0++7W1ipu+aGnpwzRM= -golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg= -golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.3.0 h1:SrNbZl6ECOS1qFzgTdQfWXZM9XBkiA6tkFrH9YSTPHM= golang.org/x/tools v0.3.0/go.mod h1:/rWhSS2+zyEVwoJf8YAX6L2f0ntZ7Kn/mGgAWcipA5k= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/internal/generate.go b/internal/generate.go index a50da25..92c1e89 100644 --- a/internal/generate.go +++ b/internal/generate.go @@ -14,6 +14,7 @@ type GenerateVersionOptions struct { PrereleaseSuffix string PrereleasePrefix string PrereleaseTimestamped bool + NextRelease string Format string } @@ -64,6 +65,7 @@ func GenerateVersion(tagName string, counter int, headHash string, timestamp tim } } } + version.Bump(opts.NextRelease) if opts.DropTagNamePrefix { version.Prefix = "" } diff --git a/internal/generate_test.go b/internal/generate_test.go index 4c6b66a..8dc4743 100644 --- a/internal/generate_test.go +++ b/internal/generate_test.go @@ -24,6 +24,21 @@ func TestGenerateVersion(t *testing.T) { test("0.0.0-rc.1+foobar", 1, "abc1234", GenerateVersionOptions{PrereleasePrefix: "dev"}, "0.0.0-rc.1.dev.1.gabc1234+foobar") test("v0.0.0-rc.1+foobar", 1, "abc1234", GenerateVersionOptions{PrereleasePrefix: "dev"}, "v0.0.0-rc.1.dev.1.gabc1234+foobar") + test("", 1, "abc1234", GenerateVersionOptions{FallbackTagName: "v0.0.0", NextRelease: "patch"}, "v0.0.0") + test("", 1, "abc1234", GenerateVersionOptions{FallbackTagName: "v0.0.0", NextRelease: "minor"}, "v0.0.0") + test("", 1, "abc1234", GenerateVersionOptions{FallbackTagName: "v0.0.0", NextRelease: "major"}, "v0.0.0") + test("v0.0.0-rc.1+foobar", 1, "abc1234", GenerateVersionOptions{NextRelease: "patch"}, "v0.0.0") + test("v0.0.1+foobar", 1, "abc1234", GenerateVersionOptions{NextRelease: "patch"}, "v0.0.2") + test("v1.2.3", 1, "abc1234", GenerateVersionOptions{NextRelease: "patch"}, "v1.2.4") + test("v0.1.0-rc1", 1, "abc1234", GenerateVersionOptions{NextRelease: "minor"}, "v0.1.0") + test("v0.1.0", 1, "abc1234", GenerateVersionOptions{NextRelease: "minor", DropTagNamePrefix: true}, "0.2.0") + test("v0.1.1-rc1", 1, "abc1234", GenerateVersionOptions{NextRelease: "minor"}, "v0.2.0") + test("v1.2.3", 1, "abc1234", GenerateVersionOptions{NextRelease: "minor"}, "v1.3.0") + test("v1.0.0-rc1", 1, "abc1234", GenerateVersionOptions{NextRelease: "major"}, "v1.0.0") + test("v1.0.0", 1, "abc1234", GenerateVersionOptions{NextRelease: "major"}, "v2.0.0") + test("v1.0.1-rc1", 1, "abc1234", GenerateVersionOptions{NextRelease: "major"}, "v2.0.0") + test("v1.2.3", 1, "abc1234", GenerateVersionOptions{NextRelease: "major", Format: "v", DropTagNamePrefix: true}, "v2.0.0") + test("", 1, "abc1234", GenerateVersionOptions{FallbackTagName: "0.0.0", PrereleasePrefix: "dev"}, "0.0.0-dev.1.gabc1234") test("", 1, "abc1234", GenerateVersionOptions{FallbackTagName: "v0.0.0", PrereleasePrefix: "dev"}, "v0.0.0-dev.1.gabc1234") diff --git a/internal/semver.go b/internal/semver.go index c377bfc..60a214a 100644 --- a/internal/semver.go +++ b/internal/semver.go @@ -28,6 +28,38 @@ func (v SemVer) Equal(v2 SemVer) bool { equalStringSlice(v.BuildMetadata, v2.BuildMetadata) } +// Bump ... +func (v *SemVer) Bump(nextRelease string) { + if nextRelease == "" { + return + } + isPrerelease := len(v.Prerelease) > 0 + patch := v.Patch + if nextRelease == "patch" && !isPrerelease { + patch++ + } + minor := v.Minor + if nextRelease == "minor" { + if v.Patch != 0 || !isPrerelease { + minor++ + } + patch = 0 + } + major := v.Major + if nextRelease == "major" { + if v.Patch != 0 || !isPrerelease { + major++ + } + minor = 0 + patch = 0 + } + v.Major = major + v.Minor = minor + v.Patch = patch + v.Prerelease = []string{} + v.BuildMetadata = []string{} +} + // String ... func (v SemVer) String() string { str := fmt.Sprintf("%s%d.%d.%d", v.Prefix, v.Major, v.Minor, v.Patch)