@@ -3,6 +3,8 @@ use std::{
3
3
path:: { Path , PathBuf } ,
4
4
} ;
5
5
6
+ use fast_glob:: glob_match;
7
+
6
8
use super :: FileObj ;
7
9
8
10
#[ derive( Debug , Clone ) ]
@@ -76,35 +78,41 @@ impl FileFilter {
76
78
}
77
79
}
78
80
79
- /// Describes if a specified `file_name` is contained within the given ` set` of paths.
81
+ /// Describes if a specified `file_name` is contained within the specified set of paths.
80
82
///
81
- /// The `is_ignored` flag describes which list of paths is used as domains.
83
+ /// The `is_ignored` flag describes which set of paths is used as domains.
82
84
/// The specified `file_name` can be a direct or distant descendant of any paths in
83
- /// the list .
85
+ /// the set .
84
86
///
85
- /// Returns a [`Some`] value of the the path/pattern that matches the given `file_name`.
86
- /// If given `file_name` is not in the specified list , then [`None`] is returned.
87
- pub fn is_file_in_list ( & self , file_name : & Path , is_ignored : bool ) -> Option < String > {
87
+ /// Returns a `true` value of the the path/pattern that matches the given `file_name`.
88
+ /// If given `file_name` is not in the specified set , then `false` is returned.
89
+ pub fn is_file_in_list ( & self , file_name : & Path , is_ignored : bool ) -> bool {
88
90
let file_name = PathBuf :: from ( format ! (
89
91
"./{}" ,
90
92
file_name
91
93
. as_os_str( )
92
94
. to_string_lossy( )
93
95
. to_string( )
94
96
. replace( "\\ " , "/" )
97
+ . trim_start_matches( "./" )
95
98
) ) ;
96
99
let set = if is_ignored {
97
100
& self . ignored
98
101
} else {
99
102
& self . not_ignored
100
103
} ;
101
104
for pattern in set {
105
+ let glob_matched =
106
+ glob_match ( pattern, file_name. to_string_lossy ( ) . to_string ( ) . as_str ( ) ) ;
102
107
let pat = PathBuf :: from ( & pattern) ;
103
- if ( pat. is_file ( ) && file_name == pat) || ( pat. is_dir ( ) && file_name. starts_with ( pat) ) {
104
- return Some ( pattern. to_owned ( ) ) ;
108
+ if glob_matched
109
+ || ( pat. is_file ( ) && file_name == pat)
110
+ || ( pat. is_dir ( ) && file_name. starts_with ( pat) )
111
+ {
112
+ return true ;
105
113
}
106
114
}
107
- None
115
+ false
108
116
}
109
117
110
118
/// A helper function that checks if `entry` satisfies the following conditions (in
@@ -130,7 +138,7 @@ impl FileFilter {
130
138
if !is_ignored {
131
139
let is_in_ignored = self . is_file_in_list ( entry, true ) ;
132
140
let is_in_not_ignored = self . is_file_in_list ( entry, false ) ;
133
- if is_in_not_ignored. is_some ( ) || is_in_ignored. is_none ( ) {
141
+ if is_in_not_ignored || ! is_in_ignored {
134
142
return true ;
135
143
}
136
144
}
@@ -145,7 +153,7 @@ impl FileFilter {
145
153
pub fn list_source_files ( & self , root_path : & str ) -> Vec < FileObj > {
146
154
let mut files: Vec < FileObj > = Vec :: new ( ) ;
147
155
let entries = fs:: read_dir ( root_path) . expect ( "repo root-path should exist" ) ;
148
- for entry in entries. flatten ( ) {
156
+ for entry in entries. filter_map ( |p| p . ok ( ) ) {
149
157
let path = entry. path ( ) ;
150
158
if path. is_dir ( ) {
151
159
let mut is_hidden = false ;
@@ -196,34 +204,31 @@ mod tests {
196
204
#[ test]
197
205
fn ignore_src ( ) {
198
206
let file_filter = setup_ignore ( "src" , vec ! [ ] ) ;
199
- assert ! ( file_filter
200
- . is_file_in_list( & PathBuf :: from( "./src/lib.rs" ) , true )
201
- . is_some( ) ) ;
202
- assert ! ( file_filter
203
- . is_file_in_list( & PathBuf :: from( "./src/lib.rs" ) , false )
204
- . is_none( ) ) ;
207
+ assert ! ( file_filter. is_file_in_list( & PathBuf :: from( "./src/lib.rs" ) , true ) ) ;
208
+ assert ! ( !file_filter. is_file_in_list( & PathBuf :: from( "./src/lib.rs" ) , false ) ) ;
205
209
}
206
210
207
211
#[ test]
208
212
fn ignore_root ( ) {
209
213
let file_filter = setup_ignore ( "!src/lib.rs|./" , vec ! [ ] ) ;
210
- assert ! ( file_filter
211
- . is_file_in_list( & PathBuf :: from( "./cargo.toml" ) , true )
212
- . is_some( ) ) ;
213
- assert ! ( file_filter
214
- . is_file_in_list( & PathBuf :: from( "./src/lib.rs" ) , false )
215
- . is_some( ) ) ;
214
+ assert ! ( file_filter. is_file_in_list( & PathBuf :: from( "./Cargo.toml" ) , true ) ) ;
215
+ assert ! ( file_filter. is_file_in_list( & PathBuf :: from( "./src/lib.rs" ) , false ) ) ;
216
216
}
217
217
218
218
#[ test]
219
219
fn ignore_root_implicit ( ) {
220
220
let file_filter = setup_ignore ( "!src|" , vec ! [ ] ) ;
221
- assert ! ( file_filter
222
- . is_file_in_list( & PathBuf :: from( "./cargo.toml" ) , true )
223
- . is_some( ) ) ;
224
- assert ! ( file_filter
225
- . is_file_in_list( & PathBuf :: from( "./src/lib.rs" ) , false )
226
- . is_some( ) ) ;
221
+ assert ! ( file_filter. is_file_in_list( & PathBuf :: from( "./Cargo.toml" ) , true ) ) ;
222
+ assert ! ( file_filter. is_file_in_list( & PathBuf :: from( "./src/lib.rs" ) , false ) ) ;
223
+ }
224
+
225
+ #[ test]
226
+ fn ignore_glob ( ) {
227
+ let file_filter = setup_ignore ( "!src/**/*" , vec ! [ ] ) ;
228
+ assert ! ( file_filter. is_file_in_list( & PathBuf :: from( "./src/lib.rs" ) , false ) ) ;
229
+ assert ! (
230
+ file_filter. is_file_in_list( & PathBuf :: from( "./src/common_fs/file_filter.rs" ) , false )
231
+ ) ;
227
232
}
228
233
229
234
#[ test]
@@ -235,17 +240,13 @@ mod tests {
235
240
// using Vec::contains() because these files don't actually exist in project files
236
241
for ignored_submodule in [ "./RF24" , "./RF24Network" , "./RF24Mesh" ] {
237
242
assert ! ( file_filter. ignored. contains( & ignored_submodule. to_string( ) ) ) ;
238
- assert ! ( file_filter
239
- . is_file_in_list(
240
- & PathBuf :: from( ignored_submodule. to_string( ) + "/some_src.cpp" ) ,
241
- true
242
- )
243
- . is_none( ) ) ;
243
+ assert ! ( !file_filter. is_file_in_list(
244
+ & PathBuf :: from( ignored_submodule. to_string( ) + "/some_src.cpp" ) ,
245
+ true
246
+ ) ) ;
244
247
}
245
248
assert ! ( file_filter. not_ignored. contains( & "./pybind11" . to_string( ) ) ) ;
246
- assert ! ( file_filter
247
- . is_file_in_list( & PathBuf :: from( "./pybind11/some_src.cpp" ) , false )
248
- . is_none( ) ) ;
249
+ assert ! ( !file_filter. is_file_in_list( & PathBuf :: from( "./pybind11/some_src.cpp" ) , false ) ) ;
249
250
}
250
251
251
252
// *********************** tests for recursive path search
0 commit comments