Skip to content

Commit

Permalink
(+semver: fix) Use hotfix as trimmed branch name not master (#25)
Browse files Browse the repository at this point in the history
* (+semver: fix) Use hotfix as trimmed branch name not master

Signed-off-by: Jeff Cuevas-Koch <[email protected]>
  • Loading branch information
cuevaskoch authored and annymsMthd committed Apr 30, 2019
1 parent 1197822 commit abc381d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 44 deletions.
27 changes: 19 additions & 8 deletions cmd/gogitver/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ var rootCmd = &cobra.Command{
}

var prereleaseCmd = &cobra.Command{
Use: "label",
Use: "label",
Short: "Gets the prerelease label, if any",
Long: ``,
Run: runPrerelease,
Long: ``,
Run: runPrerelease,
}

func init() {
Expand Down Expand Up @@ -83,24 +83,35 @@ func getBoolFromFlag(cmd *cobra.Command, flagName string) bool {
return result
}

func getBranchSettings(cmd *cobra.Command) *git.BranchSettings {
fbm := getBoolFromFlag(cmd, "forbid-behind-master")
trimPrefix := getBoolFromFlag(cmd, "trim-branch-prefix")
return &git.BranchSettings{
ForbidBehindMaster: fbm,
TrimBranchPrefix: trimPrefix,
}
}

func runRoot(cmd *cobra.Command, args []string) {
r, s := getRepoAndSettings(cmd)

fbm := getBoolFromFlag(cmd, "forbid-behind-master")
trimPrefix := getBoolFromFlag(cmd, "trim-branch-prefix")
version, err := git.GetCurrentVersion(r, s, false, fbm, trimPrefix)
branchSettings := getBranchSettings(cmd)
version, err := git.GetCurrentVersion(r, s, branchSettings)
if err != nil {
panic(err)
}

fmt.Println(version)
}

func runPrerelease(cmd *cobra.Command, args[]string) {
func runPrerelease(cmd *cobra.Command, args []string) {
r, s := getRepoAndSettings(cmd)
trimPrefix := getBoolFromFlag(cmd, "trim-branch-prefix")
branchSettings := &git.BranchSettings{
TrimBranchPrefix: trimPrefix,
}

label, err := git.GetPrereleaseLabel(r, s, trimPrefix)
label, err := git.GetPrereleaseLabel(r, s, branchSettings)
if err != nil {
panic(err)
}
Expand Down
71 changes: 40 additions & 31 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ import (
"gopkg.in/src-d/go-git.v4/plumbing/object"
)

// BranchSettings contains flags that determine how branches are handled when calculating versions.
type BranchSettings struct {
ForbidBehindMaster bool
TrimBranchPrefix bool
IgnoreEnvVars bool
}

type gitVersion struct {
IsSolid bool
Name *semver.Version
Expand All @@ -23,9 +30,9 @@ type gitVersion struct {
}

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

v, err := getVersion(r, h, tagMap, forbidBehindMaster, trimPrefix, settings)
v, err := getVersion(r, h, tagMap, branchSettings, settings)
if err != nil {
return "", errors.Wrap(err, "GetCurrentVersion failed")
}
Expand All @@ -80,16 +87,16 @@ func GetCurrentVersion(r *git.Repository, settings *Settings, ignoreTravisTag bo
}

// GetPrereleaseLabel returns the prerelease label for the current branch
func GetPrereleaseLabel(r *git.Repository, settings *Settings, trimPrefix bool) (result string, err error) {
func GetPrereleaseLabel(r *git.Repository, settings *Settings, branchSettings *BranchSettings) (result string, err error) {
h, err := r.Head()
if err != nil {
return "", errors.Wrap(err, "GetCurrentVersion failed")
}
return getCurrentBranch(r, h, trimPrefix)
return getCurrentBranch(r, h, branchSettings)
}

func getVersion(r *git.Repository, h *plumbing.Reference, tagMap map[string]string, forbidBehindMaster bool, trimPrefix bool, settings *Settings) (version *semver.Version, err error) {
currentBranch, err := getCurrentBranch(r, h, trimPrefix)
func getVersion(r *git.Repository, h *plumbing.Reference, tagMap map[string]string, branchSettings *BranchSettings, settings *Settings) (version *semver.Version, err error) {
currentBranch, err := getCurrentBranch(r, h, branchSettings)
if err != nil {
return nil, errors.Wrap(err, "getVersion failed")
}
Expand Down Expand Up @@ -161,41 +168,43 @@ 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) {
if branchSettings.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
}

func getCurrentBranch(r *git.Repository, h *plumbing.Reference, trimPrefix bool) (name string, err error) {
func getCurrentBranch(r *git.Repository, h *plumbing.Reference, branchSettings *BranchSettings) (name string, err error) {
branchName := ""

name, ok := os.LookupEnv("TRAVIS_PULL_REQUEST_BRANCH") // Travis
if ok {
branchName, err := cleanseBranchName(name, trimPrefix)
if err != nil {
return "", err
if !branchSettings.IgnoreEnvVars {
name, ok := os.LookupEnv("TRAVIS_PULL_REQUEST_BRANCH") // Travis
if ok {
branchName, err := cleanseBranchName(name, branchSettings.TrimBranchPrefix)
if err != nil {
return "", err
}
return branchName, nil
}
return branchName, nil
}

name, ok = os.LookupEnv("TRAVIS_BRANCH")
if ok {
branchName, err := cleanseBranchName(name, trimPrefix)
if err != nil {
return "", err
name, ok = os.LookupEnv("TRAVIS_BRANCH")
if ok {
branchName, err := cleanseBranchName(name, branchSettings.TrimBranchPrefix)
if err != nil {
return "", err
}
return branchName, nil
}
return branchName, nil
}

name, ok = os.LookupEnv("CI_COMMIT_REF_NAME") // GitLab
if ok {
branchName, err := cleanseBranchName(name, trimPrefix)
if err != nil {
return "", err
name, ok = os.LookupEnv("CI_COMMIT_REF_NAME") // GitLab
if ok {
branchName, err := cleanseBranchName(name, branchSettings.TrimBranchPrefix)
if err != nil {
return "", err
}
return branchName, nil
}
return branchName, nil
}

refs, err := r.References()
Expand All @@ -216,7 +225,7 @@ func getCurrentBranch(r *git.Repository, h *plumbing.Reference, trimPrefix bool)
return "", fmt.Errorf("Cannot determine branch")
}

branch, err := cleanseBranchName(branchName, trimPrefix)
branch, err := cleanseBranchName(branchName, branchSettings.TrimBranchPrefix)
if err != nil {
return "", err
}
Expand All @@ -234,7 +243,7 @@ func cleanseBranchName(name string, trimPrefix bool) (string, error) {
return branchName, nil
}

reg, err = regexp.Compile("^(feature|master)-")
reg, err = regexp.Compile("^(feature|hotfix)-")
if err != nil {
return "", err
}
Expand Down
19 changes: 14 additions & 5 deletions pkg/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ func TestUseLightweightTagForVersionAnchor(t *testing.T) {
}

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

s := igit.GetDefaultSettings()
version, err := igit.GetCurrentVersion(r, s, true, false, false)
version, err := igit.GetCurrentVersion(r, s, &igit.BranchSettings{
IgnoreEnvVars: true,
})
if err != nil {
t.Error(err)
t.FailNow()
Expand All @@ -131,7 +135,10 @@ func TestUseAnnotatedTagForVersionAnchor(t *testing.T) {
func TestTrimBranchPrefix(t *testing.T) {
r := getSingleBranchCommit("feature/should-be-trimmed", t)
s := igit.GetDefaultSettings()
label, err := igit.GetPrereleaseLabel(r, s, true)
label, err := igit.GetPrereleaseLabel(r, s, &igit.BranchSettings{
IgnoreEnvVars: true,
TrimBranchPrefix: true,
})
if err != nil {
t.Error(err)
t.FailNow()
Expand All @@ -143,7 +150,9 @@ func TestTrimBranchPrefix(t *testing.T) {
func TestCleanseBranchName(t *testing.T) {
r := getSingleBranchCommit("author's-branch", t)
s := igit.GetDefaultSettings()
label, err := igit.GetPrereleaseLabel(r, s, true)
label, err := igit.GetPrereleaseLabel(r, s, &igit.BranchSettings{
IgnoreEnvVars: true,
})
if err != nil {
t.Error(err)
t.FailNow()
Expand Down Expand Up @@ -184,7 +193,7 @@ func getSingleBranchCommit(branchName string, t *testing.T) *git.Repository {
b := plumbing.ReferenceName(ref)
w.Checkout(&git.CheckoutOptions{
Create: true,
Force: false,
Force: false,
Branch: b,
})

Expand Down

0 comments on commit abc381d

Please sign in to comment.