From 9c0e4171ae73b141344c07a2baa9c47263c4d2d3 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Sat, 15 May 2021 18:54:48 +0100 Subject: [PATCH] Add strict matching which disables automatic incrementation of the patch until the trigger word is found. --- README.md | 16 ++++++++++++++-- cmd/main.go | 47 ++++++++++++++++++++--------------------------- cmd/main_test.go | 46 +++++++++++++++++++++++++++++++++++++++++++--- cmd/root.go | 2 ++ 4 files changed, 79 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 2d92d16..b997f6b 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,8 @@ Project created overnight, to prove that management of semantic versioning is NO - [As a binary](#as-a-binary) - [As a github action](#as-a-github-action) - [As a docker container](#as-a-docker-container) - - [Calculations example](#calculations-example) + - [Calculations example [standard]](#calculations-example-standard) + - [Calculations example [strict matching]](#calculations-example-strict-matching) - [Example configuration](#example-configuration) - [Good to know](#good-to-know) @@ -64,6 +65,7 @@ Flags: -h, --help help for semver-gen -l, --local Use local repository -r, --repository string Remote repository URL. (default "https://github.com/lukaszraczylo/simple-gql-client") + -s, --strict Strict matching -u, --update Update binary with latest -v, --version Display version ``` @@ -110,7 +112,7 @@ docker pull ghcr.io/lukaszraczylo/semver-generator:latest **Docker supported architectures:** Linux/arm64, Linux/amd64 -#### Calculations example +#### Calculations example [standard] * 0.0.1 - PATCH - starting commit * 0.0.2 - PATCH - another commit @@ -120,6 +122,16 @@ Linux/arm64, Linux/amd64 * 1.0.1 - MAJOR - commit with word 'BREAKING' = > INCREMENT MAJOR, reset MINOR * 1.0.2 - PATCH - another commit +#### Calculations example [strict matching] + +* 0.0.1 - PATCH - starting commit +* 0.0.1 - PATCH - another commit +* 0.0.1 - PATCH - another commit with word 'Update' => DOUBLE increment PATCH +* 0.1.0 - MINOR - after commit with word 'Change' => increment MINOR, reset PATCH +* 0.1.0 - PATCH - additional commit +* 1.0.0 - MAJOR - commit with word 'BREAKING' = > INCREMENT MAJOR, reset MINOR +* 1.0.0 - PATCH - another commit + #### Example configuration ```yaml diff --git a/cmd/main.go b/cmd/main.go index bc00e01..45b2505 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -89,11 +89,17 @@ func checkMatches(content []string, targets []string) bool { return false } +func debugPrint(content string) { + if varDebug { + fmt.Println("DEBUG:", content) + } +} + func (s *Setup) CalculateSemver() SemVer { for _, commit := range s.Commits { - s.Semver.Patch++ - if varDebug { - fmt.Println("DEBUG: Incrementing patch (DEFAULT) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver()) + if !varStrict { + s.Semver.Patch++ + debugPrint(fmt.Sprintln("Incrementing patch (DEFAULT) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver())) } commitSlice := strings.Fields(commit.Message) matchPatch := checkMatches(commitSlice, s.Wording.Patch) @@ -101,24 +107,18 @@ func (s *Setup) CalculateSemver() SemVer { matchMajor := checkMatches(commitSlice, s.Wording.Major) if matchPatch { s.Semver.Patch++ - if varDebug { - fmt.Println("DEBUG: Incrementing patch (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver()) - } + debugPrint(fmt.Sprintln("Incrementing patch (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver())) } if matchMinor { s.Semver.Minor++ s.Semver.Patch = 1 - if varDebug { - fmt.Println("DEBUG: Incrementing minor (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver()) - } + debugPrint(fmt.Sprintln("Incrementing minor (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver())) } if matchMajor { s.Semver.Major++ s.Semver.Minor = 0 s.Semver.Patch = 1 - if varDebug { - fmt.Println("DEBUG: Incrementing major (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver()) - } + debugPrint(fmt.Sprintln("Incrementing major (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver())) } } return s.Semver @@ -144,9 +144,7 @@ func (s *Setup) ListCommits() ([]CommitDetails, error) { return nil }) - if varDebug { - fmt.Println("\n---COMMITS BEFORE CUT---\n", tmpResults) - } + debugPrint(fmt.Sprintln("\n---COMMITS BEFORE CUT---\n", s.Commits)) for commitId, cmt := range tmpResults { if s.Force.Commit != "" && cmt.Hash == s.Force.Commit { @@ -156,9 +154,7 @@ func (s *Setup) ListCommits() ([]CommitDetails, error) { } } - if varDebug { - fmt.Println("\n---COMMITS AFTER CUT---\n", s.Commits) - } + debugPrint(fmt.Sprintln("\n---COMMITS AFTER CUT---\n", s.Commits)) return s.Commits, err } @@ -197,21 +193,15 @@ func (s *Setup) Prepare() error { func (s *Setup) ForcedVersioning() { if !zero.IsZero(s.Force.Major) { - if varDebug { - fmt.Println("DEBUG: Forced versioning (MAJOR)", s.Force.Major) - } + debugPrint(fmt.Sprintln("Forced versioning (MAJOR)", s.Force.Major)) s.Semver.Major = s.Force.Major } if !zero.IsZero(s.Force.Minor) { - if varDebug { - fmt.Println("DEBUG: Forced versioning (MINOR)", s.Force.Minor) - } + debugPrint(fmt.Sprintln("Forced versioning (MINOR)", s.Force.Minor)) s.Semver.Minor = s.Force.Minor } if !zero.IsZero(s.Force.Patch) { - if varDebug { - fmt.Println("DEBUG: Forced versioning (PATCH)", s.Force.Patch) - } + debugPrint(fmt.Sprintln("Forced versioning (PATCH)", s.Force.Patch)) s.Semver.Patch = s.Force.Patch } } @@ -240,6 +230,9 @@ func main() { outdatedMsg = fmt.Sprintf("(Latest available: %s)", latestRelease) } fmt.Println("semver-gen", PKG_VERSION, "", outdatedMsg, "\tMore information: https://github.com/lukaszraczylo/semver-generator") + if outdatedMsg != "" { + fmt.Println("You can update automatically with: semver-gen -u") + } return } if varUpdate { diff --git a/cmd/main_test.go b/cmd/main_test.go index 55c22f9..357f88b 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -332,9 +332,10 @@ func (suite *Tests) TestSetup_CalculateSemver() { Patch int } tests := []struct { - name string - fields fields - wantSemver wantSemver + name string + fields fields + wantSemver wantSemver + strictMatching bool }{ { name: "Test on existing repository", @@ -351,6 +352,22 @@ func (suite *Tests) TestSetup_CalculateSemver() { Patch: 7, }, }, + { + name: "Test on existing repository with strict matching", + fields: fields{ + RepositoryName: "https://github.com/lukaszraczylo/semver-generator-test-repo", + LocalConfigFile: "meta.yaml", + Force: Force{ + Commit: "", + }, + }, + strictMatching: true, + wantSemver: wantSemver{ + Major: 2, + Minor: 4, + Patch: 1, + }, + }, { name: "Test on existing repository, starting with certain hash", fields: fields{ @@ -387,6 +404,7 @@ func (suite *Tests) TestSetup_CalculateSemver() { s.ForcedVersioning() s.Force = tt.fields.Force s.ListCommits() + varStrict = tt.strictMatching semver := s.CalculateSemver() assert.Equal(tt.wantSemver.Major, semver.Major, "Unexpected MAJOR semver result in "+tt.name) assert.Equal(tt.wantSemver.Minor, semver.Minor, "Unexpected MINOR semver result in "+tt.name) @@ -394,3 +412,25 @@ func (suite *Tests) TestSetup_CalculateSemver() { }) } } + +func (suite *Tests) Test_debugPrint() { + type args struct { + content string + } + tests := []struct { + name string + args args + }{ + { + name: "Test debug print", + args: args{ + content: "Test debug", + }, + }, + } + for _, tt := range tests { + suite.T().Run(tt.name, func(t *testing.T) { + debugPrint(tt.args.content) + }) + } +} diff --git a/cmd/root.go b/cmd/root.go index 8ee192e..66d1fe8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -60,6 +60,7 @@ var ( varShowVersion bool varDebug bool varUpdate bool + varStrict bool ) func init() { @@ -72,5 +73,6 @@ func init() { rootCmd.PersistentFlags().BoolVarP(&varShowVersion, "version", "v", false, "Display version") rootCmd.PersistentFlags().BoolVarP(&varDebug, "debug", "d", false, "Enable debug mode") rootCmd.PersistentFlags().BoolVarP(&varUpdate, "update", "u", false, "Update binary with latest") + rootCmd.PersistentFlags().BoolVarP(&varStrict, "strict", "s", false, "Strict matching") } }