Skip to content
This repository was archived by the owner on Apr 12, 2019. It is now read-only.

Commit a3ee12b

Browse files
authored
Add get tags info method for releases (#27)
* add get tags info method for releases * composite variable decleare
1 parent 5f81d4b commit a3ee12b

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-1
lines changed

repo_tag.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package git
66

77
import (
88
"strings"
9+
"time"
910

1011
"github.com/mcuadros/go-version"
1112
)
@@ -94,6 +95,87 @@ func (repo *Repository) GetTag(name string) (*Tag, error) {
9495
return tag, nil
9596
}
9697

98+
// TagOption describes tag options
99+
type TagOption struct {
100+
}
101+
102+
// parseTag parse the line
103+
// 2016-10-14 20:54:25 +0200 (tag: translation/20161014.01) d3b76dcf2 Dirk Baeumer [email protected] Merge in translations
104+
func parseTag(line string, opt TagOption) (*Tag, error) {
105+
line = strings.TrimSpace(line)
106+
if len(line) < 40 {
107+
return nil, nil
108+
}
109+
110+
var (
111+
err error
112+
tag Tag
113+
sig Signature
114+
)
115+
sig.When, err = time.Parse("2006-01-02 15:04:05 -0700", line[0:25])
116+
if err != nil {
117+
return nil, err
118+
}
119+
120+
left := strings.TrimSpace(line[25:])
121+
start := strings.Index(left, "(tag: ")
122+
if start < 0 {
123+
return nil, nil
124+
}
125+
end := strings.IndexByte(left[start+1:], ')')
126+
if end < 0 {
127+
return nil, nil
128+
}
129+
end = end + start + 1
130+
part := strings.IndexByte(left[start+6:end], ',')
131+
if part > 0 {
132+
tag.Name = strings.TrimSpace(left[start+6 : start+6+part])
133+
} else {
134+
tag.Name = strings.TrimSpace(left[start+6 : end])
135+
}
136+
next := strings.IndexByte(left[end+2:], ' ')
137+
if next < 0 {
138+
return nil, nil
139+
}
140+
tag.Object = MustIDFromString(strings.TrimSpace(left[end+2 : end+2+next]))
141+
next = end + 2 + next
142+
143+
emailStart := strings.IndexByte(left[next:], '<')
144+
sig.Name = strings.TrimSpace(left[next:][:emailStart-1])
145+
emailEnd := strings.IndexByte(left[next:], '>')
146+
sig.Email = strings.TrimSpace(left[next:][emailStart+1 : emailEnd])
147+
tag.Tagger = &sig
148+
tag.Message = strings.TrimSpace(left[next+emailEnd+1:])
149+
return &tag, nil
150+
}
151+
152+
// GetTagInfos returns all tag infos of the repository.
153+
func (repo *Repository) GetTagInfos(opt TagOption) ([]*Tag, error) {
154+
cmd := NewCommand("log", "--tags", "--simplify-by-decoration", `--pretty=format:"%ci %d %H %cn<%ce> %s"`)
155+
stdout, err := cmd.RunInDir(repo.Path)
156+
if err != nil {
157+
return nil, err
158+
}
159+
160+
tagSlices := strings.Split(stdout, "\n")
161+
var tags []*Tag
162+
for _, line := range tagSlices {
163+
line := strings.Trim(line, `"`)
164+
tag, err := parseTag(line, opt)
165+
if err != nil {
166+
return nil, err
167+
}
168+
if tag != nil {
169+
tag.repo = repo
170+
tags = append(tags, tag)
171+
}
172+
}
173+
174+
sortTagsByTime(tags)
175+
176+
return tags, nil
177+
}
178+
97179
// GetTags returns all tags of the repository.
98180
func (repo *Repository) GetTags() ([]string, error) {
99181
cmd := NewCommand("tag", "-l")

tag.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
package git
66

7-
import "bytes"
7+
import (
8+
"bytes"
9+
"sort"
10+
)
811

912
// Tag represents a Git tag.
1013
type Tag struct {
@@ -64,3 +67,23 @@ l:
6467
}
6568
return tag, nil
6669
}
70+
71+
type tagSorter []*Tag
72+
73+
func (ts tagSorter) Len() int {
74+
return len([]*Tag(ts))
75+
}
76+
77+
func (ts tagSorter) Less(i, j int) bool {
78+
return []*Tag(ts)[i].Tagger.When.After([]*Tag(ts)[j].Tagger.When)
79+
}
80+
81+
func (ts tagSorter) Swap(i, j int) {
82+
[]*Tag(ts)[i], []*Tag(ts)[j] = []*Tag(ts)[j], []*Tag(ts)[i]
83+
}
84+
85+
// sortTagsByTime
86+
func sortTagsByTime(tags []*Tag) {
87+
sorter := tagSorter(tags)
88+
sort.Sort(sorter)
89+
}

0 commit comments

Comments
 (0)