comparison mercurial/match.py @ 42081:bf777c1e78dd

match: add a docstring with doctest examples to patternmatcher Doctest examples aim at illustrating how __call__() and exact() are different, depending on the pattern kind.
author Denis Laxalde <denis@laxalde.org>
date Sat, 06 Apr 2019 15:21:55 +0200
parents f3db5c805a67
children 413a75da98ce
comparison
equal deleted inserted replaced
42080:f3db5c805a67 42081:bf777c1e78dd
430 s = (stringutil.buildrepr(self._predrepr) 430 s = (stringutil.buildrepr(self._predrepr)
431 or pycompat.byterepr(self.matchfn)) 431 or pycompat.byterepr(self.matchfn))
432 return '<predicatenmatcher pred=%s>' % s 432 return '<predicatenmatcher pred=%s>' % s
433 433
434 class patternmatcher(basematcher): 434 class patternmatcher(basematcher):
435 """Matches a set of (kind, pat, source) against a 'root' directory.
436
437 >>> kindpats = [
438 ... ('re', '.*\.c$', ''),
439 ... ('path', 'foo/a', ''),
440 ... ('relpath', 'b', ''),
441 ... ('glob', '*.h', ''),
442 ... ]
443 >>> m = patternmatcher('foo', kindpats)
444 >>> bool(m('main.c')) # matches re:.*\.c$
445 True
446 >>> bool(m('b.txt'))
447 False
448 >>> bool(m('foo/a')) # matches path:foo/a
449 True
450 >>> bool(m('a')) # does not match path:b, since 'root' is 'foo'
451 False
452 >>> bool(m('b')) # matches relpath:b, since 'root' is 'foo'
453 True
454 >>> bool(m('lib.h')) # matches glob:*.h
455 True
456
457 >>> m.files()
458 ['.', 'foo/a', 'b', '.']
459 >>> m.exact('foo/a')
460 True
461 >>> m.exact('b')
462 True
463 >>> m.exact('lib.h') # exact matches are for (rel)path kinds
464 False
465 """
435 466
436 def __init__(self, root, kindpats, badfn=None): 467 def __init__(self, root, kindpats, badfn=None):
437 super(patternmatcher, self).__init__(badfn) 468 super(patternmatcher, self).__init__(badfn)
438 469
439 self._files = _explicitfiles(kindpats) 470 self._files = _explicitfiles(kindpats)