aboutsummaryrefslogtreecommitdiffstats
path: root/modules/git/tag.go
blob: c97f574fa62b991c454831665ab3a2ce5a6b2edb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2015 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package git

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

// Tag represents a Git tag.
type Tag struct {
	Name    string
	ID      SHA1
	repo    *Repository
	Object  SHA1 // The id of this commit object
	Type    string
	Tagger  *Signature
	Message string
}

// Commit return the commit of the tag reference
func (tag *Tag) Commit() (*Commit, error) {
	return tag.repo.getCommit(tag.Object)
}

// Parse commit information from the (uncompressed) raw
// data from the commit object.
// \n\n separate headers from message
func parseTagData(data []byte) (*Tag, error) {
	tag := new(Tag)
	// we now have the contents of the commit object. Let's investigate...
	nextline := 0
l:
	for {
		eol := bytes.IndexByte(data[nextline:], '\n')
		switch {
		case eol > 0:
			line := data[nextline : nextline+eol]
			spacepos := bytes.IndexByte(line, ' ')
			reftype := line[:spacepos]
			switch string(reftype) {
			case "object":
				id, err := NewIDFromString(string(line[spacepos+1:]))
				if err != nil {
					return nil, err
				}
				tag.Object = id
			case "type":
				// A commit can have one or more parents
				tag.Type = string(line[spacepos+1:])
			case "tagger":
				sig, err := newSignatureFromCommitline(line[spacepos+1:])
				if err != nil {
					return nil, err
				}
				tag.Tagger = sig
			}
			nextline += eol + 1
		case eol == 0:
			tag.Message = strings.TrimRight(string(data[nextline+1:]), "\n")
			break l
		default:
			break 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)
}