-
Notifications
You must be signed in to change notification settings - Fork 327
Expand file tree
/
Copy pathcommit.go
More file actions
297 lines (244 loc) · 7.25 KB
/
commit.go
File metadata and controls
297 lines (244 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package git
/*
#include <git2.h>
extern int _go_git_treewalk(git_tree *tree, git_treewalk_mode mode, void *ptr);
static void _go_git_commit_create_options_set_allow_empty(git_commit_create_options *opts, int allow) {
opts->allow_empty_commit = allow ? 1 : 0;
}
*/
import "C"
import (
"runtime"
"unsafe"
)
// MessageEncoding is the encoding of commit messages.
type MessageEncoding string
const (
// MessageEncodingUTF8 is the default message encoding.
MessageEncodingUTF8 MessageEncoding = "UTF-8"
)
// Commit
type Commit struct {
doNotCompare
Object
cast_ptr *C.git_commit
}
func (c *Commit) AsObject() *Object {
return &c.Object
}
func (c *Commit) Message() string {
ret := C.GoString(C.git_commit_message(c.cast_ptr))
runtime.KeepAlive(c)
return ret
}
func (c *Commit) MessageEncoding() MessageEncoding {
ptr := C.git_commit_message_encoding(c.cast_ptr)
if ptr == nil {
return MessageEncodingUTF8
}
ret := C.GoString(ptr)
runtime.KeepAlive(c)
return MessageEncoding(ret)
}
func (c *Commit) RawMessage() string {
ret := C.GoString(C.git_commit_message_raw(c.cast_ptr))
runtime.KeepAlive(c)
return ret
}
// 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)
// 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)
// 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) {
return c.Owner().CreateCommitWithSignature(
c.ContentToSign(),
signature,
signatureField,
)
}
func (c *Commit) ExtractSignature() (string, string, error) {
var c_signed C.git_buf
defer C.git_buf_dispose(&c_signed)
var c_signature C.git_buf
defer C.git_buf_dispose(&c_signature)
oid := c.Id()
repo := C.git_commit_owner(c.cast_ptr)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_commit_extract_signature(&c_signature, &c_signed, repo, oid.toC(), nil)
runtime.KeepAlive(oid)
if ret < 0 {
return "", "", MakeGitError(ret)
} else {
return C.GoString(c_signature.ptr), C.GoString(c_signed.ptr), nil
}
}
func (c *Commit) Summary() string {
ret := C.GoString(C.git_commit_summary(c.cast_ptr))
runtime.KeepAlive(c)
return ret
}
func (c *Commit) Tree() (*Tree, error) {
var ptr *C.git_tree
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C.git_commit_tree(&ptr, c.cast_ptr)
runtime.KeepAlive(c)
if err < 0 {
return nil, MakeGitError(err)
}
return allocTree(ptr, c.repo), nil
}
func (c *Commit) TreeId() *Oid {
ret := newOidFromC(C.git_commit_tree_id(c.cast_ptr))
runtime.KeepAlive(c)
return ret
}
func (c *Commit) Author() *Signature {
cast_ptr := C.git_commit_author(c.cast_ptr)
ret := newSignatureFromC(cast_ptr)
runtime.KeepAlive(c)
return ret
}
func (c *Commit) Committer() *Signature {
cast_ptr := C.git_commit_committer(c.cast_ptr)
ret := newSignatureFromC(cast_ptr)
runtime.KeepAlive(c)
return ret
}
func (c *Commit) Parent(n uint) *Commit {
var cobj *C.git_commit
ret := C.git_commit_parent(&cobj, c.cast_ptr, C.uint(n))
if ret != 0 {
return nil
}
parent := allocCommit(cobj, c.repo)
runtime.KeepAlive(c)
return parent
}
func (c *Commit) ParentId(n uint) *Oid {
ret := newOidFromC(C.git_commit_parent_id(c.cast_ptr, C.uint(n)))
runtime.KeepAlive(c)
return ret
}
func (c *Commit) ParentCount() uint {
ret := uint(C.git_commit_parentcount(c.cast_ptr))
runtime.KeepAlive(c)
return ret
}
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)
runtime.KeepAlive(oid)
runtime.KeepAlive(c)
runtime.KeepAlive(tree)
if cerr < 0 {
return nil, MakeGitError(cerr)
}
return oid, nil
}
// CommitCreateOptions contains options for creating a commit from stage.
type CommitCreateOptions struct {
// If set, allow an empty commit (no changes from parent).
AllowEmptyCommit bool
// The commit author, or nil for the default.
Author *Signature
// The committer, or nil for the default.
Committer *Signature
// Encoding for the commit message; leave empty for default (UTF-8).
MessageEncoding string
}
// CreateCommitFromStage commits the staged changes in the repository.
// This is a near analog to `git commit -m message`.
// By default, empty commits are not allowed.
func (v *Repository) CreateCommitFromStage(message string, opts *CommitCreateOptions) (*Oid, error) {
oid := new(Oid)
cmsg := C.CString(message)
defer C.free(unsafe.Pointer(cmsg))
var copts C.git_commit_create_options
copts.version = C.GIT_COMMIT_CREATE_OPTIONS_VERSION
var authorSig *C.git_signature
var committerSig *C.git_signature
if opts != nil {
if opts.AllowEmptyCommit {
C._go_git_commit_create_options_set_allow_empty(&copts, 1)
}
if opts.Author != nil {
authorSig, _ = opts.Author.toC()
if authorSig != nil {
defer C.git_signature_free(authorSig)
copts.author = authorSig
}
}
if opts.Committer != nil {
committerSig, _ = opts.Committer.toC()
if committerSig != nil {
defer C.git_signature_free(committerSig)
copts.committer = committerSig
}
}
if opts.MessageEncoding != "" {
cenc := C.CString(opts.MessageEncoding)
defer C.free(unsafe.Pointer(cenc))
copts.message_encoding = cenc
}
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_commit_create_from_stage(oid.toC(), v.ptr, cmsg, &copts)
runtime.KeepAlive(v)
if ret < 0 {
return nil, MakeGitError(ret)
}
return oid, nil
}