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
use std::kinds::marker;
use std::str;
use libc;
use {raw, Oid, Repository, Error, Signature, Tree};
/// A structure to represent a git [commit][1]
///
/// [1]: http://git-scm.com/book/en/Git-Internals-Git-Objects
pub struct Commit<'a> {
raw: *mut raw::git_commit,
marker1: marker::ContravariantLifetime<'a>,
marker2: marker::NoSend,
marker3: marker::NoSync,
}
/// An iterator over the parent commits of a commit.
pub struct Parents<'a, 'b:'a> {
cur: uint,
max: uint,
commit: &'a Commit<'b>,
}
impl<'a> Commit<'a> {
/// Create a new object from its raw component.
///
/// This method is unsafe as there is no guarantee that `raw` is a valid
/// pointer.
pub unsafe fn from_raw(_repo: &Repository,
raw: *mut raw::git_commit) -> Commit {
Commit {
raw: raw,
marker1: marker::ContravariantLifetime,
marker2: marker::NoSend,
marker3: marker::NoSync,
}
}
/// Get the id (SHA1) of a repository commit
pub fn id(&self) -> Oid {
unsafe { Oid::from_raw(raw::git_commit_id(&*self.raw)) }
}
/// Get the id of the tree pointed to by this commit.
///
/// No attempts are made to fetch an object from the
pub fn tree_id(&self) -> Oid {
unsafe { Oid::from_raw(raw::git_commit_tree_id(&*self.raw)) }
}
/// Get access to the underlying raw pointer.
pub fn raw(&self) -> *mut raw::git_commit { self.raw }
/// Get the full message of a commit.
///
/// The returned message will be slightly prettified by removing any
/// potential leading newlines.
///
/// `None` will be returned if the message is not valid utf-8
pub fn message(&self) -> Option<&str> {
str::from_utf8(self.message_bytes())
}
/// Get the full message of a commit as a byte slice.
///
/// The returned message will be slightly prettified by removing any
/// potential leading newlines.
pub fn message_bytes(&self) -> &[u8] {
unsafe {
::opt_bytes(self, raw::git_commit_message(&*self.raw)).unwrap()
}
}
/// Get the encoding for the message of a commit, as a string representing a
/// standard encoding name.
///
/// `None` will be returned if the encoding is not known
pub fn message_encoding(&self) -> Option<&str> {
let bytes = unsafe {
::opt_bytes(self, raw::git_commit_message(&*self.raw))
};
bytes.map(|b| str::from_utf8(b).unwrap())
}
/// Get the full raw message of a commit.
///
/// `None` will be returned if the message is not valid utf-8
pub fn message_raw(&self) -> Option<&str> {
str::from_utf8(self.message_raw_bytes())
}
/// Get the full raw message of a commit.
pub fn message_raw_bytes(&self) -> &[u8] {
unsafe {
::opt_bytes(self, raw::git_commit_message_raw(&*self.raw)).unwrap()
}
}
/// Get the full raw text of the commit header.
///
/// `None` will be returned if the message is not valid utf-8
pub fn raw_header(&self) -> Option<&str> {
str::from_utf8(self.raw_header_bytes())
}
/// Get the full raw text of the commit header.
pub fn raw_header_bytes(&self) -> &[u8] {
unsafe {
::opt_bytes(self, raw::git_commit_raw_header(&*self.raw)).unwrap()
}
}
/// Get the short "summary" of the git commit message.
///
/// The returned message is the summary of the commit, comprising the first
/// paragraph of the message with whitespace trimmed and squashed.
///
/// `None` may be returned if an error occurs or if the summary is not valid
/// utf-8.
pub fn summary(&mut self) -> Option<&str> {
self.summary_bytes().and_then(str::from_utf8)
}
/// Get the short "summary" of the git commit message.
///
/// The returned message is the summary of the commit, comprising the first
/// paragraph of the message with whitespace trimmed and squashed.
///
/// `None` may be returned if an error occurs
pub fn summary_bytes(&mut self) -> Option<&[u8]> {
unsafe { ::opt_bytes(self, raw::git_commit_summary(self.raw)) }
}
/// Get the commit time (i.e. committer time) of a commit.
///
/// The first element of the tuple is the time, in seconds, since the epoch.
/// The second element is the offset, in minutes, of the time zone of the
/// committer's preferred time zone.
pub fn time(&self) -> (u64, int) {
unsafe {
(raw::git_commit_time(&*self.raw) as u64,
raw::git_commit_time_offset(&*self.raw) as int)
}
}
/// Creates a new iterator over the parents of this commit.
pub fn parents<'b>(&'b self) -> Parents<'b, 'a> {
Parents {
cur: 0,
max: unsafe { raw::git_commit_parentcount(&*self.raw) as uint },
commit: self,
}
}
/// Get the author of this commit.
pub fn author(&self) -> Signature {
unsafe {
let ptr = raw::git_commit_author(&*self.raw);
Signature::from_raw_const(self, ptr)
}
}
/// Get the committer of this commit.
pub fn committer(&self) -> Signature {
unsafe {
let ptr = raw::git_commit_committer(&*self.raw);
Signature::from_raw_const(self, ptr)
}
}
/// Amend this existing commit with all non-`None` values
///
/// This creates a new commit that is exactly the same as the old commit,
/// except that any non-`None` values will be updated. The new commit has
/// the same parents as the old commit.
///
/// For information about `update_ref`, see `new`.
pub fn amend(&self,
update_ref: Option<&str>,
author: Option<&Signature>,
committer: Option<&Signature>,
message_encoding: Option<&str>,
message: Option<&str>,
tree: Option<&Tree<'a>>) -> Result<Oid, Error> {
let mut raw = raw::git_oid { id: [0, ..raw::GIT_OID_RAWSZ] };
unsafe {
try_call!(raw::git_commit_amend(&mut raw,
&*self.raw(),
update_ref.map(|s| s.to_c_str()),
author.map(|s| &*s.raw()),
committer.map(|s| &*s.raw()),
message_encoding.map(|s| s.to_c_str()),
message.map(|s| s.to_c_str()),
tree.map(|t| &*t.raw())));
Ok(Oid::from_raw(&raw))
}
}
}
impl<'a, 'b> Iterator<Commit<'a>> for Parents<'b, 'a> {
fn next(&mut self) -> Option<Commit<'a>> {
if self.cur == self.max { return None }
self.cur += 1;
let mut raw = 0 as *mut raw::git_commit;
assert_eq!(unsafe {
raw::git_commit_parent(&mut raw, &*self.commit.raw,
(self.cur - 1) as libc::c_uint)
}, 0);
Some(Commit {
raw: raw,
marker1: marker::ContravariantLifetime,
marker2: marker::NoSend,
marker3: marker::NoSync,
})
}
}
#[unsafe_destructor]
impl<'a> Drop for Commit<'a> {
fn drop(&mut self) {
unsafe { raw::git_commit_free(self.raw) }
}
}
#[cfg(test)]
mod tests {
#[test]
fn smoke() {
let (_td, repo) = ::test::repo_init();
let head = repo.head().unwrap();
let target = head.target().unwrap();
let mut 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();
assert_eq!(commit.parents().count(), 0);
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"));
}
}