2009-06-09 17:25:17 +04:00
|
|
|
# match.py - filename matching
|
2009-04-26 03:57:00 +04:00
|
|
|
#
|
|
|
|
# Copyright 2008, 2009 Matt Mackall <mpm@selenic.com> and others
|
|
|
|
#
|
|
|
|
# This software may be used and distributed according to the terms of the
|
2010-01-20 07:20:08 +03:00
|
|
|
# GNU General Public License version 2 or any later version.
|
2009-04-26 03:57:00 +04:00
|
|
|
|
2015-08-09 05:39:45 +03:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import copy
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
|
|
|
from .i18n import _
|
|
|
|
from . import (
|
2015-10-08 22:55:45 +03:00
|
|
|
error,
|
2015-08-09 05:39:45 +03:00
|
|
|
pathutil,
|
|
|
|
util,
|
|
|
|
)
|
2008-05-12 20:37:07 +04:00
|
|
|
|
2015-04-06 20:51:53 +03:00
|
|
|
propertycache = util.propertycache
|
|
|
|
|
2013-10-03 20:01:21 +04:00
|
|
|
def _rematcher(regex):
|
|
|
|
'''compile the regexp with the best available regexp engine and return a
|
|
|
|
matcher function'''
|
2014-07-16 01:49:45 +04:00
|
|
|
m = util.re.compile(regex)
|
2012-06-02 02:26:20 +04:00
|
|
|
try:
|
|
|
|
# slightly faster, provided by facebook's re2 bindings
|
|
|
|
return m.test_match
|
|
|
|
except AttributeError:
|
|
|
|
return m.match
|
|
|
|
|
2015-05-16 07:36:35 +03:00
|
|
|
def _expandsets(kindpats, ctx, listsubrepos):
|
2013-10-03 20:01:21 +04:00
|
|
|
'''Returns the kindpats list with the 'set' patterns expanded.'''
|
2011-06-19 01:53:44 +04:00
|
|
|
fset = set()
|
|
|
|
other = []
|
|
|
|
|
2015-05-17 01:51:03 +03:00
|
|
|
for kind, pat, source in kindpats:
|
2011-06-19 01:53:44 +04:00
|
|
|
if kind == 'set':
|
|
|
|
if not ctx:
|
2015-10-08 22:55:45 +03:00
|
|
|
raise error.Abort("fileset expression with no context")
|
2013-10-03 20:01:21 +04:00
|
|
|
s = ctx.getfileset(pat)
|
2011-06-19 01:53:44 +04:00
|
|
|
fset.update(s)
|
2015-05-16 07:36:35 +03:00
|
|
|
|
|
|
|
if listsubrepos:
|
|
|
|
for subpath in ctx.substate:
|
|
|
|
s = ctx.sub(subpath).getfileset(pat)
|
|
|
|
fset.update(subpath + '/' + f for f in s)
|
|
|
|
|
2011-06-19 01:53:44 +04:00
|
|
|
continue
|
2015-05-17 01:51:03 +03:00
|
|
|
other.append((kind, pat, source))
|
2011-06-19 01:53:44 +04:00
|
|
|
return fset, other
|
|
|
|
|
2015-05-17 02:25:05 +03:00
|
|
|
def _expandsubinclude(kindpats, root):
|
|
|
|
'''Returns the list of subinclude matchers and the kindpats without the
|
|
|
|
subincludes in it.'''
|
|
|
|
relmatchers = []
|
|
|
|
other = []
|
|
|
|
|
|
|
|
for kind, pat, source in kindpats:
|
|
|
|
if kind == 'subinclude':
|
2015-05-27 20:28:16 +03:00
|
|
|
sourceroot = pathutil.dirname(util.normpath(source))
|
2015-05-17 02:25:05 +03:00
|
|
|
pat = util.pconvert(pat)
|
|
|
|
path = pathutil.join(sourceroot, pat)
|
|
|
|
|
|
|
|
newroot = pathutil.dirname(path)
|
|
|
|
relmatcher = match(newroot, '', [], ['include:%s' % path])
|
|
|
|
|
|
|
|
prefix = pathutil.canonpath(root, root, newroot)
|
|
|
|
if prefix:
|
|
|
|
prefix += '/'
|
|
|
|
relmatchers.append((prefix, relmatcher))
|
|
|
|
else:
|
|
|
|
other.append((kind, pat, source))
|
|
|
|
|
|
|
|
return relmatchers, other
|
|
|
|
|
2014-11-20 02:56:58 +03:00
|
|
|
def _kindpatsalwaysmatch(kindpats):
|
|
|
|
""""Checks whether the kindspats match everything, as e.g.
|
|
|
|
'relpath:.' does.
|
|
|
|
"""
|
2015-05-17 01:51:03 +03:00
|
|
|
for kind, pat, source in kindpats:
|
2014-11-20 02:56:58 +03:00
|
|
|
if pat != '' or kind not in ['relpath', 'glob']:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2009-05-24 11:56:22 +04:00
|
|
|
class match(object):
|
2009-05-24 11:56:14 +04:00
|
|
|
def __init__(self, root, cwd, patterns, include=[], exclude=[],
|
2015-05-16 07:36:35 +03:00
|
|
|
default='glob', exact=False, auditor=None, ctx=None,
|
2015-06-06 01:56:33 +03:00
|
|
|
listsubrepos=False, warn=None, badfn=None):
|
2009-05-24 11:56:14 +04:00
|
|
|
"""build an object to match a set of file patterns
|
|
|
|
|
|
|
|
arguments:
|
|
|
|
root - the canonical root of the tree you're matching against
|
|
|
|
cwd - the current working directory, if relevant
|
|
|
|
patterns - patterns to find
|
2013-10-03 20:01:21 +04:00
|
|
|
include - patterns to include (unless they are excluded)
|
|
|
|
exclude - patterns to exclude (even if they are included)
|
|
|
|
default - if a pattern in patterns has no explicit type, assume this one
|
|
|
|
exact - patterns are actually filenames (include/exclude still apply)
|
2015-05-19 02:27:56 +03:00
|
|
|
warn - optional function used for printing warnings
|
2015-06-06 01:56:33 +03:00
|
|
|
badfn - optional bad() callback for this matcher instead of the default
|
2009-05-24 11:56:14 +04:00
|
|
|
|
|
|
|
a pattern is one of:
|
|
|
|
'glob:<glob>' - a glob relative to cwd
|
|
|
|
're:<regexp>' - a regular expression
|
2012-08-16 00:39:18 +04:00
|
|
|
'path:<path>' - a path relative to repository root
|
2009-05-24 11:56:14 +04:00
|
|
|
'relglob:<glob>' - an unrooted glob (*.c matches C files in all dirs)
|
|
|
|
'relpath:<path>' - a path relative to cwd
|
2009-05-24 11:56:22 +04:00
|
|
|
'relre:<regexp>' - a regexp that needn't match the start of a name
|
2011-06-19 01:53:44 +04:00
|
|
|
'set:<fileset>' - a fileset expression
|
2015-05-17 01:56:52 +03:00
|
|
|
'include:<path>' - a file of patterns to read and include
|
2015-05-17 02:25:05 +03:00
|
|
|
'subinclude:<path>' - a file of patterns to match against files under
|
|
|
|
the same directory
|
2009-05-24 11:56:22 +04:00
|
|
|
'<something>' - a pattern of the specified default type
|
2009-05-24 11:56:14 +04:00
|
|
|
"""
|
|
|
|
|
2009-05-24 11:56:22 +04:00
|
|
|
self._root = root
|
|
|
|
self._cwd = cwd
|
2013-10-03 20:01:21 +04:00
|
|
|
self._files = [] # exact files and roots of patterns
|
2009-05-24 11:56:22 +04:00
|
|
|
self._anypats = bool(include or exclude)
|
2013-02-22 00:55:39 +04:00
|
|
|
self._always = False
|
2014-12-05 07:04:55 +03:00
|
|
|
self._pathrestricted = bool(include or exclude or patterns)
|
2015-05-19 02:27:56 +03:00
|
|
|
self._warn = warn
|
2015-05-19 00:29:20 +03:00
|
|
|
self._includeroots = set()
|
|
|
|
self._includedirs = set(['.'])
|
|
|
|
self._excluderoots = set()
|
2009-05-24 11:56:14 +04:00
|
|
|
|
2015-06-06 01:56:33 +03:00
|
|
|
if badfn is not None:
|
|
|
|
self.bad = badfn
|
|
|
|
|
2014-09-20 00:49:58 +04:00
|
|
|
matchfns = []
|
2009-05-24 11:56:14 +04:00
|
|
|
if include:
|
2015-04-12 07:29:17 +03:00
|
|
|
kindpats = self._normalize(include, 'glob', root, cwd, auditor)
|
2015-05-16 07:36:35 +03:00
|
|
|
self.includepat, im = _buildmatch(ctx, kindpats, '(?:/|$)',
|
2015-05-17 02:12:00 +03:00
|
|
|
listsubrepos, root)
|
2015-05-19 00:29:20 +03:00
|
|
|
self._includeroots.update(_roots(kindpats))
|
|
|
|
self._includedirs.update(util.dirs(self._includeroots))
|
2014-09-20 00:49:58 +04:00
|
|
|
matchfns.append(im)
|
2009-05-24 11:56:14 +04:00
|
|
|
if exclude:
|
2015-04-12 07:29:17 +03:00
|
|
|
kindpats = self._normalize(exclude, 'glob', root, cwd, auditor)
|
2015-05-16 07:36:35 +03:00
|
|
|
self.excludepat, em = _buildmatch(ctx, kindpats, '(?:/|$)',
|
2015-05-17 02:12:00 +03:00
|
|
|
listsubrepos, root)
|
2015-05-27 20:44:04 +03:00
|
|
|
if not _anypats(kindpats):
|
|
|
|
self._excluderoots.update(_roots(kindpats))
|
2014-09-20 00:49:58 +04:00
|
|
|
matchfns.append(lambda f: not em(f))
|
2009-05-24 11:56:20 +04:00
|
|
|
if exact:
|
2012-05-22 19:25:29 +04:00
|
|
|
if isinstance(patterns, list):
|
|
|
|
self._files = patterns
|
|
|
|
else:
|
|
|
|
self._files = list(patterns)
|
2014-09-20 00:49:58 +04:00
|
|
|
matchfns.append(self.exact)
|
2009-05-24 11:56:20 +04:00
|
|
|
elif patterns:
|
2015-04-12 07:29:17 +03:00
|
|
|
kindpats = self._normalize(patterns, default, root, cwd, auditor)
|
2014-11-20 02:56:58 +03:00
|
|
|
if not _kindpatsalwaysmatch(kindpats):
|
|
|
|
self._files = _roots(kindpats)
|
|
|
|
self._anypats = self._anypats or _anypats(kindpats)
|
2015-05-16 07:36:35 +03:00
|
|
|
self.patternspat, pm = _buildmatch(ctx, kindpats, '$',
|
2015-05-17 02:12:00 +03:00
|
|
|
listsubrepos, root)
|
2014-11-20 02:56:58 +03:00
|
|
|
matchfns.append(pm)
|
2009-05-24 11:56:14 +04:00
|
|
|
|
2014-09-20 00:49:58 +04:00
|
|
|
if not matchfns:
|
|
|
|
m = util.always
|
|
|
|
self._always = True
|
|
|
|
elif len(matchfns) == 1:
|
|
|
|
m = matchfns[0]
|
2009-05-24 11:56:14 +04:00
|
|
|
else:
|
2014-09-20 00:49:58 +04:00
|
|
|
def m(f):
|
|
|
|
for matchfn in matchfns:
|
|
|
|
if not matchfn(f):
|
|
|
|
return False
|
|
|
|
return True
|
2009-05-24 11:56:14 +04:00
|
|
|
|
2009-05-24 11:56:22 +04:00
|
|
|
self.matchfn = m
|
2015-05-08 22:30:51 +03:00
|
|
|
self._fileroots = set(self._files)
|
2009-05-24 11:56:22 +04:00
|
|
|
|
|
|
|
def __call__(self, fn):
|
|
|
|
return self.matchfn(fn)
|
|
|
|
def __iter__(self):
|
|
|
|
for f in self._files:
|
|
|
|
yield f
|
2013-10-03 20:01:21 +04:00
|
|
|
|
|
|
|
# Callbacks related to how the matcher is used by dirstate.walk.
|
|
|
|
# Subscribers to these events must monkeypatch the matcher object.
|
2009-05-24 11:56:22 +04:00
|
|
|
def bad(self, f, msg):
|
2013-10-03 20:01:21 +04:00
|
|
|
'''Callback from dirstate.walk for each explicit file that can't be
|
|
|
|
found/accessed, with an error message.'''
|
2009-06-01 02:54:18 +04:00
|
|
|
pass
|
2013-10-03 20:01:21 +04:00
|
|
|
|
|
|
|
# If an explicitdir is set, it will be called when an explicitly listed
|
|
|
|
# directory is visited.
|
2013-05-04 01:41:58 +04:00
|
|
|
explicitdir = None
|
2013-10-03 20:01:21 +04:00
|
|
|
|
|
|
|
# If an traversedir is set, it will be called when a directory discovered
|
|
|
|
# by recursive traversal is visited.
|
2013-05-04 01:41:58 +04:00
|
|
|
traversedir = None
|
2013-10-03 20:01:21 +04:00
|
|
|
|
2014-11-29 04:15:46 +03:00
|
|
|
def abs(self, f):
|
|
|
|
'''Convert a repo path back to path that is relative to the root of the
|
|
|
|
matcher.'''
|
|
|
|
return f
|
|
|
|
|
2009-05-24 11:56:22 +04:00
|
|
|
def rel(self, f):
|
2013-10-03 20:01:21 +04:00
|
|
|
'''Convert repo path back to path that is relative to cwd of matcher.'''
|
2009-05-24 11:56:22 +04:00
|
|
|
return util.pathto(self._root, self._cwd, f)
|
2013-10-03 20:01:21 +04:00
|
|
|
|
2014-12-05 07:04:55 +03:00
|
|
|
def uipath(self, f):
|
|
|
|
'''Convert repo path to a display path. If patterns or -I/-X were used
|
|
|
|
to create this matcher, the display path will be relative to cwd.
|
|
|
|
Otherwise it is relative to the root of the repo.'''
|
2014-11-27 18:16:56 +03:00
|
|
|
return (self._pathrestricted and self.rel(f)) or self.abs(f)
|
2014-12-05 07:04:55 +03:00
|
|
|
|
2009-05-24 11:56:22 +04:00
|
|
|
def files(self):
|
2013-10-03 20:01:21 +04:00
|
|
|
'''Explicitly listed files or patterns or roots:
|
|
|
|
if no patterns or .always(): empty list,
|
|
|
|
if exact: list exact files,
|
|
|
|
if not .anypats(): list all files and dirs,
|
|
|
|
else: optimal roots'''
|
2009-05-24 11:56:22 +04:00
|
|
|
return self._files
|
2013-10-03 20:01:21 +04:00
|
|
|
|
2015-04-06 20:51:53 +03:00
|
|
|
@propertycache
|
|
|
|
def _dirs(self):
|
2015-05-08 22:30:51 +03:00
|
|
|
return set(util.dirs(self._fileroots)) | set(['.'])
|
2015-04-06 20:51:53 +03:00
|
|
|
|
|
|
|
def visitdir(self, dir):
|
2015-05-19 00:29:20 +03:00
|
|
|
'''Decides whether a directory should be visited based on whether it
|
|
|
|
has potential matches in it or one of its subdirectories. This is
|
|
|
|
based on the match's primary, included, and excluded patterns.
|
|
|
|
|
treemanifest: don't iterate entire matching submanifests on match()
Before a4236180df5e (match: remove unnecessary optimization where
visitdir() returns 'all', 2015-05-06), match.visitdir() used to return
the special value 'all' to indicate that it was known that all
subdirectories would also be included in the match. The purpose for
that value was to avoid calling the matcher on all the paths. It
turned out that calling the matcher was not a problem, so the special
return value was removed and the code was simplified. However, if we
use the same special value for not just avoiding calling the matcher
on each file, but to avoid iterating over each file, it's a much
bigger win. On commands like
hg st --rev .^ --rev . dom/
we run the matcher (dom/) on the two manifests, then diff the narrowed
manifest. If the size of the match is much larger than the size of the
diff, this is wasteful. In the above case, we would end up iterating
over the 15k-or-so files in dom/ for each of the manifests, only to
later discover that they are mostly the same. This means that runningt
the command above is usually slower than getting the status for the
entire repo, because that code avoids calling treemanifest.match() and
only calls treemanifest.diff(), which loads only what's needed for the
diff.
Let's fix this by reintroducing the 'all' value in match.visitdir()
and making treemanifest.match() return a lazy copy of the manifest
from dom/ and down (in the above case). This speeds up the above
command on the Firefox repo from 0.357s to 0.137s (best of 5). The
wider the match, the bigger the speedup.
2015-12-12 20:57:05 +03:00
|
|
|
Returns the string 'all' if the given directory and all subdirectories
|
|
|
|
should be visited. Otherwise returns True or False indicating whether
|
|
|
|
the given directory should be visited.
|
|
|
|
|
2015-05-19 00:29:20 +03:00
|
|
|
This function's behavior is undefined if it has returned False for
|
|
|
|
one of the dir's parent directories.
|
|
|
|
'''
|
treemanifest: don't iterate entire matching submanifests on match()
Before a4236180df5e (match: remove unnecessary optimization where
visitdir() returns 'all', 2015-05-06), match.visitdir() used to return
the special value 'all' to indicate that it was known that all
subdirectories would also be included in the match. The purpose for
that value was to avoid calling the matcher on all the paths. It
turned out that calling the matcher was not a problem, so the special
return value was removed and the code was simplified. However, if we
use the same special value for not just avoiding calling the matcher
on each file, but to avoid iterating over each file, it's a much
bigger win. On commands like
hg st --rev .^ --rev . dom/
we run the matcher (dom/) on the two manifests, then diff the narrowed
manifest. If the size of the match is much larger than the size of the
diff, this is wasteful. In the above case, we would end up iterating
over the 15k-or-so files in dom/ for each of the manifests, only to
later discover that they are mostly the same. This means that runningt
the command above is usually slower than getting the status for the
entire repo, because that code avoids calling treemanifest.match() and
only calls treemanifest.diff(), which loads only what's needed for the
diff.
Let's fix this by reintroducing the 'all' value in match.visitdir()
and making treemanifest.match() return a lazy copy of the manifest
from dom/ and down (in the above case). This speeds up the above
command on the Firefox repo from 0.357s to 0.137s (best of 5). The
wider the match, the bigger the speedup.
2015-12-12 20:57:05 +03:00
|
|
|
if self.prefix() and dir in self._fileroots:
|
|
|
|
return 'all'
|
2015-05-19 00:29:20 +03:00
|
|
|
if dir in self._excluderoots:
|
|
|
|
return False
|
2015-05-27 19:34:00 +03:00
|
|
|
if (self._includeroots and
|
2015-05-27 23:22:48 +03:00
|
|
|
'.' not in self._includeroots and
|
2015-05-27 19:34:00 +03:00
|
|
|
dir not in self._includeroots and
|
2015-05-31 23:17:41 +03:00
|
|
|
dir not in self._includedirs and
|
|
|
|
not any(parent in self._includeroots
|
|
|
|
for parent in util.finddirs(dir))):
|
|
|
|
return False
|
2015-05-27 19:34:00 +03:00
|
|
|
return (not self._fileroots or
|
|
|
|
'.' in self._fileroots or
|
|
|
|
dir in self._fileroots or
|
|
|
|
dir in self._dirs or
|
2015-05-08 22:30:51 +03:00
|
|
|
any(parentdir in self._fileroots
|
2015-05-27 21:47:55 +03:00
|
|
|
for parentdir in util.finddirs(dir)))
|
2015-04-06 20:51:53 +03:00
|
|
|
|
2013-10-03 20:01:21 +04:00
|
|
|
def exact(self, f):
|
|
|
|
'''Returns True if f is in .files().'''
|
2015-05-08 22:30:51 +03:00
|
|
|
return f in self._fileroots
|
2013-10-03 20:01:21 +04:00
|
|
|
|
2009-05-24 11:56:22 +04:00
|
|
|
def anypats(self):
|
2013-10-03 20:01:21 +04:00
|
|
|
'''Matcher uses patterns or include/exclude.'''
|
2009-05-24 11:56:22 +04:00
|
|
|
return self._anypats
|
2013-10-03 20:01:21 +04:00
|
|
|
|
2012-05-04 23:54:55 +04:00
|
|
|
def always(self):
|
2013-10-03 20:01:21 +04:00
|
|
|
'''Matcher will match everything and .files() will be empty
|
|
|
|
- optimization might be possible and necessary.'''
|
2013-02-22 00:55:39 +04:00
|
|
|
return self._always
|
2009-05-24 11:56:14 +04:00
|
|
|
|
2015-05-16 01:43:26 +03:00
|
|
|
def ispartial(self):
|
|
|
|
'''True if the matcher won't always match.
|
|
|
|
|
|
|
|
Although it's just the inverse of _always in this implementation,
|
2015-10-17 01:58:46 +03:00
|
|
|
an extension such as narrowhg might make it return something
|
2015-05-16 01:43:26 +03:00
|
|
|
slightly different.'''
|
|
|
|
return not self._always
|
|
|
|
|
2014-10-29 18:43:39 +03:00
|
|
|
def isexact(self):
|
|
|
|
return self.matchfn == self.exact
|
|
|
|
|
match: introduce boolean prefix() method
tl;dr: This is another step towards a (previously unstated) goal of
eliminating match.files() in conditions.
There are four types of matchers:
* always: Matches everything, checked with always(), files() is empty
* exact: Matches exact set of files, checked with isexact(), files()
contains the files to match
* patterns: Matches more complex patterns, checked with anypats(),
files() contains roots of the matched patterns
* prefix: Matches simple 'path:' patterns as prefixes ('foo' matches
both 'foo' and 'foo/bar'), no single method to check, files()
contains the prefixes to match
For completeness, it would be nice to have a method for checking for
the "prefix" type of matcher as well, so let's add that, making it
return True simply when none of the others do.
The larger goal here is to eliminate uses of match.files() in
conditions (i.e. bool(match.files())). The reason for this is that
there are scenarios when you would like to create a "prefix" matcher
that happens to match no files. One example is for 'hg files -I foo
bar'. The narrowmatcher also restricts the set of files given and it
would not surprise me if have bugs caused by that already. Note that
'if m.files() and not m.anypats()' and similar is sometimes used to
catch the "exact" and "prefix" cases above.
2014-10-29 08:47:22 +03:00
|
|
|
def prefix(self):
|
|
|
|
return not self.always() and not self.isexact() and not self.anypats()
|
|
|
|
|
2015-04-12 07:29:17 +03:00
|
|
|
def _normalize(self, patterns, default, root, cwd, auditor):
|
|
|
|
'''Convert 'kind:pat' from the patterns list to tuples with kind and
|
|
|
|
normalized and rooted patterns and with listfiles expanded.'''
|
|
|
|
kindpats = []
|
|
|
|
for kind, pat in [_patsplit(p, default) for p in patterns]:
|
|
|
|
if kind in ('glob', 'relpath'):
|
|
|
|
pat = pathutil.canonpath(root, cwd, pat, auditor)
|
|
|
|
elif kind in ('relglob', 'path'):
|
|
|
|
pat = util.normpath(pat)
|
|
|
|
elif kind in ('listfile', 'listfile0'):
|
|
|
|
try:
|
|
|
|
files = util.readfile(pat)
|
|
|
|
if kind == 'listfile0':
|
|
|
|
files = files.split('\0')
|
|
|
|
else:
|
|
|
|
files = files.splitlines()
|
|
|
|
files = [f for f in files if f]
|
|
|
|
except EnvironmentError:
|
2015-10-08 22:55:45 +03:00
|
|
|
raise error.Abort(_("unable to read file list (%s)") % pat)
|
2015-05-17 01:51:03 +03:00
|
|
|
for k, p, source in self._normalize(files, default, root, cwd,
|
|
|
|
auditor):
|
|
|
|
kindpats.append((k, p, pat))
|
2015-04-12 07:29:17 +03:00
|
|
|
continue
|
2015-05-17 01:56:52 +03:00
|
|
|
elif kind == 'include':
|
|
|
|
try:
|
2015-07-27 16:14:40 +03:00
|
|
|
fullpath = os.path.join(root, util.localpath(pat))
|
2015-07-25 02:44:52 +03:00
|
|
|
includepats = readpatternfile(fullpath, self._warn)
|
2015-05-17 01:56:52 +03:00
|
|
|
for k, p, source in self._normalize(includepats, default,
|
|
|
|
root, cwd, auditor):
|
|
|
|
kindpats.append((k, p, source or pat))
|
2015-10-08 22:55:45 +03:00
|
|
|
except error.Abort as inst:
|
|
|
|
raise error.Abort('%s: %s' % (pat, inst[0]))
|
2015-06-24 08:20:08 +03:00
|
|
|
except IOError as inst:
|
2015-05-17 01:56:52 +03:00
|
|
|
if self._warn:
|
|
|
|
self._warn(_("skipping unreadable pattern file "
|
|
|
|
"'%s': %s\n") % (pat, inst.strerror))
|
|
|
|
continue
|
2015-04-12 07:29:17 +03:00
|
|
|
# else: re or relre - which cannot be normalized
|
2015-05-17 01:51:03 +03:00
|
|
|
kindpats.append((kind, pat, ''))
|
2015-04-12 07:29:17 +03:00
|
|
|
return kindpats
|
|
|
|
|
2015-06-06 01:56:33 +03:00
|
|
|
def exact(root, cwd, files, badfn=None):
|
|
|
|
return match(root, cwd, files, exact=True, badfn=badfn)
|
2009-05-24 11:56:14 +04:00
|
|
|
|
2014-11-02 08:56:49 +03:00
|
|
|
def always(root, cwd):
|
|
|
|
return match(root, cwd, [])
|
2009-05-24 11:56:14 +04:00
|
|
|
|
2015-06-05 04:19:22 +03:00
|
|
|
def badmatch(match, badfn):
|
|
|
|
"""Make a copy of the given matcher, replacing its bad method with the given
|
|
|
|
one.
|
|
|
|
"""
|
|
|
|
m = copy.copy(match)
|
|
|
|
m.bad = badfn
|
|
|
|
return m
|
|
|
|
|
2010-09-03 14:58:51 +04:00
|
|
|
class narrowmatcher(match):
|
|
|
|
"""Adapt a matcher to work on a subdirectory only.
|
|
|
|
|
|
|
|
The paths are remapped to remove/insert the path as needed:
|
|
|
|
|
|
|
|
>>> m1 = match('root', '', ['a.txt', 'sub/b.txt'])
|
|
|
|
>>> m2 = narrowmatcher('sub', m1)
|
|
|
|
>>> bool(m2('a.txt'))
|
|
|
|
False
|
|
|
|
>>> bool(m2('b.txt'))
|
|
|
|
True
|
|
|
|
>>> bool(m2.matchfn('a.txt'))
|
|
|
|
False
|
|
|
|
>>> bool(m2.matchfn('b.txt'))
|
|
|
|
True
|
|
|
|
>>> m2.files()
|
|
|
|
['b.txt']
|
|
|
|
>>> m2.exact('b.txt')
|
|
|
|
True
|
2014-11-27 18:16:56 +03:00
|
|
|
>>> util.pconvert(m2.rel('b.txt'))
|
|
|
|
'sub/b.txt'
|
2010-09-13 15:09:09 +04:00
|
|
|
>>> def bad(f, msg):
|
|
|
|
... print "%s: %s" % (f, msg)
|
|
|
|
>>> m1.bad = bad
|
|
|
|
>>> m2.bad('x.txt', 'No such file')
|
|
|
|
sub/x.txt: No such file
|
2014-11-29 04:15:46 +03:00
|
|
|
>>> m2.abs('c.txt')
|
|
|
|
'sub/c.txt'
|
2010-09-03 14:58:51 +04:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, path, matcher):
|
2010-09-13 15:08:18 +04:00
|
|
|
self._root = matcher._root
|
|
|
|
self._cwd = matcher._cwd
|
2010-09-03 14:58:51 +04:00
|
|
|
self._path = path
|
|
|
|
self._matcher = matcher
|
2013-02-22 00:55:39 +04:00
|
|
|
self._always = matcher._always
|
2014-12-05 07:04:55 +03:00
|
|
|
self._pathrestricted = matcher._pathrestricted
|
2010-09-03 14:58:51 +04:00
|
|
|
|
|
|
|
self._files = [f[len(path) + 1:] for f in matcher._files
|
|
|
|
if f.startswith(path + "/")]
|
2015-05-18 05:09:37 +03:00
|
|
|
|
|
|
|
# If the parent repo had a path to this subrepo and no patterns are
|
|
|
|
# specified, this submatcher always matches.
|
|
|
|
if not self._always and not matcher._anypats:
|
2015-05-19 15:17:57 +03:00
|
|
|
self._always = any(f == path for f in matcher._files)
|
2015-05-18 05:09:37 +03:00
|
|
|
|
2010-09-03 14:58:51 +04:00
|
|
|
self._anypats = matcher._anypats
|
|
|
|
self.matchfn = lambda fn: matcher.matchfn(self._path + "/" + fn)
|
2015-05-08 22:30:51 +03:00
|
|
|
self._fileroots = set(self._files)
|
2010-09-03 14:58:51 +04:00
|
|
|
|
2014-11-29 04:15:46 +03:00
|
|
|
def abs(self, f):
|
|
|
|
return self._matcher.abs(self._path + "/" + f)
|
|
|
|
|
2010-09-13 15:09:09 +04:00
|
|
|
def bad(self, f, msg):
|
|
|
|
self._matcher.bad(self._path + "/" + f, msg)
|
|
|
|
|
2014-11-27 18:16:56 +03:00
|
|
|
def rel(self, f):
|
|
|
|
return self._matcher.rel(self._path + "/" + f)
|
|
|
|
|
match: add a subclass for dirstate normalizing of the matched patterns
This class is only needed on case insensitive filesystems, and only
for wdir context matches. It allows the user to not match the case of
the items in the filesystem- especially for naming directories, which
dirstate doesn't handle[1]. Making dirstate handle mismatched
directory cases is too expensive[2].
Since dirstate doesn't apply to committed csets, this is only created by
overriding basectx.match() in workingctx, and only on icasefs. The default
arguments have been dropped, because the ctx must be passed to the matcher in
order to function.
For operations that can apply to both wdir and some other context, this ends up
normalizing the filename to the case as it exists in the filesystem, and using
that case for the lookup in the other context. See the diff example in the
test.
Previously, given a directory with an inexact case:
- add worked as expected
- diff, forget and status would silently ignore the request
- files would exit with 1
- commit, revert and remove would fail (even when the commands leading up to
them worked):
$ hg ci -m "AbCDef" capsdir1/capsdir
abort: CapsDir1/CapsDir: no match under directory!
$ hg revert -r '.^' capsdir1/capsdir
capsdir1\capsdir: no such file in rev 64dae27060b7
$ hg remove capsdir1/capsdir
not removing capsdir1\capsdir: no tracked files
[1]
Globs are normalized, so that the -I and -X don't need to be specified with a
case match. Without that, the second last remove (with -X) removes the files,
leaving nothing for the last remove. However, specifying the files as
'glob:**.Txt' does not work. Perhaps this requires 're.IGNORECASE'?
There are only a handful of places that create matchers directly, instead of
being routed through the context.match() method. Some may benefit from changing
over to using ctx.match() as a factory function:
revset.checkstatus()
revset.contains()
revset.filelog()
revset._matchfiles()
localrepository._loadfilter()
ignore.ignore()
fileset.subrepo()
filemerge._picktool()
overrides.addlargefiles()
lfcommands.lfconvert()
kwtemplate.__init__()
eolfile.__init__()
eolfile.checkrev()
acl.buildmatch()
Currently, a toplevel subrepo can be named with an inexact case. However, the
path auditor gets in the way of naming _anything_ in the subrepo if the top
level case doesn't match. That is trickier to handle, because there's the user
provided case, the case in the filesystem, and the case stored in .hgsub. This
can be fixed next cycle.
--- a/tests/test-subrepo-deep-nested-change.t
+++ b/tests/test-subrepo-deep-nested-change.t
@@ -170,8 +170,15 @@
R sub1/sub2/test.txt
$ hg update -Cq
$ touch sub1/sub2/folder/bar
+#if icasefs
+ $ hg addremove Sub1/sub2
+ abort: path 'Sub1\sub2' is inside nested repo 'Sub1'
+ [255]
+ $ hg -q addremove sub1/sub2
+#else
$ hg addremove sub1/sub2
adding sub1/sub2/folder/bar (glob)
+#endif
$ hg status -S
A sub1/sub2/folder/bar
? foo/bar/abc
The narrowmatcher class may need to be tweaked when that is fixed.
[1] http://www.selenic.com/pipermail/mercurial-devel/2015-April/068183.html
[2] http://www.selenic.com/pipermail/mercurial-devel/2015-April/068191.html
2015-04-12 08:39:21 +03:00
|
|
|
class icasefsmatcher(match):
|
|
|
|
"""A matcher for wdir on case insensitive filesystems, which normalizes the
|
|
|
|
given patterns to the case in the filesystem.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, root, cwd, patterns, include, exclude, default, auditor,
|
2015-06-06 01:56:33 +03:00
|
|
|
ctx, listsubrepos=False, badfn=None):
|
match: add a subclass for dirstate normalizing of the matched patterns
This class is only needed on case insensitive filesystems, and only
for wdir context matches. It allows the user to not match the case of
the items in the filesystem- especially for naming directories, which
dirstate doesn't handle[1]. Making dirstate handle mismatched
directory cases is too expensive[2].
Since dirstate doesn't apply to committed csets, this is only created by
overriding basectx.match() in workingctx, and only on icasefs. The default
arguments have been dropped, because the ctx must be passed to the matcher in
order to function.
For operations that can apply to both wdir and some other context, this ends up
normalizing the filename to the case as it exists in the filesystem, and using
that case for the lookup in the other context. See the diff example in the
test.
Previously, given a directory with an inexact case:
- add worked as expected
- diff, forget and status would silently ignore the request
- files would exit with 1
- commit, revert and remove would fail (even when the commands leading up to
them worked):
$ hg ci -m "AbCDef" capsdir1/capsdir
abort: CapsDir1/CapsDir: no match under directory!
$ hg revert -r '.^' capsdir1/capsdir
capsdir1\capsdir: no such file in rev 64dae27060b7
$ hg remove capsdir1/capsdir
not removing capsdir1\capsdir: no tracked files
[1]
Globs are normalized, so that the -I and -X don't need to be specified with a
case match. Without that, the second last remove (with -X) removes the files,
leaving nothing for the last remove. However, specifying the files as
'glob:**.Txt' does not work. Perhaps this requires 're.IGNORECASE'?
There are only a handful of places that create matchers directly, instead of
being routed through the context.match() method. Some may benefit from changing
over to using ctx.match() as a factory function:
revset.checkstatus()
revset.contains()
revset.filelog()
revset._matchfiles()
localrepository._loadfilter()
ignore.ignore()
fileset.subrepo()
filemerge._picktool()
overrides.addlargefiles()
lfcommands.lfconvert()
kwtemplate.__init__()
eolfile.__init__()
eolfile.checkrev()
acl.buildmatch()
Currently, a toplevel subrepo can be named with an inexact case. However, the
path auditor gets in the way of naming _anything_ in the subrepo if the top
level case doesn't match. That is trickier to handle, because there's the user
provided case, the case in the filesystem, and the case stored in .hgsub. This
can be fixed next cycle.
--- a/tests/test-subrepo-deep-nested-change.t
+++ b/tests/test-subrepo-deep-nested-change.t
@@ -170,8 +170,15 @@
R sub1/sub2/test.txt
$ hg update -Cq
$ touch sub1/sub2/folder/bar
+#if icasefs
+ $ hg addremove Sub1/sub2
+ abort: path 'Sub1\sub2' is inside nested repo 'Sub1'
+ [255]
+ $ hg -q addremove sub1/sub2
+#else
$ hg addremove sub1/sub2
adding sub1/sub2/folder/bar (glob)
+#endif
$ hg status -S
A sub1/sub2/folder/bar
? foo/bar/abc
The narrowmatcher class may need to be tweaked when that is fixed.
[1] http://www.selenic.com/pipermail/mercurial-devel/2015-April/068183.html
[2] http://www.selenic.com/pipermail/mercurial-devel/2015-April/068191.html
2015-04-12 08:39:21 +03:00
|
|
|
init = super(icasefsmatcher, self).__init__
|
match: fix a caseonly rename + explicit path commit on icasefs (issue4768)
The problem was that the former name and the new name are both normalized to the
case in dirstate, so matcher._files would be ['ABC.txt', 'ABC.txt'].
localrepo.commit() calls localrepo.status(), passing along the matcher. Inside
dirstate.status(), _walkexplicit() simply grabs matcher.files() and processes
those items. Since the old name isn't present, it is silently dropped. There's
a fundamental tension here, because the status command should also accept files
that don't match the filesystem, so we can't drop the normalization in status.
The problem originated in d70aa474bd84.
Unfortunately with this change, the case of the old file must still be specified
exactly, or the old file is again silently excluded. I went back to
d70aa474bd84^, and that had the same behavior, so we are no worse off. I'm open
to ideas from a matcher or dirstate expert on how to fix that half.
2015-08-07 04:00:16 +03:00
|
|
|
self._dirstate = ctx.repo().dirstate
|
|
|
|
self._dsnormalize = self._dirstate.normalize
|
match: add a subclass for dirstate normalizing of the matched patterns
This class is only needed on case insensitive filesystems, and only
for wdir context matches. It allows the user to not match the case of
the items in the filesystem- especially for naming directories, which
dirstate doesn't handle[1]. Making dirstate handle mismatched
directory cases is too expensive[2].
Since dirstate doesn't apply to committed csets, this is only created by
overriding basectx.match() in workingctx, and only on icasefs. The default
arguments have been dropped, because the ctx must be passed to the matcher in
order to function.
For operations that can apply to both wdir and some other context, this ends up
normalizing the filename to the case as it exists in the filesystem, and using
that case for the lookup in the other context. See the diff example in the
test.
Previously, given a directory with an inexact case:
- add worked as expected
- diff, forget and status would silently ignore the request
- files would exit with 1
- commit, revert and remove would fail (even when the commands leading up to
them worked):
$ hg ci -m "AbCDef" capsdir1/capsdir
abort: CapsDir1/CapsDir: no match under directory!
$ hg revert -r '.^' capsdir1/capsdir
capsdir1\capsdir: no such file in rev 64dae27060b7
$ hg remove capsdir1/capsdir
not removing capsdir1\capsdir: no tracked files
[1]
Globs are normalized, so that the -I and -X don't need to be specified with a
case match. Without that, the second last remove (with -X) removes the files,
leaving nothing for the last remove. However, specifying the files as
'glob:**.Txt' does not work. Perhaps this requires 're.IGNORECASE'?
There are only a handful of places that create matchers directly, instead of
being routed through the context.match() method. Some may benefit from changing
over to using ctx.match() as a factory function:
revset.checkstatus()
revset.contains()
revset.filelog()
revset._matchfiles()
localrepository._loadfilter()
ignore.ignore()
fileset.subrepo()
filemerge._picktool()
overrides.addlargefiles()
lfcommands.lfconvert()
kwtemplate.__init__()
eolfile.__init__()
eolfile.checkrev()
acl.buildmatch()
Currently, a toplevel subrepo can be named with an inexact case. However, the
path auditor gets in the way of naming _anything_ in the subrepo if the top
level case doesn't match. That is trickier to handle, because there's the user
provided case, the case in the filesystem, and the case stored in .hgsub. This
can be fixed next cycle.
--- a/tests/test-subrepo-deep-nested-change.t
+++ b/tests/test-subrepo-deep-nested-change.t
@@ -170,8 +170,15 @@
R sub1/sub2/test.txt
$ hg update -Cq
$ touch sub1/sub2/folder/bar
+#if icasefs
+ $ hg addremove Sub1/sub2
+ abort: path 'Sub1\sub2' is inside nested repo 'Sub1'
+ [255]
+ $ hg -q addremove sub1/sub2
+#else
$ hg addremove sub1/sub2
adding sub1/sub2/folder/bar (glob)
+#endif
$ hg status -S
A sub1/sub2/folder/bar
? foo/bar/abc
The narrowmatcher class may need to be tweaked when that is fixed.
[1] http://www.selenic.com/pipermail/mercurial-devel/2015-April/068183.html
[2] http://www.selenic.com/pipermail/mercurial-devel/2015-April/068191.html
2015-04-12 08:39:21 +03:00
|
|
|
|
|
|
|
init(root, cwd, patterns, include, exclude, default, auditor=auditor,
|
2015-06-06 01:56:33 +03:00
|
|
|
ctx=ctx, listsubrepos=listsubrepos, badfn=badfn)
|
match: add a subclass for dirstate normalizing of the matched patterns
This class is only needed on case insensitive filesystems, and only
for wdir context matches. It allows the user to not match the case of
the items in the filesystem- especially for naming directories, which
dirstate doesn't handle[1]. Making dirstate handle mismatched
directory cases is too expensive[2].
Since dirstate doesn't apply to committed csets, this is only created by
overriding basectx.match() in workingctx, and only on icasefs. The default
arguments have been dropped, because the ctx must be passed to the matcher in
order to function.
For operations that can apply to both wdir and some other context, this ends up
normalizing the filename to the case as it exists in the filesystem, and using
that case for the lookup in the other context. See the diff example in the
test.
Previously, given a directory with an inexact case:
- add worked as expected
- diff, forget and status would silently ignore the request
- files would exit with 1
- commit, revert and remove would fail (even when the commands leading up to
them worked):
$ hg ci -m "AbCDef" capsdir1/capsdir
abort: CapsDir1/CapsDir: no match under directory!
$ hg revert -r '.^' capsdir1/capsdir
capsdir1\capsdir: no such file in rev 64dae27060b7
$ hg remove capsdir1/capsdir
not removing capsdir1\capsdir: no tracked files
[1]
Globs are normalized, so that the -I and -X don't need to be specified with a
case match. Without that, the second last remove (with -X) removes the files,
leaving nothing for the last remove. However, specifying the files as
'glob:**.Txt' does not work. Perhaps this requires 're.IGNORECASE'?
There are only a handful of places that create matchers directly, instead of
being routed through the context.match() method. Some may benefit from changing
over to using ctx.match() as a factory function:
revset.checkstatus()
revset.contains()
revset.filelog()
revset._matchfiles()
localrepository._loadfilter()
ignore.ignore()
fileset.subrepo()
filemerge._picktool()
overrides.addlargefiles()
lfcommands.lfconvert()
kwtemplate.__init__()
eolfile.__init__()
eolfile.checkrev()
acl.buildmatch()
Currently, a toplevel subrepo can be named with an inexact case. However, the
path auditor gets in the way of naming _anything_ in the subrepo if the top
level case doesn't match. That is trickier to handle, because there's the user
provided case, the case in the filesystem, and the case stored in .hgsub. This
can be fixed next cycle.
--- a/tests/test-subrepo-deep-nested-change.t
+++ b/tests/test-subrepo-deep-nested-change.t
@@ -170,8 +170,15 @@
R sub1/sub2/test.txt
$ hg update -Cq
$ touch sub1/sub2/folder/bar
+#if icasefs
+ $ hg addremove Sub1/sub2
+ abort: path 'Sub1\sub2' is inside nested repo 'Sub1'
+ [255]
+ $ hg -q addremove sub1/sub2
+#else
$ hg addremove sub1/sub2
adding sub1/sub2/folder/bar (glob)
+#endif
$ hg status -S
A sub1/sub2/folder/bar
? foo/bar/abc
The narrowmatcher class may need to be tweaked when that is fixed.
[1] http://www.selenic.com/pipermail/mercurial-devel/2015-April/068183.html
[2] http://www.selenic.com/pipermail/mercurial-devel/2015-April/068191.html
2015-04-12 08:39:21 +03:00
|
|
|
|
|
|
|
# m.exact(file) must be based off of the actual user input, otherwise
|
|
|
|
# inexact case matches are treated as exact, and not noted without -v.
|
|
|
|
if self._files:
|
2015-05-08 22:30:51 +03:00
|
|
|
self._fileroots = set(_roots(self._kp))
|
match: add a subclass for dirstate normalizing of the matched patterns
This class is only needed on case insensitive filesystems, and only
for wdir context matches. It allows the user to not match the case of
the items in the filesystem- especially for naming directories, which
dirstate doesn't handle[1]. Making dirstate handle mismatched
directory cases is too expensive[2].
Since dirstate doesn't apply to committed csets, this is only created by
overriding basectx.match() in workingctx, and only on icasefs. The default
arguments have been dropped, because the ctx must be passed to the matcher in
order to function.
For operations that can apply to both wdir and some other context, this ends up
normalizing the filename to the case as it exists in the filesystem, and using
that case for the lookup in the other context. See the diff example in the
test.
Previously, given a directory with an inexact case:
- add worked as expected
- diff, forget and status would silently ignore the request
- files would exit with 1
- commit, revert and remove would fail (even when the commands leading up to
them worked):
$ hg ci -m "AbCDef" capsdir1/capsdir
abort: CapsDir1/CapsDir: no match under directory!
$ hg revert -r '.^' capsdir1/capsdir
capsdir1\capsdir: no such file in rev 64dae27060b7
$ hg remove capsdir1/capsdir
not removing capsdir1\capsdir: no tracked files
[1]
Globs are normalized, so that the -I and -X don't need to be specified with a
case match. Without that, the second last remove (with -X) removes the files,
leaving nothing for the last remove. However, specifying the files as
'glob:**.Txt' does not work. Perhaps this requires 're.IGNORECASE'?
There are only a handful of places that create matchers directly, instead of
being routed through the context.match() method. Some may benefit from changing
over to using ctx.match() as a factory function:
revset.checkstatus()
revset.contains()
revset.filelog()
revset._matchfiles()
localrepository._loadfilter()
ignore.ignore()
fileset.subrepo()
filemerge._picktool()
overrides.addlargefiles()
lfcommands.lfconvert()
kwtemplate.__init__()
eolfile.__init__()
eolfile.checkrev()
acl.buildmatch()
Currently, a toplevel subrepo can be named with an inexact case. However, the
path auditor gets in the way of naming _anything_ in the subrepo if the top
level case doesn't match. That is trickier to handle, because there's the user
provided case, the case in the filesystem, and the case stored in .hgsub. This
can be fixed next cycle.
--- a/tests/test-subrepo-deep-nested-change.t
+++ b/tests/test-subrepo-deep-nested-change.t
@@ -170,8 +170,15 @@
R sub1/sub2/test.txt
$ hg update -Cq
$ touch sub1/sub2/folder/bar
+#if icasefs
+ $ hg addremove Sub1/sub2
+ abort: path 'Sub1\sub2' is inside nested repo 'Sub1'
+ [255]
+ $ hg -q addremove sub1/sub2
+#else
$ hg addremove sub1/sub2
adding sub1/sub2/folder/bar (glob)
+#endif
$ hg status -S
A sub1/sub2/folder/bar
? foo/bar/abc
The narrowmatcher class may need to be tweaked when that is fixed.
[1] http://www.selenic.com/pipermail/mercurial-devel/2015-April/068183.html
[2] http://www.selenic.com/pipermail/mercurial-devel/2015-April/068191.html
2015-04-12 08:39:21 +03:00
|
|
|
|
|
|
|
def _normalize(self, patterns, default, root, cwd, auditor):
|
|
|
|
self._kp = super(icasefsmatcher, self)._normalize(patterns, default,
|
|
|
|
root, cwd, auditor)
|
|
|
|
kindpats = []
|
2015-05-17 01:51:03 +03:00
|
|
|
for kind, pats, source in self._kp:
|
match: add a subclass for dirstate normalizing of the matched patterns
This class is only needed on case insensitive filesystems, and only
for wdir context matches. It allows the user to not match the case of
the items in the filesystem- especially for naming directories, which
dirstate doesn't handle[1]. Making dirstate handle mismatched
directory cases is too expensive[2].
Since dirstate doesn't apply to committed csets, this is only created by
overriding basectx.match() in workingctx, and only on icasefs. The default
arguments have been dropped, because the ctx must be passed to the matcher in
order to function.
For operations that can apply to both wdir and some other context, this ends up
normalizing the filename to the case as it exists in the filesystem, and using
that case for the lookup in the other context. See the diff example in the
test.
Previously, given a directory with an inexact case:
- add worked as expected
- diff, forget and status would silently ignore the request
- files would exit with 1
- commit, revert and remove would fail (even when the commands leading up to
them worked):
$ hg ci -m "AbCDef" capsdir1/capsdir
abort: CapsDir1/CapsDir: no match under directory!
$ hg revert -r '.^' capsdir1/capsdir
capsdir1\capsdir: no such file in rev 64dae27060b7
$ hg remove capsdir1/capsdir
not removing capsdir1\capsdir: no tracked files
[1]
Globs are normalized, so that the -I and -X don't need to be specified with a
case match. Without that, the second last remove (with -X) removes the files,
leaving nothing for the last remove. However, specifying the files as
'glob:**.Txt' does not work. Perhaps this requires 're.IGNORECASE'?
There are only a handful of places that create matchers directly, instead of
being routed through the context.match() method. Some may benefit from changing
over to using ctx.match() as a factory function:
revset.checkstatus()
revset.contains()
revset.filelog()
revset._matchfiles()
localrepository._loadfilter()
ignore.ignore()
fileset.subrepo()
filemerge._picktool()
overrides.addlargefiles()
lfcommands.lfconvert()
kwtemplate.__init__()
eolfile.__init__()
eolfile.checkrev()
acl.buildmatch()
Currently, a toplevel subrepo can be named with an inexact case. However, the
path auditor gets in the way of naming _anything_ in the subrepo if the top
level case doesn't match. That is trickier to handle, because there's the user
provided case, the case in the filesystem, and the case stored in .hgsub. This
can be fixed next cycle.
--- a/tests/test-subrepo-deep-nested-change.t
+++ b/tests/test-subrepo-deep-nested-change.t
@@ -170,8 +170,15 @@
R sub1/sub2/test.txt
$ hg update -Cq
$ touch sub1/sub2/folder/bar
+#if icasefs
+ $ hg addremove Sub1/sub2
+ abort: path 'Sub1\sub2' is inside nested repo 'Sub1'
+ [255]
+ $ hg -q addremove sub1/sub2
+#else
$ hg addremove sub1/sub2
adding sub1/sub2/folder/bar (glob)
+#endif
$ hg status -S
A sub1/sub2/folder/bar
? foo/bar/abc
The narrowmatcher class may need to be tweaked when that is fixed.
[1] http://www.selenic.com/pipermail/mercurial-devel/2015-April/068183.html
[2] http://www.selenic.com/pipermail/mercurial-devel/2015-April/068191.html
2015-04-12 08:39:21 +03:00
|
|
|
if kind not in ('re', 'relre'): # regex can't be normalized
|
match: fix a caseonly rename + explicit path commit on icasefs (issue4768)
The problem was that the former name and the new name are both normalized to the
case in dirstate, so matcher._files would be ['ABC.txt', 'ABC.txt'].
localrepo.commit() calls localrepo.status(), passing along the matcher. Inside
dirstate.status(), _walkexplicit() simply grabs matcher.files() and processes
those items. Since the old name isn't present, it is silently dropped. There's
a fundamental tension here, because the status command should also accept files
that don't match the filesystem, so we can't drop the normalization in status.
The problem originated in d70aa474bd84.
Unfortunately with this change, the case of the old file must still be specified
exactly, or the old file is again silently excluded. I went back to
d70aa474bd84^, and that had the same behavior, so we are no worse off. I'm open
to ideas from a matcher or dirstate expert on how to fix that half.
2015-08-07 04:00:16 +03:00
|
|
|
p = pats
|
match: add a subclass for dirstate normalizing of the matched patterns
This class is only needed on case insensitive filesystems, and only
for wdir context matches. It allows the user to not match the case of
the items in the filesystem- especially for naming directories, which
dirstate doesn't handle[1]. Making dirstate handle mismatched
directory cases is too expensive[2].
Since dirstate doesn't apply to committed csets, this is only created by
overriding basectx.match() in workingctx, and only on icasefs. The default
arguments have been dropped, because the ctx must be passed to the matcher in
order to function.
For operations that can apply to both wdir and some other context, this ends up
normalizing the filename to the case as it exists in the filesystem, and using
that case for the lookup in the other context. See the diff example in the
test.
Previously, given a directory with an inexact case:
- add worked as expected
- diff, forget and status would silently ignore the request
- files would exit with 1
- commit, revert and remove would fail (even when the commands leading up to
them worked):
$ hg ci -m "AbCDef" capsdir1/capsdir
abort: CapsDir1/CapsDir: no match under directory!
$ hg revert -r '.^' capsdir1/capsdir
capsdir1\capsdir: no such file in rev 64dae27060b7
$ hg remove capsdir1/capsdir
not removing capsdir1\capsdir: no tracked files
[1]
Globs are normalized, so that the -I and -X don't need to be specified with a
case match. Without that, the second last remove (with -X) removes the files,
leaving nothing for the last remove. However, specifying the files as
'glob:**.Txt' does not work. Perhaps this requires 're.IGNORECASE'?
There are only a handful of places that create matchers directly, instead of
being routed through the context.match() method. Some may benefit from changing
over to using ctx.match() as a factory function:
revset.checkstatus()
revset.contains()
revset.filelog()
revset._matchfiles()
localrepository._loadfilter()
ignore.ignore()
fileset.subrepo()
filemerge._picktool()
overrides.addlargefiles()
lfcommands.lfconvert()
kwtemplate.__init__()
eolfile.__init__()
eolfile.checkrev()
acl.buildmatch()
Currently, a toplevel subrepo can be named with an inexact case. However, the
path auditor gets in the way of naming _anything_ in the subrepo if the top
level case doesn't match. That is trickier to handle, because there's the user
provided case, the case in the filesystem, and the case stored in .hgsub. This
can be fixed next cycle.
--- a/tests/test-subrepo-deep-nested-change.t
+++ b/tests/test-subrepo-deep-nested-change.t
@@ -170,8 +170,15 @@
R sub1/sub2/test.txt
$ hg update -Cq
$ touch sub1/sub2/folder/bar
+#if icasefs
+ $ hg addremove Sub1/sub2
+ abort: path 'Sub1\sub2' is inside nested repo 'Sub1'
+ [255]
+ $ hg -q addremove sub1/sub2
+#else
$ hg addremove sub1/sub2
adding sub1/sub2/folder/bar (glob)
+#endif
$ hg status -S
A sub1/sub2/folder/bar
? foo/bar/abc
The narrowmatcher class may need to be tweaked when that is fixed.
[1] http://www.selenic.com/pipermail/mercurial-devel/2015-April/068183.html
[2] http://www.selenic.com/pipermail/mercurial-devel/2015-April/068191.html
2015-04-12 08:39:21 +03:00
|
|
|
pats = self._dsnormalize(pats)
|
match: fix a caseonly rename + explicit path commit on icasefs (issue4768)
The problem was that the former name and the new name are both normalized to the
case in dirstate, so matcher._files would be ['ABC.txt', 'ABC.txt'].
localrepo.commit() calls localrepo.status(), passing along the matcher. Inside
dirstate.status(), _walkexplicit() simply grabs matcher.files() and processes
those items. Since the old name isn't present, it is silently dropped. There's
a fundamental tension here, because the status command should also accept files
that don't match the filesystem, so we can't drop the normalization in status.
The problem originated in d70aa474bd84.
Unfortunately with this change, the case of the old file must still be specified
exactly, or the old file is again silently excluded. I went back to
d70aa474bd84^, and that had the same behavior, so we are no worse off. I'm open
to ideas from a matcher or dirstate expert on how to fix that half.
2015-08-07 04:00:16 +03:00
|
|
|
|
|
|
|
# Preserve the original to handle a case only rename.
|
|
|
|
if p != pats and p in self._dirstate:
|
|
|
|
kindpats.append((kind, p, source))
|
|
|
|
|
2015-05-17 01:51:03 +03:00
|
|
|
kindpats.append((kind, pats, source))
|
match: add a subclass for dirstate normalizing of the matched patterns
This class is only needed on case insensitive filesystems, and only
for wdir context matches. It allows the user to not match the case of
the items in the filesystem- especially for naming directories, which
dirstate doesn't handle[1]. Making dirstate handle mismatched
directory cases is too expensive[2].
Since dirstate doesn't apply to committed csets, this is only created by
overriding basectx.match() in workingctx, and only on icasefs. The default
arguments have been dropped, because the ctx must be passed to the matcher in
order to function.
For operations that can apply to both wdir and some other context, this ends up
normalizing the filename to the case as it exists in the filesystem, and using
that case for the lookup in the other context. See the diff example in the
test.
Previously, given a directory with an inexact case:
- add worked as expected
- diff, forget and status would silently ignore the request
- files would exit with 1
- commit, revert and remove would fail (even when the commands leading up to
them worked):
$ hg ci -m "AbCDef" capsdir1/capsdir
abort: CapsDir1/CapsDir: no match under directory!
$ hg revert -r '.^' capsdir1/capsdir
capsdir1\capsdir: no such file in rev 64dae27060b7
$ hg remove capsdir1/capsdir
not removing capsdir1\capsdir: no tracked files
[1]
Globs are normalized, so that the -I and -X don't need to be specified with a
case match. Without that, the second last remove (with -X) removes the files,
leaving nothing for the last remove. However, specifying the files as
'glob:**.Txt' does not work. Perhaps this requires 're.IGNORECASE'?
There are only a handful of places that create matchers directly, instead of
being routed through the context.match() method. Some may benefit from changing
over to using ctx.match() as a factory function:
revset.checkstatus()
revset.contains()
revset.filelog()
revset._matchfiles()
localrepository._loadfilter()
ignore.ignore()
fileset.subrepo()
filemerge._picktool()
overrides.addlargefiles()
lfcommands.lfconvert()
kwtemplate.__init__()
eolfile.__init__()
eolfile.checkrev()
acl.buildmatch()
Currently, a toplevel subrepo can be named with an inexact case. However, the
path auditor gets in the way of naming _anything_ in the subrepo if the top
level case doesn't match. That is trickier to handle, because there's the user
provided case, the case in the filesystem, and the case stored in .hgsub. This
can be fixed next cycle.
--- a/tests/test-subrepo-deep-nested-change.t
+++ b/tests/test-subrepo-deep-nested-change.t
@@ -170,8 +170,15 @@
R sub1/sub2/test.txt
$ hg update -Cq
$ touch sub1/sub2/folder/bar
+#if icasefs
+ $ hg addremove Sub1/sub2
+ abort: path 'Sub1\sub2' is inside nested repo 'Sub1'
+ [255]
+ $ hg -q addremove sub1/sub2
+#else
$ hg addremove sub1/sub2
adding sub1/sub2/folder/bar (glob)
+#endif
$ hg status -S
A sub1/sub2/folder/bar
? foo/bar/abc
The narrowmatcher class may need to be tweaked when that is fixed.
[1] http://www.selenic.com/pipermail/mercurial-devel/2015-April/068183.html
[2] http://www.selenic.com/pipermail/mercurial-devel/2015-April/068191.html
2015-04-12 08:39:21 +03:00
|
|
|
return kindpats
|
|
|
|
|
2013-10-03 20:01:21 +04:00
|
|
|
def patkind(pattern, default=None):
|
|
|
|
'''If pattern is 'kind:pat' with a known kind, return kind.'''
|
|
|
|
return _patsplit(pattern, default)[0]
|
2009-05-24 11:56:14 +04:00
|
|
|
|
2013-10-03 20:01:21 +04:00
|
|
|
def _patsplit(pattern, default):
|
|
|
|
"""Split a string into the optional pattern kind prefix and the actual
|
|
|
|
pattern."""
|
|
|
|
if ':' in pattern:
|
|
|
|
kind, pat = pattern.split(':', 1)
|
2010-12-24 00:12:24 +03:00
|
|
|
if kind in ('re', 'glob', 'path', 'relglob', 'relpath', 'relre',
|
2015-05-17 02:25:05 +03:00
|
|
|
'listfile', 'listfile0', 'set', 'include', 'subinclude'):
|
2013-10-03 20:01:21 +04:00
|
|
|
return kind, pat
|
|
|
|
return default, pattern
|
2009-05-24 11:56:14 +04:00
|
|
|
|
2009-05-24 11:56:14 +04:00
|
|
|
def _globre(pat):
|
2014-04-14 00:00:08 +04:00
|
|
|
r'''Convert an extended glob string to a regexp string.
|
|
|
|
|
|
|
|
>>> print _globre(r'?')
|
|
|
|
.
|
|
|
|
>>> print _globre(r'*')
|
|
|
|
[^/]*
|
|
|
|
>>> print _globre(r'**')
|
|
|
|
.*
|
2014-06-26 01:50:48 +04:00
|
|
|
>>> print _globre(r'**/a')
|
|
|
|
(?:.*/)?a
|
|
|
|
>>> print _globre(r'a/**/b')
|
|
|
|
a\/(?:.*/)?b
|
2014-04-14 00:00:08 +04:00
|
|
|
>>> print _globre(r'[a*?!^][^b][!c]')
|
|
|
|
[a*?!^][\^b][^c]
|
|
|
|
>>> print _globre(r'{a,b}')
|
|
|
|
(?:a|b)
|
|
|
|
>>> print _globre(r'.\*\?')
|
|
|
|
\.\*\?
|
|
|
|
'''
|
2009-05-24 11:56:14 +04:00
|
|
|
i, n = 0, len(pat)
|
|
|
|
res = ''
|
|
|
|
group = 0
|
2014-07-16 02:34:50 +04:00
|
|
|
escape = util.re.escape
|
2010-01-25 09:05:27 +03:00
|
|
|
def peek():
|
|
|
|
return i < n and pat[i]
|
2009-05-24 11:56:14 +04:00
|
|
|
while i < n:
|
|
|
|
c = pat[i]
|
2010-01-25 09:05:27 +03:00
|
|
|
i += 1
|
2009-05-24 11:56:14 +04:00
|
|
|
if c not in '*?[{},\\':
|
|
|
|
res += escape(c)
|
|
|
|
elif c == '*':
|
2009-05-24 11:56:14 +04:00
|
|
|
if peek() == '*':
|
|
|
|
i += 1
|
2014-06-26 01:50:48 +04:00
|
|
|
if peek() == '/':
|
|
|
|
i += 1
|
|
|
|
res += '(?:.*/)?'
|
|
|
|
else:
|
|
|
|
res += '.*'
|
2009-05-24 11:56:14 +04:00
|
|
|
else:
|
|
|
|
res += '[^/]*'
|
|
|
|
elif c == '?':
|
|
|
|
res += '.'
|
|
|
|
elif c == '[':
|
|
|
|
j = i
|
|
|
|
if j < n and pat[j] in '!]':
|
|
|
|
j += 1
|
|
|
|
while j < n and pat[j] != ']':
|
|
|
|
j += 1
|
|
|
|
if j >= n:
|
|
|
|
res += '\\['
|
|
|
|
else:
|
|
|
|
stuff = pat[i:j].replace('\\','\\\\')
|
|
|
|
i = j + 1
|
|
|
|
if stuff[0] == '!':
|
|
|
|
stuff = '^' + stuff[1:]
|
|
|
|
elif stuff[0] == '^':
|
|
|
|
stuff = '\\' + stuff
|
|
|
|
res = '%s[%s]' % (res, stuff)
|
|
|
|
elif c == '{':
|
|
|
|
group += 1
|
|
|
|
res += '(?:'
|
|
|
|
elif c == '}' and group:
|
|
|
|
res += ')'
|
|
|
|
group -= 1
|
|
|
|
elif c == ',' and group:
|
|
|
|
res += '|'
|
|
|
|
elif c == '\\':
|
|
|
|
p = peek()
|
|
|
|
if p:
|
|
|
|
i += 1
|
2009-05-24 11:56:14 +04:00
|
|
|
res += escape(p)
|
2009-05-24 11:56:14 +04:00
|
|
|
else:
|
2009-05-24 11:56:14 +04:00
|
|
|
res += escape(c)
|
2009-05-24 11:56:14 +04:00
|
|
|
else:
|
2009-05-24 11:56:14 +04:00
|
|
|
res += escape(c)
|
2009-05-24 11:56:14 +04:00
|
|
|
return res
|
2009-05-24 11:56:14 +04:00
|
|
|
|
2013-10-03 20:01:21 +04:00
|
|
|
def _regex(kind, pat, globsuffix):
|
|
|
|
'''Convert a (normalized) pattern of any kind into a regular expression.
|
|
|
|
globsuffix is appended to the regexp of globs.'''
|
|
|
|
if not pat:
|
2009-05-24 11:56:14 +04:00
|
|
|
return ''
|
|
|
|
if kind == 're':
|
2013-10-03 20:01:21 +04:00
|
|
|
return pat
|
|
|
|
if kind == 'path':
|
2015-06-21 02:59:26 +03:00
|
|
|
if pat == '.':
|
|
|
|
return ''
|
2014-07-16 02:34:50 +04:00
|
|
|
return '^' + util.re.escape(pat) + '(?:/|$)'
|
2013-10-03 20:01:21 +04:00
|
|
|
if kind == 'relglob':
|
|
|
|
return '(?:|.*/)' + _globre(pat) + globsuffix
|
|
|
|
if kind == 'relpath':
|
2014-07-16 02:34:50 +04:00
|
|
|
return util.re.escape(pat) + '(?:/|$)'
|
2013-10-03 20:01:21 +04:00
|
|
|
if kind == 'relre':
|
|
|
|
if pat.startswith('^'):
|
|
|
|
return pat
|
|
|
|
return '.*' + pat
|
|
|
|
return _globre(pat) + globsuffix
|
|
|
|
|
2015-05-17 02:12:00 +03:00
|
|
|
def _buildmatch(ctx, kindpats, globsuffix, listsubrepos, root):
|
2013-10-03 20:01:21 +04:00
|
|
|
'''Return regexp string and a matcher function for kindpats.
|
|
|
|
globsuffix is appended to the regexp of globs.'''
|
2015-05-17 02:16:18 +03:00
|
|
|
matchfuncs = []
|
2011-06-19 01:53:44 +04:00
|
|
|
|
2015-05-17 02:25:05 +03:00
|
|
|
subincludes, kindpats = _expandsubinclude(kindpats, root)
|
|
|
|
if subincludes:
|
|
|
|
def matchsubinclude(f):
|
|
|
|
for prefix, mf in subincludes:
|
|
|
|
if f.startswith(prefix) and mf(f[len(prefix):]):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
matchfuncs.append(matchsubinclude)
|
|
|
|
|
2015-05-17 02:16:18 +03:00
|
|
|
fset, kindpats = _expandsets(kindpats, ctx, listsubrepos)
|
2011-06-19 01:53:44 +04:00
|
|
|
if fset:
|
2015-05-17 02:16:18 +03:00
|
|
|
matchfuncs.append(fset.__contains__)
|
|
|
|
|
|
|
|
regex = ''
|
|
|
|
if kindpats:
|
|
|
|
regex, mf = _buildregexmatch(kindpats, globsuffix)
|
|
|
|
matchfuncs.append(mf)
|
|
|
|
|
|
|
|
if len(matchfuncs) == 1:
|
|
|
|
return regex, matchfuncs[0]
|
|
|
|
else:
|
|
|
|
return regex, lambda f: any(mf(f) for mf in matchfuncs)
|
2011-06-19 01:53:44 +04:00
|
|
|
|
2013-10-03 20:01:21 +04:00
|
|
|
def _buildregexmatch(kindpats, globsuffix):
|
|
|
|
"""Build a match function from a list of kinds and kindpats,
|
|
|
|
return regexp string and a matcher function."""
|
2009-05-24 11:56:14 +04:00
|
|
|
try:
|
2013-10-03 20:01:21 +04:00
|
|
|
regex = '(?:%s)' % '|'.join([_regex(k, p, globsuffix)
|
2015-05-17 01:51:03 +03:00
|
|
|
for (k, p, s) in kindpats])
|
2013-10-03 20:01:21 +04:00
|
|
|
if len(regex) > 20000:
|
2012-05-12 18:00:58 +04:00
|
|
|
raise OverflowError
|
2013-10-03 20:01:21 +04:00
|
|
|
return regex, _rematcher(regex)
|
2009-05-24 11:56:14 +04:00
|
|
|
except OverflowError:
|
|
|
|
# We're using a Python with a tiny regex engine and we
|
|
|
|
# made it explode, so we'll divide the pattern list in two
|
|
|
|
# until it works
|
2013-10-03 20:01:21 +04:00
|
|
|
l = len(kindpats)
|
2009-05-24 11:56:14 +04:00
|
|
|
if l < 2:
|
|
|
|
raise
|
2013-10-03 20:01:21 +04:00
|
|
|
regexa, a = _buildregexmatch(kindpats[:l//2], globsuffix)
|
|
|
|
regexb, b = _buildregexmatch(kindpats[l//2:], globsuffix)
|
2014-04-29 06:02:40 +04:00
|
|
|
return regex, lambda s: a(s) or b(s)
|
2009-05-24 11:56:14 +04:00
|
|
|
except re.error:
|
2015-05-17 01:51:03 +03:00
|
|
|
for k, p, s in kindpats:
|
2009-05-24 11:56:14 +04:00
|
|
|
try:
|
2013-10-03 20:01:21 +04:00
|
|
|
_rematcher('(?:%s)' % _regex(k, p, globsuffix))
|
2009-05-24 11:56:14 +04:00
|
|
|
except re.error:
|
2015-05-17 01:51:03 +03:00
|
|
|
if s:
|
2015-10-08 22:55:45 +03:00
|
|
|
raise error.Abort(_("%s: invalid pattern (%s): %s") %
|
2015-05-17 01:51:03 +03:00
|
|
|
(s, k, p))
|
|
|
|
else:
|
2015-10-08 22:55:45 +03:00
|
|
|
raise error.Abort(_("invalid pattern (%s): %s") % (k, p))
|
|
|
|
raise error.Abort(_("invalid pattern"))
|
2009-05-24 11:56:14 +04:00
|
|
|
|
2013-10-03 20:01:21 +04:00
|
|
|
def _roots(kindpats):
|
2013-10-03 20:01:21 +04:00
|
|
|
'''return roots and exact explicitly listed files from patterns
|
|
|
|
|
2015-05-17 01:51:03 +03:00
|
|
|
>>> _roots([('glob', 'g/*', ''), ('glob', 'g', ''), ('glob', 'g*', '')])
|
2013-10-03 20:01:21 +04:00
|
|
|
['g', 'g', '.']
|
2015-05-17 01:51:03 +03:00
|
|
|
>>> _roots([('relpath', 'r', ''), ('path', 'p/p', ''), ('path', '', '')])
|
2013-10-03 20:01:21 +04:00
|
|
|
['r', 'p/p', '.']
|
2015-05-17 01:51:03 +03:00
|
|
|
>>> _roots([('relglob', 'rg*', ''), ('re', 're/', ''), ('relre', 'rr', '')])
|
2013-10-03 20:01:21 +04:00
|
|
|
['.', '.', '.']
|
|
|
|
'''
|
2009-05-24 11:56:14 +04:00
|
|
|
r = []
|
2015-05-17 01:51:03 +03:00
|
|
|
for kind, pat, source in kindpats:
|
2009-05-24 11:56:14 +04:00
|
|
|
if kind == 'glob': # find the non-glob prefix
|
|
|
|
root = []
|
2013-10-03 20:01:21 +04:00
|
|
|
for p in pat.split('/'):
|
2009-05-24 11:56:14 +04:00
|
|
|
if '[' in p or '{' in p or '*' in p or '?' in p:
|
|
|
|
break
|
|
|
|
root.append(p)
|
|
|
|
r.append('/'.join(root) or '.')
|
2009-05-24 11:56:14 +04:00
|
|
|
elif kind in ('relpath', 'path'):
|
2013-10-03 20:01:21 +04:00
|
|
|
r.append(pat or '.')
|
2013-04-30 03:04:35 +04:00
|
|
|
else: # relglob, re, relre
|
2009-05-24 11:56:14 +04:00
|
|
|
r.append('.')
|
|
|
|
return r
|
|
|
|
|
2013-10-03 20:01:21 +04:00
|
|
|
def _anypats(kindpats):
|
2015-05-17 01:51:03 +03:00
|
|
|
for kind, pat, source in kindpats:
|
2012-02-26 20:10:55 +04:00
|
|
|
if kind in ('glob', 're', 'relglob', 'relre', 'set'):
|
2009-05-24 11:56:14 +04:00
|
|
|
return True
|
2015-05-17 01:46:54 +03:00
|
|
|
|
|
|
|
_commentre = None
|
|
|
|
|
2015-12-27 06:40:38 +03:00
|
|
|
def readpatternfile(filepath, warn, sourceinfo=False):
|
2015-05-17 01:46:54 +03:00
|
|
|
'''parse a pattern file, returning a list of
|
|
|
|
patterns. These patterns should be given to compile()
|
2015-05-17 02:06:22 +03:00
|
|
|
to be validated and converted into a match function.
|
|
|
|
|
|
|
|
trailing white space is dropped.
|
|
|
|
the escape character is backslash.
|
|
|
|
comments start with #.
|
|
|
|
empty lines are skipped.
|
|
|
|
|
|
|
|
lines can be of the following formats:
|
|
|
|
|
|
|
|
syntax: regexp # defaults following lines to non-rooted regexps
|
|
|
|
syntax: glob # defaults following lines to non-rooted globs
|
|
|
|
re:pattern # non-rooted regular expression
|
|
|
|
glob:pattern # non-rooted glob
|
2015-12-27 06:40:38 +03:00
|
|
|
pattern # pattern of the current default type
|
|
|
|
|
|
|
|
if sourceinfo is set, returns a list of tuples:
|
|
|
|
(pattern, lineno, originalline). This is useful to debug ignore patterns.
|
|
|
|
'''
|
2015-05-17 02:06:22 +03:00
|
|
|
|
2015-05-17 01:56:52 +03:00
|
|
|
syntaxes = {'re': 'relre:', 'regexp': 'relre:', 'glob': 'relglob:',
|
2015-05-17 02:25:05 +03:00
|
|
|
'include': 'include', 'subinclude': 'subinclude'}
|
2015-05-17 01:46:54 +03:00
|
|
|
syntax = 'relre:'
|
|
|
|
patterns = []
|
|
|
|
|
|
|
|
fp = open(filepath)
|
2015-12-27 06:40:38 +03:00
|
|
|
for lineno, line in enumerate(fp, start=1):
|
2015-05-17 01:46:54 +03:00
|
|
|
if "#" in line:
|
|
|
|
global _commentre
|
|
|
|
if not _commentre:
|
2015-12-11 08:33:55 +03:00
|
|
|
_commentre = util.re.compile(r'((?:^|[^\\])(?:\\\\)*)#.*')
|
2015-05-17 01:46:54 +03:00
|
|
|
# remove comments prefixed by an even number of escapes
|
2015-12-11 08:33:55 +03:00
|
|
|
m = _commentre.search(line)
|
|
|
|
if m:
|
|
|
|
line = line[:m.end(1)]
|
2015-05-17 01:46:54 +03:00
|
|
|
# fixup properly escaped comments that survived the above
|
|
|
|
line = line.replace("\\#", "#")
|
|
|
|
line = line.rstrip()
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if line.startswith('syntax:'):
|
|
|
|
s = line[7:].strip()
|
|
|
|
try:
|
|
|
|
syntax = syntaxes[s]
|
|
|
|
except KeyError:
|
2015-05-19 02:27:56 +03:00
|
|
|
if warn:
|
|
|
|
warn(_("%s: ignoring invalid syntax '%s'\n") %
|
|
|
|
(filepath, s))
|
2015-05-17 01:46:54 +03:00
|
|
|
continue
|
|
|
|
|
|
|
|
linesyntax = syntax
|
|
|
|
for s, rels in syntaxes.iteritems():
|
|
|
|
if line.startswith(rels):
|
|
|
|
linesyntax = rels
|
|
|
|
line = line[len(rels):]
|
|
|
|
break
|
|
|
|
elif line.startswith(s+':'):
|
|
|
|
linesyntax = rels
|
|
|
|
line = line[len(s) + 1:]
|
|
|
|
break
|
2015-12-27 06:40:38 +03:00
|
|
|
if sourceinfo:
|
|
|
|
patterns.append((linesyntax + line, lineno, line))
|
|
|
|
else:
|
|
|
|
patterns.append(linesyntax + line)
|
2015-05-17 01:46:54 +03:00
|
|
|
fp.close()
|
|
|
|
return patterns
|