changeset 4192:9814d600011e

util._matcher: unify pattern normalization This should fix issue347. It also highlights one issue with the directory walking code when you have an --include pattern that matches the end of a filename. This is fixed by the next patch.
author Alexis S. L. Carvalho <alexis@cecm.usp.br>
date Sat, 10 Mar 2007 23:00:53 -0300
parents 02de0f98ca33
children dd0d9bd91e0a
files mercurial/util.py tests/test-walk tests/test-walk.out
diffstat 3 files changed, 105 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/util.py	Sat Mar 10 23:00:52 2007 -0300
+++ b/mercurial/util.py	Sat Mar 10 23:00:53 2007 -0300
@@ -437,7 +437,7 @@
         elif kind == 'relglob':
             return head + globre(name, '(?:|.*/)', '(?:/|$)')
         elif kind == 'relpath':
-            return head + re.escape(name) + tail
+            return head + re.escape(name) + '(?:/|$)'
         elif kind == 'relre':
             if name.startswith('^'):
                 return name
@@ -473,43 +473,45 @@
             root.append(p)
         return '/'.join(root) or '.'
 
-    pats = []
-    files = []
-    roots = []
-    for kind, name in [patkind(p, dflt_pat) for p in names]:
-        if kind in ('glob', 'relpath'):
-            name = canonpath(canonroot, cwd, name)
-        elif kind in ('relglob', 'path'):
-            name = normpath(name)
-        if kind in ('glob', 're', 'relglob'):
-            pats.append((kind, name))
-        if kind == 'glob':
-            root = globprefix(name)
-            roots.append(root)
-        elif kind in ('relpath', 'path'):
-            files.append((kind, name))
-            roots.append(name)
-        elif kind == 'relglob':
-            roots.append('.')
+    def normalizepats(names, default):
+        pats = []
+        files = []
+        roots = []
+        anypats = False
+        for kind, name in [patkind(p, default) for p in names]:
+            if kind in ('glob', 'relpath'):
+                name = canonpath(canonroot, cwd, name)
+            elif kind in ('relglob', 'path'):
+                name = normpath(name)
+            if kind in ('glob', 're', 'relglob', 'relre'):
+                pats.append((kind, name))
+                anypats = True
+            if kind == 'glob':
+                root = globprefix(name)
+                roots.append(root)
+            elif kind in ('relpath', 'path'):
+                files.append((kind, name))
+                roots.append(name)
+            elif kind == 'relglob':
+                roots.append('.')
+        return roots, pats + files, anypats
+
+    roots, pats, anypats = normalizepats(names, dflt_pat)
 
     patmatch = matchfn(pats, '$') or always
-    filematch = matchfn(files, '(?:/|$)') or always
     incmatch = always
     if inc:
-        inckinds = [patkind(canonpath(canonroot, cwd, i)) for i in inc]
+        dummy, inckinds, dummy = normalizepats(inc, 'glob')
         incmatch = matchfn(inckinds, '(?:/|$)')
     excmatch = lambda fn: False
     if exc:
-        exckinds = [patkind(canonpath(canonroot, cwd, x)) for x in exc]
+        dummy, exckinds, dummy = normalizepats(exc, 'glob')
         excmatch = matchfn(exckinds, '(?:/|$)')
 
     return (roots,
             lambda fn: (incmatch(fn) and not excmatch(fn) and
-                        (fn.endswith('/') or
-                         (not pats and not files) or
-                         (pats and patmatch(fn)) or
-                         (files and filematch(fn)))),
-            (inc or exc or pats) and True)
+                        (fn.endswith('/') or patmatch(fn))),
+            (inc or exc or anypats) and True)
 
 def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None):
     '''enhanced shell command execution.
--- a/tests/test-walk	Sat Mar 10 23:00:52 2007 -0300
+++ b/tests/test-walk	Sat Mar 10 23:00:53 2007 -0300
@@ -35,6 +35,16 @@
 debugwalk
 debugwalk -I.
 chdir mammals
+debugwalk
+debugwalk -X ../beans
+debugwalk -I '*k'
+debugwalk -I 'glob:*k'
+debugwalk -I 'relglob:*k'
+debugwalk -I 'relglob:*k' .
+debugwalk -I 're:.*k$'
+debugwalk -I 'relre:.*k$'
+debugwalk -I 'path:beans'
+debugwalk -I 'relpath:../beans'
 debugwalk .
 debugwalk -I.
 debugwalk Procyonidae
--- a/tests/test-walk.out	Sat Mar 10 23:00:52 2007 -0300
+++ b/tests/test-walk.out	Sat Mar 10 23:00:53 2007 -0300
@@ -46,6 +46,72 @@
 
 cd mammals
 
+hg debugwalk 
+f  beans/black                     ../beans/black
+f  beans/borlotti                  ../beans/borlotti
+f  beans/kidney                    ../beans/kidney
+f  beans/navy                      ../beans/navy
+f  beans/pinto                     ../beans/pinto
+f  beans/turtle                    ../beans/turtle
+f  fennel                          ../fennel
+f  fenugreek                       ../fenugreek
+f  fiddlehead                      ../fiddlehead
+f  glob:glob                       ../glob:glob
+f  mammals/Procyonidae/cacomistle  Procyonidae/cacomistle
+f  mammals/Procyonidae/coatimundi  Procyonidae/coatimundi
+f  mammals/Procyonidae/raccoon     Procyonidae/raccoon
+f  mammals/skunk                   skunk
+
+hg debugwalk -X ../beans
+f  fennel                          ../fennel
+f  fenugreek                       ../fenugreek
+f  fiddlehead                      ../fiddlehead
+f  glob:glob                       ../glob:glob
+f  mammals/Procyonidae/cacomistle  Procyonidae/cacomistle
+f  mammals/Procyonidae/coatimundi  Procyonidae/coatimundi
+f  mammals/Procyonidae/raccoon     Procyonidae/raccoon
+f  mammals/skunk                   skunk
+
+hg debugwalk -I *k
+m  mammals/skunk  skunk
+
+hg debugwalk -I glob:*k
+m  mammals/skunk  skunk
+
+hg debugwalk -I relglob:*k
+f  fenugreek      ../fenugreek
+m  beans/black    ../beans/black
+m  mammals/skunk  skunk
+
+hg debugwalk -I relglob:*k .
+f  mammals/skunk  skunk
+
+hg debugwalk -I re:.*k$
+f  fenugreek      ../fenugreek
+m  beans/black    ../beans/black
+m  mammals/skunk  skunk
+
+hg debugwalk -I relre:.*k$
+f  fenugreek      ../fenugreek
+m  beans/black    ../beans/black
+m  mammals/skunk  skunk
+
+hg debugwalk -I path:beans
+f  beans/black     ../beans/black
+f  beans/borlotti  ../beans/borlotti
+f  beans/kidney    ../beans/kidney
+f  beans/navy      ../beans/navy
+f  beans/pinto     ../beans/pinto
+f  beans/turtle    ../beans/turtle
+
+hg debugwalk -I relpath:../beans
+f  beans/black     ../beans/black
+f  beans/borlotti  ../beans/borlotti
+f  beans/kidney    ../beans/kidney
+f  beans/navy      ../beans/navy
+f  beans/pinto     ../beans/pinto
+f  beans/turtle    ../beans/turtle
+
 hg debugwalk .
 f  mammals/Procyonidae/cacomistle  Procyonidae/cacomistle
 f  mammals/Procyonidae/coatimundi  Procyonidae/coatimundi