Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite1bb0e2

Browse files
committed
git: worktree_commit, Add crypto.Signer option to CommitOptions.
This change adds a new crypto.Signer option to CommitOptions as analternative to SignKey to allow alternative commit signers to be used.This change byitself does not add other signing methods (e.g. ssh,x509, gitsign), but gives callers the ability to add their own.This roughly follows git's sign_buffer approach where go-git handles thecommit message body encoding, and hands off the encoded []byte to the signingimplementation for the signature to be returned.Signed-off-by: Billy Lynch <billy@chainguard.dev>
1 parenta6e934f commite1bb0e2

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

‎options.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package git
22

33
import (
4+
"crypto"
45
"errors"
56
"fmt"
67
"regexp"
@@ -507,6 +508,10 @@ type CommitOptions struct {
507508
// commit will not be signed. The private key must be present and already
508509
// decrypted.
509510
SignKey*openpgp.Entity
511+
// Signer denotes a cryptographic signer to sign the commit with.
512+
// A nil value here means the commit will not be signed.
513+
// Takes precedence over SignKey.
514+
Signer crypto.Signer
510515
// Amend will create a new commit object and replace the commit that HEAD currently
511516
// points to. Cannot be used with All nor Parents.
512517
Amendbool

‎worktree_commit.go‎

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package git
22

33
import (
44
"bytes"
5+
"crypto"
6+
"crypto/rand"
57
"errors"
8+
"io"
69
"path"
710
"sort"
811
"strings"
@@ -14,6 +17,7 @@ import (
1417
"github.com/go-git/go-git/v5/storage"
1518

1619
"github.com/ProtonMail/go-crypto/openpgp"
20+
"github.com/ProtonMail/go-crypto/openpgp/packet"
1721
"github.com/go-git/go-billy/v5"
1822
)
1923

@@ -125,12 +129,17 @@ func (w *Worktree) buildCommitObject(msg string, opts *CommitOptions, tree plumb
125129
ParentHashes:opts.Parents,
126130
}
127131

128-
ifopts.SignKey!=nil {
129-
sig,err:=w.buildCommitSignature(commit,opts.SignKey)
132+
// Convert SignKey into a Signer if set. Existing Signer should take priority.
133+
signer:=opts.Signer
134+
ifsigner==nil&&opts.SignKey!=nil {
135+
signer=&gpgSigner{key:opts.SignKey}
136+
}
137+
ifsigner!=nil {
138+
sig,err:=w.buildCommitSignature(commit,signer)
130139
iferr!=nil {
131140
returnplumbing.ZeroHash,err
132141
}
133-
commit.PGPSignature=sig
142+
commit.PGPSignature=string(sig)
134143
}
135144

136145
obj:=w.r.Storer.NewEncodedObject()
@@ -140,20 +149,44 @@ func (w *Worktree) buildCommitObject(msg string, opts *CommitOptions, tree plumb
140149
returnw.r.Storer.SetEncodedObject(obj)
141150
}
142151

143-
func (w*Worktree)buildCommitSignature(commit*object.Commit,signKey*openpgp.Entity) (string,error) {
152+
typegpgSignerstruct {
153+
key*openpgp.Entity
154+
}
155+
156+
func (s*gpgSigner)Public() crypto.PublicKey {
157+
returns.key.PrimaryKey
158+
}
159+
160+
func (s*gpgSigner)Sign(rand io.Reader,digest []byte,opts crypto.SignerOpts) ([]byte,error) {
161+
varcfg*packet.Config
162+
ifopts!=nil {
163+
cfg=&packet.Config{
164+
DefaultHash:opts.HashFunc(),
165+
}
166+
}
167+
168+
varb bytes.Buffer
169+
iferr:=openpgp.ArmoredDetachSign(&b,s.key,bytes.NewReader(digest),cfg);err!=nil {
170+
returnnil,err
171+
}
172+
returnb.Bytes(),nil
173+
}
174+
175+
func (w*Worktree)buildCommitSignature(commit*object.Commit,signer crypto.Signer) ([]byte,error) {
144176
encoded:=&plumbing.MemoryObject{}
145177
iferr:=commit.Encode(encoded);err!=nil {
146-
return"",err
178+
returnnil,err
147179
}
148180
r,err:=encoded.Reader()
149181
iferr!=nil {
150-
return"",err
182+
returnnil,err
151183
}
152-
varb bytes.Buffer
153-
iferr:=openpgp.ArmoredDetachSign(&b,signKey,r,nil);err!=nil {
154-
return"",err
184+
b,err:=io.ReadAll(r)
185+
iferr!=nil {
186+
returnnil,err
155187
}
156-
returnb.String(),nil
188+
189+
returnsigner.Sign(rand.Reader,b,nil)
157190
}
158191

159192
// buildTreeHelper converts a given index.Index file into multiple git objects

‎worktree_commit_test.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ func (s *WorktreeSuite) TestCommitAmend(c *C) {
131131
_,err=w.Commit("foo\n",&CommitOptions{Author:defaultSignature()})
132132
c.Assert(err,IsNil)
133133

134-
135134
amendedHash,err:=w.Commit("bar\n",&CommitOptions{Amend:true})
136135
c.Assert(err,IsNil)
137136

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp