From 9ab186ba7d8844b24fbae5716b64b58acf9d7d7a Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 4 Jan 2017 17:16:09 +0800 Subject: [PATCH 1/2] add get tags info method for releases --- repo_tag.go | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tag.go | 25 ++++++++++++++++- 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/repo_tag.go b/repo_tag.go index dfaa41f0c..8de89d94b 100644 --- a/repo_tag.go +++ b/repo_tag.go @@ -6,6 +6,7 @@ package git import ( "strings" + "time" "github.com/mcuadros/go-version" ) @@ -94,6 +95,85 @@ func (repo *Repository) GetTag(name string) (*Tag, error) { return tag, nil } +// TagOption describes tag options +type TagOption struct { +} + +// parseTag parse the line +// 2016-10-14 20:54:25 +0200 (tag: translation/20161014.01) d3b76dcf2 Dirk Baeumer dirkb@microsoft.com Merge in translations +func parseTag(line string, opt TagOption) (*Tag, error) { + line = strings.TrimSpace(line) + if len(line) < 40 { + return nil, nil + } + + var err error + var tag Tag + var sig Signature + sig.When, err = time.Parse("2006-01-02 15:04:05 -0700", line[0:25]) + if err != nil { + return nil, err + } + + left := strings.TrimSpace(line[25:]) + start := strings.Index(left, "(tag: ") + if start < 0 { + return nil, nil + } + end := strings.IndexByte(left[start+1:], ')') + if end < 0 { + return nil, nil + } + end = end + start + 1 + part := strings.IndexByte(left[start+6:end], ',') + if part > 0 { + tag.Name = strings.TrimSpace(left[start+6 : start+6+part]) + } else { + tag.Name = strings.TrimSpace(left[start+6 : end]) + } + next := strings.IndexByte(left[end+2:], ' ') + if next < 0 { + return nil, nil + } + tag.Object = MustIDFromString(strings.TrimSpace(left[end+2 : end+2+next])) + next = end + 2 + next + + emailStart := strings.IndexByte(left[next:], '<') + sig.Name = strings.TrimSpace(left[next:][:emailStart-1]) + emailEnd := strings.IndexByte(left[next:], '>') + sig.Email = strings.TrimSpace(left[next:][emailStart+1 : emailEnd]) + tag.Tagger = &sig + tag.Message = strings.TrimSpace(left[next+emailEnd+1:]) + return &tag, nil +} + +// GetTagInfos returns all tag infos of the repository. +func (repo *Repository) GetTagInfos(opt TagOption) ([]*Tag, error) { + cmd := NewCommand("log", "--tags", "--simplify-by-decoration", `--pretty=format:"%ci %d %H %cn<%ce> %s"`) + stdout, err := cmd.RunInDir(repo.Path) + if err != nil { + return nil, err + } + + tagSlices := strings.Split(stdout, "\n") + var tags []*Tag + for _, line := range tagSlices { + line := strings.Trim(line, `"`) + tag, err := parseTag(line, opt) + if err != nil { + return nil, err + } + if tag != nil { + tag.repo = repo + tags = append(tags, tag) + } + } + + sortTagsByTime(tags) + + return tags, nil +} + // GetTags returns all tags of the repository. func (repo *Repository) GetTags() ([]string, error) { cmd := NewCommand("tag", "-l") diff --git a/tag.go b/tag.go index f2a3d31d2..500fd2749 100644 --- a/tag.go +++ b/tag.go @@ -4,7 +4,10 @@ package git -import "bytes" +import ( + "bytes" + "sort" +) // Tag represents a Git tag. type Tag struct { @@ -64,3 +67,23 @@ l: } return tag, nil } + +type tagSorter []*Tag + +func (ts tagSorter) Len() int { + return len([]*Tag(ts)) +} + +func (ts tagSorter) Less(i, j int) bool { + return []*Tag(ts)[i].Tagger.When.After([]*Tag(ts)[j].Tagger.When) +} + +func (ts tagSorter) Swap(i, j int) { + []*Tag(ts)[i], []*Tag(ts)[j] = []*Tag(ts)[j], []*Tag(ts)[i] +} + +// sortTagsByTime +func sortTagsByTime(tags []*Tag) { + sorter := tagSorter(tags) + sort.Sort(sorter) +} From d51d47f0ea9ebd07a606735d39270750e1512eaf Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 4 Jan 2017 21:06:43 +0800 Subject: [PATCH 2/2] composite variable decleare --- repo_tag.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/repo_tag.go b/repo_tag.go index 8de89d94b..33a833d3a 100644 --- a/repo_tag.go +++ b/repo_tag.go @@ -107,9 +107,11 @@ func parseTag(line string, opt TagOption) (*Tag, error) { return nil, nil } - var err error - var tag Tag - var sig Signature + var ( + err error + tag Tag + sig Signature + ) sig.When, err = time.Parse("2006-01-02 15:04:05 -0700", line[0:25]) if err != nil { return nil, err