@@ -12,6 +12,11 @@ import (
12
12
"gopkg.in/src-d/go-git.v4/utils/ioutil"
13
13
)
14
14
15
+ const (
16
+ beginpgp string = "-----BEGIN PGP SIGNATURE-----"
17
+ endpgp string = "-----END PGP SIGNATURE-----"
18
+ )
19
+
15
20
// Hash represents the hash of an object
16
21
type Hash plumbing.Hash
17
22
@@ -28,6 +33,8 @@ type Commit struct {
28
33
// Committer is the one performing the commit, might be different from
29
34
// Author.
30
35
Committer Signature
36
+ // GPGSignature is the GPG signature of the commit.
37
+ GPGSignature string
31
38
// Message is the commit message, contains arbitrary text.
32
39
Message string
33
40
// TreeHash is the hash of the root tree of the commit.
@@ -145,12 +152,33 @@ func (c *Commit) Decode(o plumbing.EncodedObject) (err error) {
145
152
r := bufio .NewReader (reader )
146
153
147
154
var message bool
155
+ var gpgsig bool
148
156
for {
149
157
line , err := r .ReadBytes ('\n' )
150
158
if err != nil && err != io .EOF {
151
159
return err
152
160
}
153
161
162
+ if gpgsig {
163
+ // Check if it's the end of a PGP signature.
164
+ if bytes .Contains (line , []byte (endpgp )) {
165
+ c .GPGSignature += string (endpgp ) + "\n "
166
+ gpgsig = false
167
+ } else {
168
+ // Trim the left padding.
169
+ line = bytes .TrimLeft (line , " " )
170
+ c .GPGSignature += string (line )
171
+ }
172
+ continue
173
+ }
174
+
175
+ // Check if it's the beginning of a PGP signature.
176
+ if bytes .Contains (line , []byte (beginpgp )) {
177
+ c .GPGSignature += string (beginpgp ) + "\n "
178
+ gpgsig = true
179
+ continue
180
+ }
181
+
154
182
if ! message {
155
183
line = bytes .TrimSpace (line )
156
184
if len (line ) == 0 {
@@ -215,6 +243,21 @@ func (b *Commit) Encode(o plumbing.EncodedObject) error {
215
243
return err
216
244
}
217
245
246
+ if b .GPGSignature != "" {
247
+ if _ , err = fmt .Fprint (w , "gpgsig" ); err != nil {
248
+ return err
249
+ }
250
+
251
+ // Split all the signature lines and write with a left padding and
252
+ // newline at the end.
253
+ lines := strings .Split (b .GPGSignature , "\n " )
254
+ for _ , line := range lines {
255
+ if _ , err = fmt .Fprintf (w , " %s\n " , line ); err != nil {
256
+ return err
257
+ }
258
+ }
259
+ }
260
+
218
261
if _ , err = fmt .Fprintf (w , "\n \n %s" , b .Message ); err != nil {
219
262
return err
220
263
}
0 commit comments