2013-03-05 13:53:04 -06:00
|
|
|
package git
|
|
|
|
|
|
|
|
/*
|
|
|
|
#include <git2.h>
|
|
|
|
|
|
|
|
extern int _go_git_treewalk(git_tree *tree, git_treewalk_mode mode, void *ptr);
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
2013-09-18 02:23:47 -05:00
|
|
|
"runtime"
|
2015-03-11 18:10:32 -05:00
|
|
|
"unsafe"
|
2013-03-05 13:53:04 -06:00
|
|
|
)
|
|
|
|
|
2021-09-04 15:04:58 -05:00
|
|
|
// MessageEncoding is the encoding of commit messages.
|
|
|
|
type MessageEncoding string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// MessageEncodingUTF8 is the default message encoding.
|
|
|
|
MessageEncodingUTF8 MessageEncoding = "UTF-8"
|
|
|
|
)
|
|
|
|
|
2013-03-05 13:53:04 -06:00
|
|
|
// Commit
|
|
|
|
type Commit struct {
|
2021-09-05 15:59:36 -05:00
|
|
|
doNotCompare
|
2015-08-04 07:47:10 -05:00
|
|
|
Object
|
2014-04-01 05:13:37 -05:00
|
|
|
cast_ptr *C.git_commit
|
2013-03-05 13:53:04 -06:00
|
|
|
}
|
|
|
|
|
2017-07-08 15:53:50 -05:00
|
|
|
func (c *Commit) AsObject() *Object {
|
|
|
|
return &c.Object
|
|
|
|
}
|
|
|
|
|
2017-07-07 16:36:04 -05:00
|
|
|
func (c *Commit) Message() string {
|
|
|
|
ret := C.GoString(C.git_commit_message(c.cast_ptr))
|
|
|
|
runtime.KeepAlive(c)
|
|
|
|
return ret
|
2013-03-05 13:53:04 -06:00
|
|
|
}
|
|
|
|
|
2021-09-03 08:40:31 -05:00
|
|
|
func (c *Commit) MessageEncoding() MessageEncoding {
|
|
|
|
ptr := C.git_commit_message_encoding(c.cast_ptr)
|
|
|
|
if ptr == nil {
|
|
|
|
return MessageEncodingUTF8
|
|
|
|
}
|
|
|
|
ret := C.GoString(ptr)
|
2019-05-10 06:31:01 -05:00
|
|
|
runtime.KeepAlive(c)
|
2021-09-03 08:40:31 -05:00
|
|
|
return MessageEncoding(ret)
|
2019-05-10 06:31:01 -05:00
|
|
|
}
|
|
|
|
|
2017-07-07 16:36:04 -05:00
|
|
|
func (c *Commit) RawMessage() string {
|
|
|
|
ret := C.GoString(C.git_commit_message_raw(c.cast_ptr))
|
|
|
|
runtime.KeepAlive(c)
|
|
|
|
return ret
|
2016-09-09 08:27:07 -05:00
|
|
|
}
|
|
|
|
|
2020-08-18 11:25:31 -05:00
|
|
|
// RawHeader gets the full raw text of the commit header.
|
|
|
|
func (c *Commit) RawHeader() string {
|
|
|
|
ret := C.GoString(C.git_commit_raw_header(c.cast_ptr))
|
|
|
|
runtime.KeepAlive(c)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContentToSign returns the content that will be passed to a signing function for this commit
|
|
|
|
func (c *Commit) ContentToSign() string {
|
|
|
|
return c.RawHeader() + "\n" + c.RawMessage()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CommitSigningCallback defines a function type that takes some data to sign and returns (signature, signature_field, error)
|
|
|
|
type CommitSigningCallback func(string) (signature, signatureField string, err error)
|
|
|
|
|
2021-09-03 08:40:31 -05:00
|
|
|
// CommitCreateCallback defines a function type that is called when another
|
|
|
|
// function is going to create commits (for example, Rebase) to allow callers
|
|
|
|
// to override the commit creation behavior. For example, users may wish to
|
|
|
|
// sign commits by providing this information to Repository.CreateCommitBuffer,
|
|
|
|
// signing that buffer, then calling Repository.CreateCommitWithSignature.
|
|
|
|
type CommitCreateCallback func(
|
|
|
|
author, committer *Signature,
|
|
|
|
messageEncoding MessageEncoding,
|
|
|
|
message string,
|
|
|
|
tree *Tree,
|
|
|
|
parents ...*Commit,
|
|
|
|
) (oid *Oid, err error)
|
|
|
|
|
2020-08-18 11:25:31 -05:00
|
|
|
// WithSignatureUsing creates a new signed commit from this one using the given signing callback
|
|
|
|
func (c *Commit) WithSignatureUsing(f CommitSigningCallback) (*Oid, error) {
|
|
|
|
signature, signatureField, err := f(c.ContentToSign())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.WithSignature(signature, signatureField)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithSignature creates a new signed commit from the given signature and signature field
|
|
|
|
func (c *Commit) WithSignature(signature string, signatureField string) (*Oid, error) {
|
2021-09-04 15:49:01 -05:00
|
|
|
return c.Owner().CreateCommitWithSignature(
|
|
|
|
c.ContentToSign(),
|
|
|
|
signature,
|
|
|
|
signatureField,
|
2020-08-18 11:25:31 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-07-07 16:36:04 -05:00
|
|
|
func (c *Commit) ExtractSignature() (string, string, error) {
|
2017-07-06 02:40:58 -05:00
|
|
|
|
|
|
|
var c_signed C.git_buf
|
2018-08-08 04:51:51 -05:00
|
|
|
defer C.git_buf_dispose(&c_signed)
|
2017-07-06 02:40:58 -05:00
|
|
|
|
|
|
|
var c_signature C.git_buf
|
2018-08-08 04:51:51 -05:00
|
|
|
defer C.git_buf_dispose(&c_signature)
|
2017-07-06 02:40:58 -05:00
|
|
|
|
|
|
|
oid := c.Id()
|
|
|
|
repo := C.git_commit_owner(c.cast_ptr)
|
2017-07-07 16:24:54 -05:00
|
|
|
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
2017-07-06 02:40:58 -05:00
|
|
|
ret := C.git_commit_extract_signature(&c_signature, &c_signed, repo, oid.toC(), nil)
|
2017-07-07 16:36:04 -05:00
|
|
|
runtime.KeepAlive(oid)
|
2017-07-06 02:40:58 -05:00
|
|
|
if ret < 0 {
|
2017-07-07 16:24:54 -05:00
|
|
|
return "", "", MakeGitError(ret)
|
2017-07-06 02:40:58 -05:00
|
|
|
} else {
|
|
|
|
return C.GoString(c_signature.ptr), C.GoString(c_signed.ptr), nil
|
|
|
|
}
|
2017-07-07 16:24:54 -05:00
|
|
|
|
2017-07-06 02:40:58 -05:00
|
|
|
}
|
|
|
|
|
2017-07-07 16:36:04 -05:00
|
|
|
func (c *Commit) Summary() string {
|
|
|
|
ret := C.GoString(C.git_commit_summary(c.cast_ptr))
|
|
|
|
runtime.KeepAlive(c)
|
|
|
|
return ret
|
2015-03-04 17:35:35 -06:00
|
|
|
}
|
|
|
|
|
2017-07-07 16:36:04 -05:00
|
|
|
func (c *Commit) Tree() (*Tree, error) {
|
2014-04-01 05:13:37 -05:00
|
|
|
var ptr *C.git_tree
|
2013-03-05 13:53:04 -06:00
|
|
|
|
2013-09-18 02:23:47 -05:00
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
2014-04-01 05:13:37 -05:00
|
|
|
err := C.git_commit_tree(&ptr, c.cast_ptr)
|
2017-07-07 16:36:04 -05:00
|
|
|
runtime.KeepAlive(c)
|
2013-03-05 13:53:04 -06:00
|
|
|
if err < 0 {
|
2013-07-07 09:43:44 -05:00
|
|
|
return nil, MakeGitError(err)
|
2013-03-05 13:53:04 -06:00
|
|
|
}
|
2013-03-05 18:43:48 -06:00
|
|
|
|
2015-08-04 07:47:10 -05:00
|
|
|
return allocTree(ptr, c.repo), nil
|
2013-03-05 13:53:04 -06:00
|
|
|
}
|
|
|
|
|
2017-07-07 16:36:04 -05:00
|
|
|
func (c *Commit) TreeId() *Oid {
|
|
|
|
ret := newOidFromC(C.git_commit_tree_id(c.cast_ptr))
|
|
|
|
runtime.KeepAlive(c)
|
2017-07-07 16:45:09 -05:00
|
|
|
return ret
|
2013-03-05 13:53:04 -06:00
|
|
|
}
|
|
|
|
|
2017-07-07 16:36:04 -05:00
|
|
|
func (c *Commit) Author() *Signature {
|
2014-04-01 05:13:37 -05:00
|
|
|
cast_ptr := C.git_commit_author(c.cast_ptr)
|
2017-07-07 16:36:04 -05:00
|
|
|
ret := newSignatureFromC(cast_ptr)
|
|
|
|
runtime.KeepAlive(c)
|
|
|
|
return ret
|
2013-03-05 13:53:04 -06:00
|
|
|
}
|
|
|
|
|
2017-07-07 16:36:04 -05:00
|
|
|
func (c *Commit) Committer() *Signature {
|
2014-04-01 05:13:37 -05:00
|
|
|
cast_ptr := C.git_commit_committer(c.cast_ptr)
|
2017-07-07 16:36:04 -05:00
|
|
|
ret := newSignatureFromC(cast_ptr)
|
|
|
|
runtime.KeepAlive(c)
|
|
|
|
return ret
|
2013-03-05 16:18:07 -06:00
|
|
|
}
|
|
|
|
|
2013-05-21 16:18:02 -05:00
|
|
|
func (c *Commit) Parent(n uint) *Commit {
|
2014-04-01 05:13:37 -05:00
|
|
|
var cobj *C.git_commit
|
|
|
|
ret := C.git_commit_parent(&cobj, c.cast_ptr, C.uint(n))
|
2013-05-21 16:18:02 -05:00
|
|
|
if ret != 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2013-11-14 07:24:43 -06:00
|
|
|
|
2017-07-07 16:45:09 -05:00
|
|
|
parent := allocCommit(cobj, c.repo)
|
|
|
|
runtime.KeepAlive(c)
|
|
|
|
return parent
|
2013-05-21 16:18:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commit) ParentId(n uint) *Oid {
|
2017-07-07 16:45:09 -05:00
|
|
|
ret := newOidFromC(C.git_commit_parent_id(c.cast_ptr, C.uint(n)))
|
|
|
|
runtime.KeepAlive(c)
|
|
|
|
return ret
|
2013-05-21 16:18:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commit) ParentCount() uint {
|
2017-07-07 16:45:09 -05:00
|
|
|
ret := uint(C.git_commit_parentcount(c.cast_ptr))
|
|
|
|
runtime.KeepAlive(c)
|
|
|
|
return ret
|
2013-05-21 16:18:02 -05:00
|
|
|
}
|
2015-03-11 18:10:32 -05:00
|
|
|
|
|
|
|
func (c *Commit) Amend(refname string, author, committer *Signature, message string, tree *Tree) (*Oid, error) {
|
|
|
|
var cref *C.char
|
|
|
|
if refname == "" {
|
|
|
|
cref = nil
|
|
|
|
} else {
|
|
|
|
cref = C.CString(refname)
|
|
|
|
defer C.free(unsafe.Pointer(cref))
|
|
|
|
}
|
|
|
|
|
|
|
|
cmsg := C.CString(message)
|
|
|
|
defer C.free(unsafe.Pointer(cmsg))
|
|
|
|
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
authorSig, err := author.toC()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer C.git_signature_free(authorSig)
|
|
|
|
|
|
|
|
committerSig, err := committer.toC()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer C.git_signature_free(committerSig)
|
|
|
|
|
|
|
|
oid := new(Oid)
|
|
|
|
|
|
|
|
cerr := C.git_commit_amend(oid.toC(), c.cast_ptr, cref, authorSig, committerSig, nil, cmsg, tree.cast_ptr)
|
2017-07-07 16:45:09 -05:00
|
|
|
runtime.KeepAlive(oid)
|
|
|
|
runtime.KeepAlive(c)
|
|
|
|
runtime.KeepAlive(tree)
|
2015-03-11 18:10:32 -05:00
|
|
|
if cerr < 0 {
|
|
|
|
return nil, MakeGitError(cerr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return oid, nil
|
|
|
|
}
|