comparison mercurial/match.py @ 42098:5753e5949b51

py3: add b'' prefixes to new doctests in match.py # skip-blame as just b'' prefixes Differential Revision: https://phab.mercurial-scm.org/D6221
author Pulkit Goyal <pulkit@yandex-team.ru>
date Wed, 10 Apr 2019 03:10:53 +0530
parents 2e2699af5649
children bccb322f1496
comparison
equal deleted inserted replaced
42097:f7fdb24d4178 42098:5753e5949b51
148 'subinclude:<path>' - a file of patterns to match against files under 148 'subinclude:<path>' - a file of patterns to match against files under
149 the same directory 149 the same directory
150 '<something>' - a pattern of the specified default type 150 '<something>' - a pattern of the specified default type
151 151
152 Usually a patternmatcher is returned: 152 Usually a patternmatcher is returned:
153 >>> match('foo', '.', ['re:.*\.c$', 'path:foo/a', '*.py']) 153 >>> match(b'foo', b'.', [b're:.*\.c$', b'path:foo/a', b'*.py'])
154 <patternmatcher patterns='.*\\.c$|foo/a(?:/|$)|[^/]*\\.py$'> 154 <patternmatcher patterns='.*\\.c$|foo/a(?:/|$)|[^/]*\\.py$'>
155 155
156 Combining 'patterns' with 'include' (resp. 'exclude') gives an 156 Combining 'patterns' with 'include' (resp. 'exclude') gives an
157 intersectionmatcher (resp. a differencematcher): 157 intersectionmatcher (resp. a differencematcher):
158 >>> type(match('foo', '.', ['re:.*\.c$'], include=['path:lib'])) 158 >>> type(match(b'foo', b'.', [b're:.*\.c$'], include=[b'path:lib']))
159 <class 'mercurial.match.intersectionmatcher'> 159 <class 'mercurial.match.intersectionmatcher'>
160 >>> type(match('foo', '.', ['re:.*\.c$'], exclude=['path:build'])) 160 >>> type(match(b'foo', b'.', [b're:.*\.c$'], exclude=[b'path:build']))
161 <class 'mercurial.match.differencematcher'> 161 <class 'mercurial.match.differencematcher'>
162 162
163 Notice that, if 'patterns' is empty, an alwaysmatcher is returned: 163 Notice that, if 'patterns' is empty, an alwaysmatcher is returned:
164 >>> match('foo', '.', []) 164 >>> match(b'foo', b'.', [])
165 <alwaysmatcher> 165 <alwaysmatcher>
166 166
167 The 'default' argument determines which kind of pattern is assumed if a 167 The 'default' argument determines which kind of pattern is assumed if a
168 pattern has no prefix: 168 pattern has no prefix:
169 >>> match('foo', '.', ['.*\.c$'], default='re') 169 >>> match(b'foo', b'.', [b'.*\.c$'], default=b're')
170 <patternmatcher patterns='.*\\.c$'> 170 <patternmatcher patterns='.*\\.c$'>
171 >>> match('foo', '.', ['main.py'], default='relpath') 171 >>> match(b'foo', b'.', [b'main.py'], default=b'relpath')
172 <patternmatcher patterns='main\\.py(?:/|$)'> 172 <patternmatcher patterns='main\\.py(?:/|$)'>
173 >>> match('foo', '.', ['main.py'], default='re') 173 >>> match(b'foo', b'.', [b'main.py'], default=b're')
174 <patternmatcher patterns='main.py'> 174 <patternmatcher patterns='main.py'>
175 175
176 The primary use of matchers is to check whether a value (usually a file 176 The primary use of matchers is to check whether a value (usually a file
177 name) matches againset one of the patterns given at initialization. There 177 name) matches againset one of the patterns given at initialization. There
178 are two ways of doing this check. 178 are two ways of doing this check.
179 179
180 >>> m = match('foo', '', ['re:.*\.c$', 'relpath:a']) 180 >>> m = match(b'foo', b'', [b're:.*\.c$', b'relpath:a'])
181 181
182 1. Calling the matcher with a file name returns True if any pattern 182 1. Calling the matcher with a file name returns True if any pattern
183 matches that file name: 183 matches that file name:
184 >>> m('a') 184 >>> m(b'a')
185 True 185 True
186 >>> m('main.c') 186 >>> m(b'main.c')
187 True 187 True
188 >>> m('test.py') 188 >>> m(b'test.py')
189 False 189 False
190 190
191 2. Using the exact() method only returns True if the file name matches one 191 2. Using the exact() method only returns True if the file name matches one
192 of the exact patterns (i.e. not re: or glob: patterns): 192 of the exact patterns (i.e. not re: or glob: patterns):
193 >>> m.exact('a') 193 >>> m.exact(b'a')
194 True 194 True
195 >>> m.exact('main.c') 195 >>> m.exact(b'main.c')
196 False 196 False
197 """ 197 """
198 normalize = _donormalize 198 normalize = _donormalize
199 if icasefs: 199 if icasefs:
200 dirstate = ctx.repo().dirstate 200 dirstate = ctx.repo().dirstate
482 482
483 class patternmatcher(basematcher): 483 class patternmatcher(basematcher):
484 """Matches a set of (kind, pat, source) against a 'root' directory. 484 """Matches a set of (kind, pat, source) against a 'root' directory.
485 485
486 >>> kindpats = [ 486 >>> kindpats = [
487 ... ('re', '.*\.c$', ''), 487 ... (b're', b'.*\.c$', b''),
488 ... ('path', 'foo/a', ''), 488 ... (b'path', b'foo/a', b''),
489 ... ('relpath', 'b', ''), 489 ... (b'relpath', b'b', b''),
490 ... ('glob', '*.h', ''), 490 ... (b'glob', b'*.h', b''),
491 ... ] 491 ... ]
492 >>> m = patternmatcher('foo', kindpats) 492 >>> m = patternmatcher(b'foo', kindpats)
493 >>> m('main.c') # matches re:.*\.c$ 493 >>> m(b'main.c') # matches re:.*\.c$
494 True 494 True
495 >>> m('b.txt') 495 >>> m(b'b.txt')
496 False 496 False
497 >>> m('foo/a') # matches path:foo/a 497 >>> m(b'foo/a') # matches path:foo/a
498 True 498 True
499 >>> m('a') # does not match path:b, since 'root' is 'foo' 499 >>> m(b'a') # does not match path:b, since 'root' is 'foo'
500 False 500 False
501 >>> m('b') # matches relpath:b, since 'root' is 'foo' 501 >>> m(b'b') # matches relpath:b, since 'root' is 'foo'
502 True 502 True
503 >>> m('lib.h') # matches glob:*.h 503 >>> m(b'lib.h') # matches glob:*.h
504 True 504 True
505 505
506 >>> m.files() 506 >>> m.files()
507 ['.', 'foo/a', 'b', '.'] 507 ['.', 'foo/a', 'b', '.']
508 >>> m.exact('foo/a') 508 >>> m.exact(b'foo/a')
509 True 509 True
510 >>> m.exact('b') 510 >>> m.exact(b'b')
511 True 511 True
512 >>> m.exact('lib.h') # exact matches are for (rel)path kinds 512 >>> m.exact(b'lib.h') # exact matches are for (rel)path kinds
513 False 513 False
514 """ 514 """
515 515
516 def __init__(self, root, kindpats, badfn=None): 516 def __init__(self, root, kindpats, badfn=None):
517 super(patternmatcher, self).__init__(badfn) 517 super(patternmatcher, self).__init__(badfn)
649 649
650 class exactmatcher(basematcher): 650 class exactmatcher(basematcher):
651 r'''Matches the input files exactly. They are interpreted as paths, not 651 r'''Matches the input files exactly. They are interpreted as paths, not
652 patterns (so no kind-prefixes). 652 patterns (so no kind-prefixes).
653 653
654 >>> m = exactmatcher(['a.txt', 're:.*\.c$']) 654 >>> m = exactmatcher([b'a.txt', b're:.*\.c$'])
655 >>> m('a.txt') 655 >>> m(b'a.txt')
656 True 656 True
657 >>> m('b.txt') 657 >>> m(b'b.txt')
658 False 658 False
659 659
660 Input files that would be matched are exactly those returned by .files() 660 Input files that would be matched are exactly those returned by .files()
661 >>> m.files() 661 >>> m.files()
662 ['a.txt', 're:.*\\.c$'] 662 ['a.txt', 're:.*\\.c$']
663 663
664 So pattern 're:.*\.c$' is not considered as a regex, but as a file name 664 So pattern 're:.*\.c$' is not considered as a regex, but as a file name
665 >>> m('main.c') 665 >>> m(b'main.c')
666 False 666 False
667 >>> m('re:.*\.c$') 667 >>> m(b're:.*\.c$')
668 True 668 True
669 ''' 669 '''
670 670
671 def __init__(self, files, badfn=None): 671 def __init__(self, files, badfn=None):
672 super(exactmatcher, self).__init__(badfn) 672 super(exactmatcher, self).__init__(badfn)
1073 return ('<unionmatcher matchers=%r>' % self._matchers) 1073 return ('<unionmatcher matchers=%r>' % self._matchers)
1074 1074
1075 def patkind(pattern, default=None): 1075 def patkind(pattern, default=None):
1076 '''If pattern is 'kind:pat' with a known kind, return kind. 1076 '''If pattern is 'kind:pat' with a known kind, return kind.
1077 1077
1078 >>> patkind('re:.*\.c$') 1078 >>> patkind(b're:.*\.c$')
1079 're' 1079 're'
1080 >>> patkind('glob:*.c') 1080 >>> patkind(b'glob:*.c')
1081 'glob' 1081 'glob'
1082 >>> patkind('relpath:test.py') 1082 >>> patkind(b'relpath:test.py')
1083 'relpath' 1083 'relpath'
1084 >>> patkind('main.py') 1084 >>> patkind(b'main.py')
1085 >>> patkind('main.py', default='re') 1085 >>> patkind(b'main.py', default=b're')
1086 're' 1086 're'
1087 ''' 1087 '''
1088 return _patsplit(pattern, default)[0] 1088 return _patsplit(pattern, default)[0]
1089 1089
1090 def _patsplit(pattern, default): 1090 def _patsplit(pattern, default):