Patchwork [1,of,3] match: remove unnecessary '^' from regexes

login
register
mail settings
Submitter via Mercurial-devel
Date July 10, 2017, 6:44 a.m.
Message ID <47e360b2290a0a03ff78.1499669081@martinvonz.svl.corp.google.com>
Download mbox | patch
Permalink /patch/22197/
State Accepted
Headers show

Comments

via Mercurial-devel - July 10, 2017, 6:44 a.m.
# HG changeset patch
# User Martin von Zweigbergk <martinvonz@google.com>
# Date 1499665982 25200
#      Sun Jul 09 22:53:02 2017 -0700
# Node ID 47e360b2290a0a03ff789cdba96af6fcd4292ae0
# Parent  ccb3e5399921db16e95e7429cc2fb9ef82ee846e
match: remove unnecessary '^' from regexes

The regexes are passed to re.match(), which matches against the
beginning of the input, so the '^' doesn't do anything.

Note that unrooted patterns, such as globs and regexes from .hgignore
are instead achieved by adding '.*' to the expression given by the
user. (That's unless the user's expression started with '^', in which
case the '.*' is not added, perhaps to keep the regex cleaner?)
Yuya Nishihara - July 10, 2017, 2:07 p.m.
On Sun, 09 Jul 2017 23:44:41 -0700, Martin von Zweigbergk via Mercurial-devel wrote:
> # HG changeset patch
> # User Martin von Zweigbergk <martinvonz@google.com>
> # Date 1499665982 25200
> #      Sun Jul 09 22:53:02 2017 -0700
> # Node ID 47e360b2290a0a03ff789cdba96af6fcd4292ae0
> # Parent  ccb3e5399921db16e95e7429cc2fb9ef82ee846e
> match: remove unnecessary '^' from regexes

Queued the first two, thanks.

Patch

diff --git a/mercurial/match.py b/mercurial/match.py
--- a/mercurial/match.py
+++ b/mercurial/match.py
@@ -797,7 +797,7 @@ 
     if kind == 'path':
         if pat == '.':
             return ''
-        return '^' + util.re.escape(pat) + '(?:/|$)'
+        return util.re.escape(pat) + '(?:/|$)'
     if kind == 'rootfilesin':
         if pat == '.':
             escaped = ''
@@ -805,7 +805,7 @@ 
             # Pattern is a directory name.
             escaped = util.re.escape(pat) + '/'
         # Anything after the pattern must be a non-directory.
-        return '^' + escaped + '[^/]+$'
+        return escaped + '[^/]+$'
     if kind == 'relglob':
         return '(?:|.*/)' + _globre(pat) + globsuffix
     if kind == 'relpath':
diff --git a/tests/test-walk.t b/tests/test-walk.t
--- a/tests/test-walk.t
+++ b/tests/test-walk.t
@@ -109,7 +109,7 @@ 
   f  fenugreek      ../fenugreek
   f  mammals/skunk  skunk
   $ hg debugwalk -I 'path:beans'
-  matcher: <includematcher includes='(?:^beans(?:/|$))'>
+  matcher: <includematcher includes='(?:beans(?:/|$))'>
   f  beans/black     ../beans/black
   f  beans/borlotti  ../beans/borlotti
   f  beans/kidney    ../beans/kidney
@@ -126,27 +126,27 @@ 
   f  beans/turtle    ../beans/turtle
 
   $ hg debugwalk 'rootfilesin:'
-  matcher: <patternmatcher patterns='(?:^[^/]+$)'>
+  matcher: <patternmatcher patterns='(?:[^/]+$)'>
   f  fennel      ../fennel
   f  fenugreek   ../fenugreek
   f  fiddlehead  ../fiddlehead
   $ hg debugwalk -I 'rootfilesin:'
-  matcher: <includematcher includes='(?:^[^/]+$)'>
+  matcher: <includematcher includes='(?:[^/]+$)'>
   f  fennel      ../fennel
   f  fenugreek   ../fenugreek
   f  fiddlehead  ../fiddlehead
   $ hg debugwalk 'rootfilesin:.'
-  matcher: <patternmatcher patterns='(?:^[^/]+$)'>
+  matcher: <patternmatcher patterns='(?:[^/]+$)'>
   f  fennel      ../fennel
   f  fenugreek   ../fenugreek
   f  fiddlehead  ../fiddlehead
   $ hg debugwalk -I 'rootfilesin:.'
-  matcher: <includematcher includes='(?:^[^/]+$)'>
+  matcher: <includematcher includes='(?:[^/]+$)'>
   f  fennel      ../fennel
   f  fenugreek   ../fenugreek
   f  fiddlehead  ../fiddlehead
   $ hg debugwalk -X 'rootfilesin:'
-  matcher: <differencematcher m1=<alwaysmatcher>, m2=<includematcher includes='(?:^[^/]+$)'>>
+  matcher: <differencematcher m1=<alwaysmatcher>, m2=<includematcher includes='(?:[^/]+$)'>>
   f  beans/black                     ../beans/black
   f  beans/borlotti                  ../beans/borlotti
   f  beans/kidney                    ../beans/kidney
@@ -158,15 +158,15 @@ 
   f  mammals/Procyonidae/raccoon     Procyonidae/raccoon
   f  mammals/skunk                   skunk
   $ hg debugwalk 'rootfilesin:fennel'
