Skip to content

Commit

Permalink
git: add new function to do the plain rev parse
Browse files Browse the repository at this point in the history
  • Loading branch information
cpanato committed Feb 19, 2021
1 parent 570ed07 commit fa13b34
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 32 deletions.
2 changes: 1 addition & 1 deletion cmd/krel/cmd/release_notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ func releaseNotesJSON(repoPath, tag string) (jsonString string, err error) {
}

// Chech if release branch already exists
_, err = repo.RevParse(releaseBranch)
_, err = repo.RevParseTag(releaseBranch)
if err == nil {
logrus.Infof("Working on branch %s instead of %s", releaseBranch, git.DefaultBranch)
branchName = releaseBranch
Expand Down
7 changes: 6 additions & 1 deletion pkg/anago/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ type stageImpl interface {
) (*release.Versions, error)
OpenRepo(repoPath string) (*git.Repo, error)
RevParse(repo *git.Repo, rev string) (string, error)
RevParseTag(repo *git.Repo, rev string) (string, error)
Checkout(repo *git.Repo, rev string, args ...string) error
CurrentBranch(repo *git.Repo) (string, error)
CommitEmpty(repo *git.Repo, msg string) error
Expand Down Expand Up @@ -191,6 +192,10 @@ func (d *defaultStageImpl) RevParse(repo *git.Repo, rev string) (string, error)
return repo.RevParse(rev)
}

func (d *defaultStageImpl) RevParseTag(repo *git.Repo, rev string) (string, error) {
return repo.RevParseTag(rev)
}

