Skip to content

Commit

Permalink
(+semver: feature) Added verbose logging for determining which commit…
Browse files Browse the repository at this point in the history
… produced which version (#26)
  • Loading branch information
thzinc authored Apr 29, 2020
1 parent cc32234 commit 95eb940
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 21 deletions.
9 changes: 8 additions & 1 deletion cmd/gogitver/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"log"
"os"
"strconv"

Expand Down Expand Up @@ -32,6 +33,7 @@ func init() {
cmd.Flags().String("path", ".", "the path to the git repository")
cmd.Flags().String("settings", "./.gogitver.yaml", "the file that contains the settings")
cmd.Flags().Bool("trim-branch-prefix", false, "Trim branch prefixes feature/ and hotfix/ from prerelease label")
cmd.Flags().BoolP("verbose", "v", false, "Show information about how the version was calculated")
}

rootCmd.Flags().Bool("forbid-behind-master", false, "error if the current branch's calculated version is behind the calculated version of refs/heads/master")
Expand Down Expand Up @@ -94,9 +96,14 @@ func getBranchSettings(cmd *cobra.Command) *git.BranchSettings {

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

if v {
log.SetFlags(0)
}

branchSettings := getBranchSettings(cmd)
version, err := git.GetCurrentVersion(r, s, branchSettings)
version, err := git.GetCurrentVersion(r, s, branchSettings, v)
if err != nil {
panic(err)
}
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/kevinburke/ssh_config v0.0.0-20180830205328-81db2a75821e h1:RgQk53JHp/Cjunrr1WlsXSZpqXn+uREuHvUVcK82CV8=
github.com/kevinburke/ssh_config v0.0.0-20180830205328-81db2a75821e/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mitchellh/go-homedir v1.0.0 h1:vKb8ShqSby24Yrqr/yDYkuFz8d0WUjys40rvnGC8aR0=
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
Expand Down Expand Up @@ -48,6 +50,7 @@ golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
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=
Expand Down
31 changes: 20 additions & 11 deletions pkg/git/branchWalker.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package git

import (
"log"
"regexp"
"strings"

Expand All @@ -19,6 +20,7 @@ type branchWalker struct {
settings *Settings
isMaster bool
endHash string
verbose bool

visited map[string]bool
commitsToReconcile map[string]*gitVersion
Expand All @@ -28,7 +30,7 @@ type versionHolder struct {
versionMap []*gitVersion
}

func newBranchWalker(repository *git.Repository, head *object.Commit, tagMap map[string]string, settings *Settings, isMaster bool, endHash string) *branchWalker {
func newBranchWalker(repository *git.Repository, head *object.Commit, tagMap map[string]string, settings *Settings, isMaster bool, endHash string, verbose bool) *branchWalker {
return &branchWalker{
repository: repository,
head: head,
Expand All @@ -38,6 +40,7 @@ func newBranchWalker(repository *git.Repository, head *object.Commit, tagMap map
endHash: endHash,
visited: make(map[string]bool),
commitsToReconcile: make(map[string]*gitVersion),
verbose: verbose,
}
}

Expand All @@ -49,8 +52,9 @@ func (b *branchWalker) GetVersion() (*semver.Version, error) {

var baseVersion *semver.Version
index := len(versionMap) - 1
if versionMap[index].IsSolid {
baseVersion = versionMap[index].Name
v := versionMap[index]
if v.IsSolid {
baseVersion = v.Name
index--
} else {
baseVersion, err = semver.NewVersion("0.0.0")
Expand All @@ -63,8 +67,11 @@ func (b *branchWalker) GetVersion() (*semver.Version, error) {
return baseVersion, nil
}

if b.verbose {
log.Printf("[%s] %s", v.Commit, baseVersion.String())
}

for ; index >= 0; index-- {
v := versionMap[index]
switch {
case v.MajorBump:
baseVersion.BumpMajor()
Expand All @@ -75,6 +82,9 @@ func (b *branchWalker) GetVersion() (*semver.Version, error) {
default: // every commit in master has at least a patch bump
baseVersion.BumpPatch()
}
if b.verbose {
log.Printf("[%s] %s", v.Commit, baseVersion.String())
}
}

return baseVersion, nil
Expand Down Expand Up @@ -116,13 +126,13 @@ func (b *branchWalker) walkVersion(ref *object.Commit, version *versionHolder, t
if err != nil {
return err
}
version.versionMap = append(version.versionMap, &gitVersion{IsSolid: true, Name: tagVersion})
version.versionMap = append(version.versionMap, &gitVersion{IsSolid: true, Name: tagVersion, Commit: ref.Hash.String()})
return nil
}

parents := ref.NumParents()
if parents > 1 {
versionToReconcile := gitVersion{IsSolid: false}
versionToReconcile := gitVersion{IsSolid: false, Commit: ref.Hash.String()}
version.versionMap = append(version.versionMap, &versionToReconcile)

b.commitsToReconcile[ref.Hash.String()] = &versionToReconcile
Expand All @@ -134,7 +144,7 @@ func (b *branchWalker) walkVersion(ref *object.Commit, version *versionHolder, t
return err
}
if matched {
version.versionMap = append(version.versionMap, &gitVersion{IsSolid: false, MajorBump: true})
version.versionMap = append(version.versionMap, &gitVersion{IsSolid: false, MajorBump: true, Commit: ref.Hash.String()})
return b.checkWalkParent(ref, version, tilVisited)
}

Expand All @@ -143,7 +153,7 @@ func (b *branchWalker) walkVersion(ref *object.Commit, version *versionHolder, t
return err
}
if matched {
version.versionMap = append(version.versionMap, &gitVersion{IsSolid: false, MinorBump: true})
version.versionMap = append(version.versionMap, &gitVersion{IsSolid: false, MinorBump: true, Commit: ref.Hash.String()})
return b.checkWalkParent(ref, version, tilVisited)
}

Expand All @@ -152,12 +162,11 @@ func (b *branchWalker) walkVersion(ref *object.Commit, version *versionHolder, t
return err
}
if matched {
version.versionMap = append(version.versionMap, &gitVersion{IsSolid: false, PatchBump: true})
version.versionMap = append(version.versionMap, &gitVersion{IsSolid: false, PatchBump: true, Commit: ref.Hash.String()})
return b.checkWalkParent(ref, version, tilVisited)
}

version.versionMap = append(version.versionMap, &gitVersion{IsSolid: false})

version.versionMap = append(version.versionMap, &gitVersion{IsSolid: false, Commit: ref.Hash.String()})
return b.checkWalkParent(ref, version, tilVisited)
}

Expand Down
28 changes: 21 additions & 7 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package git

import (
"fmt"
"log"
"os"
"regexp"
"strings"
Expand All @@ -27,17 +28,20 @@ type gitVersion struct {
MajorBump bool
MinorBump bool
PatchBump bool
Commit string
}

// GetCurrentVersion returns the current version
func GetCurrentVersion(r *git.Repository, settings *Settings, branchSettings *BranchSettings) (version string, err error) {
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)
if err != nil {
return "", err
}

if verbose {
log.Printf("Version determined using TRAVIS_TAG")
}
return version.String(), err
}

Expand All @@ -51,7 +55,11 @@ func GetCurrentVersion(r *git.Repository, settings *Settings, branchSettings *Br

err = ltags.ForEach(func(ref *plumbing.Reference) error {
name := ref.Name().String()
tagMap[ref.Hash().String()] = strings.Replace(name, "refs/tags/", "", -1)
tag := strings.Replace(name, "refs/tags/", "", -1)
if verbose {
log.Printf("Found lightweight tag %s for ref %s", tag, name)
}
tagMap[ref.Hash().String()] = tag
return nil
})

Expand All @@ -66,6 +74,9 @@ func GetCurrentVersion(r *git.Repository, settings *Settings, branchSettings *Br
if err != nil {
return errors.Wrap(err, "get commit failed")
}
if verbose {
log.Printf("Found tag %s", ref.Name)
}
tagMap[c.Hash.String()] = ref.Name
return nil
})
Expand All @@ -78,7 +89,7 @@ func GetCurrentVersion(r *git.Repository, settings *Settings, branchSettings *Br
return "", errors.Wrap(err, "GetCurrentVersion failed")
}

v, err := getVersion(r, h, tagMap, branchSettings, settings)
v, err := getVersion(r, h, tagMap, branchSettings, settings, verbose)
if err != nil {
return "", errors.Wrap(err, "GetCurrentVersion failed")
}
Expand All @@ -95,11 +106,14 @@ func GetPrereleaseLabel(r *git.Repository, settings *Settings, branchSettings *B
return getCurrentBranch(r, h, branchSettings)
}

func getVersion(r *git.Repository, h *plumbing.Reference, tagMap map[string]string, branchSettings *BranchSettings, settings *Settings) (version *semver.Version, err error) {
func getVersion(r *git.Repository, h *plumbing.Reference, tagMap map[string]string, branchSettings *BranchSettings, settings *Settings, verbose bool) (version *semver.Version, err error) {
currentBranch, err := getCurrentBranch(r, h, branchSettings)
if err != nil {
return nil, errors.Wrap(err, "getVersion failed")
}
if verbose {
log.Printf("Current branch is %s", currentBranch)
}

masterHead, err := r.Reference("refs/heads/master", false)
if err != nil {
Expand All @@ -114,7 +128,7 @@ func getVersion(r *git.Repository, h *plumbing.Reference, tagMap map[string]stri
return nil, errors.Wrap(err, "failed to get master commit from reference")
}

masterWalker := newBranchWalker(r, masterCommit, tagMap, settings, true, "")
masterWalker := newBranchWalker(r, masterCommit, tagMap, settings, true, "", verbose)
masterVersion, err := masterWalker.GetVersion()
if err != nil {
return nil, err
Expand All @@ -129,7 +143,7 @@ func getVersion(r *git.Repository, h *plumbing.Reference, tagMap map[string]stri
return nil, errors.Wrap(err, "getVersion failed")
}

walker := newBranchWalker(r, c, tagMap, settings, false, masterHead.Hash().String())
walker := newBranchWalker(r, c, tagMap, settings, false, masterHead.Hash().String(), verbose)
versionMap, err := walker.GetVersionMap()
if err != nil {
return nil, err
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 @@ -66,7 +66,7 @@ func TestUseLightweightTagForVersionAnchor(t *testing.T) {
s := igit.GetDefaultSettings()
version, err := igit.GetCurrentVersion(r, s, &igit.BranchSettings{
IgnoreEnvVars: true,
})
}, false)
if err != nil {
t.Error(err)
t.FailNow()
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestUseAnnotatedTagForVersionAnchor(t *testing.T) {
s := igit.GetDefaultSettings()
version, err := igit.GetCurrentVersion(r, s, &igit.BranchSettings{
IgnoreEnvVars: true,
})
}, false)
if err != nil {
t.Error(err)
t.FailNow()
Expand Down

0 comments on commit 95eb940

Please sign in to comment.