Debian Codegraph @

plumbing/object/tag.go

raw
1 package object
2
3 import (
4 "errors"
5 "fmt"
6 "strings"
7
8 "github.com/ProtonMail/go-crypto/openpgp"
9 "github.com/go-git/go-git/v5/plumbing"
10 "github.com/go-git/go-git/v5/plumbing/storer"
11 "github.com/go-git/go-git/v5/utils/ioutil"
12 "github.com/go-git/go-git/v5/utils/sync"
13 )
14
15 // ErrMalformedTag is returned when a tag object cannot be decoded because
16 // its required headers (object, type, tag) are missing or out of order.
17 var ErrMalformedTag = errors.New("malformed tag")
18
19 // Tag represents an annotated tag object. It points to a single git object of
20 // any type, but tags typically are applied to commit or blob objects. It
21 // provides a reference that associates the target with a tag name. It also
22 // contains meta-information about the tag, including the tagger, tag date and
23 // message.
24 //
25 // Note that this is not used for lightweight tags.
26 //
27 // https://git-scm.com/book/en/v2/Git-Internals-Git-References#Tags
28 type Tag struct {
29 // Hash of the tag.
30 Hash plumbing.Hash
31 // Name of the tag.
32 Name string
33 // Tagger is the one who created the tag.
34 Tagger Signature
35 // Message is an arbitrary text message.
36 Message string
37 // PGPSignature is the PGP signature of the tag.
38 PGPSignature string
39 // TargetType is the object type of the target.
40 TargetType plumbing.ObjectType
41 // Target is the hash of the target object.
42 Target plumbing.Hash
43
44 s storer.EncodedObjectStorer
45 // src holds the encoded object this Tag was decoded from, used by
46 // EncodeWithoutSignature to recover the canonical signed bytes.
47 src plumbing.EncodedObject
48 }
49
50 // GetTag gets a tag from an object storer and decodes it.
51 func GetTag(s storer.EncodedObjectStorer, h plumbing.Hash) (*Tag, error) {
52 o, err := s.EncodedObject(plumbing.TagObject, h)
53 if err != nil {
54 return nil, err
55 }
56
57 return DecodeTag(s, o)
58 }
59
60 // DecodeTag decodes an encoded object into a *Commit and associates it to the
61 // given object storer.
62 func DecodeTag(s storer.EncodedObjectStorer, o plumbing.EncodedObject) (*Tag, error) {
63 t := &Tag{s: s}
64 if err := t.Decode(o); err != nil {
65 return nil, err
66 }
67
68 return t, nil
69 }
70
71 // ID returns the object ID of the tag, not the object that the tag references.
72 // The returned value will always match the current value of Tag.Hash.
73 //
74 // ID is present to fulfill the Object interface.
75 func (t *Tag) ID() plumbing.Hash {
76 return t.Hash
77 }
78
79 // Type returns the type of object. It always returns plumbing.TagObject.
80 //
81 // Type is present to fulfill the Object interface.
82 func (t *Tag) Type() plumbing.ObjectType {
83 return plumbing.TagObject
84 }
85
86 func (t *Tag) reset() {
87 storer := t.s
88 *t = Tag{s: storer}
89 }
90
91 // Decode transforms a plumbing.EncodedObject into a Tag struct.
92 func (t *Tag) Decode(o plumbing.EncodedObject) (err error) {
93 if o.Type() != plumbing.TagObject {
94 return ErrUnsupportedObject
95 }
96
97 t.reset()
98 t.Hash = o.Hash()
99 t.src = o
100
101 reader, err := o.Reader()
102 if err != nil {
103 return err
104 }
105 defer ioutil.CheckClose(reader, &err)
106
107 r := sync.GetBufioReader(reader)
108 defer sync.PutBufioReader(r)
109
110 scanner := &tagScanner{r: r, t: t}
111 for state := scanTagObject; state != nil; {
112 state, err = state(scanner)
113 if err != nil {
114 return err
115 }
116 }
117
118 data := scanner.msgbuf.Bytes()
119 if sm, _ := parseSignedBytes(data); sm >= 0 {
120 t.PGPSignature = string(data[sm:])
121 data = data[:sm]
122 }
123 t.Message = string(data)
124
125 return nil
126 }
127
128 // Encode transforms a Tag into a plumbing.EncodedObject.
129 func (t *Tag) Encode(o plumbing.EncodedObject) error {
130 return t.encode(o, true)
131 }
132
133 // EncodeWithoutSignature exports a Tag into a plumbing.EncodedObject without
134 // any signature data, producing the payload that PGP/GPG signatures are
135 // computed over.
136 //
137 // Behaviour mirrors Commit.EncodeWithoutSignature:
138 //
139 // - For Tags populated by Decode whose exported fields still match the
140 // source object, the payload is streamed from the raw source bytes with
141 // the inline trailing signature truncated and gpgsig/gpgsig-sha256
142 // headers (and their continuation lines) stripped verbatim. This
143 // preserves the exact bytes the signature was computed over, regardless
144 // of any normalization performed by Decode.
145 //
146 // - For Tags constructed in memory, or for decoded Tags whose exported
147 // fields have been mutated, the payload is derived from the current
148 // struct fields. Mutation is detected by re-decoding the source object
149 // and comparing exported fields; if any differ, the in-memory
150 // representation prevails.
151 func (t *Tag) EncodeWithoutSignature(o plumbing.EncodedObject) error {
152 if t.matchesSource() {
153 return stripObjectSignatures(o, t.src, plumbing.TagObject)
154 }
155 return t.encode(o, false)
156 }
157
158 // matchesSource reports whether t.src is set and re-decoding it produces a
159 // Tag whose payload-affecting exported fields are identical to those of t.
160 //
161 // PGPSignature is intentionally excluded from the comparison: neither path
162 // emits it as part of the verification payload, so mutating it must not
163 // trigger a switch to struct-encode (which would change the byte layout the
164 // caller is trying to verify against).
165 func (t *Tag) matchesSource() bool {
166 if t.src == nil {
167 return false
168 }
169 fresh := &Tag{}
170 if err := fresh.Decode(t.src); err != nil {
171 return false
172 }
173 return t.Hash == fresh.Hash &&
174 t.Name == fresh.Name &&
175 signatureEqual(t.Tagger, fresh.Tagger) &&
176 t.Message == fresh.Message &&
177 t.TargetType == fresh.TargetType &&
178 t.Target == fresh.Target
179 }
180
181 func (t *Tag) encode(o plumbing.EncodedObject, includeSig bool) (err error) {
182 o.SetType(plumbing.TagObject)
183 w, err := o.Writer()
184 if err != nil {
185 return err
186 }
187 defer ioutil.CheckClose(w, &err)
188
189 if _, err = fmt.Fprintf(w,
190 "object %s\ntype %s\ntag %s\n",
191 t.Target.String(), t.TargetType.Bytes(), t.Name); err != nil {
192 return err
193 }
194
195 if !isZeroSignature(t.Tagger) {
196 if _, err = fmt.Fprint(w, "tagger "); err != nil {
197 return err
198 }
199
200 if err = t.Tagger.Encode(w); err != nil {
201 return err
202 }
203
204 if _, err = fmt.Fprint(w, "\n"); err != nil {
205 return err
206 }
207 }
208
209 if _, err = fmt.Fprint(w, "\n"); err != nil {
210 return err
211 }
212
213 if _, err = fmt.Fprint(w, t.Message); err != nil {
214 return err
215 }
216
217 // Note that this is highly sensitive to what is sent along in the
218 // message. Message *always* needs to end with a newline, or else the
219 // message and the trailing signature will be concatenated into a
220 // corrupt object. Since this is a lower-level method, we assume you
221 // know what you are doing and have already done the needful on the
222 // message in the caller.
223 if includeSig {
224 if _, err = fmt.Fprint(w, t.PGPSignature); err != nil {
225 return err
226 }
227 }
228
229 return err
230 }
231
232 func isZeroSignature(s Signature) bool {
233 return s.Name == "" && s.Email == "" && s.When.IsZero()
234 }
235
236 // Commit returns the commit pointed to by the tag. If the tag points to a
237 // different type of object ErrUnsupportedObject will be returned.
238 func (t *Tag) Commit() (*Commit, error) {
239 if t.TargetType != plumbing.CommitObject {
240 return nil, ErrUnsupportedObject
241 }
242
243 o, err := t.s.EncodedObject(plumbing.CommitObject, t.Target)
244 if err != nil {
245 return nil, err
246 }
247
248 return DecodeCommit(t.s, o)
249 }
250
251 // Tree returns the tree pointed to by the tag. If the tag points to a commit
252 // object the tree of that commit will be returned. If the tag does not point
253 // to a commit or tree object ErrUnsupportedObject will be returned.
254 func (t *Tag) Tree() (*Tree, error) {
255 switch t.TargetType {
256 case plumbing.CommitObject:
257 c, err := t.Commit()
258 if err != nil {
259 return nil, err
260 }
261
262 return c.Tree()
263 case plumbing.TreeObject:
264 return GetTree(t.s, t.Target)
265 default:
266 return nil, ErrUnsupportedObject
267 }
268 }
269
270 // Blob returns the blob pointed to by the tag. If the tag points to a
271 // different type of object ErrUnsupportedObject will be returned.
272 func (t *Tag) Blob() (*Blob, error) {
273 if t.TargetType != plumbing.BlobObject {
274 return nil, ErrUnsupportedObject
275 }
276
277 return GetBlob(t.s, t.Target)
278 }
279
280 // Object returns the object pointed to by the tag.
281 func (t *Tag) Object() (Object, error) {
282 o, err := t.s.EncodedObject(t.TargetType, t.Target)
283 if err != nil {
284 return nil, err
285 }
286
287 return DecodeObject(t.s, o)
288 }
289
290 // String returns the meta information contained in the tag as a formatted
291 // string.
292 func (t *Tag) String() string {
293 obj, _ := t.Object()
294
295 return fmt.Sprintf(
296 "%s %s\nTagger: %s\nDate: %s\n\n%s\n%s",
297 plumbing.TagObject, t.Name, t.Tagger.String(), t.Tagger.When.Format(DateFormat),
298 t.Message, objectAsString(obj),
299 )
300 }
301
302 // Verify performs PGP verification of the tag with a provided armored
303 // keyring and returns openpgp.Entity associated with verifying key on
304 // success.
305 func (t *Tag) Verify(armoredKeyRing string) (*openpgp.Entity, error) {
306 keyRingReader := strings.NewReader(armoredKeyRing)
307 keyring, err := openpgp.ReadArmoredKeyRing(keyRingReader)
308 if err != nil {
309 return nil, err
310 }
311
312 // Extract signature.
313 signature := strings.NewReader(t.PGPSignature)
314
315 encoded := &plumbing.MemoryObject{}
316 // Encode tag components, excluding signature and get a reader object.
317 if err := t.EncodeWithoutSignature(encoded); err != nil {
318 return nil, err
319 }
320 er, err := encoded.Reader()
321 if err != nil {
322 return nil, err
323 }
324
325 return openpgp.CheckArmoredDetachedSignature(keyring, er, signature, nil)
326 }
327
328 // TagIter provides an iterator for a set of tags.
329 type TagIter struct {
330 storer.EncodedObjectIter
331 s storer.EncodedObjectStorer
332 }
333
334 // NewTagIter takes a storer.EncodedObjectStorer and a
335 // storer.EncodedObjectIter and returns a *TagIter that iterates over all
336 // tags contained in the storer.EncodedObjectIter.
337 //
338 // Any non-tag object returned by the storer.EncodedObjectIter is skipped.
339 func NewTagIter(s storer.EncodedObjectStorer, iter storer.EncodedObjectIter) *TagIter {
340 return &TagIter{iter, s}
341 }
342
343 // Next moves the iterator to the next tag and returns a pointer to it. If
344 // there are no more tags, it returns io.EOF.
345 func (iter *TagIter) Next() (*Tag, error) {
346 obj, err := iter.EncodedObjectIter.Next()
347 if err != nil {
348 return nil, err
349 }
350
351 return DecodeTag(iter.s, obj)
352 }
353
354 // ForEach call the cb function for each tag contained on this iter until
355 // an error happens or the end of the iter is reached. If ErrStop is sent
356 // the iteration is stop but no error is returned. The iterator is closed.
357 func (iter *TagIter) ForEach(cb func(*Tag) error) error {
358 return iter.EncodedObjectIter.ForEach(func(obj plumbing.EncodedObject) error {
359 t, err := DecodeTag(iter.s, obj)
360 if err != nil {
361 return err
362 }
363
364 return cb(t)
365 })
366 }
367
368 func objectAsString(obj Object) string {
369 switch o := obj.(type) {
370 case *Commit:
371 return o.String()
372 default:
373 return ""
374 }
375 }