comparison mercurial/match.py @ 32460:f9445b528687

match: make subdirmatcher extend basematcher This makes the subdirmatcher not depend on the main matcher, giving us more freedom to modify that (specifically, it will lose it _always field in a while).
author Martin von Zweigbergk <martinvonz@google.com>
date Wed, 17 May 2017 23:02:42 -0700
parents 9f781f43f2ce
children 2b5953a49f14
comparison
equal deleted inserted replaced
32459:9f781f43f2ce 32460:f9445b528687
407 def __repr__(self): 407 def __repr__(self):
408 return ('<matcher files=%r, patterns=%r, includes=%r, excludes=%r>' % 408 return ('<matcher files=%r, patterns=%r, includes=%r, excludes=%r>' %
409 (self._files, self.patternspat, self.includepat, 409 (self._files, self.patternspat, self.includepat,
410 self.excludepat)) 410 self.excludepat))
411 411
412 class subdirmatcher(matcher): 412 class subdirmatcher(basematcher):
413 """Adapt a matcher to work on a subdirectory only. 413 """Adapt a matcher to work on a subdirectory only.
414 414
415 The paths are remapped to remove/insert the path as needed: 415 The paths are remapped to remove/insert the path as needed:
416 416
417 >>> m1 = match('root', '', ['a.txt', 'sub/b.txt']) 417 >>> m1 = match('root', '', ['a.txt', 'sub/b.txt'])
438 >>> m2.abs('c.txt') 438 >>> m2.abs('c.txt')
439 'sub/c.txt' 439 'sub/c.txt'
440 """ 440 """
441 441
442 def __init__(self, path, matcher): 442 def __init__(self, path, matcher):
443 self._root = matcher._root 443 super(subdirmatcher, self).__init__(matcher._root, matcher._cwd)
444 self._cwd = matcher._cwd
445 self._path = path 444 self._path = path
446 self._matcher = matcher 445 self._matcher = matcher
447 self._always = matcher._always 446 self._always = matcher.always()
448 447
449 self._files = [f[len(path) + 1:] for f in matcher._files 448 self._files = [f[len(path) + 1:] for f in matcher._files
450 if f.startswith(path + "/")] 449 if f.startswith(path + "/")]
451 450
452 # If the parent repo had a path to this subrepo and the matcher is 451 # If the parent repo had a path to this subrepo and the matcher is
453 # a prefix matcher, this submatcher always matches. 452 # a prefix matcher, this submatcher always matches.
454 if matcher.prefix(): 453 if matcher.prefix():
455 self._always = any(f == path for f in matcher._files) 454 self._always = any(f == path for f in matcher._files)
456 455
457 self._anypats = matcher._anypats
458 # Some information is lost in the superclass's constructor, so we 456 # Some information is lost in the superclass's constructor, so we
459 # can not accurately create the matching function for the subdirectory 457 # can not accurately create the matching function for the subdirectory
460 # from the inputs. Instead, we override matchfn() and visitdir() to 458 # from the inputs. Instead, we override matchfn() and visitdir() to
461 # call the original matcher with the subdirectory path prepended. 459 # call the original matcher with the subdirectory path prepended.
462 self.matchfn = lambda fn: matcher.matchfn(self._path + "/" + fn) 460 self.matchfn = lambda fn: matcher.matchfn(self._path + "/" + fn)
477 if dir == '.': 475 if dir == '.':
478 dir = self._path 476 dir = self._path
479 else: 477 else:
480 dir = self._path + "/" + dir 478 dir = self._path + "/" + dir
481 return self._matcher.visitdir(dir) 479 return self._matcher.visitdir(dir)
480
481 def always(self):
482 return self._always
483
484 def anypats(self):
485 return self._matcher.anypats()
482 486
483 def patkind(pattern, default=None): 487 def patkind(pattern, default=None):
484 '''If pattern is 'kind:pat' with a known kind, return kind.''' 488 '''If pattern is 'kind:pat' with a known kind, return kind.'''
485 return _patsplit(pattern, default)[0] 489 return _patsplit(pattern, default)[0]
486 490