pub struct FileFilter {
pub ignored: HashSet<String>,
pub not_ignored: HashSet<String>,
pub extensions: HashSet<String>,
/* private fields */
}file-changes only.Expand description
A structure to encapsulate file path filtering behavior.
Fields§
§ignored: HashSet<String>A set of paths or glob patterns to be ignored.
These paths/patterns are relative to the working directory. An empty entry represents the working directory itself.
not_ignored: HashSet<String>A set of paths or glob patterns to be explicitly not ignored.
These paths/patterns are relative to the working directory. An empty entry represents the working directory itself.
extensions: HashSet<String>A set of valid file extensions.
These extensions do not include the leading dot. For example, use “txt” instead of “.txt”.
A blank extension ("") can be used to match files with
no extension (eg. “.clang-format”).
Implementations§
Source§impl FileFilter
impl FileFilter
Sourcepub fn new(
ignore: &[&str],
extensions: &[&str],
log_scope: Option<&str>,
) -> Self
pub fn new( ignore: &[&str], extensions: &[&str], log_scope: Option<&str>, ) -> Self
Construct a FileFilter instance.
The ignore parameter is a list of paths (or glob patterns).
These paths/patterns get split into 2 sets: Self::ignored and Self::not_ignored.
A path/pattern is explicitly not ignored if it is prefixed with !.
Otherwise it is ignored.
Leading and trailing spaces are stripped from each item in the ignore list.
Also, leading ./ sequences are stripped.
#[cfg(feature = "file-changes")]
use git_bot_feedback::FileFilter;
let ignore_list = [" src ", " ! src/lib.rs "];
let extensions = ["rs", "toml"];
let filter = FileFilter::new(
&ignore_list,
&extensions,
None, // optional log scope for debug messages
);
assert!(filter.ignored.contains("src"));
assert!(filter.not_ignored.contains("src/lib.rs"));Note, the log_scope parameter is used to differentiate debug log messages
from different FileFilter instances.
Sourcepub fn parse_submodules(&mut self, manifest_path: Option<&Path>)
pub fn parse_submodules(&mut self, manifest_path: Option<&Path>)
This function will read a .gitmodules file located in the working directory.
The named submodules’ paths will be automatically added to the FileFilter::ignored set,
unless the submodule’s path is already specified in the FileFilter::not_ignored set.
The optional manifest_path parameter can be used to specify a custom path to the .gitmodules file.
If None, a .gitmodules file in the working directory is sought.
It is important that the Self::ignored and Self::not_ignored should be
relative to the manifest_path’s parent directory, because the
paths in the .gitmodules file are relative to it’s own directory.
Sourcepub fn is_file_in_list(&self, file_name: &Path, is_ignored: bool) -> bool
pub fn is_file_in_list(&self, file_name: &Path, is_ignored: bool) -> bool
Describes if a specified file_name is contained within the specified set of paths.
The is_ignored flag describes which set of paths is used as domains.
The specified file_name can be a direct or distant descendant of any
paths in the set.
Returns a true value of the the path/pattern that matches the given file_name.
If given file_name is not in the specified set, then false is returned.
Sourcepub fn is_file_ignored(&self, file_name: &Path) -> bool
pub fn is_file_ignored(&self, file_name: &Path) -> bool
Convenience function to check if a given file_name is ignored.
Equivalent to calling
file_filter.is_file_in_list(file_name, true).
Sourcepub fn is_file_not_ignored(&self, file_name: &Path) -> bool
pub fn is_file_not_ignored(&self, file_name: &Path) -> bool
Convenience function to check if a given file_name is not ignored.
Equivalent to calling
file_filter.is_file_in_list(file_name, false).
Sourcepub fn is_qualified(&self, file_path: &Path) -> bool
pub fn is_qualified(&self, file_path: &Path) -> bool
A function that checks if file_path satisfies the following conditions (in
ordered priority):
- Does
file_pathuse at least 1 ofFileFilter::extensions? Not applicable ifFileFilter::extensionsis empty. - Is
file_pathspecified inFileFilter::not_ignored? - Is
file_pathnot specified inFileFilter::ignored? - Is
file_pathnot a hidden path (any parts of the path start with “.”)? Mutually exclusive with last condition; does not apply to “./” or “../”.
Note, the given file_path should be relative to the same directory that
the paths in FileFilter::ignored and FileFilter::not_ignored are relative to.
Sourcepub fn walk_dir<P: AsRef<Path>>(
&self,
root_path: P,
) -> Result<HashSet<String>, DirWalkError>
pub fn walk_dir<P: AsRef<Path>>( &self, root_path: P, ) -> Result<HashSet<String>, DirWalkError>
Walks a given root_path recursively and returns a set of discovered source files.
Only files that satisfy the following conditions are included in the returned set:
- uses at least 1 of the given
FileFilter::extensions. - is specified in the internal list
FileFilter::not_ignoredpaths/patterns - is not specified in the set of
FileFilter::ignoredpaths/patterns and is not a hidden path (starts with “.”).
The file paths returned (as strings with posix style path separators) will be relative to the given path.