|
| 1 | +use crate::Search; |
| 2 | +use bstr::{BStr, ByteSlice}; |
| 3 | +use gix_glob::search::{pattern, Pattern}; |
| 4 | +use std::ffi::OsString; |
| 5 | +use std::path::{Path, PathBuf}; |
| 6 | + |
| 7 | +/// Describes a matching pattern within a search for ignored paths. |
| 8 | +#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)] |
| 9 | +pub struct Match<'a, T> { |
| 10 | + /// The glob pattern itself, like `/target/*`. |
| 11 | + pub pattern: &'a gix_glob::Pattern, |
| 12 | + /// The value associated with the pattern. |
| 13 | + pub value: &'a T, |
| 14 | + /// The path to the source from which the pattern was loaded, or `None` if it was specified by other means. |
| 15 | + pub source: Option<&'a Path>, |
| 16 | + /// The line at which the pattern was found in its `source` file, or the occurrence in which it was provided. |
| 17 | + pub sequence_number: usize, |
| 18 | +} |
| 19 | + |
| 20 | +/// An implementation of the [`Pattern`] trait for ignore patterns. |
| 21 | +#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Default)] |
| 22 | +pub struct Ignore; |
| 23 | + |
| 24 | +impl Pattern for Ignore { |
| 25 | + type Value = (); |
| 26 | + |
| 27 | + fn bytes_to_patterns(bytes: &[u8], _source: &std::path::Path) -> Vec<pattern::Mapping<Self::Value>> { |
| 28 | + crate::parse(bytes) |
| 29 | + .map(|(pattern, line_number)| pattern::Mapping { |
| 30 | + pattern, |
| 31 | + value: (), |
| 32 | + sequence_number: line_number, |
| 33 | + }) |
| 34 | + .collect() |
| 35 | + } |
| 36 | + |
| 37 | + fn may_use_glob_pattern(_pattern: &gix_glob::Pattern) -> bool { |
| 38 | + true |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/// Instantiation of a search for ignore patterns. |
| 43 | +impl Search { |
| 44 | + /// Given `git_dir`, a `.git` repository, load static ignore patterns from `info/exclude` |
| 45 | + /// and from `excludes_file` if it is provided. |
| 46 | + /// Note that it's not considered an error if the provided `excludes_file` does not exist. |
| 47 | + pub fn from_git_dir( |
| 48 | + git_dir: impl AsRef<Path>, |
| 49 | + excludes_file: Option<PathBuf>, |
| 50 | + buf: &mut Vec<u8>, |
| 51 | + ) -> std::io::Result<Self> { |
| 52 | + let mut group = Self::default(); |
| 53 | + |
| 54 | + let follow_symlinks = true; |
| 55 | + // order matters! More important ones first. |
| 56 | + group.patterns.extend( |
| 57 | + excludes_file |
| 58 | + .and_then(|file| pattern::List::<Ignore>::from_file(file, None, follow_symlinks, buf).transpose()) |
| 59 | + .transpose()?, |
| 60 | + ); |
| 61 | + group.patterns.extend(pattern::List::<Ignore>::from_file( |
| 62 | + git_dir.as_ref().join("info").join("exclude"), |
| 63 | + None, |
| 64 | + follow_symlinks, |
| 65 | + buf, |
| 66 | + )?); |
| 67 | + Ok(group) |
| 68 | + } |
| 69 | + |
| 70 | + /// Parse a list of patterns, using slashes as path separators |
| 71 | + pub fn from_overrides(patterns: impl IntoIterator<Item = impl Into<OsString>>) -> Self { |
| 72 | + Search { |
| 73 | + patterns: vec![pattern::List { |
| 74 | + patterns: patterns |
| 75 | + .into_iter() |
| 76 | + .map(Into::into) |
| 77 | + .enumerate() |
| 78 | + .filter_map(|(seq_id, pattern)| { |
| 79 | + let pattern = gix_path::try_into_bstr(PathBuf::from(pattern)).ok()?; |
| 80 | + gix_glob::parse(pattern.as_ref()).map(|p| pattern::Mapping { |
| 81 | + pattern: p, |
| 82 | + value: (), |
| 83 | + sequence_number: seq_id, |
| 84 | + }) |
| 85 | + }) |
| 86 | + .collect(), |
| 87 | + source: None, |
| 88 | + base: None, |
| 89 | + }], |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/// Mutation |
| 95 | +impl Search { |
| 96 | + /// Add patterns as parsed from `bytes`, providing their `source` path and possibly their `root` path, the path they |
| 97 | + /// 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>) { |
| 99 | + self.patterns |
| 100 | + .push(pattern::List::from_bytes(bytes, source.into(), root)); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +/// Return a match if a pattern matches `relative_path`, providing a pre-computed `basename_pos` which is the |
| 105 | +/// starting position of the basename of `relative_path`. `is_dir` is true if `relative_path` is a directory. |
| 106 | +/// `case` specifies whether cases should be folded during matching or not. |
| 107 | +pub fn pattern_matching_relative_path<'a>( |
| 108 | + list: &'a gix_glob::search::pattern::List<Ignore>, |
| 109 | + relative_path: &BStr, |
| 110 | + basename_pos: Option<usize>, |
| 111 | + is_dir: Option<bool>, |
| 112 | + case: gix_glob::pattern::Case, |
| 113 | +) -> Option<Match<'a, ()>> { |
| 114 | + let (relative_path, basename_start_pos) = |
| 115 | + list.strip_base_handle_recompute_basename_pos(relative_path, basename_pos, case)?; |
| 116 | + list.patterns |
| 117 | + .iter() |
| 118 | + .rev() |
| 119 | + .filter(|pm| Ignore::may_use_glob_pattern(&pm.pattern)) |
| 120 | + .find_map( |
| 121 | + |pattern::Mapping { |
| 122 | + pattern, |
| 123 | + value, |
| 124 | + sequence_number, |
| 125 | + }| { |
| 126 | + pattern |
| 127 | + .matches_repo_relative_path(relative_path, basename_start_pos, is_dir, case) |
| 128 | + .then_some(Match { |
| 129 | + pattern, |
| 130 | + value, |
| 131 | + source: list.source.as_deref(), |
| 132 | + sequence_number: *sequence_number, |
| 133 | + }) |
| 134 | + }, |
| 135 | + ) |
| 136 | +} |
| 137 | + |
| 138 | +/// Like [`pattern_matching_relative_path()`], but returns an index to the pattern |
| 139 | +/// that matched `relative_path`, instead of the match itself. |
| 140 | +pub fn pattern_idx_matching_relative_path( |
| 141 | + list: &gix_glob::search::pattern::List<Ignore>, |
| 142 | + relative_path: &BStr, |
| 143 | + basename_pos: Option<usize>, |
| 144 | + is_dir: Option<bool>, |
| 145 | + case: gix_glob::pattern::Case, |
| 146 | +) -> Option<usize> { |
| 147 | + let (relative_path, basename_start_pos) = |
| 148 | + list.strip_base_handle_recompute_basename_pos(relative_path, basename_pos, case)?; |
| 149 | + list.patterns |
| 150 | + .iter() |
| 151 | + .enumerate() |
| 152 | + .rev() |
| 153 | + .filter(|(_, pm)| Ignore::may_use_glob_pattern(&pm.pattern)) |
| 154 | + .find_map(|(idx, pm)| { |
| 155 | + pm.pattern |
| 156 | + .matches_repo_relative_path(relative_path, basename_start_pos, is_dir, case) |
| 157 | + .then_some(idx) |
| 158 | + }) |
| 159 | +} |
| 160 | + |
| 161 | +/// Matching of ignore patterns. |
| 162 | +impl Search { |
| 163 | + /// Match `relative_path` and return the first match if found. |
| 164 | + /// `is_dir` is true if `relative_path` is a directory. |
| 165 | + /// `case` specifies whether cases should be folded during matching or not. |
| 166 | + pub fn pattern_matching_relative_path<'a>( |
| 167 | + &self, |
| 168 | + relative_path: impl Into<&'a BStr>, |
| 169 | + is_dir: Option<bool>, |
| 170 | + case: gix_glob::pattern::Case, |
| 171 | + ) -> Option<Match<'_, ()>> { |
| 172 | + let relative_path = relative_path.into(); |
| 173 | + let basename_pos = relative_path.rfind(b"/").map(|p| p + 1); |
| 174 | + self.patterns |
| 175 | + .iter() |
| 176 | + .rev() |
| 177 | + .find_map(|pl| pattern_matching_relative_path(pl, relative_path, basename_pos, is_dir, case)) |
| 178 | + } |
| 179 | +} |
0 commit comments