Skip to content

Commit 531f4dc

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

File tree

4 files changed

+61
-12
lines changed

4 files changed

+61
-12
lines changed

plumbing/object/commit.go

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

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

350-
encoded := &plumbing.MemoryObject{}
351360
// Encode commit components, excluding signature and get a reader object.
352-
if err := c.encode(encoded, false); err != nil {
353-
return nil, err
354-
}
355-
er, err := encoded.Reader()
361+
er, err := c.EncodeWithoutSignature()
356362
if err != nil {
357363
return nil, err
358364
}

plumbing/object/commit_test.go

+19-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,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

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

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

292-
encoded := &plumbing.MemoryObject{}
293302
// Encode tag components, excluding signature and get a reader object.
294-
if err := t.encode(encoded, false); err != nil {
295-
return nil, err
296-
}
297-
er, err := encoded.Reader()
303+
er, err := t.EncodeWithoutSignature()
298304
if err != nil {
299305
return nil, err
300306
}

plumbing/object/tag_test.go

+20-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,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)