Skip to content

Commit 1543461

Browse files
authored
Add CreateCommitWithSignature (#782)
This change adds the wrapper for `git_commit_create_with_signature`.
1 parent be5a99a commit 1543461

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

commit.go

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -75,40 +75,11 @@ func (c *Commit) WithSignatureUsing(f CommitSigningCallback) (*Oid, error) {
7575

7676
// WithSignature creates a new signed commit from the given signature and signature field
7777
func (c *Commit) WithSignature(signature string, signatureField string) (*Oid, error) {
78-
totalCommit := c.ContentToSign()
79-
80-
oid := new(Oid)
81-
82-
var csf *C.char = nil
83-
if signatureField != "" {
84-
csf = C.CString(signatureField)
85-
defer C.free(unsafe.Pointer(csf))
86-
}
87-
88-
runtime.LockOSThread()
89-
defer runtime.UnlockOSThread()
90-
91-
cTotalCommit := C.CString(totalCommit)
92-
cSignature := C.CString(signature)
93-
defer C.free(unsafe.Pointer(cTotalCommit))
94-
defer C.free(unsafe.Pointer(cSignature))
95-
96-
ret := C.git_commit_create_with_signature(
97-
oid.toC(),
98-
c.Owner().ptr,
99-
cTotalCommit,
100-
cSignature,
101-
csf,
78+
return c.Owner().CreateCommitWithSignature(
79+
c.ContentToSign(),
80+
signature,
81+
signatureField,
10282
)
103-
104-
runtime.KeepAlive(c)
105-
runtime.KeepAlive(oid)
106-
107-
if ret < 0 {
108-
return nil, MakeGitError(ret)
109-
}
110-
111-
return oid, nil
11283
}
11384

11485
func (c *Commit) ExtractSignature() (string, string, error) {

repository.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,39 @@ func (v *Repository) CreateCommit(
485485
return oid, nil
486486
}
487487

488+
// CreateCommitWithSignature creates a commit object from the given contents and
489+
// signature.
490+
func (v *Repository) CreateCommitWithSignature(
491+
commitContent, signature, signatureField string,
492+
) (*Oid, error) {
493+
cCommitContent := C.CString(commitContent)
494+
defer C.free(unsafe.Pointer(cCommitContent))
495+
var cSignature *C.char
496+
if signature != "" {
497+
cSignature = C.CString(string(signature))
498+
defer C.free(unsafe.Pointer(cSignature))
499+
}
500+
var cSignatureField *C.char
501+
if signatureField != "" {
502+
cSignatureField = C.CString(string(signatureField))
503+
defer C.free(unsafe.Pointer(cSignatureField))
504+
}
505+
506+
runtime.LockOSThread()
507+
defer runtime.UnlockOSThread()
508+
509+
oid := new(Oid)
510+
ret := C.git_commit_create_with_signature(oid.toC(), v.ptr, cCommitContent, cSignature, cSignatureField)
511+
512+
runtime.KeepAlive(v)
513+
runtime.KeepAlive(oid)
514+
if ret < 0 {
515+
return nil, MakeGitError(ret)
516+
}
517+
518+
return oid, nil
519+
}
520+
488521
// CreateCommitBuffer creates a commit and write it into a buffer.
489522
func (v *Repository) CreateCommitBuffer(
490523
author, committer *Signature,

0 commit comments

Comments
 (0)