Skip to content

Commit a04488c

Browse files
committed
plumbing: object/{commit,tag} add EncodeWithoutSignature, Implement src-d#1116
Signed-off-by: Antoine GIRARD <[email protected]>
1 parent 5c6d199 commit a04488c

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

plumbing/object/commit.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ func (b *Commit) Encode(o plumbing.EncodedObject) error {
235235
return b.encode(o, true)
236236
}
237237

238+
// EncodeWithoutSignature export a Commit into a plumbing.EncodedObject without the signature (correspond to the payload of the PGP signature).
239+
func (b *Commit) EncodeWithoutSignature(o plumbing.EncodedObject) error {
240+
return b.encode(o, false)
241+
}
242+
238243
func (b *Commit) encode(o plumbing.EncodedObject, includeSig bool) (err error) {
239244
o.SetType(plumbing.CommitObject)
240245
w, err := o.Writer()
@@ -349,7 +354,7 @@ func (c *Commit) Verify(armoredKeyRing string) (*openpgp.Entity, error) {
349354

350355
encoded := &plumbing.MemoryObject{}
351356
// Encode commit components, excluding signature and get a reader object.
352-
if err := c.encode(encoded, false); err != nil {
357+
if err := c.EncodeWithoutSignature(encoded); err != nil {
353358
return nil, err
354359
}
355360
er, err := encoded.Reader()

plumbing/object/commit_test.go

+22-1
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,23 @@ 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+
encoded := &plumbing.MemoryObject{}
503+
err := s.Commit.EncodeWithoutSignature(encoded)
504+
c.Assert(err, IsNil)
505+
er, err := encoded.Reader()
506+
c.Assert(err, IsNil)
507+
payload, err := ioutil.ReadAll(er)
508+
c.Assert(err, IsNil)
509+
510+
c.Assert(string(payload), Equals, ""+
511+
"tree eba74343e2f15d62adedfd8c883ee0262b5c8021\n"+
512+
"parent 35e85108805c84807bc66a02d91535e1e24b38b9\n"+
513+
"parent a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69\n"+
514+
"author Máximo Cuadros Ortiz <[email protected]> 1427802494 +0200\n"+
515+
"committer Máximo Cuadros Ortiz <[email protected]> 1427802494 +0200\n"+
516+
"\n"+
517+
"Merge branch 'master' of github.com:tyba/git-fixture\n")
518+
}

plumbing/object/tag.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ func (t *Tag) Encode(o plumbing.EncodedObject) error {
171171
return t.encode(o, true)
172172
}
173173

174+
// EncodeWithoutSignature export a Tag into a plumbing.EncodedObject without the signature (correspond to the payload of the PGP signature).
175+
func (t *Tag) EncodeWithoutSignature(o plumbing.EncodedObject) error {
176+
return t.encode(o, false)
177+
}
178+
174179
func (t *Tag) encode(o plumbing.EncodedObject, includeSig bool) (err error) {
175180
o.SetType(plumbing.TagObject)
176181
w, err := o.Writer()
@@ -291,7 +296,7 @@ func (t *Tag) Verify(armoredKeyRing string) (*openpgp.Entity, error) {
291296

292297
encoded := &plumbing.MemoryObject{}
293298
// Encode tag components, excluding signature and get a reader object.
294-
if err := t.encode(encoded, false); err != nil {
299+
if err := t.EncodeWithoutSignature(encoded); err != nil {
295300
return nil, err
296301
}
297302
er, err := encoded.Reader()

plumbing/object/tag_test.go

+23-1
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,24 @@ 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+
encoded := &plumbing.MemoryObject{}
455+
tag := s.tag(c, plumbing.NewHash("b742a2a9fa0afcfa9a6fad080980fbc26b007c69"))
456+
err := tag.EncodeWithoutSignature(encoded)
457+
c.Assert(err, IsNil)
458+
er, err := encoded.Reader()
459+
c.Assert(err, IsNil)
460+
payload, err := ioutil.ReadAll(er)
461+
c.Assert(err, IsNil)
462+
463+
c.Assert(string(payload), Equals, ""+
464+
"object f7b877701fbf855b44c0a9e86f3fdce2c298b07f\n"+
465+
"type commit\n"+
466+
"tag annotated-tag\n"+
467+
"tagger Máximo Cuadros <[email protected]> 1474485215 +0200\n"+
468+
"\n"+
469+
"example annotated tag\n",
470+
)
471+
}

0 commit comments

Comments
 (0)