[PATCH 1 of 5 v2] revset: add pattern matching to 'tag' revset expression

Simon King simon at simonking.org.uk
Mon May 28 04:56:32 CDT 2012


On Sun, May 27, 2012 at 1:06 AM, Simon King <simon at simonking.org.uk> wrote:
> # HG changeset patch
> # User Simon King <simon at simonking.org.uk>
> # Date 1338073777 -3600
> # Node ID 379f9cd195c0c31bae73e7d317db4bb22d0c9cc3
> # 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.
>
> diff --git a/mercurial/revset.py b/mercurial/revset.py
> --- a/mercurial/revset.py
> +++ b/mercurial/revset.py
> @@ -1119,20 +1119,53 @@
>     l.sort()
>     return [e[-1] for e in l]
>
> +def _stringmatcher(pattern):
> +    # accept a string, possibly starting with 're:' or 'literal:' prefix.
> +    # return the matcher name, pattern, and matcher function.
> +    # missing or unknown prefixes are treated as literal matches
> +    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: regex.search(s)
> +    elif prefix == 'literal':
> +        return prefix, pattern, lambda s: s == pattern
> +    else:
> +        # unknown prefix, treat as literal
> +        return 'literal', pattern, lambda s: s == pattern
> +
> +

Oops, just noticed a bug here: unknown prefixes will be stripped from
the pattern. I'll resend with a fix and a doctest later, as long as
someone can confirm that the general approach is acceptable.

Thanks,

Simon


More information about the Mercurial-devel mailing list