Skip to content
This repository has been archived by the owner on Apr 27, 2022. It is now read-only.

Commit

Permalink
Make bff work with nonlinear history (#11)
Browse files Browse the repository at this point in the history
Make bff work with nonlinear historyWhen I tried to create a new release for bff, `bff bump` failed because 5f0c3f8 has two parents: 3200804, and b31bdf7.
This fix finds the parent that gets committed the most recently.

### Test plan
`bff bump` runs successfully, updating the `VERSION` file.
  • Loading branch information
chauvm authored and czimergebot committed Jun 14, 2019
1 parent 1d9eaae commit ffdbfdd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
15 changes: 4 additions & 11 deletions cmd/bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,11 @@ var bumpCmd = &cobra.Command{
break
}

if len(commit.ParentHashes) > 1 {
log.Fatal("bff only works with linear history")
}

if len(commit.ParentHashes) == 0 {
// When we get here we should be at the beginning of this repo's history
break
}
commit, err = commit.Parent(0)
commit, err = util.GetLatestParentCommit(commit)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -161,15 +157,11 @@ var bumpCmd = &cobra.Command{
feature = true
}

if len(commit.ParentHashes) > 1 {
log.Fatal("bff only works with linear history")
}

if len(commit.ParentHashes) == 0 {
// When we get here we should be at the beginning of this repo's history
break
}
commit, err = commit.Parent(0)
commit, err = util.GetLatestParentCommit(commit)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -234,7 +226,7 @@ var bumpCmd = &cobra.Command{
},
}

// ReleaseType will calculate whether the next release should be major, minor or patch
// ReleaseType will calculate whether the next release should be major, minor or patch
func ReleaseType(major uint64, breaking, feature bool) string {
if major < 1 {
if breaking || feature {
Expand All @@ -252,6 +244,7 @@ func ReleaseType(major uint64, breaking, feature bool) string {
}
}

// NewVersion returns the next version based on the current version and next release type
func NewVersion(ver semver.Version, releaseType string) semver.Version {
switch releaseType {
case "major":
Expand Down
25 changes: 19 additions & 6 deletions pkg/util/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"gopkg.in/src-d/go-git.v4/plumbing/storer"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

// GetGitAuthor returns the author name and email
Expand Down Expand Up @@ -60,7 +61,6 @@ func LatestTagCommitHash(repo *git.Repository) (*string, *plumbing.Hash, error)
}

err = tags.ForEach(func(tag *plumbing.Reference) error {
// logrus.Infof("Visiting tag %s", tag.Name().String())
tagIndex[tag.Hash().String()] = strings.Replace(tag.Name().String(), "refs/tags/v", "", -1)
return nil
})
Expand All @@ -82,16 +82,11 @@ func LatestTagCommitHash(repo *git.Repository) (*string, *plumbing.Hash, error)

err = gitLog.ForEach(func(c *object.Commit) error {
if v, ok := tagIndex[c.Hash.String()]; ok {
// logrus.Warnf("Found tag %s; break", v)
latestVersionTag = v
latestVersionHash = c.Hash
return storer.ErrStop
}

if len(c.ParentHashes) > 1 {
return errors.New("bff only works with linear history")
}

if len(c.ParentHashes) == 0 {
// When we get here we should be at the beginning of the history
return storer.ErrStop
Expand All @@ -101,3 +96,21 @@ func LatestTagCommitHash(repo *git.Repository) (*string, *plumbing.Hash, error)
return &latestVersionTag, &latestVersionHash, errors.Wrap(err, "error searching git history for latest tag")

}

// GetLatestParentCommit returns the most recent parent commit
func GetLatestParentCommit(commit *object.Commit) (*object.Commit, error) {
var recentParentCommit *object.Commit
if commit.NumParents() > 1 {
log.Warnf("Commit %s has more than 1 parent", commit.Hash.String())
}
for i := 0; i < commit.NumParents(); i++ {
currentParentCommit, err := commit.Parent(i)
if err != nil {
return recentParentCommit, errors.Wrap(err, "unable to retrieve a parent hash")
}
if i == 0 || currentParentCommit.Author.When.After(recentParentCommit.Author.When) {
recentParentCommit = currentParentCommit
}
}
return recentParentCommit, nil
}

0 comments on commit ffdbfdd

Please sign in to comment.