aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2018-08-07 11:22:16 +0200
committerGitHub <noreply@github.com>2018-08-07 11:22:16 +0200
commit43d17e14b714665ab5bc2ecc220b6740779d733f (patch)
treef651b47abb65c9e8400e4058552cdb397eb14bbb
parentc58d68a56c0e1c0f0d544b6ef88a5ea0e2bdc60d (diff)
parent8df413fe09e6cb2069a76b6df6715d0e610c8458 (diff)
downloadgo-git-43d17e14b714665ab5bc2ecc220b6740779d733f.tar.gz
Merge pull request #892 from jfontan/fix/get-pgp-signature
plumbing/object: fix pgp signature encoder/decoder
-rw-r--r--plumbing/object/commit.go31
-rw-r--r--plumbing/object/commit_test.go32
2 files changed, 45 insertions, 18 deletions
diff --git a/plumbing/object/commit.go b/plumbing/object/commit.go
index 3ed85ba..b1c0e01 100644
--- a/plumbing/object/commit.go
+++ b/plumbing/object/commit.go
@@ -17,8 +17,9 @@ import (
)
const (
- beginpgp string = "-----BEGIN PGP SIGNATURE-----"
- endpgp string = "-----END PGP SIGNATURE-----"
+ beginpgp string = "-----BEGIN PGP SIGNATURE-----"
+ endpgp string = "-----END PGP SIGNATURE-----"
+ headerpgp string = "gpgsig"
)
// Hash represents the hash of an object
@@ -181,23 +182,13 @@ func (c *Commit) Decode(o plumbing.EncodedObject) (err error) {
}
if pgpsig {
- // Check if it's the end of a PGP signature.
- if bytes.Contains(line, []byte(endpgp)) {
- c.PGPSignature += endpgp + "\n"
- pgpsig = false
- } else {
- // Trim the left padding.
+ if len(line) > 0 && line[0] == ' ' {
line = bytes.TrimLeft(line, " ")
c.PGPSignature += string(line)
+ continue
+ } else {
+ pgpsig = false
}
- continue
- }
-
- // Check if it's the beginning of a PGP signature.
- if bytes.Contains(line, []byte(beginpgp)) {
- c.PGPSignature += beginpgp + "\n"
- pgpsig = true
- continue
}
if !message {
@@ -217,6 +208,9 @@ func (c *Commit) Decode(o plumbing.EncodedObject) (err error) {
c.Author.Decode(split[1])
case "committer":
c.Committer.Decode(split[1])
+ case headerpgp:
+ c.PGPSignature += string(split[1]) + "\n"
+ pgpsig = true
}
} else {
c.Message += string(line)
@@ -269,13 +263,14 @@ func (b *Commit) encode(o plumbing.EncodedObject, includeSig bool) (err error) {
}
if b.PGPSignature != "" && includeSig {
- if _, err = fmt.Fprint(w, "pgpsig"); err != nil {
+ if _, err = fmt.Fprint(w, "\n"+headerpgp); err != nil {
return err
}
// Split all the signature lines and write with a left padding and
// newline at the end.
- lines := strings.Split(b.PGPSignature, "\n")
+ signature := strings.TrimSuffix(b.PGPSignature, "\n")
+ lines := strings.Split(signature, "\n")
for _, line := range lines {
if _, err = fmt.Fprintf(w, " %s\n", line); err != nil {
return err
diff --git a/plumbing/object/commit_test.go b/plumbing/object/commit_test.go
index 996d481..b5dfbe3 100644
--- a/plumbing/object/commit_test.go
+++ b/plumbing/object/commit_test.go
@@ -324,6 +324,38 @@ RUysgqjcpT8+iQM1PblGfHR4XAhuOqN5Fx06PSaFZhqvWFezJ28/CLyX5q+oIVk=
err = decoded.Decode(encoded)
c.Assert(err, IsNil)
c.Assert(decoded.PGPSignature, Equals, pgpsignature)
+
+ // signature in author name
+
+ commit.PGPSignature = ""
+ commit.Author.Name = beginpgp
+ encoded = &plumbing.MemoryObject{}
+ decoded = &Commit{}
+
+ err = commit.Encode(encoded)
+ c.Assert(err, IsNil)
+
+ err = decoded.Decode(encoded)
+ c.Assert(err, IsNil)
+ c.Assert(decoded.PGPSignature, Equals, "")
+ c.Assert(decoded.Author.Name, Equals, beginpgp)
+
+ // broken signature
+
+ commit.PGPSignature = beginpgp + "\n" +
+ "some\n" +
+ "trash\n" +
+ endpgp +
+ "text\n"
+ encoded = &plumbing.MemoryObject{}
+ decoded = &Commit{}
+
+ err = commit.Encode(encoded)
+ c.Assert(err, IsNil)
+
+ err = decoded.Decode(encoded)
+ c.Assert(err, IsNil)
+ c.Assert(decoded.PGPSignature, Equals, commit.PGPSignature)
}
func (s *SuiteCommit) TestStat(c *C) {