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
use libc::{c_char, c_uint, size_t};
use std::ffi::CString;
use std::marker;
use std::mem;
use std::ops::Range;
use std::str;
use crate::util::Binding;
use crate::{raw, DiffDelta, IntoCString, Repository, Status};
pub struct StatusOptions {
raw: raw::git_status_options,
pathspec: Vec<CString>,
ptrs: Vec<*const c_char>,
}
#[derive(Copy, Clone)]
pub enum StatusShow {
Index,
Workdir,
IndexAndWorkdir,
}
pub struct Statuses<'repo> {
raw: *mut raw::git_status_list,
_marker: marker::PhantomData<&'repo Repository>,
}
pub struct StatusIter<'statuses> {
statuses: &'statuses Statuses<'statuses>,
range: Range<usize>,
}
pub struct StatusEntry<'statuses> {
raw: *const raw::git_status_entry,
_marker: marker::PhantomData<&'statuses DiffDelta<'statuses>>,
}
impl Default for StatusOptions {
fn default() -> Self {
Self::new()
}
}
impl StatusOptions {
pub fn new() -> StatusOptions {
unsafe {
let mut raw = mem::zeroed();
let r = raw::git_status_init_options(&mut raw, raw::GIT_STATUS_OPTIONS_VERSION);
assert_eq!(r, 0);
StatusOptions {
raw: raw,
pathspec: Vec::new(),
ptrs: Vec::new(),
}
}
}
pub fn show(&mut self, show: StatusShow) -> &mut StatusOptions {
self.raw.show = match show {
StatusShow::Index => raw::GIT_STATUS_SHOW_INDEX_ONLY,
StatusShow::Workdir => raw::GIT_STATUS_SHOW_WORKDIR_ONLY,
StatusShow::IndexAndWorkdir => raw::GIT_STATUS_SHOW_INDEX_AND_WORKDIR,
};
self
}
pub fn pathspec<T: IntoCString>(&mut self, pathspec: T) -> &mut StatusOptions {
let s = pathspec.into_c_string().unwrap();
self.ptrs.push(s.as_ptr());
self.pathspec.push(s);
self
}
fn flag(&mut self, flag: raw::git_status_opt_t, val: bool) -> &mut StatusOptions {
if val {
self.raw.flags |= flag as c_uint;
} else {
self.raw.flags &= !(flag as c_uint);
}
self
}
pub fn include_untracked(&mut self, include: bool) -> &mut StatusOptions {
self.flag(raw::GIT_STATUS_OPT_INCLUDE_UNTRACKED, include)
}
pub fn include_ignored(&mut self, include: bool) -> &mut StatusOptions {
self.flag(raw::GIT_STATUS_OPT_INCLUDE_IGNORED, include)
}
pub fn include_unmodified(&mut self, include: bool) -> &mut StatusOptions {
self.flag(raw::GIT_STATUS_OPT_INCLUDE_UNMODIFIED, include)
}
pub fn exclude_submodules(&mut self, exclude: bool) -> &mut StatusOptions {
self.flag(raw::GIT_STATUS_OPT_EXCLUDE_SUBMODULES, exclude)
}
pub fn recurse_untracked_dirs(&mut self, include: bool) -> &mut StatusOptions {
self.flag(raw::GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS, include)
}
pub fn disable_pathspec_match(&mut self, include: bool) -> &mut StatusOptions {
self.flag(raw::GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH, include)
}
pub fn recurse_ignored_dirs(&mut self, include: bool) -> &mut StatusOptions {
self.flag(raw::GIT_STATUS_OPT_RECURSE_IGNORED_DIRS, include)
}
pub fn renames_head_to_index(&mut self, include: bool) -> &mut StatusOptions {
self.flag(raw::GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX, include)
}
pub fn renames_index_to_workdir(&mut self, include: bool) -> &mut StatusOptions {
self.flag(raw::GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR, include)
}
pub fn sort_case_sensitively(&mut self, include: bool) -> &mut StatusOptions {
self.flag(raw::GIT_STATUS_OPT_SORT_CASE_SENSITIVELY, include)
}
pub fn sort_case_insensitively(&mut self, include: bool) -> &mut StatusOptions {
self.flag(raw::GIT_STATUS_OPT_SORT_CASE_INSENSITIVELY, include)
}
pub fn renames_from_rewrites(&mut self, include: bool) -> &mut StatusOptions {
self.flag(raw::GIT_STATUS_OPT_RENAMES_FROM_REWRITES, include)
}
pub fn no_refresh(&mut self, include: bool) -> &mut StatusOptions {
self.flag(raw::GIT_STATUS_OPT_NO_REFRESH, include)
}
pub fn update_index(&mut self, include: bool) -> &mut StatusOptions {
self.flag(raw::GIT_STATUS_OPT_UPDATE_INDEX, include)
}
#[allow(missing_docs)]
pub fn include_unreadable(&mut self, include: bool) -> &mut StatusOptions {
self.flag(raw::GIT_STATUS_OPT_INCLUDE_UNREADABLE, include)
}
#[allow(missing_docs)]
pub fn include_unreadable_as_untracked(&mut self, include: bool) -> &mut StatusOptions {
self.flag(raw::GIT_STATUS_OPT_INCLUDE_UNREADABLE_AS_UNTRACKED, include)
}
pub unsafe fn raw(&mut self) -> *const raw::git_status_options {
self.raw.pathspec.strings = self.ptrs.as_ptr() as *mut _;
self.raw.pathspec.count = self.ptrs.len() as size_t;
&self.raw
}
}
impl<'repo> Statuses<'repo> {
pub fn get(&self, index: usize) -> Option<StatusEntry<'_>> {
unsafe {
let p = raw::git_status_byindex(self.raw, index as size_t);
Binding::from_raw_opt(p)
}
}
pub fn len(&self) -> usize {
unsafe { raw::git_status_list_entrycount(self.raw) as usize }
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn iter(&self) -> StatusIter<'_> {
StatusIter {
statuses: self,
range: 0..self.len(),
}
}
}
impl<'repo> Binding for Statuses<'repo> {
type Raw = *mut raw::git_status_list;
unsafe fn from_raw(raw: *mut raw::git_status_list) -> Statuses<'repo> {
Statuses {
raw: raw,
_marker: marker::PhantomData,
}
}
fn raw(&self) -> *mut raw::git_status_list {
self.raw
}
}
impl<'repo> Drop for Statuses<'repo> {
fn drop(&mut self) {
unsafe {
raw::git_status_list_free(self.raw);
}
}
}
impl<'a> Iterator for StatusIter<'a> {
type Item = StatusEntry<'a>;
fn next(&mut self) -> Option<StatusEntry<'a>> {
self.range.next().and_then(|i| self.statuses.get(i))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.range.size_hint()
}
}
impl<'a> DoubleEndedIterator for StatusIter<'a> {
fn next_back(&mut self) -> Option<StatusEntry<'a>> {
self.range.next_back().and_then(|i| self.statuses.get(i))
}
}
impl<'a> ExactSizeIterator for StatusIter<'a> {}
impl<'statuses> StatusEntry<'statuses> {
pub fn path_bytes(&self) -> &[u8] {
unsafe {
if (*self.raw).head_to_index.is_null() {
crate::opt_bytes(self, (*(*self.raw).index_to_workdir).old_file.path)
} else {
crate::opt_bytes(self, (*(*self.raw).head_to_index).old_file.path)
}
.unwrap()
}
}
pub fn path(&self) -> Option<&str> {
str::from_utf8(self.path_bytes()).ok()
}
pub fn status(&self) -> Status {
Status::from_bits_truncate(unsafe { (*self.raw).status as u32 })
}
pub fn head_to_index(&self) -> Option<DiffDelta<'statuses>> {
unsafe { Binding::from_raw_opt((*self.raw).head_to_index) }
}
pub fn index_to_workdir(&self) -> Option<DiffDelta<'statuses>> {
unsafe { Binding::from_raw_opt((*self.raw).index_to_workdir) }
}
}
impl<'statuses> Binding for StatusEntry<'statuses> {
type Raw = *const raw::git_status_entry;
unsafe fn from_raw(raw: *const raw::git_status_entry) -> StatusEntry<'statuses> {
StatusEntry {
raw: raw,
_marker: marker::PhantomData,
}
}
fn raw(&self) -> *const raw::git_status_entry {
self.raw
}
}
#[cfg(test)]
mod tests {
use super::StatusOptions;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
#[test]
fn smoke() {
let (td, repo) = crate::test::repo_init();
assert_eq!(repo.statuses(None).unwrap().len(), 0);
File::create(&td.path().join("foo")).unwrap();
let statuses = repo.statuses(None).unwrap();
assert_eq!(statuses.iter().count(), 1);
let status = statuses.iter().next().unwrap();
assert_eq!(status.path(), Some("foo"));
assert!(status.status().contains(crate::Status::WT_NEW));
assert!(!status.status().contains(crate::Status::INDEX_NEW));
assert!(status.head_to_index().is_none());
let diff = status.index_to_workdir().unwrap();
assert_eq!(diff.old_file().path_bytes().unwrap(), b"foo");
assert_eq!(diff.new_file().path_bytes().unwrap(), b"foo");
}
#[test]
fn filter() {
let (td, repo) = crate::test::repo_init();
t!(File::create(&td.path().join("foo")));
t!(File::create(&td.path().join("bar")));
let mut opts = StatusOptions::new();
opts.include_untracked(true).pathspec("foo");
let statuses = t!(repo.statuses(Some(&mut opts)));
assert_eq!(statuses.iter().count(), 1);
let status = statuses.iter().next().unwrap();
assert_eq!(status.path(), Some("foo"));
}
#[test]
fn gitignore() {
let (td, repo) = crate::test::repo_init();
t!(t!(File::create(td.path().join(".gitignore"))).write_all(b"foo\n"));
assert!(!t!(repo.status_should_ignore(Path::new("bar"))));
assert!(t!(repo.status_should_ignore(Path::new("foo"))));
}
#[test]
fn status_file() {
let (td, repo) = crate::test::repo_init();
assert!(repo.status_file(Path::new("foo")).is_err());
if cfg!(windows) {
assert!(repo.status_file(Path::new("bar\\foo.txt")).is_err());
}
t!(File::create(td.path().join("foo")));
if cfg!(windows) {
t!(::std::fs::create_dir_all(td.path().join("bar")));
t!(File::create(td.path().join("bar").join("foo.txt")));
}
let status = t!(repo.status_file(Path::new("foo")));
assert!(status.contains(crate::Status::WT_NEW));
if cfg!(windows) {
let status = t!(repo.status_file(Path::new("bar\\foo.txt")));
assert!(status.contains(crate::Status::WT_NEW));
}
}
}