Mercurial > public > mercurial-scm > hg
comparison mercurial/util.py @ 4306:6cecaec07cc9
Revert changeset ef1f1a4b2efb; add another test for glob: patterns
With that changeset, it's impossible to use a glob: pattern to match
e.g. all files ending in .py - glob:**.py would also match all files
in a directory called dir.py.
author | Alexis S. L. Carvalho <alexis@cecm.usp.br> |
---|---|
date | Wed, 04 Apr 2007 04:22:05 -0300 |
parents | ef1f1a4b2efb |
children | 702f48570eb3 |
comparison
equal
deleted
inserted
replaced
4255:ef1f1a4b2efb | 4306:6cecaec07cc9 |
---|---|
425 def contains_glob(name): | 425 def contains_glob(name): |
426 for c in name: | 426 for c in name: |
427 if c in _globchars: return True | 427 if c in _globchars: return True |
428 return False | 428 return False |
429 | 429 |
430 def regex(kind, name): | 430 def regex(kind, name, tail): |
431 '''convert a pattern into a regular expression''' | 431 '''convert a pattern into a regular expression''' |
432 if not name: | 432 if not name: |
433 return '' | 433 return '' |
434 if kind == 're': | 434 if kind == 're': |
435 return name | 435 return name |
441 return re.escape(name) + '(?:/|$)' | 441 return re.escape(name) + '(?:/|$)' |
442 elif kind == 'relre': | 442 elif kind == 'relre': |
443 if name.startswith('^'): | 443 if name.startswith('^'): |
444 return name | 444 return name |
445 return '.*' + name | 445 return '.*' + name |
446 return globre(name, '', '(?:/|$)') | 446 return globre(name, '', tail) |
447 | 447 |
448 def matchfn(pats): | 448 def matchfn(pats, tail): |
449 """build a matching function from a set of patterns""" | 449 """build a matching function from a set of patterns""" |
450 if not pats: | 450 if not pats: |
451 return | 451 return |
452 matches = [] | 452 matches = [] |
453 for k, p in pats: | 453 for k, p in pats: |
454 try: | 454 try: |
455 pat = '(?:%s)' % regex(k, p) | 455 pat = '(?:%s)' % regex(k, p, tail) |
456 matches.append(re.compile(pat).match) | 456 matches.append(re.compile(pat).match) |
457 except re.error: | 457 except re.error: |
458 if src: raise Abort("%s: invalid pattern (%s): %s" % (src, k, p)) | 458 if src: raise Abort("%s: invalid pattern (%s): %s" % (src, k, p)) |
459 else: raise Abort("invalid pattern (%s): %s" % (k, p)) | 459 else: raise Abort("invalid pattern (%s): %s" % (k, p)) |
460 | 460 |
498 roots.append('.') | 498 roots.append('.') |
499 return roots, pats, anypats | 499 return roots, pats, anypats |
500 | 500 |
501 roots, pats, anypats = normalizepats(names, dflt_pat) | 501 roots, pats, anypats = normalizepats(names, dflt_pat) |
502 | 502 |
503 patmatch = matchfn(pats) or always | 503 patmatch = matchfn(pats, '$') or always |
504 incmatch = always | 504 incmatch = always |
505 if inc: | 505 if inc: |
506 dummy, inckinds, dummy = normalizepats(inc, 'glob') | 506 dummy, inckinds, dummy = normalizepats(inc, 'glob') |
507 incmatch = matchfn(inckinds) | 507 incmatch = matchfn(inckinds, '(?:/|$)') |
508 excmatch = lambda fn: False | 508 excmatch = lambda fn: False |
509 if exc: | 509 if exc: |
510 dummy, exckinds, dummy = normalizepats(exc, 'glob') | 510 dummy, exckinds, dummy = normalizepats(exc, 'glob') |
511 excmatch = matchfn(exckinds) | 511 excmatch = matchfn(exckinds, '(?:/|$)') |
512 | 512 |
513 if not names and inc and not exc: | 513 if not names and inc and not exc: |
514 # common case: hgignore patterns | 514 # common case: hgignore patterns |
515 match = incmatch | 515 match = incmatch |
516 else: | 516 else: |