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
use crate::util::{self, Binding};
use crate::{raw, signature, Oid, Repository, Signature};
use std::marker;
use std::mem;
use std::ops::Range;
use std::path::Path;
pub struct Blame<'repo> {
raw: *mut raw::git_blame,
_marker: marker::PhantomData<&'repo Repository>,
}
pub struct BlameHunk<'blame> {
raw: *mut raw::git_blame_hunk,
_marker: marker::PhantomData<&'blame raw::git_blame>,
}
pub struct BlameOptions {
raw: raw::git_blame_options,
}
pub struct BlameIter<'blame> {
range: Range<usize>,
blame: &'blame Blame<'blame>,
}
impl<'repo> Blame<'repo> {
pub fn len(&self) -> usize {
unsafe { raw::git_blame_get_hunk_count(self.raw) as usize }
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn get_index(&self, index: usize) -> Option<BlameHunk<'_>> {
unsafe {
let ptr = raw::git_blame_get_hunk_byindex(self.raw(), index as u32);
if ptr.is_null() {
None
} else {
Some(BlameHunk::from_raw_const(ptr))
}
}
}
pub fn get_line(&self, lineno: usize) -> Option<BlameHunk<'_>> {
unsafe {
let ptr = raw::git_blame_get_hunk_byline(self.raw(), lineno);
if ptr.is_null() {
None
} else {
Some(BlameHunk::from_raw_const(ptr))
}
}
}
pub fn iter(&self) -> BlameIter<'_> {
BlameIter {
range: 0..self.len(),
blame: self,
}
}
}
impl<'blame> BlameHunk<'blame> {
unsafe fn from_raw_const(raw: *const raw::git_blame_hunk) -> BlameHunk<'blame> {
BlameHunk {
raw: raw as *mut raw::git_blame_hunk,
_marker: marker::PhantomData,
}
}
pub fn final_commit_id(&self) -> Oid {
unsafe { Oid::from_raw(&(*self.raw).final_commit_id) }
}
pub fn final_signature(&self) -> Signature<'_> {
unsafe { signature::from_raw_const(self, (*self.raw).final_signature) }
}
pub fn final_start_line(&self) -> usize {
unsafe { (*self.raw).final_start_line_number }
}
pub fn orig_commit_id(&self) -> Oid {
unsafe { Oid::from_raw(&(*self.raw).orig_commit_id) }
}
pub fn orig_signature(&self) -> Signature<'_> {
unsafe { signature::from_raw_const(self, (*self.raw).orig_signature) }
}
pub fn orig_start_line(&self) -> usize {
unsafe { (*self.raw).orig_start_line_number }
}
pub fn path(&self) -> Option<&Path> {
unsafe {
if let Some(bytes) = crate::opt_bytes(self, (*self.raw).orig_path) {
Some(util::bytes2path(bytes))
} else {
None
}
}
}
pub fn is_boundary(&self) -> bool {
unsafe { (*self.raw).boundary == 1 }
}
pub fn lines_in_hunk(&self) -> usize {
unsafe { (*self.raw).lines_in_hunk as usize }
}
}
impl Default for BlameOptions {
fn default() -> Self {
Self::new()
}
}
impl BlameOptions {
pub fn new() -> BlameOptions {
unsafe {
let mut raw: raw::git_blame_options = mem::zeroed();
assert_eq!(
raw::git_blame_init_options(&mut raw, raw::GIT_BLAME_OPTIONS_VERSION),
0
);
Binding::from_raw(&raw as *const _ as *mut _)
}
}
fn flag(&mut self, opt: u32, val: bool) -> &mut BlameOptions {
if val {
self.raw.flags |= opt;
} else {
self.raw.flags &= !opt;
}
self
}
pub fn track_copies_same_file(&mut self, opt: bool) -> &mut BlameOptions {
self.flag(raw::GIT_BLAME_TRACK_COPIES_SAME_FILE, opt)
}
pub fn track_copies_same_commit_moves(&mut self, opt: bool) -> &mut BlameOptions {
self.flag(raw::GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES, opt)
}
pub fn track_copies_same_commit_copies(&mut self, opt: bool) -> &mut BlameOptions {
self.flag(raw::GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES, opt)
}
pub fn track_copies_any_commit_copies(&mut self, opt: bool) -> &mut BlameOptions {
self.flag(raw::GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES, opt)
}
pub fn first_parent(&mut self, opt: bool) -> &mut BlameOptions {
self.flag(raw::GIT_BLAME_FIRST_PARENT, opt)
}
pub fn newest_commit(&mut self, id: Oid) -> &mut BlameOptions {
unsafe {
self.raw.newest_commit = *id.raw();
}
self
}
pub fn oldest_commit(&mut self, id: Oid) -> &mut BlameOptions {
unsafe {
self.raw.oldest_commit = *id.raw();
}
self
}
}
impl<'repo> Binding for Blame<'repo> {
type Raw = *mut raw::git_blame;
unsafe fn from_raw(raw: *mut raw::git_blame) -> Blame<'repo> {
Blame {
raw: raw,
_marker: marker::PhantomData,
}
}
fn raw(&self) -> *mut raw::git_blame {
self.raw
}
}
impl<'repo> Drop for Blame<'repo> {
fn drop(&mut self) {
unsafe { raw::git_blame_free(self.raw) }
}
}
impl<'blame> Binding for BlameHunk<'blame> {
type Raw = *mut raw::git_blame_hunk;
unsafe fn from_raw(raw: *mut raw::git_blame_hunk) -> BlameHunk<'blame> {
BlameHunk {
raw: raw,
_marker: marker::PhantomData,
}
}
fn raw(&self) -> *mut raw::git_blame_hunk {
self.raw
}
}
impl Binding for BlameOptions {
type Raw = *mut raw::git_blame_options;
unsafe fn from_raw(opts: *mut raw::git_blame_options) -> BlameOptions {
BlameOptions { raw: *opts }
}
fn raw(&self) -> *mut raw::git_blame_options {
&self.raw as *const _ as *mut _
}
}
impl<'blame> Iterator for BlameIter<'blame> {
type Item = BlameHunk<'blame>;
fn next(&mut self) -> Option<BlameHunk<'blame>> {
self.range.next().and_then(|i| self.blame.get_index(i))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.range.size_hint()
}
}
impl<'blame> DoubleEndedIterator for BlameIter<'blame> {
fn next_back(&mut self) -> Option<BlameHunk<'blame>> {
self.range.next_back().and_then(|i| self.blame.get_index(i))
}
}
impl<'blame> ExactSizeIterator for BlameIter<'blame> {}
#[cfg(test)]
mod tests {
use std::fs::{self, File};
use std::path::Path;
#[test]
fn smoke() {
let (_td, repo) = crate::test::repo_init();
let mut index = repo.index().unwrap();
let root = repo.path().parent().unwrap();
fs::create_dir(&root.join("foo")).unwrap();
File::create(&root.join("foo/bar")).unwrap();
index.add_path(Path::new("foo/bar")).unwrap();
let id = index.write_tree().unwrap();
let tree = repo.find_tree(id).unwrap();
let sig = repo.signature().unwrap();
let id = repo.refname_to_id("HEAD").unwrap();
let parent = repo.find_commit(id).unwrap();
let commit = repo
.commit(Some("HEAD"), &sig, &sig, "commit", &tree, &[&parent])
.unwrap();
let blame = repo.blame_file(Path::new("foo/bar"), None).unwrap();
assert_eq!(blame.len(), 1);
assert_eq!(blame.iter().count(), 1);
let hunk = blame.get_index(0).unwrap();
assert_eq!(hunk.final_commit_id(), commit);
assert_eq!(hunk.final_signature().name(), sig.name());
assert_eq!(hunk.final_signature().email(), sig.email());
assert_eq!(hunk.final_start_line(), 1);
assert_eq!(hunk.path(), Some(Path::new("foo/bar")));
assert_eq!(hunk.lines_in_hunk(), 0);
assert!(!hunk.is_boundary())
}
}