mirror of
https://github.com/lukaszraczylo/semver-generator.git
synced 2026-07-08 04:14:32 +00:00
Add strict matching which disables automatic incrementation of the patch until the trigger word is found.
This commit is contained in:
@@ -11,7 +11,8 @@ Project created overnight, to prove that management of semantic versioning is NO
|
|||||||
- [As a binary](#as-a-binary)
|
- [As a binary](#as-a-binary)
|
||||||
- [As a github action](#as-a-github-action)
|
- [As a github action](#as-a-github-action)
|
||||||
- [As a docker container](#as-a-docker-container)
|
- [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)
|
- [Example configuration](#example-configuration)
|
||||||
- [Good to know](#good-to-know)
|
- [Good to know](#good-to-know)
|
||||||
|
|
||||||
@@ -64,6 +65,7 @@ Flags:
|
|||||||
-h, --help help for semver-gen
|
-h, --help help for semver-gen
|
||||||
-l, --local Use local repository
|
-l, --local Use local repository
|
||||||
-r, --repository string Remote repository URL. (default "https://github.com/lukaszraczylo/simple-gql-client")
|
-r, --repository string Remote repository URL. (default "https://github.com/lukaszraczylo/simple-gql-client")
|
||||||
|
-s, --strict Strict matching
|
||||||
-u, --update Update binary with latest
|
-u, --update Update binary with latest
|
||||||
-v, --version Display version
|
-v, --version Display version
|
||||||
```
|
```
|
||||||
@@ -110,7 +112,7 @@ docker pull ghcr.io/lukaszraczylo/semver-generator:latest
|
|||||||
**Docker supported architectures:**
|
**Docker supported architectures:**
|
||||||
Linux/arm64, Linux/amd64
|
Linux/arm64, Linux/amd64
|
||||||
|
|
||||||
#### Calculations example
|
#### Calculations example [standard]
|
||||||
|
|
||||||
* 0.0.1 - PATCH - starting commit
|
* 0.0.1 - PATCH - starting commit
|
||||||
* 0.0.2 - PATCH - another 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.1 - MAJOR - commit with word 'BREAKING' = > INCREMENT MAJOR, reset MINOR
|
||||||
* 1.0.2 - PATCH - another commit
|
* 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
|
#### Example configuration
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
|
|||||||
+20
-27
@@ -89,11 +89,17 @@ func checkMatches(content []string, targets []string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func debugPrint(content string) {
|
||||||
|
if varDebug {
|
||||||
|
fmt.Println("DEBUG:", content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Setup) CalculateSemver() SemVer {
|
func (s *Setup) CalculateSemver() SemVer {
|
||||||
for _, commit := range s.Commits {
|
for _, commit := range s.Commits {
|
||||||
s.Semver.Patch++
|
if !varStrict {
|
||||||
if varDebug {
|
s.Semver.Patch++
|
||||||
fmt.Println("DEBUG: Incrementing patch (DEFAULT) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver())
|
debugPrint(fmt.Sprintln("Incrementing patch (DEFAULT) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver()))
|
||||||
}
|
}
|
||||||
commitSlice := strings.Fields(commit.Message)
|
commitSlice := strings.Fields(commit.Message)
|
||||||
matchPatch := checkMatches(commitSlice, s.Wording.Patch)
|
matchPatch := checkMatches(commitSlice, s.Wording.Patch)
|
||||||
@@ -101,24 +107,18 @@ func (s *Setup) CalculateSemver() SemVer {
|
|||||||
matchMajor := checkMatches(commitSlice, s.Wording.Major)
|
matchMajor := checkMatches(commitSlice, s.Wording.Major)
|
||||||
if matchPatch {
|
if matchPatch {
|
||||||
s.Semver.Patch++
|
s.Semver.Patch++
|
||||||
if varDebug {
|
debugPrint(fmt.Sprintln("Incrementing patch (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver()))
|
||||||
fmt.Println("DEBUG: Incrementing patch (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if matchMinor {
|
if matchMinor {
|
||||||
s.Semver.Minor++
|
s.Semver.Minor++
|
||||||
s.Semver.Patch = 1
|
s.Semver.Patch = 1
|
||||||
if varDebug {
|
debugPrint(fmt.Sprintln("Incrementing minor (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver()))
|
||||||
fmt.Println("DEBUG: Incrementing minor (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if matchMajor {
|
if matchMajor {
|
||||||
s.Semver.Major++
|
s.Semver.Major++
|
||||||
s.Semver.Minor = 0
|
s.Semver.Minor = 0
|
||||||
s.Semver.Patch = 1
|
s.Semver.Patch = 1
|
||||||
if varDebug {
|
debugPrint(fmt.Sprintln("Incrementing major (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver()))
|
||||||
fmt.Println("DEBUG: Incrementing major (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return s.Semver
|
return s.Semver
|
||||||
@@ -144,9 +144,7 @@ func (s *Setup) ListCommits() ([]CommitDetails, error) {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
if varDebug {
|
debugPrint(fmt.Sprintln("\n---COMMITS BEFORE CUT---\n", s.Commits))
|
||||||
fmt.Println("\n---COMMITS BEFORE CUT---\n", tmpResults)
|
|
||||||
}
|
|
||||||
|
|
||||||
for commitId, cmt := range tmpResults {
|
for commitId, cmt := range tmpResults {
|
||||||
if s.Force.Commit != "" && cmt.Hash == s.Force.Commit {
|
if s.Force.Commit != "" && cmt.Hash == s.Force.Commit {
|
||||||
@@ -156,9 +154,7 @@ func (s *Setup) ListCommits() ([]CommitDetails, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if varDebug {
|
debugPrint(fmt.Sprintln("\n---COMMITS AFTER CUT---\n", s.Commits))
|
||||||
fmt.Println("\n---COMMITS AFTER CUT---\n", s.Commits)
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.Commits, err
|
return s.Commits, err
|
||||||
}
|
}
|
||||||
@@ -197,21 +193,15 @@ func (s *Setup) Prepare() error {
|
|||||||
|
|
||||||
func (s *Setup) ForcedVersioning() {
|
func (s *Setup) ForcedVersioning() {
|
||||||
if !zero.IsZero(s.Force.Major) {
|
if !zero.IsZero(s.Force.Major) {
|
||||||
if varDebug {
|
debugPrint(fmt.Sprintln("Forced versioning (MAJOR)", s.Force.Major))
|
||||||
fmt.Println("DEBUG: Forced versioning (MAJOR)", s.Force.Major)
|
|
||||||
}
|
|
||||||
s.Semver.Major = s.Force.Major
|
s.Semver.Major = s.Force.Major
|
||||||
}
|
}
|
||||||
if !zero.IsZero(s.Force.Minor) {
|
if !zero.IsZero(s.Force.Minor) {
|
||||||
if varDebug {
|
debugPrint(fmt.Sprintln("Forced versioning (MINOR)", s.Force.Minor))
|
||||||
fmt.Println("DEBUG: Forced versioning (MINOR)", s.Force.Minor)
|
|
||||||
}
|
|
||||||
s.Semver.Minor = s.Force.Minor
|
s.Semver.Minor = s.Force.Minor
|
||||||
}
|
}
|
||||||
if !zero.IsZero(s.Force.Patch) {
|
if !zero.IsZero(s.Force.Patch) {
|
||||||
if varDebug {
|
debugPrint(fmt.Sprintln("Forced versioning (PATCH)", s.Force.Patch))
|
||||||
fmt.Println("DEBUG: Forced versioning (PATCH)", s.Force.Patch)
|
|
||||||
}
|
|
||||||
s.Semver.Patch = s.Force.Patch
|
s.Semver.Patch = s.Force.Patch
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -240,6 +230,9 @@ func main() {
|
|||||||
outdatedMsg = fmt.Sprintf("(Latest available: %s)", latestRelease)
|
outdatedMsg = fmt.Sprintf("(Latest available: %s)", latestRelease)
|
||||||
}
|
}
|
||||||
fmt.Println("semver-gen", PKG_VERSION, "", outdatedMsg, "\tMore information: https://github.com/lukaszraczylo/semver-generator")
|
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
|
return
|
||||||
}
|
}
|
||||||
if varUpdate {
|
if varUpdate {
|
||||||
|
|||||||
+43
-3
@@ -332,9 +332,10 @@ func (suite *Tests) TestSetup_CalculateSemver() {
|
|||||||
Patch int
|
Patch int
|
||||||
}
|
}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
fields fields
|
fields fields
|
||||||
wantSemver wantSemver
|
wantSemver wantSemver
|
||||||
|
strictMatching bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Test on existing repository",
|
name: "Test on existing repository",
|
||||||
@@ -351,6 +352,22 @@ func (suite *Tests) TestSetup_CalculateSemver() {
|
|||||||
Patch: 7,
|
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",
|
name: "Test on existing repository, starting with certain hash",
|
||||||
fields: fields{
|
fields: fields{
|
||||||
@@ -387,6 +404,7 @@ func (suite *Tests) TestSetup_CalculateSemver() {
|
|||||||
s.ForcedVersioning()
|
s.ForcedVersioning()
|
||||||
s.Force = tt.fields.Force
|
s.Force = tt.fields.Force
|
||||||
s.ListCommits()
|
s.ListCommits()
|
||||||
|
varStrict = tt.strictMatching
|
||||||
semver := s.CalculateSemver()
|
semver := s.CalculateSemver()
|
||||||
assert.Equal(tt.wantSemver.Major, semver.Major, "Unexpected MAJOR semver result in "+tt.name)
|
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)
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -60,6 +60,7 @@ var (
|
|||||||
varShowVersion bool
|
varShowVersion bool
|
||||||
varDebug bool
|
varDebug bool
|
||||||
varUpdate bool
|
varUpdate bool
|
||||||
|
varStrict bool
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -72,5 +73,6 @@ func init() {
|
|||||||
rootCmd.PersistentFlags().BoolVarP(&varShowVersion, "version", "v", false, "Display version")
|
rootCmd.PersistentFlags().BoolVarP(&varShowVersion, "version", "v", false, "Display version")
|
||||||
rootCmd.PersistentFlags().BoolVarP(&varDebug, "debug", "d", false, "Enable debug mode")
|
rootCmd.PersistentFlags().BoolVarP(&varDebug, "debug", "d", false, "Enable debug mode")
|
||||||
rootCmd.PersistentFlags().BoolVarP(&varUpdate, "update", "u", false, "Update binary with latest")
|
rootCmd.PersistentFlags().BoolVarP(&varUpdate, "update", "u", false, "Update binary with latest")
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(&varStrict, "strict", "s", false, "Strict matching")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user