Mercurial > public > mercurial-scm > hg
comparison mercurial/revset.py @ 16819:5260a9e93113
revset: add helper function for matching strings to patterns
author | Simon King <simon@simonking.org.uk> |
---|---|
date | Wed, 30 May 2012 23:13:33 +0100 |
parents | 107a3270a24a |
children | 20f55613fb2a |
comparison
equal
deleted
inserted
replaced
16818:d0fc1e689227 | 16819:5260a9e93113 |
---|---|
1117 e.append(r) | 1117 e.append(r) |
1118 l.append(e) | 1118 l.append(e) |
1119 l.sort() | 1119 l.sort() |
1120 return [e[-1] for e in l] | 1120 return [e[-1] for e in l] |
1121 | 1121 |
1122 def _stringmatcher(pattern): | |
1123 """ | |
1124 accepts a string, possibly starting with 're:' or 'literal:' prefix. | |
1125 returns the matcher name, pattern, and matcher function. | |
1126 missing or unknown prefixes are treated as literal matches. | |
1127 | |
1128 helper for tests: | |
1129 >>> def test(pattern, *tests): | |
1130 ... kind, pattern, matcher = _stringmatcher(pattern) | |
1131 ... return (kind, pattern, [bool(matcher(t)) for t in tests]) | |
1132 | |
1133 exact matching (no prefix): | |
1134 >>> test('abcdefg', 'abc', 'def', 'abcdefg') | |
1135 ('literal', 'abcdefg', [False, False, True]) | |
1136 | |
1137 regex matching ('re:' prefix) | |
1138 >>> test('re:a.+b', 'nomatch', 'fooadef', 'fooadefbar') | |
1139 ('re', 'a.+b', [False, False, True]) | |
1140 | |
1141 force exact matches ('literal:' prefix) | |
1142 >>> test('literal:re:foobar', 'foobar', 're:foobar') | |
1143 ('literal', 're:foobar', [False, True]) | |
1144 | |
1145 unknown prefixes are ignored and treated as literals | |
1146 >>> test('foo:bar', 'foo', 'bar', 'foo:bar') | |
1147 ('literal', 'foo:bar', [False, False, True]) | |
1148 """ | |
1149 if pattern.startswith('re:'): | |
1150 pattern = pattern[3:] | |
1151 try: | |
1152 regex = re.compile(pattern) | |
1153 except re.error, e: | |
1154 raise error.ParseError(_('invalid regular expression: %s') | |
1155 % e) | |
1156 return 're', pattern, regex.search | |
1157 elif pattern.startswith('literal:'): | |
1158 pattern = pattern[8:] | |
1159 return 'literal', pattern, pattern.__eq__ | |
1160 | |
1161 | |
1122 def tag(repo, subset, x): | 1162 def tag(repo, subset, x): |
1123 """``tag([name])`` | 1163 """``tag([name])`` |
1124 The specified tag by name, or all tagged revisions if no name is given. | 1164 The specified tag by name, or all tagged revisions if no name is given. |
1125 """ | 1165 """ |
1126 # i18n: "tag" is a keyword | 1166 # i18n: "tag" is a keyword |