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
use libc::size_t;
use std::iter::{FusedIterator, IntoIterator};
use std::marker;
use std::ops::Range;
use std::path::Path;
use std::ptr;

use crate::util::{path_to_repo_path, Binding};
use crate::{raw, Diff, DiffDelta, Error, Index, IntoCString, PathspecFlags, Repository, Tree};

/// Structure representing a compiled pathspec used for matching against various
/// structures.
pub struct Pathspec {
    raw: *mut raw::git_pathspec,
}

/// List of filenames matching a pathspec.
pub struct PathspecMatchList<'ps> {
    raw: *mut raw::git_pathspec_match_list,
    _marker: marker::PhantomData<&'ps Pathspec>,
}

/// Iterator over the matched paths in a pathspec.
pub struct PathspecEntries<'list> {
    range: Range<usize>,
    list: &'list PathspecMatchList<'list>,
}

/// Iterator over the matching diff deltas.
pub struct PathspecDiffEntries<'list> {
    range: Range<usize>,
    list: &'list PathspecMatchList<'list>,
}

/// Iterator over the failed list of pathspec items that did not match.
pub struct PathspecFailedEntries<'list> {
    range: Range<usize>,
    list: &'list PathspecMatchList<'list>,
}

impl Pathspec {
    /// Creates a new pathspec from a list of specs to match against.
    pub fn new<I, T>(specs: I) -> Result<Pathspec, Error>
    where
        T: IntoCString,
        I: IntoIterator<Item = T>,
    {
        crate::init();
        let (_a, _b, arr) = crate::util::iter2cstrs_paths(specs)?;
        unsafe {
            let mut ret = ptr::null_mut();
            try_call!(raw::git_pathspec_new(&mut ret, &arr));
            Ok(Binding::from_raw(ret))
        }
    }

    /// Match a pathspec against files in a diff.
    ///
    /// The list returned contains the list of all matched filenames (unless you
    /// pass `PATHSPEC_FAILURES_ONLY` in the flags) and may also contain the
    /// list of pathspecs with no match if the `PATHSPEC_FIND_FAILURES` flag is
    /// specified.
    pub fn match_diff(
        &self,
        diff: &Diff<'_>,
        flags: PathspecFlags,
    ) -> Result<PathspecMatchList<'_>, Error> {
        let mut ret = ptr::null_mut();
        unsafe {
            try_call!(raw::git_pathspec_match_diff(
                &mut ret,
                diff.raw(),
                flags.bits(),
                self.raw
            ));
            Ok(Binding::from_raw(ret))
        }
    }

    /// Match a pathspec against files in a tree.
    ///
    /// The list returned contains the list of all matched filenames (unless you
    /// pass `PATHSPEC_FAILURES_ONLY` in the flags) and may also contain the
    /// list of pathspecs with no match if the `PATHSPEC_FIND_FAILURES` flag is
    /// specified.
    pub fn match_tree(
        &self,
        tree: &Tree<'_>,
        flags: PathspecFlags,
    ) -> Result<PathspecMatchList<'_>, Error> {
        let mut ret = ptr::null_mut();
        unsafe {
            try_call!(raw::git_pathspec_match_tree(
                &mut ret,
                tree.raw(),
                flags.bits(),
                self.raw
            ));
            Ok(Binding::from_raw(ret))
        }
    }

    /// This matches the pathspec against the files in the repository index.
    ///
    /// The list returned contains the list of all matched filenames (unless you
    /// pass `PATHSPEC_FAILURES_ONLY` in the flags) and may also contain the
    /// list of pathspecs with no match if the `PATHSPEC_FIND_FAILURES` flag is
    /// specified.
    pub fn match_index(
        &self,
        index: &Index,
        flags: PathspecFlags,
    ) -> Result<PathspecMatchList<'_>, Error> {
        let mut ret = ptr::null_mut();
        unsafe {
            try_call!(raw::git_pathspec_match_index(
                &mut ret,
                index.raw(),
                flags.bits(),
                self.raw
            ));
            Ok(Binding::from_raw(ret))
        }
    }

    /// Match a pathspec against the working directory of a repository.
    ///
    /// This matches the pathspec against the current files in the working
    /// directory of the repository. It is an error to invoke this on a bare
    /// repo. This handles git ignores (i.e. ignored files will not be
    /// considered to match the pathspec unless the file is tracked in the
    /// index).
    ///
    /// The list returned contains the list of all matched filenames (unless you
    /// pass `PATHSPEC_FAILURES_ONLY` in the flags) and may also contain the
    /// list of pathspecs with no match if the `PATHSPEC_FIND_FAILURES` flag is
    /// specified.
    pub fn match_workdir(
        &self,
        repo: &Repository,
        flags: PathspecFlags,
    ) -> Result<PathspecMatchList<'_>, Error> {
        let mut ret = ptr::null_mut();
        unsafe {
            try_call!(raw::git_pathspec_match_workdir(
                &mut ret,
                repo.raw(),
                flags.bits(),
                self.raw
            ));
            Ok(Binding::from_raw(ret))
        }
    }

    /// Try to match a path against a pathspec
    ///
    /// Unlike most of the other pathspec matching functions, this will not fall
    /// back on the native case-sensitivity for your platform. You must
    /// explicitly pass flags to control case sensitivity or else this will fall
    /// back on being case sensitive.
    pub fn matches_path(&self, path: &Path, flags: PathspecFlags) -> bool {
        let path = path_to_repo_path(path).unwrap();
        unsafe { raw::git_pathspec_matches_path(&*self.raw, flags.bits(), path.as_ptr()) == 1 }
    }
}

