diff --git a/.gitmodules b/.gitmodules index 232ac40..8869ec9 100644 --- a/.gitmodules +++ b/.gitmodules @@ -5,3 +5,6 @@ path = tests/notags url = https://github.com/sstarcher/helm-release-notags.git branch = master +[submodule "tests/tags_bad"] + path = tests/tags_bad + url = https://github.com/sstarcher/helm-release-tags_bad.git diff --git a/cmd/root.go b/cmd/root.go index cd25ed4..df9129e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -13,12 +13,15 @@ import ( "github.com/sstarcher/helm-release/version" ) -var cfgFile string -var tag string -var tagPath string -var printComputedVersion bool -var bump string -var source string +var ( + cfgFile string + tag string + tagPath string + printComputedVersion bool + bump string + source string + strict bool +) // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ @@ -55,7 +58,11 @@ var rootCmd = &cobra.Command{ nextType := version.NewNextType(bump) version, err := getter.NextVersion(nextType) - if err != nil { + if version == nil { + return err + } + + if err != nil && strict { return err } @@ -108,6 +115,7 @@ func init() { rootCmd.Flags().BoolVar(&printComputedVersion, "print-computed-version", false, "Print the computed version string to stdout") rootCmd.Flags().StringVar(&bump, "bump", "", "Specifies to bump major, minor, or patch when using print-computed-version") rootCmd.Flags().StringVar(&source, "source", "git", "Specifies the source of the version information options (git, helm)") + rootCmd.Flags().BoolVar(&strict, "strict", false, "When enabled it will look through all tags for semver tags and fail if tags exist outside of master") } // initConfig reads in config file and ENV variables if set. diff --git a/git/git.go b/git/git.go index 4618b72..7dd66c3 100644 --- a/git/git.go +++ b/git/git.go @@ -230,10 +230,58 @@ func (g *Git) NextVersion(nextType *version.NextType) (*semver.Version, error) { return nil, err } + tags, err := g.tags() + if err != nil { + return nil, err + } + + var nextVersion *semver.Version if nextType == nil { // Determine from git history - return g.versionFromHistory(ver) + nextVersion, err = g.versionFromHistory(ver) + if err != nil { + return nil, err + } + } else { + nextVersion, err = version.NextVersion(ver, nextType) + if err != nil { + return nil, err + } + } + + wrongTags := false + for _, item := range tags { + if nextVersion.Compare(item) != 1 { + wrongTags = true + log.Warnf("next version is behind one of the semver tags %v", item) + } + } + + if wrongTags { + err = fmt.Errorf("tags in history are out of sync with next version %d.%d.%d", nextVersion.Major(), nextVersion.Minor(), nextVersion.Patch()) + } + + return nextVersion, err +} + +func (g *Git) tags() ([]*semver.Version, error) { + cmd := exec.Command("git", "tag") + cmd.Dir = g.directory + out, err := cmd.CombinedOutput() + if err != nil { + return nil, err } - return version.NextVersion(ver, nextType) + + s := strings.TrimSpace(string(out)) + tagString := strings.Split(s, "\n") + tags := []*semver.Version{} + for _, item := range tagString { + ver, err := semver.NewVersion(item) + if err == nil { + tags = append(tags, ver) + } + } + + return tags, nil } func validate(dir string) error { diff --git a/git/git_test.go b/git/git_test.go index 073ac58..abbeb8a 100644 --- a/git/git_test.go +++ b/git/git_test.go @@ -9,6 +9,7 @@ import ( var dir = "../tests/tags" var noTagsDir = "../tests/notags" +var badTags = "../tests/tags_bad" func TestValidGitRepo(t *testing.T) { assert := assert.New(t) @@ -103,3 +104,14 @@ func TestNoTags(t *testing.T) { assert.NotNil(err) assert.Empty(tag) } + +func TestBadTags(t *testing.T) { + assert := assert.New(t) + + git := Git{ + directory: badTags, + } + + _, err := git.NextVersion(nil) + assert.NotNil(err) +} diff --git a/helm/helm_test.go b/helm/helm_test.go index 4a2a580..295efb6 100644 --- a/helm/helm_test.go +++ b/helm/helm_test.go @@ -90,7 +90,7 @@ func TestVersions(t *testing.T) { assert.Nil(err) actual, err := git.NextVersion(nil) - assert.Nil(err) + assert.NotNil(actual) if actual != nil { assert.Equal(tt.expected, actual.String()) } else { diff --git a/tests/tags_bad b/tests/tags_bad new file mode 160000 index 0000000..6724498 --- /dev/null +++ b/tests/tags_bad @@ -0,0 +1 @@ +Subproject commit 67244983661c812244ced1bc934d8082e128c030