summaryrefslogtreecommitdiffstats
path: root/git
diff options
context:
space:
mode:
authorLibravatar Derek Stevens <nilix@nilfm.cc>2023-01-31 21:29:38 -0700
committerLibravatar Anirudh Oppiliappan <x@icyphox.sh>2023-02-05 15:11:16 +0530
commit3060c752f8bbc4852939ee7f15732e9752d0c6ec (patch)
treecfeafc26cd9950991d89799622c3c7e2edcfd5c0 /git
parente782f36f196006f4fad6747811a5c883151e5102 (diff)
downloadlegit-3060c752f8bbc4852939ee7f15732e9752d0c6ec.tar.gz
legit-3060c752f8bbc4852939ee7f15732e9752d0c6ec.tar.bz2
legit-3060c752f8bbc4852939ee7f15732e9752d0c6ec.zip
sort tags and deduplicate
This sorts the tags reverse-chronologically. If any tags have the same name (shouldn't happen but it does in some of my repos), we use whichever one is "newer". Signed-off-by: Derek Stevens <nilix@nilfm.cc>
Diffstat (limited to '')
-rw-r--r--git/git.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/git/git.go b/git/git.go
index 34e8a80..cd079c0 100644
--- a/git/git.go
+++ b/git/git.go
@@ -2,6 +2,7 @@ package git
import (
"fmt"
+ "sort"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
@@ -13,6 +14,21 @@ type GitRepo struct {
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) {
var err error
g := GitRepo{}
@@ -94,10 +110,22 @@ func (g *GitRepo) Tags() ([]*object.Tag, error) {
tags := []*object.Tag{}
_ = 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)
return nil
})
+ var tagList TagList
+ tagList = tags
+ sort.Sort(tagList)
+
return tags, nil
}