impl Binding for Pathspec {
    type Raw = *mut raw::git_pathspec;

    unsafe fn from_raw(raw: *mut raw::git_pathspec) -> Pathspec {
        Pathspec { raw }
    }
    fn raw(&self) -> *mut raw::git_pathspec {
        self.raw
    }
}

impl Drop for Pathspec {
    fn drop(&mut self) {
        unsafe { raw::git_pathspec_free(self.raw) }
    }
}

impl<'ps> PathspecMatchList<'ps> {
    fn entrycount(&self) -> usize {
        unsafe { raw::git_pathspec_match_list_entrycount(&*self.raw) as usize }
    }

    fn failed_entrycount(&self) -> usize {
        unsafe { raw::git_pathspec_match_list_failed_entrycount(&*self.raw) as usize }
    }

    /// Returns an iterator over the matching filenames in this list.
    pub fn entries(&self) -> PathspecEntries<'_> {
        let n = self.entrycount();
        let n = if n > 0 && self.entry(0).is_none() {
            0
        } else {
            n
        };
        PathspecEntries {
            range: 0..n,
            list: self,
        }
    }

    /// Get a matching filename by position.
    ///
    /// If this list was generated from a diff, then the return value will
    /// always be `None.
    pub fn entry(&self, i: usize) -> Option<&[u8]> {
        unsafe {
            let ptr = raw::git_pathspec_match_list_entry(&*self.raw, i as size_t);
            crate::opt_bytes(self, ptr)
        }
    }

    /// Returns an iterator over the matching diff entries in this list.
    pub fn diff_entries(&self) -> PathspecDiffEntries<'_> {
        let n = self.entrycount();
        let n = if n > 0 && self.diff_entry(0).is_none() {
            0
        } else {
            n
        };
        PathspecDiffEntries {
            range: 0..n,
            list: self,
        }
    }

    /// Get a matching diff delta by position.
    ///
    /// If the list was not generated from a diff, then the return value will
    /// always be `None`.
    pub fn diff_entry(&self, i: usize) -> Option<DiffDelta<'_>> {
        unsafe {
            let ptr = raw::git_pathspec_match_list_diff_entry(&*self.raw, i as size_t);
            Binding::from_raw_opt(ptr as *mut _)
        }
    }

    /// Returns an iterator over the non-matching entries in this list.
    pub fn failed_entries(&self) -> PathspecFailedEntries<'_> {
        let n = self.failed_entrycount();
        let n = if n > 0 && self.failed_entry(0).is_none() {
            0
        } else {
            n
        };
        PathspecFailedEntries {
            range: 0..n,
            list: self,
        }
    }

    /// Get an original pathspec string that had no matches.
    pub fn failed_entry(&self, i: usize) -> Option<&[u8]> {
        unsafe {
            let ptr = raw::git_pathspec_match_list_failed_entry(&*self.raw, i as size_t);
            crate::opt_bytes(self, ptr)
        }
    }
}

