60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package gitcmd
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Tag struct {
|
|
Name string `json:"name"`
|
|
SHA string `json:"sha"`
|
|
ShortSHA string `json:"short_sha"`
|
|
Message string `json:"message"`
|
|
Tagger string `json:"tagger"`
|
|
TagDate time.Time `json:"tag_date"`
|
|
IsAnnotated bool `json:"is_annotated"`
|
|
}
|
|
|
|
func (r *Repository) ListTags() ([]Tag, error) {
|
|
|
|
out, _, err := NewCommand("for-each-ref").
|
|
Add("--format=%(refname:short)%1f%(objectname)%1f%(subject)%1f%(taggername)%1f%(taggerdate:iso8601)%1f%(objecttype)").
|
|
Add("refs/tags").
|
|
WithDir(r.Path).
|
|
RunStdString(r.ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var tags []Tag
|
|
for _, line := range strings.Split(strings.TrimSpace(out), "\n") {
|
|
if line == "" {
|
|
continue
|
|
}
|
|
fields := strings.Split(line, "\x1f")
|
|
if len(fields) < 6 {
|
|
continue
|
|
}
|
|
sha := fields[1]
|
|
shortSHA := sha
|
|
if len(sha) >= 7 {
|
|
shortSHA = sha[:7]
|
|
}
|
|
isAnnotated := fields[5] == "tag"
|
|
var when time.Time
|
|
if isAnnotated {
|
|
when, _ = time.Parse("2006-01-02 15:04:05 -0700", fields[4])
|
|
}
|
|
tags = append(tags, Tag{
|
|
Name: fields[0],
|
|
SHA: sha,
|
|
ShortSHA: shortSHA,
|
|
Message: fields[2],
|
|
Tagger: fields[3],
|
|
TagDate: when,
|
|
IsAnnotated: isAnnotated,
|
|
})
|
|
}
|
|
return tags, nil
|
|
}
|