Add support for creating signed commits and signing commits during a rebase #626
|
@ -51,11 +51,11 @@ func (c *Commit) ContentToSign() string {
|
||||||
return c.RawHeader() + "\n" + c.RawMessage()
|
return c.RawHeader() + "\n" + c.RawMessage()
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommitSigningCb defines a function type that takes some data to sign and returns (signature, signature_field, error)
|
// CommitSigningCallback defines a function type that takes some data to sign and returns (signature, signature_field, error)
|
||||||
type CommitSigningCb func(string) (string, string, error)
|
type CommitSigningCallback func(string) (signature, signatureField string, err error)
|
||||||
|
|
||||||
// WithSignatureUsing creates a new signed commit from this one using the given signing callback
|
// WithSignatureUsing creates a new signed commit from this one using the given signing callback
|
||||||
func (c *Commit) WithSignatureUsing(f CommitSigningCb) (*Oid, error) {
|
func (c *Commit) WithSignatureUsing(f CommitSigningCallback) (*Oid, error) {
|
||||||
signature, signatureField, err := f(c.ContentToSign())
|
signature, signatureField, err := f(c.ContentToSign())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -47,7 +47,7 @@ func createBareTestRepo(t *testing.T) *Repository {
|
||||||
|
|
||||||
// commitOpts contains any extra options for creating commits in the seed repo
|
// commitOpts contains any extra options for creating commits in the seed repo
|
||||||
type commitOpts struct {
|
type commitOpts struct {
|
||||||
CommitSigningCb
|
CommitSigningCallback
|
||||||
}
|
}
|
||||||
|
|
||||||
func seedTestRepo(t *testing.T, repo *Repository) (*Oid, *Oid) {
|
func seedTestRepo(t *testing.T, repo *Repository) (*Oid, *Oid) {
|
||||||
|
@ -78,11 +78,11 @@ func seedTestRepoOpt(t *testing.T, repo *Repository, opts commitOpts) (*Oid, *Oi
|
||||||
commitId, err := repo.CreateCommit("HEAD", sig, sig, message, tree)
|
commitId, err := repo.CreateCommit("HEAD", sig, sig, message, tree)
|
||||||
checkFatal(t, err)
|
checkFatal(t, err)
|
||||||
|
|
||||||
if opts.CommitSigningCb != nil {
|
if opts.CommitSigningCallback != nil {
|
||||||
commit, err := repo.LookupCommit(commitId)
|
commit, err := repo.LookupCommit(commitId)
|
||||||
checkFatal(t, err)
|
checkFatal(t, err)
|
||||||
|
|
||||||
signature, signatureField, err := opts.CommitSigningCb(commit.ContentToSign())
|
signature, signatureField, err := opts.CommitSigningCallback(commit.ContentToSign())
|
||||||
checkFatal(t, err)
|
checkFatal(t, err)
|
||||||
|
|
||||||
oid, err := commit.WithSignature(signature, signatureField)
|
oid, err := commit.WithSignature(signature, signatureField)
|
||||||
|
|
20
rebase.go
20
rebase.go
|
@ -79,13 +79,13 @@ func commitSignCallback(_signature *C.git_buf, _signature_field *C.git_buf, _com
|
||||||
panic("invalid sign payload")
|
panic("invalid sign payload")
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.SigningCallback == nil {
|
if opts.CommitSigningCallback == nil {
|
||||||
return C.GIT_PASSTHROUGH
|
return C.GIT_PASSTHROUGH
|
||||||
}
|
}
|
||||||
|
|
||||||
commitContent := C.GoString(_commit_content)
|
commitContent := C.GoString(_commit_content)
|
||||||
|
|
||||||
signature, signatureField, err := opts.SigningCallback(commitContent)
|
signature, signatureField, err := opts.CommitSigningCallback(commitContent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if gitError, ok := err.(*GitError); ok {
|
if gitError, ok := err.(*GitError); ok {
|
||||||
return C.int(gitError.Code)
|
return C.int(gitError.Code)
|
||||||
|
@ -129,13 +129,13 @@ func commitSignCallback(_signature *C.git_buf, _signature_field *C.git_buf, _com
|
||||||
|
|
||||||
// RebaseOptions are used to tell the rebase machinery how to operate
|
// RebaseOptions are used to tell the rebase machinery how to operate
|
||||||
type RebaseOptions struct {
|
type RebaseOptions struct {
|
||||||
Version uint
|
Version uint
|
||||||
Quiet int
|
Quiet int
|
||||||
InMemory int
|
InMemory int
|
||||||
RewriteNotesRef string
|
RewriteNotesRef string
|
||||||
MergeOptions MergeOptions
|
MergeOptions MergeOptions
|
||||||
CheckoutOptions CheckoutOpts
|
CheckoutOptions CheckoutOpts
|
||||||
SigningCallback CommitSigningCb
|
CommitSigningCallback CommitSigningCallback
|
||||||
}
|
}
|
||||||
|
|
||||||
// DefaultRebaseOptions returns a RebaseOptions with default values.
|
// DefaultRebaseOptions returns a RebaseOptions with default values.
|
||||||
|
@ -176,7 +176,7 @@ func (ro *RebaseOptions) toC() *C.git_rebase_options {
|
||||||
checkout_options: *ro.CheckoutOptions.toC(),
|
checkout_options: *ro.CheckoutOptions.toC(),
|
||||||
}
|
}
|
||||||
|
|
||||||
if ro.SigningCallback != nil {
|
if ro.CommitSigningCallback != nil {
|
||||||
C._go_git_populate_commit_sign_cb(opts)
|
C._go_git_populate_commit_sign_cb(opts)
|
||||||
opts.payload = pointerHandles.Track(ro)
|
opts.payload = pointerHandles.Track(ro)
|
||||||
}
|
}
|
||||||
|
|
|
@ -155,10 +155,10 @@ func TestRebaseGpgSigned(t *testing.T) {
|
||||||
|
|
||||||
return cipherText.String(), "", nil
|
return cipherText.String(), "", nil
|
||||||
}
|
}
|
||||||
opts.SigningCallback = signCommitContent
|
opts.CommitSigningCallback = signCommitContent
|
||||||
|
|
||||||
commitOpts := commitOpts{
|
commitOpts := commitOpts{
|
||||||
CommitSigningCb: signCommitContent,
|
CommitSigningCallback: signCommitContent,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inputs
|
// Inputs
|
||||||
|
@ -441,13 +441,13 @@ func commitSomething(repo *Repository, something, content string, commitOpts com
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if commitOpts.CommitSigningCb != nil {
|
if commitOpts.CommitSigningCallback != nil {
|
||||||
commit, err := repo.LookupCommit(commit)
|
commit, err := repo.LookupCommit(commit)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
oid, err := commit.WithSignatureUsing(commitOpts.CommitSigningCb)
|
oid, err := commit.WithSignatureUsing(commitOpts.CommitSigningCallback)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue