changeset 42438:48f1f864d928

rust-regex: fix shortcut for exact matches The current shortcut for rootglobs that can be simplified to exact matches does not work, it instead treats the pattern as a regex, which is not the same thing. This changes fixes the behavior and introduces a test for this behavior. Differential Revision: https://phab.mercurial-scm.org/D6489
author Raphaël Gomès <rgomes@octobus.net>
date Thu, 06 Jun 2019 18:37:21 +0200
parents 9609430d3625
children aae93201f758
files rust/hg-core/src/filepatterns.rs
diffstat 1 files changed, 20 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-core/src/filepatterns.rs	Thu Jun 06 15:30:56 2019 +0200
+++ b/rust/hg-core/src/filepatterns.rs	Thu Jun 06 18:37:21 2019 +0200
@@ -200,9 +200,11 @@
 ) -> Result<Vec<u8>, PatternError> {
     let enum_kind = parse_pattern_syntax(kind)?;
     if enum_kind == PatternSyntax::RootGlob
-        && pat.iter().all(|b| GLOB_SPECIAL_CHARACTERS.contains(b))
+        && !pat.iter().any(|b| GLOB_SPECIAL_CHARACTERS.contains(b))
     {
-        Ok(pat.to_vec())
+        let mut escaped = escape_pattern(pat);
+        escaped.extend(b"(?:/|$)");
+        Ok(escaped)
     } else {
         Ok(_build_single_regex(enum_kind, pat, globsuffix))
     }
@@ -351,4 +353,20 @@
             vec![(b"relglob:**.o".to_vec(), 1, b"**.o".to_vec())]
         );
     }
+
+    #[test]
+    fn test_build_single_regex_shortcut() {
+        assert_eq!(
+            br"(?:/|$)".to_vec(),
+            build_single_regex(b"rootglob", b"", b"").unwrap()
+        );
+        assert_eq!(
+            br"whatever(?:/|$)".to_vec(),
+            build_single_regex(b"rootglob", b"whatever", b"").unwrap()
+        );
+        assert_eq!(
+            br"[^/]*\.o".to_vec(),
+            build_single_regex(b"rootglob", b"*.o", b"").unwrap()
+        );
+    }
 }