-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.go
208 lines (178 loc) · 4.63 KB
/
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
206
207
208
package object
import (
"bytes"
"fmt"
"github.com/Nivl/git-go/ginternals"
"github.com/Nivl/git-go/internal/readutil"
)
// TagParams represents all the data needed to create a Tag
// Params starting by Opt are optionals
type TagParams struct {
Target *Object
Name string
Tagger Signature
Message string
OptGPGSig string
}
// Tag represents a Tag object
type Tag struct {
rawObject *Object
tagger Signature
tag string
message string
gpgSig string
id ginternals.Oid
target ginternals.Oid
typ Type
}
// NewTag creates a new Tag object
func NewTag(p *TagParams) *Tag {
return &Tag{
target: p.Target.ID(),
typ: p.Target.Type(),
tag: p.Name,
tagger: p.Tagger,
message: p.Message,
gpgSig: p.OptGPGSig,
}
}
// NewTagFromObject creates a new Tag from a raw git object
//
// A tag has following format:
//
// object {sha}
// type {target_object_type}
// tag {tag_name}
// tagger {author_name} <{author_email}> {author_date_seconds} {author_date_timezone}
// gpgsig -----BEGIN PGP SIGNATURE-----
// {gpg key over multiple lines}
// -----END PGP SIGNATURE-----
// {a blank line}
// {tag message}
//
// Note:
// - The gpgsig is optional
func NewTagFromObject(o *Object) (*Tag, error) {
if o.typ != TypeTag {
return nil, fmt.Errorf("type %s is not a tag: %w", o.typ, ErrObjectInvalid)
}
tag := &Tag{
id: o.ID(),
rawObject: o,
}
offset := 0
objData := o.Bytes()
var err error
for {
line := readutil.ReadTo(objData[offset:], '\n')
offset += len(line) + 1 // +1 to count the \n
// If we didn't find anything then something is wrong
if len(line) == 0 && offset == 1 {
return nil, fmt.Errorf("could not find tag first line: %w", ErrTagInvalid)
}
// if we got an empty line, it means everything from now to the end
// will be the tag message
if len(line) == 0 {
if offset < len(objData) {
tag.message = string(objData[offset:])
}
break
}
// Otherwise we're getting a key/value pair, separated by a space
kv := bytes.SplitN(line, []byte{' '}, 2)
switch string(kv[0]) {
case "object":
tag.target, err = ginternals.NewOidFromChars(kv[1])
if err != nil {
return nil, fmt.Errorf("could not parse target id %#v: %w", kv[1], err)
}
case "type":
tag.typ, err = NewTypeFromString(string(kv[1]))
if err != nil {
return nil, fmt.Errorf("invalid object type %s: %w", string(kv[1]), err)
}
case "tagger":
tag.tagger, err = NewSignatureFromBytes(kv[1])
if err != nil {
return nil, fmt.Errorf("could not parse tagger [%s]: %w", string(kv[1]), err)
}
case "tag":
tag.tag = string(kv[1])
case "gpgsig":
begin := string(kv[1]) + "\n"
end := "-----END PGP SIGNATURE-----"
i := bytes.Index(objData[offset:], []byte(end))
tag.gpgSig = begin + string(objData[offset:offset+i]) + end
offset += len(end) + i + 1 // +1 to count the \n
}
}
// validate the tag
if tag.tagger.IsZero() {
return nil, fmt.Errorf("tag has no tagger: %w", ErrTagInvalid)
}
if tag.target.IsZero() {
return nil, fmt.Errorf("tag has no target: %w", ErrTagInvalid)
}
if !tag.typ.IsValid() {
return nil, fmt.Errorf("tag has no type: %w", ErrTagInvalid)
}
return tag, nil
}
// ID returns the SHA of the tag object
func (t *Tag) ID() ginternals.Oid {
return t.id
}
// Target returns the ID of the object targeted by the tag
func (t *Tag) Target() ginternals.Oid {
return t.target
}
// Type returns the type of the targeted object
func (t *Tag) Type() Type {
return t.typ
}
// Name returns the tag's name
func (t *Tag) Name() string {
return t.tag
}
// Tagger returns the Signature of the person that created the tag
func (t *Tag) Tagger() Signature {
return t.tagger
}
// Message returns the tag's message
func (t *Tag) Message() string {
return t.message
}
// GPGSig returns the GPG signature of the tag, if any
func (t *Tag) GPGSig() string {
return t.gpgSig
}
// ToObject returns the underlying Object
func (t *Tag) ToObject() *Object {
if t.rawObject != nil {
return t.rawObject
}
// Quick reminder that the Write* methods on bytes.Buffer never fails,
// the error returned is always nil
buf := new(bytes.Buffer)
buf.WriteString("object ")
buf.WriteString(t.target.String())
buf.WriteByte('\n')
buf.WriteString("tag ")
buf.WriteString(t.Name())
buf.WriteByte('\n')
buf.WriteString("type ")
buf.WriteString(t.Type().String())
buf.WriteByte('\n')
buf.WriteString("tagger ")
buf.WriteString(t.Tagger().String())
buf.WriteByte('\n')
if t.gpgSig != "" {
buf.WriteString("gpgsig ")
buf.WriteString(t.gpgSig)
buf.WriteByte('\n')
}
buf.WriteByte('\n')
buf.WriteString(t.message)
t.rawObject = New(TypeTag, buf.Bytes())
return t.rawObject
}