Skip to content

Commit

Permalink
Use old "is tag" test
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe committed Sep 13, 2023
1 parent 4caf9ad commit 04dcf39
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
45 changes: 33 additions & 12 deletions pkg/gitversion/gitversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func determineBaseVersion(repo *git.Repository, revision *plumbing.Hash,
if err != nil {
return "", false, fmt.Errorf("error resolving repo tags: %w", err)
}
isExact, exactMatch, err := isExactTag(tags, commit.Hash, isPrerelease, tagFilter)
isExact, exactMatch, err := isExactTag(repo, tags, commit.Hash, isPrerelease, tagFilter)
if err != nil {
return "", false, fmt.Errorf("isExactTag: %w", err)
}
Expand Down Expand Up @@ -300,7 +300,7 @@ func StripModuleTagPrefixes(tag string) string {

// isExactTag returns true if the given hash has a tag associated with it. If
// true is returned, the second return value is a reference representing the tag.
func isExactTag(tags storer.ReferenceIter, hash plumbing.Hash,
func isExactTag(repo *git.Repository, tags storer.ReferenceIter, hash plumbing.Hash,
isPrerease bool, tagFilter func(string) bool) (bool, *plumbing.Reference, error) {

var exactTag *plumbing.Reference
Expand All @@ -311,9 +311,6 @@ func isExactTag(tags storer.ReferenceIter, hash plumbing.Hash,
}

refName := ref.Name().String()
if !strings.HasPrefix(refName, "refs/tags/") {
return nil
}

// if we are marking the release as a pre-release, then we want to take into account
// the beta and rc versions
Expand All @@ -331,10 +328,27 @@ func isExactTag(tags storer.ReferenceIter, hash plumbing.Hash,
return nil
}

if ref.Hash() == hash {
exactTag = ref
return storer.ErrStop
obj, err := repo.TagObject(ref.Hash())
switch err {
case nil:
// This is an annotated tag, check the hash of the target of the tag object
if obj.Target == hash {
exactTag = ref
return storer.ErrStop
}

case plumbing.ErrObjectNotFound:
// Not a tag object, pointing directly to the commit
if ref.Hash() == hash {
exactTag = ref
return storer.ErrStop
}

default:
return err

}

return nil
}); err != nil {
return false, nil, fmt.Errorf("error iterating on tags: %w", err)
Expand All @@ -360,7 +374,7 @@ func mostRecentTag(repo *git.Repository, ref plumbing.Hash, isPrerelease bool,
if err != nil {
return fmt.Errorf("resolving repo tags: %w", err)
}
isExact, exact, err := isExactTag(tags, commit.Hash, isPrerelease, tagFilter)
isExact, exact, err := isExactTag(repo, tags, commit.Hash, isPrerelease, tagFilter)
if err != nil {
return err
}
Expand All @@ -378,12 +392,19 @@ func mostRecentTag(repo *git.Repository, ref plumbing.Hash, isPrerelease bool,
// Walk the commits that are checked out, in order.
walker := object.NewCommitPreorderIter(commit, nil, nil)
err = walker.ForEach(walk(repo.Tags))
if err != nil && !errors.Is(err, plumbing.ErrObjectNotFound) {

// If we are missing an object, then this is not a full clone, and we should check
// for remote tags.
isBareCheckout := errors.Is(err, plumbing.ErrObjectNotFound)
if isBareCheckout {
err = nil
}
if err != nil {
return false, nil, fmt.Errorf("could not find local object: %w", err)
}
// We have found a local tag that is a parent, so return it.
if mostRecentTag != nil {
return true, mostRecentTag, nil
if mostRecentTag != nil || !isBareCheckout {
return mostRecentTag != nil, mostRecentTag, nil
}

// See if we can find a remote tag that matches our commit.
Expand Down
10 changes: 5 additions & 5 deletions pkg/gitversion/gitversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestIsExactTag(t *testing.T) {
t.Run("Not an exact tag", func(t *testing.T) {
tags, err := repo.Tags()
require.NoError(t, err)
isExact, exact, err := isExactTag(tags, headRef.Hash(), false, nil)
isExact, exact, err := isExactTag(repo, tags, headRef.Hash(), false, nil)
require.NoError(t, err)
assert.Nil(t, exact)
assert.False(t, isExact)
Expand All @@ -122,7 +122,7 @@ func TestIsExactTag(t *testing.T) {
tags, err := repo.Tags()
require.NoError(t, err)

isExact, exact, err := isExactTag(tags, exactRef.Hash(), false, nil)
isExact, exact, err := isExactTag(repo, tags, exactRef.Hash(), false, nil)
require.NoError(t, err)
assert.NotNil(t, exact)
assert.True(t, isExact)
Expand All @@ -136,7 +136,7 @@ func TestIsExactTag(t *testing.T) {
tags, err := repo.Tags()
require.NoError(t, err)

isExact, exact, err := isExactTag(tags, exactRef.Hash(), false, nil)
isExact, exact, err := isExactTag(repo, tags, exactRef.Hash(), false, nil)
require.NoError(t, err)
assert.NotNil(t, exact)
assert.True(t, isExact)
Expand All @@ -150,7 +150,7 @@ func TestIsExactTag(t *testing.T) {
tags, err := repo.Tags()
require.NoError(t, err)

isExact, exact, err := isExactTag(tags, exactRef.Hash(), true, nil)
isExact, exact, err := isExactTag(repo, tags, exactRef.Hash(), true, nil)
require.NoError(t, err)
assert.NotNil(t, exact)
assert.True(t, isExact)
Expand All @@ -164,7 +164,7 @@ func TestIsExactTag(t *testing.T) {
tags, err := repo.Tags()
require.NoError(t, err)

isExact, exact, err := isExactTag(tags, exactRef.Hash(), false, nil)
isExact, exact, err := isExactTag(repo, tags, exactRef.Hash(), false, nil)
require.NoError(t, err)
assert.Nil(t, exact)
assert.False(t, isExact)
Expand Down

0 comments on commit 04dcf39

Please sign in to comment.