@@ -21,15 +21,20 @@ pub struct Match<'a> {
21
21
pub sequence_number: usize,
22
22
}
23
23
24
-
/// An implementation of the [`Pattern`] trait for ignore patterns.
25
-
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Default)]
26
-
pub struct Ignore;
24
+
/// An implementation of the [`Pattern`] trait for ignore-patterns.
25
+
#[derive(Default, PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
26
+
pub struct Ignore {
27
+
/// If `support_precious` is `true`, we will parse `$` prefixed entries as precious.
28
+
/// This is backward-incompatible as files that actually start with `$` like `$houdini`
29
+
/// will then not be ignored anymore, instead it ignores `houdini`.
30
+
pub support_precious: bool,
31
+
}
27
32
28
33
impl Pattern for Ignore {
29
34
type Value = crate::Kind;
30
35
31
-
fn bytes_to_patterns(bytes: &[u8], _source: &std::path::Path) -> Vec<pattern::Mapping<Self::Value>> {
32
-
crate::parse(bytes)
36
+
fn bytes_to_patterns(&self, bytes: &[u8], _source: &std::path::Path) -> Vec<pattern::Mapping<Self::Value>> {
37
+
crate::parse(bytes, self.support_precious)
33
38
.map(|(pattern, line_number, kind)| pattern::Mapping {
34
39
pattern,
35
40
value: kind,
@@ -44,38 +49,48 @@ impl Search {
44
49
/// Given `git_dir`, a `.git` repository, load static ignore patterns from `info/exclude`
45
50
/// and from `excludes_file` if it is provided.
46
51
/// Note that it's not considered an error if the provided `excludes_file` does not exist.
47
-
pub fn from_git_dir(git_dir: &Path, excludes_file: Option<PathBuf>, buf: &mut Vec<u8>) -> std::io::Result<Self> {
52
+
/// `parse` is a way to parse bytes to ignore patterns.
53
+
pub fn from_git_dir(
54
+
git_dir: &Path,
55
+
excludes_file: Option<PathBuf>,
56
+
buf: &mut Vec<u8>,
57
+
parse: Ignore,
58
+
) -> std::io::Result<Self> {
48
59
let mut group = Self::default();
49
60
50
61
let follow_symlinks = true;
51
62
// order matters! More important ones first.
52
63
group.patterns.extend(
53
64
excludes_file
54
-
.and_then(|file| pattern::List::<Ignore>::from_file(file, None, follow_symlinks, buf).transpose())
65
+
.and_then(|file| {
66
+
pattern::List::<Ignore>::from_file(file, None, follow_symlinks, buf, parse).transpose()
67
+
})
55
68
.transpose()?,
56
69
);
57
70
group.patterns.extend(pattern::List::<Ignore>::from_file(
58
71
git_dir.join("info").join("exclude"),
59
72
None,
60
73
follow_symlinks,
61
74
buf,
75
+
parse,
62
76
)?);
63
77
Ok(group)
64
78
}
65
79
66
80
/// Parse a list of ignore patterns, using slashes as path separators.
67
-
pub fn from_overrides(patterns: impl IntoIterator<Item = impl Into<OsString>>) -> Self {
68
-
Self::from_overrides_inner(&mut patterns.into_iter().map(Into::into))
81
+
/// `parse` is a way to parse bytes to ignore patterns.
82
+
pub fn from_overrides(patterns: impl IntoIterator<Item = impl Into<OsString>>, parse: Ignore) -> Self {
83
+
Self::from_overrides_inner(&mut patterns.into_iter().map(Into::into), parse)
69
84
}
70
85
71
-
fn from_overrides_inner(patterns: &mut dyn Iterator<Item = OsString>) -> Self {
86
+
fn from_overrides_inner(patterns: &mut dyn Iterator<Item = OsString>, parse: Ignore) -> Self {
72
87
Search {
73
88
patterns: vec![pattern::List {
74
89
patterns: patterns
75
90
.enumerate()
76
91
.filter_map(|(seq_id, pattern)| {
77
92
let pattern = gix_path::try_into_bstr(PathBuf::from(pattern)).ok()?;
78
-
crate::parse(pattern.as_ref())
93
+
crate::parse(pattern.as_ref(), parse.support_precious)
79
94
.next()
80
95
.map(|(p, _seq_id, kind)| pattern::Mapping {
81
96
pattern: p,
@@ -95,9 +110,16 @@ impl Search {
95
110
impl Search {
96
111
/// Add patterns as parsed from `bytes`, providing their `source` path and possibly their `root` path, the path they
97
112
/// are relative to. This also means that `source` is contained within `root` if `root` is provided.
98
-
pub fn add_patterns_buffer(&mut self, bytes: &[u8], source: impl Into<PathBuf>, root: Option<&Path>) {
113
+
/// Use `parse` to control how ignore patterns are parsed.
114
+
pub fn add_patterns_buffer(
115
+
&mut self,
116
+
bytes: &[u8],
117
+
source: impl Into<PathBuf>,
118
+
root: Option<&Path>,
119
+
parse: Ignore,
120
+
) {
99
121
self.patterns
100
-
.push(pattern::List::from_bytes(bytes, source.into(), root));
122
+
.push(pattern::List::from_bytes(bytes, source.into(), root, parse));
101
123
}
102
124
}
103
125
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4