@@ -17,8 +17,9 @@ import (
1717)
1818
1919const (
20- beginpgp string = "-----BEGIN PGP SIGNATURE-----"
21- endpgp string = "-----END PGP SIGNATURE-----"
20+ beginpgp string = "-----BEGIN PGP SIGNATURE-----"
21+ endpgp string = "-----END PGP SIGNATURE-----"
22+ headerpgp string = "gpgsig"
2223)
2324
2425// Hash represents the hash of an object
@@ -181,23 +182,13 @@ func (c *Commit) Decode(o plumbing.EncodedObject) (err error) {
181182 }
182183
183184 if pgpsig {
184- // Check if it's the end of a PGP signature.
185- if bytes .Contains (line , []byte (endpgp )) {
186- c .PGPSignature += endpgp + "\n "
187- pgpsig = false
188- } else {
189- // Trim the left padding.
185+ if len (line ) > 0 && line [0 ] == ' ' {
190186 line = bytes .TrimLeft (line , " " )
191187 c .PGPSignature += string (line )
188+ continue
189+ } else {
190+ pgpsig = false
192191 }
193- continue
194- }
195-
196- // Check if it's the beginning of a PGP signature.
197- if bytes .Contains (line , []byte (beginpgp )) {
198- c .PGPSignature += beginpgp + "\n "
199- pgpsig = true
200- continue
201192 }
202193
203194 if ! message {
@@ -217,6 +208,9 @@ func (c *Commit) Decode(o plumbing.EncodedObject) (err error) {
217208 c .Author .Decode (split [1 ])
218209 case "committer" :
219210 c .Committer .Decode (split [1 ])
211+ case headerpgp :
212+ c .PGPSignature += string (split [1 ]) + "\n "
213+ pgpsig = true
220214 }
221215 } else {
222216 c .Message += string (line )
@@ -269,13 +263,14 @@ func (b *Commit) encode(o plumbing.EncodedObject, includeSig bool) (err error) {
269263 }
270264
271265 if b .PGPSignature != "" && includeSig {
272- if _ , err = fmt .Fprint (w , "pgpsig" ); err != nil {
266+ if _ , err = fmt .Fprint (w , "\n " + headerpgp ); err != nil {
273267 return err
274268 }
275269
276270 // Split all the signature lines and write with a left padding and
277271 // newline at the end.
278- lines := strings .Split (b .PGPSignature , "\n " )
272+ signature := strings .TrimSuffix (b .PGPSignature , "\n " )
273+ lines := strings .Split (signature , "\n " )
279274 for _ , line := range lines {
280275 if _ , err = fmt .Fprintf (w , " %s\n " , line ); err != nil {
281276 return err
0 commit comments