From 01a58abc78cb08acd4e5d5308fb71bbc1bbf8085 Mon Sep 17 00:00:00 2001 From: joe miller Date: Thu, 21 May 2020 07:32:35 -0700 Subject: [PATCH] [minor] add -e flag to disable appending literal v in tag implements #16 --- autotag.go | 11 ++++++++++- autotag/main.go | 2 ++ autotag_test.go | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/autotag.go b/autotag.go index 5a69ad2..e56bef0 100644 --- a/autotag.go +++ b/autotag.go @@ -33,7 +33,7 @@ var ( conventionalCommitRex = regexp.MustCompile(`^\s*(?P\w+)(?P(?:\([^()\r\n]*\)|\()?(?P!)?)(?P:.*)?`) // versionRex matches semVer style versions, eg: `v1.0.0` - versionRex = regexp.MustCompile(`^v([\d]+\.?.*)`) + versionRex = regexp.MustCompile(`^v?([\d]+\.?.*)`) // semVerBuildMetaRex validates SemVer build metadata strings according to // https://semver.org/#spec-item-10 @@ -104,6 +104,9 @@ type GitRepoConfig struct { // * "conventional" implements the Conventional Commits v1.0.0 scheme. // * https://www.conventionalcommits.org/en/v1.0.0/#summary w Scheme string + + // Prefix prepends literal 'v' to the tag, eg: v1.0.0. Enabled by default + Prefix bool } // GitRepo represents a repository we want to run actions against @@ -121,6 +124,8 @@ type GitRepo struct { buildMetadata string scheme string + + prefix bool } // NewRepo is a constructor for a repo object, parsing the tags that exist @@ -152,6 +157,7 @@ func NewRepo(cfg GitRepoConfig) (*GitRepo, error) { preReleaseTimestampLayout: cfg.PreReleaseTimestampLayout, buildMetadata: cfg.BuildMetadata, scheme: cfg.Scheme, + prefix: cfg.Prefix, } err = r.parseTags() @@ -412,6 +418,9 @@ func (r *GitRepo) AutoTag() error { func (r *GitRepo) tagNewVersion() error { // TODO:(jnelson) These should be configurable? Mon Sep 14 12:02:52 2015 tagName := fmt.Sprintf("v%s", r.newVersion.String()) + if !r.prefix { + tagName = r.newVersion.String() + } log.Println("Writing Tag", tagName) err := r.repo.CreateTag(tagName, r.branchID) diff --git a/autotag/main.go b/autotag/main.go index 4e15dd9..68e16cd 100644 --- a/autotag/main.go +++ b/autotag/main.go @@ -20,6 +20,7 @@ type Options struct { PreReleaseTimestamp string `short:"T" long:"pre-release-timestamp" description:"create a pre-release tag and append a timestamp (can be: datetime|epoch)"` BuildMetadata string `short:"m" long:"build-metadata" description:"optional SemVer build metadata to append to the version with '+' character"` Scheme string `short:"s" long:"scheme" description:"The commit message scheme to use (can be: autotag|conventional)" default:"autotag"` + NoVersionPrefix bool `short:"e" long:"empty-version-prefix" description:"Do not prepend v to version tag"` } var opts Options @@ -45,6 +46,7 @@ func main() { PreReleaseTimestampLayout: opts.PreReleaseTimestamp, BuildMetadata: opts.BuildMetadata, Scheme: opts.Scheme, + Prefix: !opts.NoVersionPrefix, }) log.Println("FUCK1") diff --git a/autotag_test.go b/autotag_test.go index fc43e3b..19eaf02 100644 --- a/autotag_test.go +++ b/autotag_test.go @@ -39,6 +39,9 @@ type testRepoSetup struct { // (optional) build metadata to append to the version buildMetadata string + // (optional) prepend literal 'v' to version tags (default: true) + disablePrefix bool + // (optional) commit message to use for the next, untagged commit. Settings this allows for testing the // commit message parsing logic. eg: "#major this is a major commit" nextCommit string @@ -61,6 +64,9 @@ func newTestRepo(t *testing.T, setup testRepoSetup) GitRepo { tag := setup.initialTag if setup.initialTag == "" { tag = "v0.0.1" + if setup.disablePrefix { + tag = "0.0.1" + } } seedTestRepo(t, tag, repo) @@ -81,6 +87,7 @@ func newTestRepo(t *testing.T, setup testRepoSetup) GitRepo { PreReleaseTimestampLayout: setup.preReleaseTimestampLayout, BuildMetadata: setup.buildMetadata, Scheme: setup.scheme, + Prefix: !setup.disablePrefix, }) if err != nil { @@ -132,6 +139,7 @@ func TestValidateConfig(t *testing.T) { PreReleaseName: "dev", PreReleaseTimestampLayout: "epoch", BuildMetadata: "g12345678", + Prefix: true, }, shouldErr: false, }, @@ -338,6 +346,16 @@ func TestAutoTag(t *testing.T) { }, expectedTag: "v1.0.1+g012345678", }, + { + name: "autotag scheme, [major] bump without prefix", + setup: testRepoSetup{ + scheme: "autotag", + nextCommit: "[major] this is a big release\n\nfoo bar baz\n", + initialTag: "1.0.0", + disablePrefix: true, + }, + expectedTag: "2.0.0", + }, // tests for conventional commits scheme. Based on: // https://www.conventionalcommits.org/en/v1.0.0/#summary // and