equal
deleted
inserted
replaced
3 # Copyright 2008, 2009 Matt Mackall <mpm@selenic.com> and others |
3 # Copyright 2008, 2009 Matt Mackall <mpm@selenic.com> and others |
4 # |
4 # |
5 # This software may be used and distributed according to the terms of the |
5 # This software may be used and distributed according to the terms of the |
6 # GNU General Public License version 2 or any later version. |
6 # GNU General Public License version 2 or any later version. |
7 |
7 |
8 from __future__ import absolute_import |
8 from __future__ import absolute_import, print_function |
9 |
9 |
10 import copy |
10 import copy |
11 import os |
11 import os |
12 import re |
12 import re |
13 |
13 |
578 class subdirmatcher(basematcher): |
578 class subdirmatcher(basematcher): |
579 """Adapt a matcher to work on a subdirectory only. |
579 """Adapt a matcher to work on a subdirectory only. |
580 |
580 |
581 The paths are remapped to remove/insert the path as needed: |
581 The paths are remapped to remove/insert the path as needed: |
582 |
582 |
|
583 >>> from . import pycompat |
583 >>> m1 = match(b'root', b'', [b'a.txt', b'sub/b.txt']) |
584 >>> m1 = match(b'root', b'', [b'a.txt', b'sub/b.txt']) |
584 >>> m2 = subdirmatcher(b'sub', m1) |
585 >>> m2 = subdirmatcher(b'sub', m1) |
585 >>> bool(m2(b'a.txt')) |
586 >>> bool(m2(b'a.txt')) |
586 False |
587 False |
587 >>> bool(m2(b'b.txt')) |
588 >>> bool(m2(b'b.txt')) |
595 >>> m2.exact(b'b.txt') |
596 >>> m2.exact(b'b.txt') |
596 True |
597 True |
597 >>> util.pconvert(m2.rel(b'b.txt')) |
598 >>> util.pconvert(m2.rel(b'b.txt')) |
598 'sub/b.txt' |
599 'sub/b.txt' |
599 >>> def bad(f, msg): |
600 >>> def bad(f, msg): |
600 ... print b"%s: %s" % (f, msg) |
601 ... print(pycompat.sysstr(b"%s: %s" % (f, msg))) |
601 >>> m1.bad = bad |
602 >>> m1.bad = bad |
602 >>> m2.bad(b'x.txt', b'No such file') |
603 >>> m2.bad(b'x.txt', b'No such file') |
603 sub/x.txt: No such file |
604 sub/x.txt: No such file |
604 >>> m2.abs(b'c.txt') |
605 >>> m2.abs(b'c.txt') |
605 'sub/c.txt' |
606 'sub/c.txt' |
701 return default, pattern |
702 return default, pattern |
702 |
703 |
703 def _globre(pat): |
704 def _globre(pat): |
704 r'''Convert an extended glob string to a regexp string. |
705 r'''Convert an extended glob string to a regexp string. |
705 |
706 |
706 >>> print _globre(br'?') |
707 >>> from . import pycompat |
|
708 >>> def bprint(s): |
|
709 ... print(pycompat.sysstr(s)) |
|
710 >>> bprint(_globre(br'?')) |
707 . |
711 . |
708 >>> print _globre(br'*') |
712 >>> bprint(_globre(br'*')) |
709 [^/]* |
713 [^/]* |
710 >>> print _globre(br'**') |
714 >>> bprint(_globre(br'**')) |
711 .* |
715 .* |
712 >>> print _globre(br'**/a') |
716 >>> bprint(_globre(br'**/a')) |
713 (?:.*/)?a |
717 (?:.*/)?a |
714 >>> print _globre(br'a/**/b') |
718 >>> bprint(_globre(br'a/**/b')) |
715 a\/(?:.*/)?b |
719 a\/(?:.*/)?b |
716 >>> print _globre(br'[a*?!^][^b][!c]') |
720 >>> bprint(_globre(br'[a*?!^][^b][!c]')) |
717 [a*?!^][\^b][^c] |
721 [a*?!^][\^b][^c] |
718 >>> print _globre(br'{a,b}') |
722 >>> bprint(_globre(br'{a,b}')) |
719 (?:a|b) |
723 (?:a|b) |
720 >>> print _globre(br'.\*\?') |
724 >>> bprint(_globre(br'.\*\?')) |
721 \.\*\? |
725 \.\*\? |
722 ''' |
726 ''' |
723 i, n = 0, len(pat) |
727 i, n = 0, len(pat) |
724 res = '' |
728 res = '' |
725 group = 0 |
729 group = 0 |