Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit e1ce83f

Browse files
committed
plumbing: object/{commit,tag}, encode method with sig optional
Adds Commit.encode() and Tag.encode() with optional `includeSig` parameter to include or exclude signature from the encoded object.
1 parent e2dbd3a commit e1ce83f

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

plumbing/object/commit.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ func (c *Commit) Decode(o plumbing.EncodedObject) (err error) {
223223

224224
// Encode transforms a Commit into a plumbing.EncodedObject.
225225
func (b *Commit) Encode(o plumbing.EncodedObject) error {
226+
return b.encode(o, true)
227+
}
228+
229+
func (b *Commit) encode(o plumbing.EncodedObject, includeSig bool) error {
226230
o.SetType(plumbing.CommitObject)
227231
w, err := o.Writer()
228232
if err != nil {
@@ -257,7 +261,7 @@ func (b *Commit) Encode(o plumbing.EncodedObject) error {
257261
return err
258262
}
259263

260-
if b.PGPSignature != "" {
264+
if b.PGPSignature != "" && includeSig {
261265
if _, err = fmt.Fprint(w, "pgpsig"); err != nil {
262266
return err
263267
}
@@ -325,12 +329,9 @@ func (c *Commit) Verify(armoredKeyRing string) (*openpgp.Entity, error) {
325329
// Extract signature.
326330
signature := strings.NewReader(c.PGPSignature)
327331

328-
// Remove signature. Keep only the commit components.
329-
c.PGPSignature = ""
330-
331-
// Encode commit and get a reader object.
332332
encoded := &plumbing.MemoryObject{}
333-
if err := c.Encode(encoded); err != nil {
333+
// Encode commit components, excluding signature and get a reader object.
334+
if err := c.encode(encoded, false); err != nil {
334335
return nil, err
335336
}
336337
er, err := encoded.Reader()

plumbing/object/tag.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ func (t *Tag) Decode(o plumbing.EncodedObject) (err error) {
165165

166166
// Encode transforms a Tag into a plumbing.EncodedObject.
167167
func (t *Tag) Encode(o plumbing.EncodedObject) error {
168+
return t.encode(o, true)
169+
}
170+
171+
func (t *Tag) encode(o plumbing.EncodedObject, includeSig bool) error {
168172
o.SetType(plumbing.TagObject)
169173
w, err := o.Writer()
170174
if err != nil {
@@ -190,7 +194,7 @@ func (t *Tag) Encode(o plumbing.EncodedObject) error {
190194
return err
191195
}
192196

193-
if t.PGPSignature != "" {
197+
if t.PGPSignature != "" && includeSig {
194198
// Split all the signature lines and write with a newline at the end.
195199
lines := strings.Split(t.PGPSignature, "\n")
196200
for _, line := range lines {
@@ -281,11 +285,9 @@ func (t *Tag) Verify(armoredKeyRing string) (*openpgp.Entity, error) {
281285
// Extract signature.
282286
signature := strings.NewReader(t.PGPSignature)
283287

284-
// Remove signature. Keep only the tag components.
285-
t.PGPSignature = ""
286-
287288
encoded := &plumbing.MemoryObject{}
288-
if err := t.Encode(encoded); err != nil {
289+
// Encode tag components, excluding signature and get a reader object.
290+
if err := t.encode(encoded, false); err != nil {
289291
return nil, err
290292
}
291293
er, err := encoded.Reader()

0 commit comments

Comments
 (0)