From cbcb41842f4fe07c2f97aa45ada51954bd81b214 Mon Sep 17 00:00:00 2001 From: Malte Poll Date: Wed, 21 Sep 2022 14:32:53 +0200 Subject: [PATCH] pseudo-version: Fix annotated tag detection and support pre-versions --- hack/pseudo-version/internal/git/git.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/hack/pseudo-version/internal/git/git.go b/hack/pseudo-version/internal/git/git.go index 6a1f94a9a..7b53c7927 100644 --- a/hack/pseudo-version/internal/git/git.go +++ b/hack/pseudo-version/internal/git/git.go @@ -19,7 +19,7 @@ import ( ) var ( - versionRegex = regexp.MustCompile(`^v\d+\.\d+\.\d+$`) + versionRegex = regexp.MustCompile(`^v\d+\.\d+\.\d+(?:-pre)?$`) tagReference = regexp.MustCompile(`^refs/tags/([^/]+)$`) ) @@ -123,11 +123,15 @@ func (g *Git) tagsByRevisionHash() (map[string][]string, error) { } if err := refs.ForEach( func(ref *plumbing.Reference) error { + hash, err := g.tagRefToHash(ref) + if err != nil { + return err + } message, err := tagRefToName(ref) if err != nil { return err } - tags[ref.Hash().String()] = append(tags[ref.Hash().String()], message) + tags[hash] = append(tags[hash], message) return nil }, ); err != nil { @@ -146,6 +150,19 @@ func (g *Git) findVersionTag(tags []string) *string { return nil } +func (g *Git) tagRefToHash(tagRef *plumbing.Reference) (string, error) { + tagObject, err := g.repo.TagObject(tagRef.Hash()) + switch err { + case nil: + return tagObject.Target.String(), nil + case plumbing.ErrObjectNotFound: + return tagRef.Hash().String(), nil + default: + // Some other error + return "", err + } +} + // tagRefToName extracts the name of a tag reference. func tagRefToName(tagRef *plumbing.Reference) (string, error) { matches := tagReference.FindStringSubmatch(tagRef.Name().String())