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

Commit 7af86cf

Browse files
committed
plumbing: object/{commit,tag} add EncodeWithoutSignature, Implement #1116
Signed-off-by: Antoine GIRARD <[email protected]>
1 parent 85f49f4 commit 7af86cf

File tree

4 files changed

+61
-12
lines changed

4 files changed

+61
-12
lines changed

plumbing/object/commit.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,16 @@ func (b *Commit) Encode(o plumbing.EncodedObject) error {
233233
return b.encode(o, true)
234234
}
235235

236+
// PGPPayload export to a Reader the commit payload that is signed
237+
func (b *Commit) PGPPayload() (io.Reader, error) {
238+
encoded := &plumbing.MemoryObject{}
239+
// Encode commit components, excluding signature and get a reader object.
240+
if err := b.encode(encoded, false); err != nil {
241+
return nil, err
242+
}
243+
return encoded.Reader()
244+
}
245+
236246
func (b *Commit) encode(o plumbing.EncodedObject, includeSig bool) (err error) {
237247
o.SetType(plumbing.CommitObject)
238248
w, err := o.Writer()
@@ -345,12 +355,8 @@ func (c *Commit) Verify(armoredKeyRing string) (*openpgp.Entity, error) {
345355
// Extract signature.
346356
signature := strings.NewReader(c.PGPSignature)
347357

348-
encoded := &plumbing.MemoryObject{}
349358
// Encode commit components, excluding signature and get a reader object.
350-
if err := c.encode(encoded, false); err != nil {
351-
return nil, err
352-
}
353-
er, err := encoded.Reader()
359+
er, err := c.PGPPayload()
354360
if err != nil {
355361
return nil, err
356362
}

plumbing/object/commit_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import (
44
"bytes"
55
"context"
66
"io"
7+
"io/ioutil"
78
"strings"
89
"time"
910

11+
fixtures "gopkg.in/src-d/go-git-fixtures.v3"
1012
"gopkg.in/src-d/go-git.v4/plumbing"
1113
"gopkg.in/src-d/go-git.v4/plumbing/cache"
1214

1315
. "gopkg.in/check.v1"
14-
"gopkg.in/src-d/go-git-fixtures.v3"
1516
"gopkg.in/src-d/go-git.v4/storage/filesystem"
1617
)
1718

@@ -495,3 +496,20 @@ func (s *SuiteCommit) TestMalformedHeader(c *C) {
495496
err = decoded.Decode(encoded)
496497
c.Assert(err, IsNil)
497498
}
499+
500+
func (s *SuiteCommit) TestEncodeWithoutSignature(c *C) {
501+
//Similar to TestString since no signature
502+
er, err := s.Commit.EncodeWithoutSignature()
503+
c.Assert(err, IsNil)
504+
payload, err := ioutil.ReadAll(er)
505+
c.Assert(err, IsNil)
506+
507+
c.Assert(string(payload), Equals, ""+
508+
"tree eba74343e2f15d62adedfd8c883ee0262b5c8021\n"+
509+
"parent 35e85108805c84807bc66a02d91535e1e24b38b9\n"+
510+
"parent a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69\n"+
511+
"author Máximo Cuadros Ortiz <[email protected]> 1427802494 +0200\n"+
512+
"committer Máximo Cuadros Ortiz <[email protected]> 1427802494 +0200\n"+
513+
"\n"+
514+
"Merge branch 'master' of github.com:tyba/git-fixture\n")
515+
}

plumbing/object/tag.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,16 @@ func (t *Tag) Encode(o plumbing.EncodedObject) error {
169169
return t.encode(o, true)
170170
}
171171

172+
// EncodeWithoutSignature export to a Reader the tag payload that is signed
173+
func (t *Tag) PGPPayload() (io.Reader, error) {
174+
encoded := &plumbing.MemoryObject{}
175+
// Encode commit components, excluding signature and get a reader object.
176+
if err := t.encode(encoded, false); err != nil {
177+
return nil, err
178+
}
179+
return encoded.Reader()
180+
}
181+
172182
func (t *Tag) encode(o plumbing.EncodedObject, includeSig bool) (err error) {
173183
o.SetType(plumbing.TagObject)
174184
w, err := o.Writer()
@@ -287,12 +297,8 @@ func (t *Tag) Verify(armoredKeyRing string) (*openpgp.Entity, error) {
287297
// Extract signature.
288298
signature := strings.NewReader(t.PGPSignature)
289299

290-
encoded := &plumbing.MemoryObject{}
291300
// Encode tag components, excluding signature and get a reader object.
292-
if err := t.encode(encoded, false); err != nil {
293-
return nil, err
294-
}
295-
er, err := encoded.Reader()
301+
er, err := t.EncodeWithoutSignature()
296302
if err != nil {
297303
return nil, err
298304
}

plumbing/object/tag_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ package object
33
import (
44
"fmt"
55
"io"
6+
"io/ioutil"
67
"strings"
78
"time"
89

10+
fixtures "gopkg.in/src-d/go-git-fixtures.v3"
911
"gopkg.in/src-d/go-git.v4/plumbing"
1012
"gopkg.in/src-d/go-git.v4/plumbing/cache"
1113
"gopkg.in/src-d/go-git.v4/storage/filesystem"
1214
"gopkg.in/src-d/go-git.v4/storage/memory"
1315

1416
. "gopkg.in/check.v1"
15-
"gopkg.in/src-d/go-git-fixtures.v3"
1617
)
1718

1819
type TagSuite struct {
@@ -447,3 +448,21 @@ HdzbB2ak/HxIeCqmHVlmUqa+WfTMUJcsgOm3/ZFPCSoL6l0bz9Z1XVbiyD03
447448
_, err = tag.Verify(armoredKeyRing)
448449
c.Assert(err, IsNil)
449450
}
451+
452+
func (s *TagSuite) TestEncodeWithoutSignature(c *C) {
453+
//Similar to TestString since no signature
454+
tag := s.tag(c, plumbing.NewHash("b742a2a9fa0afcfa9a6fad080980fbc26b007c69"))
455+
er, err := tag.EncodeWithoutSignature()
456+
c.Assert(err, IsNil)
457+
payload, err := ioutil.ReadAll(er)
458+
c.Assert(err, IsNil)
459+
460+
c.Assert(string(payload), Equals, ""+
461+
"object f7b877701fbf855b44c0a9e86f3fdce2c298b07f\n"+
462+
"type commit\n"+
463+
"tag annotated-tag\n"+
464+
"tagger Máximo Cuadros <[email protected]> 1474485215 +0200\n"+
465+
"\n"+
466+
"example annotated tag\n",
467+
)
468+
}

0 commit comments

Comments
 (0)