-  matcher: <patternmatcher patterns='(?:^fennel/[^/]+$)'>
+  matcher: <patternmatcher patterns='(?:fennel/[^/]+$)'>
   $ hg debugwalk -I 'rootfilesin:fennel'
-  matcher: <includematcher includes='(?:^fennel/[^/]+$)'>
+  matcher: <includematcher includes='(?:fennel/[^/]+$)'>
   $ hg debugwalk 'rootfilesin:skunk'
-  matcher: <patternmatcher patterns='(?:^skunk/[^/]+$)'>
+  matcher: <patternmatcher patterns='(?:skunk/[^/]+$)'>
   $ hg debugwalk -I 'rootfilesin:skunk'
-  matcher: <includematcher includes='(?:^skunk/[^/]+$)'>
+  matcher: <includematcher includes='(?:skunk/[^/]+$)'>
   $ hg debugwalk 'rootfilesin:beans'
-  matcher: <patternmatcher patterns='(?:^beans/[^/]+$)'>
+  matcher: <patternmatcher patterns='(?:beans/[^/]+$)'>
   f  beans/black     ../beans/black
   f  beans/borlotti  ../beans/borlotti
   f  beans/kidney    ../beans/kidney
@@ -174,7 +174,7 @@ 
   f  beans/pinto     ../beans/pinto
   f  beans/turtle    ../beans/turtle
   $ hg debugwalk -I 'rootfilesin:beans'
-  matcher: <includematcher includes='(?:^beans/[^/]+$)'>
+  matcher: <includematcher includes='(?:beans/[^/]+$)'>
   f  beans/black     ../beans/black
   f  beans/borlotti  ../beans/borlotti
   f  beans/kidney    ../beans/kidney
@@ -182,19 +182,19 @@ 
   f  beans/pinto     ../beans/pinto
   f  beans/turtle    ../beans/turtle
   $ hg debugwalk 'rootfilesin:mammals'
-  matcher: <patternmatcher patterns='(?:^mammals/[^/]+$)'>
+  matcher: <patternmatcher patterns='(?:mammals/[^/]+$)'>
   f  mammals/skunk  skunk
   $ hg debugwalk -I 'rootfilesin:mammals'
-  matcher: <includematcher includes='(?:^mammals/[^/]+$)'>
+  matcher: <includematcher includes='(?:mammals/[^/]+$)'>
   f  mammals/skunk  skunk
   $ hg debugwalk 'rootfilesin:mammals/'
-  matcher: <patternmatcher patterns='(?:^mammals/[^/]+$)'>
+  matcher: <patternmatcher patterns='(?:mammals/[^/]+$)'>
   f  mammals/skunk  skunk
   $ hg debugwalk -I 'rootfilesin:mammals/'
-  matcher: <includematcher includes='(?:^mammals/[^/]+$)'>
+  matcher: <includematcher includes='(?:mammals/[^/]+$)'>
   f  mammals/skunk  skunk
   $ hg debugwalk -X 'rootfilesin:mammals'
-  matcher: <differencematcher m1=<alwaysmatcher>, m2=<includematcher includes='(?:^mammals/[^/]+$)'>>
+  matcher: <differencematcher m1=<alwaysmatcher>, m2=<includematcher includes='(?:mammals/[^/]+$)'>>
   f  beans/black                     ../beans/black
   f  beans/borlotti                  ../beans/borlotti
   f  beans/kidney                    ../beans/kidney
@@ -320,7 +320,7 @@ 
   matcher: <differencematcher m1=<patternmatcher patterns='(?:mammals(?:/|$))'>, m2=<includematcher includes='(?:[^/]*\\/Procyonidae(?:/|$))'>>
   f  mammals/skunk  mammals/skunk
   $ hg debugwalk path:mammals
-  matcher: <patternmatcher patterns='(?:^mammals(?:/|$))'>
+  matcher: <patternmatcher patterns='(?:mammals(?:/|$))'>
   f  mammals/Procyonidae/cacomistle  mammals/Procyonidae/cacomistle
   f  mammals/Procyonidae/coatimundi  mammals/Procyonidae/coatimundi
   f  mammals/Procyonidae/raccoon     mammals/Procyonidae/raccoon
@@ -383,7 +383,7 @@ 
   matcher: <patternmatcher patterns='(?:glob\\:glob$)'>
   f  glob:glob  glob:glob  exact
   $ hg debugwalk path:glob:glob
-  matcher: <patternmatcher patterns='(?:^glob\\:glob(?:/|$))'>
+  matcher: <patternmatcher patterns='(?:glob\\:glob(?:/|$))'>
   f  glob:glob  glob:glob  exact
   $ rm glob:glob
   $ hg addremove
@@ -402,10 +402,10 @@ 
   f  mammals/skunk  mammals/skunk
 
   $ hg debugwalk path:beans/black
-  matcher: <patternmatcher patterns='(?:^beans\\/black(?:/|$))'>
+  matcher: <patternmatcher patterns='(?:beans\\/black(?:/|$))'>
   f  beans/black  beans/black  exact
   $ hg debugwalk path:beans//black
-  matcher: <patternmatcher patterns='(?:^beans\\/black(?:/|$))'>
+  matcher: <patternmatcher patterns='(?:beans\\/black(?:/|$))'>
   f  beans/black  beans/black  exact
 
   $ hg debugwalk relglob:Procyonidae