Skip to content

Commit

Permalink
(+semver: fix) Fix broken version calculation and improve test covera…
Browse files Browse the repository at this point in the history
…ge (#29)

* (+semver: fix) Fix egregious bug that counts the same commit several times

* (+semver: feature) Support 2+ parents on merge commits

* Update go.mod/go.sum

* (+semver: fix) Consolitated tag string parsing

* (+semver: patch) Rewrote tests

* (+semver: fix) Ran formatter
  • Loading branch information
thzinc authored May 12, 2020
1 parent 73b3076 commit e423ce1
Show file tree
Hide file tree
Showing 5 changed files with 302 additions and 150 deletions.
16 changes: 2 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,11 @@ go 1.13

require (
github.com/coreos/go-semver v0.2.0
github.com/emirpasic/gods v1.9.0
github.com/inconshreveable/mousetrap v1.0.0
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99
github.com/kevinburke/ssh_config v0.0.0-20180830205328-81db2a75821e
github.com/mitchellh/go-homedir v1.0.0
github.com/pelletier/go-buffruneio v0.2.0
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/pkg/errors v0.8.0
github.com/sergi/go-diff v1.0.0
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.1
github.com/src-d/gcfg v1.3.0
github.com/spf13/pflag v1.0.1 // indirect
github.com/stretchr/testify v1.2.2
github.com/xanzy/ssh-agent v0.2.0
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd
golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9
golang.org/x/text v0.3.0
gopkg.in/src-d/go-billy.v4 v4.2.1
gopkg.in/src-d/go-git.v4 v4.7.1
gopkg.in/yaml.v2 v2.2.1
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs=
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/coreos/go-semver v0.2.0 h1:3Jm3tLmsgAYcjC+4Up7hJrFBPr+n7rAqYeSw/SZazuY=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emirpasic/gods v1.9.0 h1:rUF4PuzEjMChMiNsVjdI+SyLu7rEqpQ5reNFnhC7oFo=
github.com/emirpasic/gods v1.9.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/gliderlabs/ssh v0.1.1 h1:j3L6gSLQalDETeEg/Jg0mGY0/y/N6zI2xX1978P0Uqw=
github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand Down Expand Up @@ -54,6 +59,7 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/src-d/go-billy.v4 v4.2.1 h1:omN5CrMrMcQ+4I8bJ0wEhOBPanIRWzFC953IiXKdYzo=
gopkg.in/src-d/go-billy.v4 v4.2.1/go.mod h1:tm33zBoOwxjYHZIE+OV8bxTWFMJLrconzFMd38aARFk=
gopkg.in/src-d/go-git-fixtures.v3 v3.1.1 h1:XWW/s5W18RaJpmo1l0IYGqXKuJITWRFuA45iOf1dKJs=
gopkg.in/src-d/go-git-fixtures.v3 v3.1.1/go.mod h1:dLBcvytrw/TYZsNTWCnkNF2DSIlzWYqTe3rJR56Ac7g=
gopkg.in/src-d/go-git.v4 v4.7.1 h1:phAV/kNULxfYEvyInGdPuq3U2MtPpJdgmtOUF3cghkQ=
gopkg.in/src-d/go-git.v4 v4.7.1/go.mod h1:xrJH/YX8uSWewT6evfocf8qsivF18JgCN7/IMitOptY=
Expand Down
38 changes: 25 additions & 13 deletions pkg/git/branchWalker.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (b *branchWalker) GetVersion() (*semver.Version, error) {
}

for ; index >= 0; index-- {
v := versionMap[index]
switch {
case v.MajorBump:
baseVersion.BumpMajor()
Expand Down Expand Up @@ -121,8 +122,7 @@ func (b *branchWalker) walkVersion(ref *object.Commit, version *versionHolder, t

tag, ok := b.tagMap[ref.Hash.String()]
if ok {
ft := strings.TrimPrefix(tag, "v")
tagVersion, err := semver.NewVersion(ft)
tagVersion, err := parseTag(tag)
if err != nil {
return err
}
Expand Down Expand Up @@ -189,27 +189,29 @@ func (b *branchWalker) checkWalkParent(ref *object.Commit, version *versionHolde
}

func (b *branchWalker) reconcileCommit(hash string, version *gitVersion) error {
versionMap := versionHolder{
versionMap: []*gitVersion{},
}

commit, err := b.repository.CommitObject(plumbing.NewHash(hash))
if err != nil {
return errors.Wrap(err, "failed to get commit in reconcile")
}

if commit.NumParents() <= 1 {
numParents := commit.NumParents()
if numParents <= 1 {
return nil
}

parentToWalk, err := commit.Parent(1)
if err != nil {
return errors.Wrap(err, "failed to get parent in reconcile")
versionMap := versionHolder{
versionMap: []*gitVersion{},
}
for i := 1; i < numParents; i++ {
parentToWalk, err := commit.Parent(i)
if err != nil {
return errors.Wrap(err, "failed to get parent in reconcile")
}

err = b.walkVersion(parentToWalk, &versionMap, true)
if err != nil {
return err
err = b.walkVersion(parentToWalk, &versionMap, true)
if err != nil {
return err
}
}

var hasMajor, hasMinor bool
Expand All @@ -232,3 +234,13 @@ func (b *branchWalker) reconcileCommit(hash string, version *gitVersion) error {

return nil
}

func parseTag(tag string) (*semver.Version, error) { // TODO: Support ignoring invalid semver tags
trimmedTag := strings.TrimPrefix(tag, "v")
version, err := semver.NewVersion(trimmedTag)
if err != nil {
return nil, err
}

return version, nil
}
4 changes: 2 additions & 2 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type gitVersion struct {
func GetCurrentVersion(r *git.Repository, settings *Settings, branchSettings *BranchSettings, verbose bool) (version string, err error) {
tag, ok := os.LookupEnv("TRAVIS_TAG")
if !branchSettings.IgnoreEnvVars && ok && tag != "" { // If this is a tagged build in travis shortcircuit here
version, err := semver.NewVersion(tag)
version, err := parseTag(tag)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -117,7 +117,7 @@ func getVersion(r *git.Repository, h *plumbing.Reference, tagMap map[string]stri

masterHead, err := r.Reference("refs/heads/master", false)
if err != nil {
masterHead, err = r.Reference("refs/remotes/origin/master", false)
masterHead, err = r.Reference("refs/remotes/origin/master", false) // TODO: This needs test coverage
if err != nil {
return nil, errors.Wrap(err, "failed to get master branch at 'refs/heads/master, 'refs/remotes/origin/master'")
}
Expand Down
Loading

0 comments on commit e423ce1

Please sign in to comment.