mercurial/match.py
changeset 43964 8f67735344ae
parent 43950 5685ce2ea3bf
child 43965 8a81fa44f7bb
equal deleted inserted replaced
43963:bbcf78c4ff90 43964:8f67735344ae
   180     'include:<path>' - a file of patterns to read and include
   180     'include:<path>' - a file of patterns to read and include
   181     'subinclude:<path>' - a file of patterns to match against files under
   181     'subinclude:<path>' - a file of patterns to match against files under
   182                           the same directory
   182                           the same directory
   183     '<something>' - a pattern of the specified default type
   183     '<something>' - a pattern of the specified default type
   184 
   184 
       
   185     >>> def _match(root, *args, **kwargs):
       
   186     ...     return match(util.localpath(root), *args, **kwargs)
       
   187 
   185     Usually a patternmatcher is returned:
   188     Usually a patternmatcher is returned:
   186     >>> match(b'/foo', b'.', [b're:.*\.c$', b'path:foo/a', b'*.py'])
   189     >>> _match(b'/foo', b'.', [b're:.*\.c$', b'path:foo/a', b'*.py'])
   187     <patternmatcher patterns='.*\\.c$|foo/a(?:/|$)|[^/]*\\.py$'>
   190     <patternmatcher patterns='.*\\.c$|foo/a(?:/|$)|[^/]*\\.py$'>
   188 
   191 
   189     Combining 'patterns' with 'include' (resp. 'exclude') gives an
   192     Combining 'patterns' with 'include' (resp. 'exclude') gives an
   190     intersectionmatcher (resp. a differencematcher):
   193     intersectionmatcher (resp. a differencematcher):
   191     >>> type(match(b'/foo', b'.', [b're:.*\.c$'], include=[b'path:lib']))
   194     >>> type(_match(b'/foo', b'.', [b're:.*\.c$'], include=[b'path:lib']))
   192     <class 'mercurial.match.intersectionmatcher'>
   195     <class 'mercurial.match.intersectionmatcher'>
   193     >>> type(match(b'/foo', b'.', [b're:.*\.c$'], exclude=[b'path:build']))
   196     >>> type(_match(b'/foo', b'.', [b're:.*\.c$'], exclude=[b'path:build']))
   194     <class 'mercurial.match.differencematcher'>
   197     <class 'mercurial.match.differencematcher'>
   195 
   198 
   196     Notice that, if 'patterns' is empty, an alwaysmatcher is returned:
   199     Notice that, if 'patterns' is empty, an alwaysmatcher is returned:
   197     >>> match(b'/foo', b'.', [])
   200     >>> _match(b'/foo', b'.', [])
   198     <alwaysmatcher>
   201     <alwaysmatcher>
   199 
   202 
   200     The 'default' argument determines which kind of pattern is assumed if a
   203     The 'default' argument determines which kind of pattern is assumed if a
   201     pattern has no prefix:
   204     pattern has no prefix:
   202     >>> match(b'/foo', b'.', [b'.*\.c$'], default=b're')
   205     >>> _match(b'/foo', b'.', [b'.*\.c$'], default=b're')
   203     <patternmatcher patterns='.*\\.c$'>
   206     <patternmatcher patterns='.*\\.c$'>
   204     >>> match(b'/foo', b'.', [b'main.py'], default=b'relpath')
   207     >>> _match(b'/foo', b'.', [b'main.py'], default=b'relpath')
   205     <patternmatcher patterns='main\\.py(?:/|$)'>
   208     <patternmatcher patterns='main\\.py(?:/|$)'>
   206     >>> match(b'/foo', b'.', [b'main.py'], default=b're')
   209     >>> _match(b'/foo', b'.', [b'main.py'], default=b're')
   207     <patternmatcher patterns='main.py'>
   210     <patternmatcher patterns='main.py'>
   208 
   211 
   209     The primary use of matchers is to check whether a value (usually a file
   212     The primary use of matchers is to check whether a value (usually a file
   210     name) matches againset one of the patterns given at initialization. There
   213     name) matches againset one of the patterns given at initialization. There
   211     are two ways of doing this check.
   214     are two ways of doing this check.
   212 
   215 
   213     >>> m = match(b'/foo', b'', [b're:.*\.c$', b'relpath:a'])
   216     >>> m = _match(b'/foo', b'', [b're:.*\.c$', b'relpath:a'])
   214 
   217 
   215     1. Calling the matcher with a file name returns True if any pattern
   218     1. Calling the matcher with a file name returns True if any pattern
   216     matches that file name:
   219     matches that file name:
   217     >>> m(b'a')
   220     >>> m(b'a')
   218     True
   221     True
   940     """Adapt a matcher to work on a subdirectory only.
   943     """Adapt a matcher to work on a subdirectory only.
   941 
   944 
   942     The paths are remapped to remove/insert the path as needed:
   945     The paths are remapped to remove/insert the path as needed:
   943 
   946 
   944     >>> from . import pycompat
   947     >>> from . import pycompat
   945     >>> m1 = match(b'/root', b'', [b'a.txt', b'sub/b.txt'])
   948     >>> m1 = match(util.localpath(b'/root'), b'', [b'a.txt', b'sub/b.txt'])
   946     >>> m2 = subdirmatcher(b'sub', m1)
   949     >>> m2 = subdirmatcher(b'sub', m1)
   947     >>> m2(b'a.txt')
   950     >>> m2(b'a.txt')
   948     False
   951     False
   949     >>> m2(b'b.txt')
   952     >>> m2(b'b.txt')
   950     True
   953     True