rust/hg-core/src/narrow.rs
branchstable
changeset 52719 77b95a4abbb2
parent 52039 ae1ab6d71f4a
child 52720 b019b5798e8f
--- a/rust/hg-core/src/narrow.rs	Fri Jan 10 16:14:40 2025 +0000
+++ b/rust/hg-core/src/narrow.rs	Tue Jan 28 17:29:59 2025 +0000
@@ -97,11 +97,26 @@
     Ok((m, warnings))
 }
 
+fn is_whitespace(b: &u8) -> bool {
+    // should match what .strip() in Python does
+    b.is_ascii_whitespace() || *b == 0x0b
+}
+
+fn starts_or_ends_with_whitespace(s: &[u8]) -> bool {
+    let w = |b: Option<&u8>| b.map(is_whitespace).unwrap_or(false);
+    w(s.first()) || w(s.last())
+}
+
 fn validate_patterns(patterns: &[u8]) -> Result<(), SparseConfigError> {
     for pattern in patterns.split(|c| *c == b'\n') {
         if pattern.is_empty() {
             continue;
         }
+        if starts_or_ends_with_whitespace(pattern) {
+            return Err(SparseConfigError::WhitespaceAtEdgeOfPattern(
+                pattern.to_owned(),
+            ));
+        }
         for prefix in VALID_PREFIXES.iter() {
             if pattern.starts_with(prefix.as_bytes()) {
                 return Ok(());