impl<'ps> Binding for PathspecMatchList<'ps> {
    type Raw = *mut raw::git_pathspec_match_list;

    unsafe fn from_raw(raw: *mut raw::git_pathspec_match_list) -> PathspecMatchList<'ps> {
        PathspecMatchList {
            raw,
            _marker: marker::PhantomData,
        }
    }
    fn raw(&self) -> *mut raw::git_pathspec_match_list {
        self.raw
    }
}

impl<'ps> Drop for PathspecMatchList<'ps> {
    fn drop(&mut self) {
        unsafe { raw::git_pathspec_match_list_free(self.raw) }
    }
}

impl<'list> Iterator for PathspecEntries<'list> {
    type Item = &'list [u8];
    fn next(&mut self) -> Option<&'list [u8]> {
        self.range.next().and_then(|i| self.list.entry(i))
    }
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.range.size_hint()
    }
}
impl<'list> DoubleEndedIterator for PathspecEntries<'list> {
    fn next_back(&mut self) -> Option<&'list [u8]> {
        self.range.next_back().and_then(|i| self.list.entry(i))
    }
}
impl<'list> FusedIterator for PathspecEntries<'list> {}
impl<'list> ExactSizeIterator for PathspecEntries<'list> {}

impl<'list> Iterator for PathspecDiffEntries<'list> {
    type Item = DiffDelta<'list>;
    fn next(&mut self) -> Option<DiffDelta<'list>> {
        self.range.next().and_then(|i| self.list.diff_entry(i))
    }
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.range.size_hint()
    }
}
impl<'list> DoubleEndedIterator for PathspecDiffEntries<'list> {
    fn next_back(&mut self) -> Option<DiffDelta<'list>> {
        self.range.next_back().and_then(|i| self.list.diff_entry(i))
    }
}
impl<'list> FusedIterator for PathspecDiffEntries<'list> {}
impl<'list> ExactSizeIterator for PathspecDiffEntries<'list> {}

impl<'list> Iterator for PathspecFailedEntries<'list> {
    type Item = &'list [u8];
    fn next(&mut self) -> Option<&'list [u8]> {
        self.range.next().and_then(|i| self.list.failed_entry(i))
    }
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.range.size_hint()
    }
}
impl<'list> DoubleEndedIterator for PathspecFailedEntries<'list> {
    fn next_back(&mut self) -> Option<&'list [u8]> {
        self.range
            .next_back()
            .and_then(|i| self.list.failed_entry(i))
    }
}
impl<'list> FusedIterator for PathspecFailedEntries<'list> {}
impl<'list> ExactSizeIterator for PathspecFailedEntries<'list> {}

#[cfg(test)]
mod tests {
    use super::Pathspec;
    use crate::PathspecFlags;
    use std::fs::File;
    use std::path::Path;

    #[test]
    fn smoke() {
        let ps = Pathspec::new(["a"].iter()).unwrap();
        assert!(ps.matches_path(Path::new("a"), PathspecFlags::DEFAULT));
        assert!(ps.matches_path(Path::new("a/b"), PathspecFlags::DEFAULT));
        assert!(!ps.matches_path(Path::new("b"), PathspecFlags::DEFAULT));
        assert!(!ps.matches_path(Path::new("ab/c"), PathspecFlags::DEFAULT));

        let (td, repo) = crate::test::repo_init();
        let list = ps.match_workdir(&repo, PathspecFlags::DEFAULT).unwrap();
        assert_eq!(list.entries().len(), 0);
        assert_eq!(list.diff_entries().len(), 0);
        assert_eq!(list.failed_entries().len(), 0);

        File::create(&td.path().join("a")).unwrap();

        let list = ps
            .match_workdir(&repo, crate::PathspecFlags::FIND_FAILURES)
            .unwrap();
        assert_eq!(list.entries().len(), 1);
        assert_eq!(list.entries().next(), Some("a".as_bytes()));
    }
}