Skip to content

Commit

Permalink
(+semver: fix) Fix "tag less than..." error (#19)
Browse files Browse the repository at this point in the history
Only error if the final calculated version of a branch is less than master, and even then only if the user opts-in via a command line flag
  • Loading branch information
cuevaskoch authored and annymsMthd committed Apr 16, 2019
1 parent aacfd0a commit 34642d2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
10 changes: 8 additions & 2 deletions cmd/gogitver/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"strconv"

"github.com/annymsmthd/gogitver/pkg/git"
"github.com/pkg/errors"
Expand All @@ -21,6 +22,7 @@ var rootCmd = &cobra.Command{
func init() {
rootCmd.Flags().String("path", ".", "the path to the git repository")
rootCmd.Flags().String("settings", "./.gogitver.yaml", "the file that contains the settings")
rootCmd.Flags().Bool("forbid-behind-master", false, "error if the current branch's calculated version is behind the calculated version of refs/heads/master")
}

// Execute gogitver
Expand All @@ -34,9 +36,13 @@ func Execute() {
func runRoot(cmd *cobra.Command, args []string) {
f := cmd.Flag("path")
sf := cmd.Flag("settings")
fbm, err := strconv.ParseBool(cmd.Flag("forbid-behind-master").Value.String())
if err != nil {
fbm = false
}

var s *git.Settings
_, err := os.Stat(sf.Value.String())
_, err = os.Stat(sf.Value.String())
if sf.Changed || err == nil {
r, err := os.Open(sf.Value.String())
if err != nil {
Expand All @@ -56,7 +62,7 @@ func runRoot(cmd *cobra.Command, args []string) {
panic(err)
}

version, err := git.GetCurrentVersion(r, s, false)
version, err := git.GetCurrentVersion(r, s, false, fbm)
if err != nil {
panic(err)
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type gitVersion struct {
}

// GetCurrentVersion returns the current version
func GetCurrentVersion(r *git.Repository, settings *Settings, ignoreTravisTag bool) (version string, err error) {
func GetCurrentVersion(r *git.Repository, settings *Settings, ignoreTravisTag bool, forbidBehindMaster bool) (version string, err error) {
tag, ok := os.LookupEnv("TRAVIS_TAG")
if !ignoreTravisTag && ok && tag != "" { // If this is a tagged build in travis shortcircuit here
version, err := semver.NewVersion(tag)
Expand Down Expand Up @@ -70,15 +70,15 @@ func GetCurrentVersion(r *git.Repository, settings *Settings, ignoreTravisTag bo
return "", errors.Wrap(err, "GetCurrentVersion failed")
}

v, err := getVersion(r, h, tagMap, settings)
v, err := getVersion(r, h, tagMap, forbidBehindMaster, settings)
if err != nil {
return "", errors.Wrap(err, "GetCurrentVersion failed")
}

return v.String(), nil
}

func getVersion(r *git.Repository, h *plumbing.Reference, tagMap map[string]string, settings *Settings) (version *semver.Version, err error) {
func getVersion(r *git.Repository, h *plumbing.Reference, tagMap map[string]string, forbidBehindMaster bool, settings *Settings) (version *semver.Version, err error) {
currentBranch, err := getCurrentBranch(r, h)
if err != nil {
return nil, errors.Wrap(err, "getVersion failed")
Expand Down Expand Up @@ -125,9 +125,6 @@ func getVersion(r *git.Repository, h *plumbing.Reference, tagMap map[string]stri
}

if versionMap[index].IsSolid {
if versionMap[index].Name.LessThan(*masterVersion) {
return nil, errors.Errorf("Branch has tag '%s' whose version is less than master '%s'", versionMap[index].Name, masterVersion)
}
baseVersion = versionMap[index].Name
index--
} else {
Expand All @@ -154,6 +151,10 @@ func getVersion(r *git.Repository, h *plumbing.Reference, tagMap map[string]stri
prerelease := fmt.Sprintf("%s-%d-%s", currentBranch, len(versionMap)-1, shortHash)
baseVersion.PreRelease = semver.PreRelease(prerelease)

if forbidBehindMaster && baseVersion.LessThan(*masterVersion) {
return nil, errors.Errorf("Branch has calculated version '%s' whose version is less than master '%s'", baseVersion, masterVersion)
}

return baseVersion, nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestUseLightweightTagForVersionAnchor(t *testing.T) {
}

s := igit.GetDefaultSettings()
version, err := igit.GetCurrentVersion(r, s, true)
version, err := igit.GetCurrentVersion(r, s, true, false)
if err != nil {
t.Error(err)
t.FailNow()
Expand Down Expand Up @@ -118,7 +118,7 @@ func TestUseAnnotatedTagForVersionAnchor(t *testing.T) {
}

s := igit.GetDefaultSettings()
version, err := igit.GetCurrentVersion(r, s, true)
version, err := igit.GetCurrentVersion(r, s, true, false)
if err != nil {
t.Error(err)
t.FailNow()
Expand Down

0 comments on commit 34642d2

Please sign in to comment.