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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
use libc;
use std::marker;
use std::mem;
use std::ops::Range;
use std::ptr;
use std::str;
use crate::util::Binding;
use crate::{raw, signature, Buf, Error, IntoCString, Mailmap, Object, Oid, Signature, Time, Tree};
pub struct Commit<'repo> {
raw: *mut raw::git_commit,
_marker: marker::PhantomData<Object<'repo>>,
}
pub struct Parents<'commit, 'repo> {
range: Range<usize>,
commit: &'commit Commit<'repo>,
}
pub struct ParentIds<'commit> {
range: Range<usize>,
commit: &'commit Commit<'commit>,
}
impl<'repo> Commit<'repo> {
pub fn id(&self) -> Oid {
unsafe { Binding::from_raw(raw::git_commit_id(&*self.raw)) }
}
pub fn tree_id(&self) -> Oid {
unsafe { Binding::from_raw(raw::git_commit_tree_id(&*self.raw)) }
}
pub fn tree(&self) -> Result<Tree<'repo>, Error> {
let mut ret = ptr::null_mut();
unsafe {
try_call!(raw::git_commit_tree(&mut ret, &*self.raw));
Ok(Binding::from_raw(ret))
}
}
pub fn raw(&self) -> *mut raw::git_commit {
self.raw
}
pub fn message(&self) -> Option<&str> {
str::from_utf8(self.message_bytes()).ok()
}
pub fn message_bytes(&self) -> &[u8] {
unsafe { crate::opt_bytes(self, raw::git_commit_message(&*self.raw)).unwrap() }
}
pub fn message_encoding(&self) -> Option<&str> {
let bytes = unsafe { crate::opt_bytes(self, raw::git_commit_message_encoding(&*self.raw)) };
bytes.and_then(|b| str::from_utf8(b).ok())
}
pub fn message_raw(&self) -> Option<&str> {
str::from_utf8(self.message_raw_bytes()).ok()
}
pub fn message_raw_bytes(&self) -> &[u8] {
unsafe { crate::opt_bytes(self, raw::git_commit_message_raw(&*self.raw)).unwrap() }
}
pub fn raw_header(&self) -> Option<&str> {
str::from_utf8(self.raw_header_bytes()).ok()
}
pub fn header_field_bytes<T: IntoCString>(&self, field: T) -> Result<Buf, Error> {
let buf = Buf::new();
let raw_field = field.into_c_string()?;
unsafe {
try_call!(raw::git_commit_header_field(
buf.raw(),
&*self.raw,
raw_field
));
}
Ok(buf)
}
pub fn raw_header_bytes(&self) -> &[u8] {
unsafe { crate::opt_bytes(self, raw::git_commit_raw_header(&*self.raw)).unwrap() }
}
pub fn summary(&self) -> Option<&str> {
self.summary_bytes().and_then(|s| str::from_utf8(s).ok())
}
pub fn summary_bytes(&self) -> Option<&[u8]> {
unsafe { crate::opt_bytes(self, raw::git_commit_summary(self.raw)) }
}
pub fn time(&self) -> Time {
unsafe {
Time::new(
raw::git_commit_time(&*self.raw) as i64,
raw::git_commit_time_offset(&*self.raw) as i32,
)
}
}
pub fn parents<'a>(&'a self) -> Parents<'a, 'repo> {
Parents {
range: 0..self.parent_count(),
commit: self,
}
}
pub fn parent_ids(&self) -> ParentIds<'_> {
ParentIds {
range: 0..self.parent_count(),
commit: self,
}
}
pub fn author(&self) -> Signature<'_> {
unsafe {
let ptr = raw::git_commit_author(&*self.raw);
signature::from_raw_const(self, ptr)
}
}
pub fn author_with_mailmap(&self, mailmap: &Mailmap) -> Result<Signature<'static>, Error> {
let mut ret = ptr::null_mut();
unsafe {
try_call!(raw::git_commit_author_with_mailmap(
&mut ret,
&*self.raw,
&*mailmap.raw()
));
Ok(Binding::from_raw(ret))
}
}
pub fn committer(&self) -> Signature<'_> {
unsafe {
let ptr = raw::git_commit_committer(&*self.raw);
signature::from_raw_const(self, ptr)
}
}
pub fn committer_with_mailmap(&self, mailmap: &Mailmap) -> Result<Signature<'static>, Error> {
let mut ret = ptr::null_mut();
unsafe {
try_call!(raw::git_commit_committer_with_mailmap(
&mut ret,
&*self.raw,
&*mailmap.raw()
));
Ok(Binding::from_raw(ret))
}
}
pub fn amend(
&self,
update_ref: Option<&str>,
author: Option<&Signature<'_>>,
committer: Option<&Signature<'_>>,
message_encoding: Option<&str>,
message: Option<&str>,
tree: Option<&Tree<'repo>>,
) -> Result<Oid, Error> {
let mut raw = raw::git_oid {
id: [0; raw::GIT_OID_RAWSZ],
};
let update_ref = crate::opt_cstr(update_ref)?;
let encoding = crate::opt_cstr(message_encoding)?;
let message = crate::opt_cstr(message)?;
unsafe {
try_call!(raw::git_commit_amend(
&mut raw,
self.raw(),
update_ref,
author.map(|s| s.raw()),
committer.map(|s| s.raw()),
encoding,
message,
tree.map(|t| t.raw())
));
Ok(Binding::from_raw(&raw as *const _))
}
}
pub fn parent_count(&self) -> usize {
unsafe { raw::git_commit_parentcount(&*self.raw) as usize }
}
pub fn parent(&self, i: usize) -> Result<Commit<'repo>, Error> {
unsafe {
let mut raw = ptr::null_mut();
try_call!(raw::git_commit_parent(
&mut raw,
&*self.raw,
i as libc::c_uint
));
Ok(Binding::from_raw(raw))
}
}
pub fn parent_id(&self, i: usize) -> Result<Oid, Error> {
unsafe {
let id = raw::git_commit_parent_id(self.raw, i as libc::c_uint);
if id.is_null() {
Err(Error::from_str("parent index out of bounds"))
} else {
Ok(Binding::from_raw(id))
}
}
}
pub fn as_object(&self) -> &Object<'repo> {
unsafe { &*(self as *const _ as *const Object<'repo>) }
}
pub fn into_object(self) -> Object<'repo> {
assert_eq!(mem::size_of_val(&self), mem::size_of::<Object<'_>>());
unsafe { mem::transmute(self) }
}
}
impl<'repo> Binding for Commit<'repo> {
type Raw = *mut raw::git_commit;
unsafe fn from_raw(raw: *mut raw::git_commit) -> Commit<'repo> {
Commit {
raw,
_marker: marker::PhantomData,
}
}
fn raw(&self) -> *mut raw::git_commit {
self.raw
}
}
impl<'repo> std::fmt::Debug for Commit<'repo> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
let mut ds = f.debug_struct("Commit");
ds.field("id", &self.id());
if let Some(summary) = self.summary() {
ds.field("summary", &summary);
}
ds.finish()
}
}
impl<'repo, 'commit> Iterator for Parents<'commit, 'repo> {
type Item = Commit<'repo>;
fn next(&mut self) -> Option<Commit<'repo>> {
self.range.next().and_then(|i| self.commit.parent(i).ok())
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.range.size_hint()
}
}
impl<'repo, 'commit> DoubleEndedIterator for Parents<'commit, 'repo> {
fn next_back(&mut self) -> Option<Commit<'repo>> {
self.range
.next_back()
.and_then(|i| self.commit.parent(i).ok())
}
}
impl<'repo, 'commit> ExactSizeIterator for Parents<'commit, 'repo> {}
impl<'commit> Iterator for ParentIds<'commit> {
type Item = Oid;
fn next(&mut self) -> Option<Oid> {
self.range
.next()
.and_then(|i| self.commit.parent_id(i).ok())
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.range.size_hint()
}
}
impl<'commit> DoubleEndedIterator for ParentIds<'commit> {
fn next_back(&mut self) -> Option<Oid> {
self.range
.next_back()
.and_then(|i| self.commit.parent_id(i).ok())
}
}
impl<'commit> ExactSizeIterator for ParentIds<'commit> {}
impl<'repo> Clone for Commit<'repo> {
fn clone(&self) -> Self {
self.as_object().clone().into_commit().ok().unwrap()
}
}
impl<'repo> Drop for Commit<'repo> {
fn drop(&mut self) {
unsafe { raw::git_commit_free(self.raw) }
}
}
#[cfg(test)]
mod tests {
#[test]
fn smoke() {
let (_td, repo) = crate::test::repo_init();
let head = repo.head().unwrap();
let target = head.target().unwrap();
let commit = repo.find_commit(target).unwrap();
assert_eq!(commit.message(), Some("initial"));
assert_eq!(commit.id(), target);
commit.message_raw().unwrap();
commit.raw_header().unwrap();
commit.message_encoding();
commit.summary().unwrap();
commit.tree_id();
commit.tree().unwrap();
assert_eq!(commit.parents().count(), 0);
let tree_header_bytes = commit.header_field_bytes("tree").unwrap();
assert_eq!(
crate::Oid::from_str(tree_header_bytes.as_str().unwrap()).unwrap(),
commit.tree_id()
);
assert_eq!(commit.author().name(), Some("name"));
assert_eq!(commit.author().email(), Some("email"));
assert_eq!(commit.committer().name(), Some("name"));
assert_eq!(commit.committer().email(), Some("email"));
let sig = repo.signature().unwrap();
let tree = repo.find_tree(commit.tree_id()).unwrap();
let id = repo
.commit(Some("HEAD"), &sig, &sig, "bar", &tree, &[&commit])
.unwrap();
let head = repo.find_commit(id).unwrap();
let new_head = head
.amend(Some("HEAD"), None, None, None, Some("new message"), None)
.unwrap();
let new_head = repo.find_commit(new_head).unwrap();
assert_eq!(new_head.message(), Some("new message"));
new_head.into_object();
repo.find_object(target, None).unwrap().as_commit().unwrap();
repo.find_object(target, None)
.unwrap()
.into_commit()
.ok()
.unwrap();
}
}