| 1 |
use std::cmp::Ordering; |
| 2 |
use std::path::Path; |
| 3 |
|
| 4 |
use crate::errors::HgError; |
| 5 |
use crate::exit_codes; |
| 6 |
use crate::file_patterns::parse_pattern_file_contents; |
| 7 |
use crate::file_patterns::FilePattern; |
| 8 |
use crate::file_patterns::PatternSyntax; |
| 9 |
use crate::matchers::AlwaysMatcher; |
| 10 |
use crate::matchers::DifferenceMatcher; |
| 11 |
use crate::matchers::IncludeMatcher; |
| 12 |
use crate::matchers::Matcher; |
| 13 |
use crate::matchers::NeverMatcher; |
| 14 |
use crate::narrow::shape::Shape; |
| 15 |
use crate::repo::Repo; |
| 16 |
use crate::requirements::NARROW_REQUIREMENT; |
| 17 |
use crate::sparse::SparseConfigError; |
| 18 |
use crate::sparse::{self}; |
| 19 |
use crate::warnings::HgWarningSender; |
| 20 |
|
| 21 |
pub mod shape; |
| 22 |
|
| 23 |
/// The file in .hg/store/ that indicates which paths exit in the store |
| 24 |
const FILENAME: &str = "narrowspec"; |
| 25 |
/// The file in .hg/ that indicates which paths exit in the dirstate |
| 26 |
const DIRSTATE_FILENAME: &str = "narrowspec.dirstate"; |
| 27 |
|
| 28 |
/// Pattern prefixes that are allowed in narrow patterns. This list MUST |
| 29 |
/// only contain patterns that are fast and safe to evaluate. Keep in mind |
| 30 |
/// that patterns are supplied by clients and executed on remote servers |
| 31 |
/// as part of wire protocol commands. That means that changes to this |
| 32 |
/// data structure influence the wire protocol and should not be taken |
| 33 |
/// lightly - especially removals. |
| 34 |
pub const VALID_PREFIXES: [&str; 2] = ["path:", "rootfilesin:"]; |
| 35 |
|
| 36 |
/// Compares two patterns. |
| 37 |
/// |
| 38 |
/// [`FilePattern`] does not implement [`Ord`] because it's complicated with |
| 39 |
/// [`PatternSyntax::Include`], but for narrowspecs we only have to deal with |
| 40 |
/// the types of patterns allowed by [`VALID_PREFIXES`]. |
| 41 |
fn cmp_patterns(lhs: &FilePattern, rhs: &FilePattern) -> Ordering { |
| 42 |
let kind = |pattern: &FilePattern| match pattern.syntax { |
| 43 |
PatternSyntax::Path => 0, |
| 44 |
PatternSyntax::RootFilesIn => 1, |
| 45 |
_ => panic!("narrowspec only allows path: and rootfilesin:"), |
| 46 |
}; |
| 47 |
let key = |pattern| (kind(pattern), &pattern.raw); |
| 48 |
Ord::cmp(&key(lhs), &key(rhs)) |
| 49 |
} |
| 50 |
|
| 51 |
/// Return the matcher for the current narrow spec, and all configuration |
| 52 |
/// warnings to display. |
| 53 |
pub fn matcher( |
| 54 |
repo: &Repo, |
| 55 |
warnings: &HgWarningSender, |
| 56 |
) -> Result<Box<dyn Matcher + Send>, SparseConfigError> { |
| 57 |
if !repo.requirements().contains(NARROW_REQUIREMENT) { |
| 58 |
return Ok(Box::new(AlwaysMatcher)); |
| 59 |
} |
| 60 |
let patterns = store_patterns(warnings, repo)?; |
| 61 |
|
| 62 |
let Some((include_patterns, exclude_patterns)) = patterns else { |
| 63 |
return Ok(Box::new(NeverMatcher)); |
| 64 |
}; |
| 65 |
|
| 66 |
// The old way only works for simple cases. Nested excludes/includes |
| 67 |
// don't work and we need them for shapes, but only for `path:` patterns. |
| 68 |
// |
| 69 |
// `rootfilesin:` does not use the new logic yet because they make the code |
| 70 |
// more complex and are not needed by shapes. Maybe we'll end up |
| 71 |
// implementing it. |
| 72 |
if let Ok(shape) = |
| 73 |
Shape::from_patterns(&include_patterns, &exclude_patterns) |
| 74 |
{ |
| 75 |
let new_matcher = shape.matcher(); |
| 76 |
return Ok(Box::new(new_matcher)); |
| 77 |
} |
| 78 |
|
| 79 |
// Fall back to the old way of matching |
| 80 |
let mut m: Box<dyn Matcher + Send> = |
| 81 |
Box::new(IncludeMatcher::new(include_patterns)?); |
| 82 |
|
| 83 |
if !exclude_patterns.is_empty() { |
| 84 |
let exclude_matcher = Box::new(IncludeMatcher::new(exclude_patterns)?); |
| 85 |
m = Box::new(DifferenceMatcher::new(m, exclude_matcher)); |
| 86 |
} |
| 87 |
|
| 88 |
Ok(m) |
| 89 |
} |
| 90 |
|
| 91 |
/// Get the store narrow patterns for `repo`. |
| 92 |
/// |
| 93 |
/// # Errors |
| 94 |
/// |
| 95 |
/// Will return an error if the working copy's narrowspec doesn't contain the |
| 96 |
/// same patterns as the one from the store, or if either fail to parse. |
| 97 |
pub fn store_patterns( |
| 98 |
warnings: &HgWarningSender, |
| 99 |
repo: &Repo, |
| 100 |
) -> Result<Option<NarrowPatterns>, HgError> { |
| 101 |
// Treat "narrowspec does not exist" the same as "narrowspec file exists |
| 102 |
// and is empty |
| 103 |
let store_patterns = patterns_from_spec( |
| 104 |
warnings, |
| 105 |
&repo.store_vfs().try_read(FILENAME)?.unwrap_or_default(), |
| 106 |
)?; |
| 107 |
let working_copy_patterns = patterns_from_spec( |
| 108 |
warnings, |
| 109 |
&repo.hg_vfs().try_read(DIRSTATE_FILENAME)?.unwrap_or_default(), |
| 110 |
)?; |
| 111 |
if store_patterns != working_copy_patterns { |
| 112 |
return Err(HgError::abort( |
| 113 |
"abort: working copy's narrowspec is stale", |
| 114 |
exit_codes::STATE_ERROR, |
| 115 |
Some("run 'hg tracked --update-working-copy'".into()), |
| 116 |
)); |
| 117 |
} |
| 118 |
Ok(store_patterns) |
| 119 |
} |
| 120 |
|
| 121 |
/// Raw patterns as deserialized and validated |
| 122 |
type NarrowPatterns = (Vec<FilePattern>, Vec<FilePattern>); |
| 123 |
|
| 124 |
/// Get the validated narrow patterns for a given spec |
| 125 |
pub fn patterns_from_spec( |
| 126 |
warnings: &HgWarningSender, |
| 127 |
spec: &[u8], |
| 128 |
) -> Result<Option<NarrowPatterns>, SparseConfigError> { |
| 129 |
let config = sparse::parse_config( |
| 130 |
spec, |
| 131 |
sparse::SparseConfigContext::Narrow, |
| 132 |
warnings, |
| 133 |
)?; |
| 134 |
if !config.profiles.is_empty() { |
| 135 |
// TODO (from Python impl) maybe do something with profiles? |
| 136 |
return Err(SparseConfigError::IncludesInNarrow); |
| 137 |
} |
| 138 |
validate_patterns(&config.includes)?; |
| 139 |
validate_patterns(&config.excludes)?; |
| 140 |
if config.includes.is_empty() { |
| 141 |
return Ok(None); |
| 142 |
} |
| 143 |
let mut include_patterns = parse_pattern_file_contents( |
| 144 |
&config.includes, |
| 145 |
Path::new(""), |
| 146 |
None, |
| 147 |
false, |
| 148 |
true, |
| 149 |
warnings, |
| 150 |
)?; |
| 151 |
let mut exclude_patterns = parse_pattern_file_contents( |
| 152 |
&config.excludes, |
| 153 |
Path::new(""), |
| 154 |
None, |
| 155 |
false, |
| 156 |
true, |
| 157 |
warnings, |
| 158 |
)?; |
| 159 |
include_patterns.sort_by(cmp_patterns); |
| 160 |
exclude_patterns.sort_by(cmp_patterns); |
| 161 |
Ok(Some((include_patterns, exclude_patterns))) |
| 162 |
} |
| 163 |
|
| 164 |
fn is_whitespace(b: &u8) -> bool { |
| 165 |
// should match what .strip() in Python does |
| 166 |
b.is_ascii_whitespace() || *b == 0x0b |
| 167 |
} |
| 168 |
|
| 169 |
fn starts_or_ends_with_whitespace(s: &[u8]) -> bool { |
| 170 |
let w = |b: Option<&u8>| b.map(is_whitespace).unwrap_or(false); |
| 171 |
w(s.first()) || w(s.last()) |
| 172 |
} |
| 173 |
|
| 174 |
fn validate_pattern(pattern: &[u8]) -> Result<(), SparseConfigError> { |
| 175 |
if starts_or_ends_with_whitespace(pattern) { |
| 176 |
return Err(SparseConfigError::WhitespaceAtEdgeOfPattern( |
| 177 |
pattern.to_owned(), |
| 178 |
)); |
| 179 |
} |
| 180 |
for prefix in VALID_PREFIXES.iter() { |
| 181 |
if pattern.starts_with(prefix.as_bytes()) { |
| 182 |
return Ok(()); |
| 183 |
} |
| 184 |
} |
| 185 |
Err(SparseConfigError::InvalidNarrowPrefix(pattern.to_owned())) |
| 186 |
} |
| 187 |
|
| 188 |
fn validate_patterns(patterns: &[u8]) -> Result<(), SparseConfigError> { |
| 189 |
for pattern in patterns.split(|c| *c == b'\n') { |
| 190 |
if pattern.is_empty() { |
| 191 |
// TODO: probably not intentionally allowed (only because `split` |
| 192 |
// produces "fake" empty line at the end) |
| 193 |
continue; |
| 194 |
} |
| 195 |
validate_pattern(pattern)? |
| 196 |
} |
| 197 |
Ok(()) |
| 198 |
} |