sort tags in reverse chronological order and deduplicate

This commit is contained in:
Iris Lightshard 2023-01-31 17:08:03 -07:00
parent 7a512b1da6
commit 3fc204c253
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398

View file

@ -2,6 +2,7 @@ package git
import ( import (
"fmt" "fmt"
"sort"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing"
@ -13,6 +14,21 @@ type GitRepo struct {
h plumbing.Hash h plumbing.Hash
} }
type TagList []*object.Tag
func (self TagList) Len() int {
return len(self)
}
func (self TagList) Swap(i, j int) {
self[i], self[j] = self[j], self[i]
}
// sorting tags in reverse chronological order
func (self TagList) Less(i, j int) bool {
return self[i].Tagger.When.After(self[j].Tagger.When)
}
func Open(path string, ref string) (*GitRepo, error) { func Open(path string, ref string) (*GitRepo, error) {
var err error var err error
g := GitRepo{} g := GitRepo{}
@ -94,10 +110,22 @@ func (g *GitRepo) Tags() ([]*object.Tag, error) {
tags := []*object.Tag{} tags := []*object.Tag{}
_ = ti.ForEach(func(t *object.Tag) error { _ = ti.ForEach(func(t *object.Tag) error {
for i, existing := range tags {
if existing.Name == t.Name {
if t.Tagger.When.After(existing.Tagger.When) {
tags[i] = t
}
return nil
}
}
tags = append(tags, t) tags = append(tags, t)
return nil return nil
}) })
var tagList TagList
tagList = tags
sort.Sort(tagList)
return tags, nil return tags, nil
} }