gix_glob/search/mod.rs
1//! Utilities for searching matches of paths to patterns.
2//!
3//! Please note that these are specific to how both excludes and attributes are searched, and this is
4//! merely a way to share code among them.
5use std::path::{Path, PathBuf};
6
7///
8pub mod pattern;
9
10/// A trait to convert bytes into patterns and their associated value.
11///
12/// This is used for `gitattributes` which have a value, and `gitignore` which don't.
13pub trait Pattern: Clone + PartialEq + Eq + std::fmt::Debug + std::hash::Hash + Ord + PartialOrd + Default {
14 /// The value associated with a pattern.
15 type Value: PartialEq + Eq + std::fmt::Debug + std::hash::Hash + Ord + PartialOrd + Clone;
16
17 /// Parse all patterns in `bytes` line by line, ignoring lines with errors, and collect them.
18 fn bytes_to_patterns(bytes: &[u8], source: &Path) -> Vec<pattern::Mapping<Self::Value>>;
19}
20
21/// Add the given file at `source` if it exists, otherwise do nothing.
22/// If a `root` is provided, it's not considered a global file anymore.
23/// Returns `true` if the file was added, or `false` if it didn't exist.
24pub fn add_patterns_file<T: Pattern>(
25 patterns: &mut Vec<pattern::List<T>>,
26 source: PathBuf,
27 follow_symlinks: bool,
28 root: Option<&Path>,
29 buf: &mut Vec<u8>,
30) -> std::io::Result<bool> {
31 let previous_len = patterns.len();
32 patterns.extend(pattern::List::<T>::from_file(source, root, follow_symlinks, buf)?);
33 Ok(patterns.len() != previous_len)
34}