1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//! Ignore patterns for a directory and its child directories.
use crate::{Result, Source};
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};
use crate::pattern::{MatchResult, Pattern};
use rayon::prelude::*;
use fast_glob::glob_match;
use xvc_logging::watch;
/// Complete set of ignore rules for a directory and its child directories.
#[derive(Debug, Clone)]
pub struct IgnoreRules {
/// The root of the ignore rules.
/// Typically this is the root directory of Git or Xvc repository.
pub root: PathBuf,
/// The name of the ignore file (e.g. `.xvcignore`, `.gitignore`) to be loaded for ignore rules.
pub ignore_filename: Option<String>,
/// All ignore patterns collected from ignore files or specified in code.
pub ignore_patterns: Arc<RwLock<Vec<Pattern>>>,
/// All whitelist patterns collected from ignore files or specified in code
pub whitelist_patterns: Arc<RwLock<Vec<Pattern>>>,
}
/// IgnoreRules shared across threads.
pub type SharedIgnoreRules = Arc<RwLock<IgnoreRules>>;
impl IgnoreRules {
/// An empty set of ignore rules that neither ignores nor whitelists any path.
pub fn empty(dir: &Path, ignore_filename: Option<&str>) -> Self {
IgnoreRules {
root: PathBuf::from(dir),
ignore_filename: ignore_filename.map(|s| s.to_string()),
ignore_patterns: Arc::new(RwLock::new(Vec::<Pattern>::new())),
whitelist_patterns: Arc::new(RwLock::new(Vec::<Pattern>::new())),
}
}
/// Constructs a new `IgnoreRules` instance from a given set of global ignore patterns.
pub fn from_global_patterns(
ignore_root: &Path,
ignore_filename: Option<&str>,
given: &str,
) -> Self {
let mut given_patterns = Vec::<Pattern>::new();
// Add given patterns to ignore_patterns
for line in given.lines() {
let pattern = Pattern::new(Source::Global, line);
given_patterns.push(pattern);
}
IgnoreRules::from_patterns(ignore_root, ignore_filename, given_patterns)
}
/// Constructs a new `IgnoreRules` instance from a vector of patterns and a root path.
///
/// This function separates the patterns into ignore patterns and whitelist patterns
/// based on their `PatternEffect`. It then stores these patterns and the root path
/// in a new `IgnoreRules` instance.
///
/// # Arguments
///
/// * `patterns` - A vector of `Pattern` instances to be used for creating the `IgnoreRules`.
/// * `ignore_root` - A reference to the root path for the ignore rules.
///
/// # Returns
///
/// A new `IgnoreRules` instance containing the given patterns and root path.
pub fn from_patterns(
ignore_root: &Path,
ignore_filename: Option<&str>,
mut patterns: Vec<Pattern>,
) -> Self {
let mut ignore_patterns = Vec::new();
let mut whitelist_patterns = Vec::new();
patterns
.drain(0..patterns.len())
.for_each(|pattern| match pattern.effect {
crate::PatternEffect::Ignore => ignore_patterns.push(pattern),
crate::PatternEffect::Whitelist => whitelist_patterns.push(pattern),
});
IgnoreRules {
root: PathBuf::from(ignore_root),
ignore_filename: ignore_filename.map(|s| s.to_string()),
ignore_patterns: Arc::new(RwLock::new(ignore_patterns)),
whitelist_patterns: Arc::new(RwLock::new(whitelist_patterns)),
}
}
/// Checks if a given path matches any of the whitelist or ignore patterns.
///
/// The function first checks if the path matches any of the whitelist patterns.
/// If a match is found, it returns `MatchResult::Whitelist`.
///
/// If the path does not match any of the whitelist patterns, the function then checks
/// if the path matches any of the ignore patterns. If a match is found, it returns
/// `MatchResult::Ignore`.
///
/// If the path does not match any of the whitelist or ignore patterns, the function
/// returns `MatchResult::NoMatch`.
///
/// # Arguments
///
/// * `path` - A reference to the path to check.
///
/// # Returns
///
/// * `MatchResult::Whitelist` if the path matches a whitelist pattern.
/// * `MatchResult::Ignore` if the path matches an ignore pattern.
/// * `MatchResult::NoMatch` if the path does not match any pattern.
pub fn check(&self, path: &Path) -> MatchResult {
let is_abs = path.is_absolute();
// strip_prefix eats the final slash, and ends_with behave differently than str, so we work
// around here
let path_str = path.to_string_lossy();
let final_slash = path_str.ends_with('/');
let path = if is_abs {
if final_slash {
format!(
"/{}/",
path.strip_prefix(&self.root)
.expect("path must be within root")
.to_string_lossy()
)
} else {
format!(
"/{}",
path.strip_prefix(&self.root)
.expect("path must be within root")
.to_string_lossy()
)
}
} else {
path_str.to_string()
};
{
let whitelist_patterns = self.whitelist_patterns.read().unwrap();
if let Some(p) = whitelist_patterns
.par_iter()
.find_any(|pattern| glob_match(&pattern.glob, &path))
{
watch!(p);
return MatchResult::Whitelist;
}
}
{
let ignore_patterns = self.ignore_patterns.read().unwrap();
if let Some(p) = ignore_patterns
.par_iter()
.find_any(|pattern| glob_match(&pattern.glob, &path))
{
watch!(p);
return MatchResult::Ignore;
}
}
MatchResult::NoMatch
}
/// Merges the ignore and whitelist patterns of another `IgnoreRules` instance into this one.
///
/// This function locks the ignore and whitelist patterns of both `IgnoreRules` instances,
/// drains the patterns from the other instance, and pushes them into this instance.
/// The other instance is left empty after this operation.
///
/// # Arguments
///
/// * `other` - A reference to the other `IgnoreRules` instance to merge with.
///
/// # Returns
///
/// * `Ok(())` if the merge operation was successful.
/// * `Err` if the merge operation failed.
///
/// # Panics
///
/// This function will panic if the roots of the two `IgnoreRules` instances are not equal.
pub fn merge_with(&self, other: &IgnoreRules) -> Result<()> {
assert_eq!(self.root, other.root);
{
let mut ignore_patterns = self.ignore_patterns.write().unwrap();
let mut other_ignore_patterns = other.ignore_patterns.write().unwrap();
let len = other_ignore_patterns.len();
other_ignore_patterns
.drain(0..len)
.for_each(|p| ignore_patterns.push(p));
}
{
let mut whitelist_patterns = self.whitelist_patterns.write().unwrap();
let mut other_whitelist_patterns = other.whitelist_patterns.write().unwrap();
let len = other_whitelist_patterns.len();
other_whitelist_patterns
.drain(0..len)
.for_each(|p| whitelist_patterns.push(p));
}
Ok(())
}
/// Adds a list of patterns to the current ignore rules.
///
/// # Arguments
///
/// * `patterns` - A vector of patterns to be added to the ignore rules.
pub fn add_patterns(&self, patterns: Vec<Pattern>) -> Result<()> {
let other = IgnoreRules::from_patterns(&self.root, None, patterns);
self.merge_with(&other)
}
}