feat: use tagger.when to find latest tag
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
RaviAnand Mohabir 2024-10-25 18:14:25 +02:00
parent e65c52c67c
commit 6c787535a8

View File

@ -11,7 +11,7 @@ import (
"io" "io"
"net/http" "net/http"
"os" "os"
"strings" "time"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/transport/client" "github.com/go-git/go-git/v5/plumbing/transport/client"
@ -62,28 +62,34 @@ func FetchTags() func(repo *git.Repository) error {
} }
} }
func getLatestGitTag(options ...GetLatestGitTagOption) (tag string, err error) { func getLatestGitTag(options ...GetLatestGitTagOption) (string, error) {
r, err := git.PlainOpen(".") var (
tag string
tagDate *time.Time
err error
)
repo, err := git.PlainOpen(".")
if err != nil { if err != nil {
return "", fmt.Errorf("error opening git repo at %s: %w", ".", err) return "", fmt.Errorf("error opening git repo at %s: %w", ".", err)
} }
for _, opt := range options { for _, opt := range options {
err := opt(r) err := opt(repo)
if err != nil { if err != nil {
return "", err return "", err
} }
} }
tagRefs, err := r.Tags() tags, err := repo.TagObjects()
if err != nil { if err != nil {
return "", fmt.Errorf("error getting git tag refs %w", err) return "", fmt.Errorf("error getting git tags %w", err)
} }
for { for {
r, err := tagRefs.Next() t, err := tags.Next()
if err == io.EOF { if err == io.EOF {
break break
@ -93,15 +99,19 @@ func getLatestGitTag(options ...GetLatestGitTagOption) (tag string, err error) {
return "", fmt.Errorf("error iterating tags %w", err) return "", fmt.Errorf("error iterating tags %w", err)
} }
parts := strings.Split(string(r.Name()), "/") if tagDate != nil && t.Tagger.When.Sub(*tagDate) > 0 {
tag = strings.Join(parts[2:], "/") continue
}
tagDate = &t.Tagger.When
tag = t.Name
} }
if tag == "" { if tag == "" {
return tag, fmt.Errorf("couldn't find any git tags") return tag, fmt.Errorf("couldn't find any git tags")
} }
return return tag, err
} }
func writeCard(path, schema string, card interface{}) { func writeCard(path, schema string, card interface{}) {