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

Add get tags info method for releases #27

Merged
merged 2 commits into from
Jan 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions repo_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package git

import (
"strings"
"time"

"github.com/mcuadros/go-version"
)
Expand Down Expand Up @@ -94,6 +95,87 @@ 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
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
}

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")
Expand Down
25 changes: 24 additions & 1 deletion tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

package git

import "bytes"
import (
"bytes"
"sort"
)

// Tag represents a Git tag.
type Tag struct {
Expand Down Expand Up @@ -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)
}