Skip to content

Commit

Permalink
fix(internal): improve git log for commits on current branch since la…
Browse files Browse the repository at this point in the history
…st release

At the moment when PRs where done in parallel it could happen that some commits where ignored in the release. This should be fixed now
  • Loading branch information
Nightapes authored and fwiedmann committed Apr 28, 2021
1 parent cb3084d commit 2cd2477
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
57 changes: 33 additions & 24 deletions internal/gitutil/gitutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/storer"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -77,14 +76,14 @@ func (g *GitUtil) GetBranch() (string, error) {
}

// GetLastVersion from git tags
func (g *GitUtil) GetLastVersion() (*semver.Version, string, error) {
func (g *GitUtil) GetLastVersion() (*semver.Version, *plumbing.Reference, error) {

var tags []*semver.Version

gitTags, err := g.Repository.Tags()

if err != nil {
return nil, "", err
return nil, nil, err
}

err = gitTags.ForEach(func(p *plumbing.Reference) error {
Expand All @@ -100,59 +99,69 @@ func (g *GitUtil) GetLastVersion() (*semver.Version, string, error) {
})

if err != nil {
return nil, "", err
return nil, nil, err
}

sort.Sort(sort.Reverse(semver.Collection(tags)))

if len(tags) == 0 {
log.Debugf("Found no tags")
return nil, "", nil
return nil, nil, nil
}

log.Debugf("Found old version %s", tags[0].String())

tag, err := g.Repository.Tag(tags[0].Original())
if err != nil {
return nil, "", err
return nil, nil, err
}

log.Debugf("Found old hash %s", tag.Hash().String())
return tags[0], tag.Hash().String(), nil
return tags[0], tag, nil
}

// GetCommits from git hash to HEAD
func (g *GitUtil) GetCommits(lastTagHash string) ([]shared.Commit, error) {
func (g *GitUtil) GetCommits(lastTagHash *plumbing.Reference) ([]shared.Commit, error) {

ref, err := g.Repository.Head()
if err != nil {
return nil, err
}

cIter, err := g.Repository.Log(&git.LogOptions{From: ref.Hash(), Order: git.LogOrderCommitterTime})
excludeIter, err := g.Repository.Log(&git.LogOptions{From: lastTagHash.Hash()})
if err != nil {
return nil, err
}

commits := make(map[string]shared.Commit)
var foundEnd bool
startCommit, err := g.Repository.CommitObject(ref.Hash())
if err != nil {
return nil, err
}
seen := map[plumbing.Hash]struct{}{}

err = cIter.ForEach(func(c *object.Commit) error {
err = excludeIter.ForEach(func(c *object.Commit) error {
seen[c.Hash] = struct{}{}
return nil
})
if err != nil {
return nil, err
}

if c.Hash.String() == lastTagHash {
log.Debugf("Found commit with hash %s, will stop here", c.Hash.String())
foundEnd = true
return storer.ErrStop
}
var isValid object.CommitFilter = func(commit *object.Commit) bool {
_, ok := seen[commit.Hash]
return !ok && len(commit.ParentHashes) < 2
}

if !foundEnd {
log.Tracef("Found commit with hash %s", c.Hash.String())
commits[c.Hash.String()] = shared.Commit{
Message: c.Message,
Author: c.Committer.Name,
Hash: c.Hash.String(),
}
cIter := object.NewFilterCommitIter(startCommit, &isValid, nil)

commits := make(map[string]shared.Commit)

err = cIter.ForEach(func(c *object.Commit) error {
log.Debugf("Found commit with hash %s", c.Hash.String())
commits[c.Hash.String()] = shared.Commit{
Message: c.Message,
Author: c.Committer.Name,
Hash: c.Hash.String(),
}
return nil
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/semanticrelease/semantic-release.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (s *SemanticRelease) GetNextVersion(provider *ci.ProviderConfig, force bool
Version: &newVersion,
},
Last: shared.ReleaseVersionEntry{
Commit: lastVersionHash,
Commit: lastVersionHash.Hash().String(),
Version: lastVersion,
},
Branch: provider.Branch,
Expand Down Expand Up @@ -171,7 +171,7 @@ func (s *SemanticRelease) SetVersion(provider *ci.ProviderConfig, version string
Version: newVersion,
},
Last: shared.ReleaseVersionEntry{
Commit: lastVersionHash,
Commit: lastVersionHash.Hash().String(),
Version: lastVersion,
},
Branch: provider.Branch,
Expand Down

0 comments on commit 2cd2477

Please sign in to comment.