[PATCH 1 of 5 v3] revset: add pattern matching to 'tag' revset expression
Matt Mackall
mpm at selenic.com
Wed May 30 15:40:13 CDT 2012
On Mon, 2012-05-28 at 22:43 +0100, Simon King wrote:
> # HG changeset patch
> # User Simon King <simon at simonking.org.uk>
> # Date 1338237440 -3600
> # Node ID cffe3364b6c7540aecb04ae2ecfd72fde251c2cb
> # Parent 2ac08d8b21aa7b6e0a062afed5a3f357ccef67f9
> revset: add pattern matching to 'tag' revset expression
>
> If the string provided to the 'tag' predicate starts with 're:', the rest
> of the string will be treated as a regular expression and matched against
> all tags in the repository.
>
> There is a slight backwards-compatibility problem for people who actually
> have tags that start with 're:'. As a workaround, these tags can be matched
> using a 'literal:' prefix.
>
> If no tags match the pattern, an error is raised. This matches the behaviour
> of the previous exact-match code.
Also:
> + if ':' in pattern:
> + prefix, pattern = pattern.split(':', 1)
> + else:
> + prefix = 'literal'
> + if prefix == 're':
> + try:
> + regex = re.compile(pattern)
> + except re.error, e:
> + raise error.ParseError(_('invalid regular expression: %s')
> + % e)
> + return 're', pattern, lambda s: bool(regex.search(s))
> + elif prefix == 'literal':
> + return prefix, pattern, lambda s: s == pattern
> + else:
> + # unknown prefix, treat as literal
> + pattern = '%s:%s' % (prefix, pattern)
> + return 'literal', pattern, lambda s: s == pattern
It'd probably be better to do:
if pattern.startswith('re:'):
regex = ...
return 're', pattern[3:], regex.search
elif pattern.startswith('literal:'):
pattern = pattern[8:]
return 'literal', pattern, pattern.__eq__
Casting to bool in a lambda is unnecessary.
--
Mathematics is the supreme nostalgia of our time.
More information about the Mercurial-devel
mailing list