From a0fa4ee4cde406a274060481e99f695ab4a2d5a5 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Sat, 11 Dec 2021 10:43:12 +0000 Subject: [PATCH] Add option to respect existing tags and adjust calculations using them. --- README.md | 12 +++++++++ cmd/main.go | 61 ++++++++++++++++++++++++++++++++++++++++++--- cmd/main_test.go | 1 + cmd/root.go | 2 ++ config-release.yaml | 1 + config.yaml | 1 + 6 files changed, 74 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 62f8f1c..9d14ed2 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ Project created overnight, to prove that management of semantic versioning is NO - [Semantic version generator](#semantic-version-generator) - [How does it work](#how-does-it-work) + - [Additional features](#additional-features) + - [Important changes](#important-changes) - [Usage](#usage) - [Authentication](#authentication) - [As a binary](#as-a-binary) @@ -23,6 +25,15 @@ Project created overnight, to prove that management of semantic versioning is NO * Iterates through the list of commits looking for the keywords specified in config file for additional bumps of versions * Returns the semantic version which can be included in the release +### Additional features + +* With flag `-e` or config `force.existing: true` the existing tags in versioning will be respected, helping you to avoid the version conflicts. +* With config `force.commit: deadbeef` where `deadbeef` is the commit hash - calculations will start from the specified commit. + +### Important changes + +* From version `1.4.2+` as pointed out in [issue #12](https://github.com/lukaszraczylo/semver-generator/issues/12) commits from merge will not be included in the calculations and commits themselves will bump the version on first match ( starting checks from `patch` upwards ). + ### Usage #### Authentication @@ -63,6 +74,7 @@ Available Commands: Flags: -c, --config string Path to config file (default "semver.yaml") -d, --debug Enable debug mode + -e, --existing Respect existing tags -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") diff --git a/cmd/main.go b/cmd/main.go index 8a9f2b2..3e388ea 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -22,6 +22,7 @@ import ( "net/url" "os" "sort" + "strconv" "strings" "time" @@ -48,10 +49,11 @@ type Wording struct { } type Force struct { - Patch int - Minor int - Major int - Commit string + Patch int + Minor int + Major int + Commit string + Existing bool } type SemVer struct { @@ -67,6 +69,7 @@ type Setup struct { RepositoryLocalPath string RepositoryHandler *git.Repository Commits []CommitDetails + Tags []TagDetails Semver SemVer Wording Wording Force Force @@ -82,7 +85,16 @@ type CommitDetails struct { Timestamp time.Time } +type TagDetails struct { + Name string + Hash string +} + func checkMatches(content []string, targets []string) bool { + if fuzzy.MatchNormalizedFold(strings.Join(content, " "), "Merge branch") { + debugPrint(fmt.Sprintln("Merge detected, ignoring commits within:", content)) + return false + } var r []string for _, tgt := range targets { r = fuzzy.FindNormalizedFold(tgt, content) @@ -100,8 +112,30 @@ func debugPrint(content string) { } } +func parseExistingSemver(tagName string) (semanticVersion SemVer) { + var tagNameParts []string + tagNameParts = strings.Split(tagName, ".") + semanticVersion.Major, _ = strconv.Atoi(tagNameParts[0]) + semanticVersion.Minor, _ = strconv.Atoi(tagNameParts[1]) + semanticVersion.Patch, _ = strconv.Atoi(tagNameParts[2]) + tagReleaseParts := strings.Split(tagNameParts[2], "-rc.") + if len(tagReleaseParts) > 1 { + semanticVersion.Release, _ = strconv.Atoi(tagNameParts[3]) + } + return +} + func (s *Setup) CalculateSemver() SemVer { for _, commit := range s.Commits { + if params.varExisting || s.Force.Existing { + for _, tagHash := range s.Tags { + if commit.Hash == tagHash.Hash { + debugPrint(fmt.Sprintln("Found existing tag:", tagHash.Name, "related to", commit.Message)) + s.Semver = parseExistingSemver(tagHash.Name) + } + } + } + if !params.varStrict { s.Semver.Patch++ debugPrint(fmt.Sprintln("Incrementing patch (DEFAULT) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver())) @@ -144,6 +178,21 @@ func (s *Setup) CalculateSemver() SemVer { return s.Semver } +func (s *Setup) ListExistingTags() { + debugPrint(fmt.Sprintln("Listing existing tags")) + refs, err := s.RepositoryHandler.Tags() + if err != nil { + panic(err) + } + if err := refs.ForEach(func(ref *plumbing.Reference) error { + s.Tags = append(s.Tags, TagDetails{Name: ref.Name().Short(), Hash: ref.Hash().String()}) + debugPrint(fmt.Sprintln("Found tag:", ref.Name().Short(), ref.Hash().String())) + return nil + }); err != nil { + panic(err) + } +} + func (s *Setup) ListCommits() ([]CommitDetails, error) { var ref *plumbing.Reference var err error @@ -195,6 +244,7 @@ func (s *Setup) Prepare() error { Username: os.Getenv("GITHUB_USERNAME"), Password: os.Getenv("GITHUB_TOKEN"), }, + Tags: git.AllTags, }) if err != nil { fmt.Println("Unable to reach repository", s.RepositoryName, "Error:", err.Error()) @@ -276,6 +326,9 @@ func main() { os.Exit(1) } repo.ListCommits() + if params.varExisting { + repo.ListExistingTags() + } repo.ForcedVersioning() repo.CalculateSemver() fmt.Println("SEMVER", repo.getSemver()) diff --git a/cmd/main_test.go b/cmd/main_test.go index 7ec0903..70cbcef 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -487,6 +487,7 @@ func (suite *Tests) Test_main() { varUpdate bool varStrict bool varGenerateInTest bool + varExisting bool } tests := []struct { name string diff --git a/cmd/root.go b/cmd/root.go index 893d706..123b642 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -60,6 +60,7 @@ type myParams struct { varUpdate bool varStrict bool varGenerateInTest bool + varExisting bool } var params myParams @@ -74,4 +75,5 @@ func init() { rootCmd.PersistentFlags().BoolVarP(¶ms.varDebug, "debug", "d", false, "Enable debug mode") rootCmd.PersistentFlags().BoolVarP(¶ms.varUpdate, "update", "u", false, "Update binary with latest") rootCmd.PersistentFlags().BoolVarP(¶ms.varStrict, "strict", "s", false, "Strict matching") + rootCmd.PersistentFlags().BoolVarP(¶ms.varExisting, "existing", "e", false, "Respect existing tags") } diff --git a/config-release.yaml b/config-release.yaml index c168067..f0c4fbf 100644 --- a/config-release.yaml +++ b/config-release.yaml @@ -1,6 +1,7 @@ version: 1 force: major: 1 + existing: true wording: patch: - update diff --git a/config.yaml b/config.yaml index 5dfe9a5..c10bc6d 100644 --- a/config.yaml +++ b/config.yaml @@ -1,6 +1,7 @@ version: 1 force: major: 1 + existing: true wording: patch: - update