func (d *defaultStageImpl) Checkout(repo *git.Repo, rev string, args ...string) error {
return repo.Checkout(rev, args...)
}
Expand Down Expand Up @@ -328,7 +333,7 @@ func (d *DefaultStage) TagRepository() error {
logrus.Infof("Preparing version %s", version)

// Ensure that the tag not already exists
if _, err := d.impl.RevParse(repo, version); err == nil {
if _, err := d.impl.RevParseTag(repo, version); err == nil {
return errors.Errorf("tag %s already exists", version)
}

Expand Down
24 changes: 12 additions & 12 deletions pkg/anago/stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func TestTagRepository(t *testing.T) {
}{
{ // success new rc creating release branch
prepare: func(mock *anagofakes.FakeStageImpl) {
mock.RevParseReturns("", err)
mock.RevParseTagReturns("", err)
mock.CurrentBranchReturnsOnCall(0, "release-1.20", nil)
},
versions: newRCVersions,
Expand All @@ -248,7 +248,7 @@ func TestTagRepository(t *testing.T) {
},
{ // failure on CommitEmpty new rc creating release branch
prepare: func(mock *anagofakes.FakeStageImpl) {
mock.RevParseReturns("", err)
mock.RevParseTagReturns("", err)
mock.CurrentBranchReturnsOnCall(0, "release-1.20", nil)
mock.CommitEmptyReturns(err)
},
Expand All @@ -259,7 +259,7 @@ func TestTagRepository(t *testing.T) {
},
{ // failure on CurrentBranch new rc creating release branch
prepare: func(mock *anagofakes.FakeStageImpl) {
mock.RevParseReturns("", err)
mock.RevParseTagReturns("", err)
mock.CurrentBranchReturnsOnCall(0, "release-1.20", nil)
mock.CurrentBranchReturns("", err)
},
Expand All @@ -270,7 +270,7 @@ func TestTagRepository(t *testing.T) {
},
{ // failure on Checkout new rc creating release branch
prepare: func(mock *anagofakes.FakeStageImpl) {
mock.RevParseReturns("", err)
mock.RevParseTagReturns("", err)
mock.CurrentBranchReturnsOnCall(0, "release-1.20", nil)
mock.CheckoutReturns(err)
},
Expand All @@ -281,7 +281,7 @@ func TestTagRepository(t *testing.T) {
},
{ // failure on RevParse new rc creating release branch
prepare: func(mock *anagofakes.FakeStageImpl) {
mock.RevParseReturns("", nil)
mock.RevParseTagReturns("", nil)
},
versions: newRCVersions,
releaseBranch: "release-1.20",
Expand All @@ -299,7 +299,7 @@ func TestTagRepository(t *testing.T) {
},
{ // success new rc checking out release branch
prepare: func(mock *anagofakes.FakeStageImpl) {
mock.RevParseReturns("", err)
mock.RevParseTagReturns("", err)
},
versions: newRCVersions,
releaseBranch: "release-1.20",
Expand All @@ -308,7 +308,7 @@ func TestTagRepository(t *testing.T) {
},
{ // failure on Checkout new rc checking out release branch
prepare: func(mock *anagofakes.FakeStageImpl) {
mock.RevParseReturns("", err)
mock.RevParseTagReturns("", err)
mock.CheckoutReturns(err)
},
versions: newRCVersions,
Expand All @@ -318,7 +318,7 @@ func TestTagRepository(t *testing.T) {
},
{ // failure on RevParse new rc checking out release branch
prepare: func(mock *anagofakes.FakeStageImpl) {
mock.RevParseReturns("", nil)
mock.RevParseTagReturns("", nil)
},
versions: newRCVersions,
releaseBranch: "release-1.20",
Expand All @@ -327,15 +327,15 @@ func TestTagRepository(t *testing.T) {
},
{ // success new beta
prepare: func(mock *anagofakes.FakeStageImpl) {
mock.RevParseReturns("", err)
mock.RevParseTagReturns("", err)
},
versions: newBetaVersions,
releaseBranch: git.DefaultBranch,
shouldError: false,
},
{ // new beta failure on Tag
prepare: func(mock *anagofakes.FakeStageImpl) {
mock.RevParseReturns("", err)
mock.RevParseTagReturns("", err)
mock.TagReturns(err)
},
versions: newBetaVersions,
Expand All @@ -344,7 +344,7 @@ func TestTagRepository(t *testing.T) {
},
{ // new beta failure on CurrentBranch
prepare: func(mock *anagofakes.FakeStageImpl) {
mock.RevParseReturns("", err)
mock.RevParseTagReturns("", err)
mock.CurrentBranchReturns("", err)
},
versions: newBetaVersions,
Expand All @@ -353,7 +353,7 @@ func TestTagRepository(t *testing.T) {
},
{ // new beta failure on Checkout
prepare: func(mock *anagofakes.FakeStageImpl) {
mock.RevParseReturns("", err)
mock.RevParseTagReturns("", err)
mock.CheckoutReturns(err)
},
versions: newBetaVersions,
Expand Down
2 changes: 1 addition & 1 deletion pkg/changelog/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (c *Changelog) Run() error {
}

remoteBranch := git.Remotify(branch)
head, err := c.impl.RevParse(repo, remoteBranch)
head, err := c.impl.RevParseTag(repo, remoteBranch)
if err != nil {
return errors.Wrap(err, "get latest branch commit")
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/changelog/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type impl interface {
OpenRepo(repoPath string) (*git.Repo, error)
CurrentBranch(repo *git.Repo) (branch string, err error)
RevParse(repo *git.Repo, rev string) (string, error)
RevParseTag(repo *git.Repo, rev string) (string, error)
CreateDownloadsTable(
w io.Writer, bucket, tars, prevTag, newTag string,
) error
Expand Down Expand Up @@ -108,6 +109,10 @@ func (*defaultImpl) RevParse(repo *git.Repo, rev string) (string, error) {
return repo.RevParse(rev)
}

func (*defaultImpl) RevParseTag(repo *git.Repo, rev string) (string, error) {
return repo.RevParseTag(rev)
}

func (*defaultImpl) CreateDownloadsTable(
w io.Writer, bucket, tars, prevTag, newTag string,
) error {
Expand Down
45 changes: 35 additions & 10 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,9 +438,10 @@ func (r *Repo) Cleanup() error {
return os.RemoveAll(r.dir)
}

// RevParse parses a git revision and returns a SHA1 on success, otherwise an
// RevParseTag parses a git revision and returns a SHA1 on success, otherwise an
// error.
func (r *Repo) RevParse(rev string) (string, error) {
// If the revision does not match a tag add the remote origin in the revision.
func (r *Repo) RevParseTag(rev string) (string, error) {
matched, err := regexp.MatchString(`v\d+\.\d+\.\d+.*`, rev)
if err != nil {
return "", err
Expand All @@ -459,7 +460,31 @@ func (r *Repo) RevParse(rev string) (string, error) {
return ref.String(), nil
}

// RevParseShort parses a git revision and returns a SHA1 trimmed to the length
// RevParseTag parses a git revision and returns a SHA1 on success, otherwise an
// error.
func (r *Repo) RevParse(rev string) (string, error) {
// Try to resolve the rev
ref, err := r.inner.ResolveRevision(plumbing.Revision(rev))
if err != nil {
return "", err
}

return ref.String(), nil
}

// RevParseTagShort parses a git revision and returns a SHA1 trimmed to the length
// 10 on success, otherwise an error.
// If the revision does not match a tag add the remote origin in the revision.
func (r *Repo) RevParseTagShort(rev string) (string, error) {
fullRev, err := r.RevParseTag(rev)
if err != nil {
return "", err
}

return fullRev[:10], nil
}

// RevParseTagShort parses a git revision and returns a SHA1 trimmed to the length
// 10 on success, otherwise an error.
func (r *Repo) RevParseShort(rev string) (string, error) {
fullRev, err := r.RevParse(rev)
Expand Down Expand Up @@ -519,15 +544,15 @@ func (r *Repo) LatestNonPatchFinalToMinor() (DiscoverResult, error) {
latestVersion := versions[0]
latestVersionTag := util.SemverToTagString(latestVersion)
logrus.Debugf("Latest non patch version %s", latestVersionTag)
end, err := r.RevParse(latestVersionTag)
end, err := r.RevParseTag(latestVersionTag)
if err != nil {
return DiscoverResult{}, err
}

previousVersion := versions[1]
previousVersionTag := util.SemverToTagString(previousVersion)
logrus.Debugf("Previous non patch version %s", previousVersionTag)
start, err := r.RevParse(previousVersionTag)
start, err := r.RevParseTag(previousVersionTag)
if err != nil {
return DiscoverResult{}, err
}
Expand Down Expand Up @@ -569,13 +594,13 @@ func (r *Repo) latestNonPatchFinalVersions() ([]semver.Version, error) {

func (r *Repo) releaseBranchOrMainRef(major, minor uint64) (sha, rev string, err error) {
relBranch := fmt.Sprintf("release-%d.%d", major, minor)
sha, err = r.RevParse(relBranch)
sha, err = r.RevParseTag(relBranch)
if err == nil {
logrus.Debugf("Found release branch %s", relBranch)
return sha, relBranch, nil
}

sha, err = r.RevParse(DefaultBranch)
sha, err = r.RevParseTag(DefaultBranch)
if err == nil {
logrus.Debugf("No release branch found, using %s", DefaultBranch)
return sha, DefaultBranch, nil
Expand Down Expand Up @@ -794,14 +819,14 @@ func (r *Repo) LatestPatchToPatch(branch string) (DiscoverResult, error) {

logrus.Debugf("Parsing latest tag %s%v", util.TagPrefix, latestTag)
latestVersionTag := util.SemverToTagString(latestTag)
end, err := r.RevParse(latestVersionTag)
end, err := r.RevParseTag(latestVersionTag)
if err != nil {
return DiscoverResult{}, errors.Wrapf(err, "parsing version %v", latestTag)
}

logrus.Debugf("Parsing previous tag %s%v", util.TagPrefix, prevTag)
previousVersionTag := util.SemverToTagString(prevTag)
start, err := r.RevParse(previousVersionTag)
start, err := r.RevParseTag(previousVersionTag)
if err != nil {
return DiscoverResult{}, errors.Wrapf(err, "parsing previous version %v", prevTag)
}
Expand Down Expand Up @@ -830,7 +855,7 @@ func (r *Repo) LatestPatchToLatest(branch string) (DiscoverResult, error) {

logrus.Debugf("Parsing latest tag %s%v", util.TagPrefix, latestTag)
latestVersionTag := util.SemverToTagString(latestTag)
start, err := r.RevParse(latestVersionTag)
start, err := r.RevParseTag(latestVersionTag)
if err != nil {
return DiscoverResult{}, errors.Wrapf(err, "parsing version %v", latestTag)
}
Expand Down
64 changes: 64 additions & 0 deletions pkg/git/git_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,27 @@ func TestSuccessRevParse(t *testing.T) {
tagRev, err := testRepo.sut.RevParse(testRepo.firstTagName)
require.Nil(t, err)
require.Equal(t, tagRev, testRepo.firstCommit)

tagRev, err = testRepo.sut.RevParse(testRepo.firstCommit)
require.Nil(t, err)
require.Equal(t, tagRev, testRepo.firstCommit)
}

func TestSuccessRevTagParse(t *testing.T) {
testRepo := newTestRepo(t)
defer testRepo.cleanup(t)

mainRev, err := testRepo.sut.RevParseTag(git.DefaultBranch)
require.Nil(t, err)
require.Equal(t, mainRev, testRepo.firstCommit)

branchRev, err := testRepo.sut.RevParseTag(testRepo.branchName)
require.Nil(t, err)
require.Equal(t, branchRev, testRepo.thirdBranchCommit)

tagRev, err := testRepo.sut.RevParseTag(testRepo.firstTagName)
require.Nil(t, err)
require.Equal(t, tagRev, testRepo.firstCommit)
}

func TestFailureRevParse(t *testing.T) {
Expand All @@ -351,6 +372,17 @@ func TestFailureRevParse(t *testing.T) {
require.NotNil(t, err)
}

func TestFailureRevParseTag(t *testing.T) {
testRepo := newTestRepo(t)
defer testRepo.cleanup(t)

_, err := testRepo.sut.RevParseTag("wrong")
require.NotNil(t, err)

_, err = testRepo.sut.RevParseTag(testRepo.firstCommit)
require.NotNil(t, err)
}

func TestSuccessRevParseShort(t *testing.T) {
testRepo := newTestRepo(t)
defer testRepo.cleanup(t)
Expand All @@ -366,6 +398,27 @@ func TestSuccessRevParseShort(t *testing.T) {
tagRev, err := testRepo.sut.RevParseShort(testRepo.firstTagName)
require.Nil(t, err)
require.Equal(t, tagRev, testRepo.firstCommit[:10])

tagRev, err = testRepo.sut.RevParseShort(testRepo.firstCommit)
require.Nil(t, err)
require.Equal(t, tagRev, testRepo.firstCommit[:10])
}

func TestSuccessRevParseTagShort(t *testing.T) {
testRepo := newTestRepo(t)
defer testRepo.cleanup(t)

mainRev, err := testRepo.sut.RevParseTagShort(git.DefaultBranch)
require.Nil(t, err)
require.Equal(t, mainRev, testRepo.firstCommit[:10])

branchRev, err := testRepo.sut.RevParseTagShort(testRepo.branchName)
require.Nil(t, err)
require.Equal(t, branchRev, testRepo.thirdBranchCommit[:10])

tagRev, err := testRepo.sut.RevParseTagShort(testRepo.firstTagName)
require.Nil(t, err)
require.Equal(t, tagRev, testRepo.firstCommit[:10])
}

func TestFailureRevParseShort(t *testing.T) {
Expand All @@ -376,6 +429,17 @@ func TestFailureRevParseShort(t *testing.T) {
require.NotNil(t, err)
}

func TestFailureRevParseTagShort(t *testing.T) {
testRepo := newTestRepo(t)
defer testRepo.cleanup(t)

_, err := testRepo.sut.RevParseTagShort("wrong")
require.NotNil(t, err)

_, err = testRepo.sut.RevParseTagShort(testRepo.firstCommit)
require.NotNil(t, err)
}

func TestSuccessPush(t *testing.T) {
testRepo := newTestRepo(t)
defer testRepo.cleanup(t)
Expand Down
4 changes: 2 additions & 2 deletions pkg/notes/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,15 @@ func (o *Options) ValidateAndFinish() (err error) {
return err
}
if o.StartRev != "" && o.StartSHA == "" {
sha, err := repo.RevParse(o.StartRev)
sha, err := repo.RevParseTag(o.StartRev)
if err != nil {
return errors.Wrapf(err, "resolving %s", o.StartRev)
}
logrus.Infof("Using found start SHA: %s", sha)
o.StartSHA = sha
}
if o.EndRev != "" && o.EndSHA == "" {
sha, err := repo.RevParse(o.EndRev)
sha, err := repo.RevParseTag(o.EndRev)
if err != nil {
return errors.Wrapf(err, "resolving %s", o.EndRev)
}
Expand Down
Loading

0 comments on commit fa13b34

Please sign in to comment.