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
use libc::c_uint;
use std::ffi::CString;
use std::marker;
use crate::util::Binding;
use crate::{raw, Error, Oid, Repository, Sort};
pub struct Revwalk<'repo> {
raw: *mut raw::git_revwalk,
_marker: marker::PhantomData<&'repo Repository>,
}
impl<'repo> Revwalk<'repo> {
pub fn reset(&mut self) {
unsafe { raw::git_revwalk_reset(self.raw()) }
}
pub fn set_sorting(&mut self, sort_mode: Sort) {
unsafe { raw::git_revwalk_sorting(self.raw(), sort_mode.bits() as c_uint) }
}
pub fn simplify_first_parent(&mut self) {
unsafe { raw::git_revwalk_simplify_first_parent(self.raw) }
}
pub fn push(&mut self, oid: Oid) -> Result<(), Error> {
unsafe {
try_call!(raw::git_revwalk_push(self.raw(), oid.raw()));
}
Ok(())
}
pub fn push_head(&mut self) -> Result<(), Error> {
unsafe {
try_call!(raw::git_revwalk_push_head(self.raw()));
}
Ok(())
}
pub fn push_glob(&mut self, glob: &str) -> Result<(), Error> {
let glob = CString::new(glob)?;
unsafe {
try_call!(raw::git_revwalk_push_glob(self.raw, glob));
}
Ok(())
}
pub fn push_range(&mut self, range: &str) -> Result<(), Error> {
let range = CString::new(range)?;
unsafe {
try_call!(raw::git_revwalk_push_range(self.raw, range));
}
Ok(())
}
pub fn push_ref(&mut self, reference: &str) -> Result<(), Error> {
let reference = CString::new(reference)?;
unsafe {
try_call!(raw::git_revwalk_push_ref(self.raw, reference));
}
Ok(())
}
pub fn hide(&mut self, oid: Oid) -> Result<(), Error> {
unsafe {
try_call!(raw::git_revwalk_hide(self.raw(), oid.raw()));
}
Ok(())
}
pub fn hide_head(&mut self) -> Result<(), Error> {
unsafe {
try_call!(raw::git_revwalk_hide_head(self.raw()));
}
Ok(())
}
pub fn hide_glob(&mut self, glob: &str) -> Result<(), Error> {
let glob = CString::new(glob)?;
unsafe {
try_call!(raw::git_revwalk_hide_glob(self.raw, glob));
}
Ok(())
}
pub fn hide_ref(&mut self, reference: &str) -> Result<(), Error> {
let reference = CString::new(reference)?;
unsafe {
try_call!(raw::git_revwalk_hide_ref(self.raw, reference));
}
Ok(())
}
}
impl<'repo> Binding for Revwalk<'repo> {
type Raw = *mut raw::git_revwalk;
unsafe fn from_raw(raw: *mut raw::git_revwalk) -> Revwalk<'repo> {
Revwalk {
raw: raw,
_marker: marker::PhantomData,
}
}
fn raw(&self) -> *mut raw::git_revwalk {
self.raw
}
}
impl<'repo> Drop for Revwalk<'repo> {
fn drop(&mut self) {
unsafe { raw::git_revwalk_free(self.raw) }
}
}
impl<'repo> Iterator for Revwalk<'repo> {
type Item = Result<Oid, Error>;
fn next(&mut self) -> Option<Result<Oid, Error>> {
let mut out: raw::git_oid = raw::git_oid {
id: [0; raw::GIT_OID_RAWSZ],
};
unsafe {
try_call_iter!(raw::git_revwalk_next(&mut out, self.raw()));
Some(Ok(Binding::from_raw(&out as *const _)))
}
}
}
#[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 mut walk = repo.revwalk().unwrap();
walk.push(target).unwrap();
let oids: Vec<crate::Oid> = walk.by_ref().collect::<Result<Vec<_>, _>>().unwrap();
assert_eq!(oids.len(), 1);
assert_eq!(oids[0], target);
walk.reset();
walk.push_head().unwrap();
assert_eq!(walk.by_ref().count(), 1);
walk.reset();
walk.push_head().unwrap();
walk.hide_head().unwrap();
assert_eq!(walk.by_ref().count(), 0);
}
}