# HG changeset patch # User Alexis S. L. Carvalho # Date 1173578453 10800 # Node ID 9814d600011eae35f6de23259586590e2fc5f64a # Parent 02de0f98ca330a7f754f6a2f94f8737889dcab8a 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. diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -437,7 +437,7 @@ def _matcher(canonroot, cwd, names, inc, 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 @@ def _matcher(canonroot, cwd, names, inc, 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. diff --git a/tests/test-walk b/tests/test-walk --- a/tests/test-walk +++ b/tests/test-walk @@ -35,6 +35,16 @@ hg commit -m "commit #0" -d "1000000 0" 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 diff --git a/tests/test-walk.out b/tests/test-walk.out --- a/tests/test-walk.out +++ b/tests/test-walk.out @@ -46,6 +46,72 @@ f mammals/skunk mamma 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