match: simplify the regexps created for glob patterns
authorValentin Gatien-Baron <valentin.gatienbaron@gmail.com>
Sun, 25 Aug 2019 22:53:42 -0400
changeset 42864 72890d8f9860
parent 42863 62eabdf91f85
child 42865 69195b6f8f97
match: simplify the regexps created for glob patterns For legibility of the resulting regexes, although it may help with performance as well. Differential Revision: https://phab.mercurial-scm.org/D6764
mercurial/match.py
rust/hg-core/src/filepatterns.rs
tests/test-hgignore.t
tests/test-walk.t
--- a/mercurial/match.py	Mon Aug 26 08:25:01 2019 -0400
+++ b/mercurial/match.py	Sun Aug 25 22:53:42 2019 -0400
@@ -1223,7 +1223,12 @@
         # Anything after the pattern must be a non-directory.
         return escaped + '[^/]+$'
     if kind == 'relglob':
-        return '(?:|.*/)' + _globre(pat) + globsuffix
+        globre = _globre(pat)
+        if globre.startswith('[^/]*'):
+            # When pat has the form *XYZ (common), make the returned regex more
+            # legible by returning the regex for **XYZ instead of **/*XYZ.
+            return '.*' + globre[len('[^/]*'):] + globsuffix
+        return '(?:|.*/)' + globre + globsuffix
     if kind == 'relre':
         if pat.startswith('^'):
             return pat
--- a/rust/hg-core/src/filepatterns.rs	Mon Aug 26 08:25:01 2019 -0400
+++ b/rust/hg-core/src/filepatterns.rs	Sun Aug 25 22:53:42 2019 -0400
@@ -185,14 +185,21 @@
             res.extend(b"[^/]+$");
             res
         }
-        PatternSyntax::Glob
-        | PatternSyntax::RelGlob
-        | PatternSyntax::RootGlob => {
+        PatternSyntax::RelGlob => {
             let mut res: Vec<u8> = vec![];
-            if syntax == PatternSyntax::RelGlob {
+            let glob_re = glob_to_re(pattern);
+            if let Some(rest) = glob_re.drop_prefix(b"[^/]*") {
+                res.extend(b".*");
+                res.extend(rest);
+            } else {
                 res.extend(b"(?:|.*/)");
+                res.extend(glob_re);
             }
-
+            res.extend(globsuffix.iter());
+            res
+        }
+        PatternSyntax::Glob | PatternSyntax::RootGlob => {
+            let mut res: Vec<u8> = vec![];
             res.extend(glob_to_re(pattern));
             res.extend(globsuffix.iter());
             res
--- a/tests/test-hgignore.t	Mon Aug 26 08:25:01 2019 -0400
+++ b/tests/test-hgignore.t	Sun Aug 25 22:53:42 2019 -0400
@@ -177,7 +177,7 @@
   ? a.c
   ? syntax
   $ hg debugignore
-  <includematcher includes='(?:|.*/)[^/]*\\.o(?:/|$)'>
+  <includematcher includes='.*\\.o(?:/|$)'>
 
   $ cd ..
   $ echo > .hg/testhgignorerel
@@ -224,7 +224,7 @@
   A b.o
 
   $ hg debugignore
-  <includematcher includes='(?:|.*/)[^/]*(?:/|$)'>
+  <includematcher includes='.*(?:/|$)'>
 
   $ hg debugignore b.o
   b.o is ignored
--- a/tests/test-walk.t	Mon Aug 26 08:25:01 2019 -0400
+++ b/tests/test-walk.t	Sun Aug 25 22:53:42 2019 -0400
@@ -100,7 +100,7 @@
   f  mammals/skunk  skunk
   $ hg debugwalk -v -I 'relglob:*k'
   * matcher:
-  <includematcher includes='(?:|.*/)[^/]*k(?:/|$)'>
+  <includematcher includes='.*k(?:/|$)'>
   f  beans/black    ../beans/black
   f  fenugreek      ../fenugreek
   f  mammals/skunk  skunk
@@ -108,7 +108,7 @@
   * matcher:
   <intersectionmatcher
     m1=<patternmatcher patterns='mammals(?:/|$)'>,
-    m2=<includematcher includes='(?:|.*/)[^/]*k(?:/|$)'>>
+    m2=<includematcher includes='.*k(?:/|$)'>>
   f  mammals/skunk  skunk
   $ hg debugwalk -v -I 're:.*k$'
   * matcher: