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