-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo_tag.go
205 lines (172 loc) · 4.19 KB
/
repo_tag.go
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package git
import (
"fmt"
"strings"
"github.com/mcuadros/go-version"
)
const TAG_PREFIX = "refs/tags/"
// IsTagExist returns true if given tag exists in the repository.
func IsTagExist(repoPath, name string) bool {
return IsReferenceExist(repoPath, TAG_PREFIX+name)
}
func (repo *Repository) IsTagExist(name string) bool {
return IsTagExist(repo.Path, name)
}
func (repo *Repository) CreateTag(name, revision string) error {
_, err := NewCommand("tag", name, revision).RunInDir(repo.Path)
return err
}
func (repo *Repository) getTag(id sha1) (*Tag, error) {
t, ok := repo.tagCache.Get(id.String())
if ok {
log("Hit cache: %s", id)
return t.(*Tag), nil
}
// Get tag type
tp, err := NewCommand("cat-file", "-t", id.String()).RunInDir(repo.Path)
if err != nil {
return nil, err
}
tp = strings.TrimSpace(tp)
// Tag is a commit.
if ObjectType(tp) == OBJECT_COMMIT {
tag := &Tag{
ID: id,
Object: id,
Type: string(OBJECT_COMMIT),
repo: repo,
}
repo.tagCache.Set(id.String(), tag)
return tag, nil
}
// Tag with message.
data, err := NewCommand("cat-file", "-p", id.String()).RunInDirBytes(repo.Path)
if err != nil {
return nil, err
}
tag, err := parseTagData(data)
if err != nil {
return nil, err
}
tag.ID = id
tag.repo = repo
repo.tagCache.Set(id.String(), tag)
return tag, nil
}
// GetTag returns a Git tag by given name.
func (repo *Repository) GetTag(name string) (*Tag, error) {
stdout, err := NewCommand("show-ref", "--tags", name).RunInDir(repo.Path)
if err != nil {
return nil, err
}
id, err := NewIDFromString(strings.Split(stdout, " ")[0])
if err != nil {
return nil, err
}
tag, err := repo.getTag(id)
if err != nil {
return nil, err
}
tag.Name = name
return tag, nil
}
// GetTags returns all tags of the repository.
func (repo *Repository) GetTags() ([]string, error) {
cmd := NewCommand("tag", "-l")
if version.Compare(gitVersion, "2.4.9", ">=") {
cmd.AddArguments("--sort=-v:taggerdate")
}
stdout, err := cmd.RunInDir(repo.Path)
if err != nil {
return nil, err
}
tags := strings.Split(stdout, "\n")
tags = tags[:len(tags)-1]
if version.Compare(gitVersion, "2.4.9", "<") {
version.Sort(tags)
// Reverse order
for i := 0; i < len(tags)/2; i++ {
j := len(tags) - i - 1
tags[i], tags[j] = tags[j], tags[i]
}
}
return tags, nil
}
type TagsResult struct {
// Indicates whether results include the latest tag.
HasLatest bool
// If results do not include the latest tag, a indicator 'after' to go back.
PreviousAfter string
// Indicates whether results include the oldest tag.
ReachEnd bool
// List of returned tags.
Tags []string
}
// GetTagsAfter returns list of tags 'after' (exlusive) given tag.
func (repo *Repository) GetTagsAfter(after string, limit int) (*TagsResult, error) {
allTags, err := repo.GetTags()
if err != nil {
return nil, fmt.Errorf("GetTags: %v", err)
}
if limit < 0 {
limit = 0
}
numAllTags := len(allTags)
if len(after) == 0 && limit == 0 {
return &TagsResult{
HasLatest: true,
ReachEnd: true,
Tags: allTags,
}, nil
} else if len(after) == 0 && limit > 0 {
endIdx := limit
if limit >= numAllTags {
endIdx = numAllTags
}
return &TagsResult{
HasLatest: true,
ReachEnd: limit >= numAllTags,
Tags: allTags[:endIdx],
}, nil
}
previousAfter := ""
hasMatch := false
tags := make([]string, 0, len(allTags))
for i := range allTags {
if hasMatch {
tags = allTags[i:]
break
}
if allTags[i] == after {
hasMatch = true
if limit > 0 && i-limit >= 0 {
previousAfter = allTags[i-limit]
}
continue
}
}
if !hasMatch {
tags = allTags
}
// If all tags after match is equal to the limit, it reaches the oldest tag as well.
if limit == 0 || len(tags) <= limit {
return &TagsResult{
HasLatest: !hasMatch,
PreviousAfter: previousAfter,
ReachEnd: true,
Tags: tags,
}, nil
}
return &TagsResult{
HasLatest: !hasMatch,
PreviousAfter: previousAfter,
Tags: tags[:limit],
}, nil
}
// DeleteTag deletes a tag from the repository
func (repo *Repository) DeleteTag(name string) error {
cmd := NewCommand("tag", "-d")
cmd.AddArguments(name)
_, err := cmd.RunInDir(repo.Path)
return err
}