D5813: revset: add expect to check the size of a set

Yuya Nishihara yuya at tcha.org
Sat Feb 9 20:26:15 EST 2019


> + at predicate('expectsize(set[, size])', safe=True, takeorder=True)
> +def expectrevsetsize(repo, subset, x, order):
> +    """Abort if the revset doesn't expect given size"""
> +    args = getargsdict(x, 'expect', 'set size')
> +    size = args.get('size')
> +    if size is not None:
> +        minsize, maxsize = getintrange(size,
> +                                       _('expectsize requires a size range'
> +                                       ' or a positive integer'),
> +                                       _('size range bounds must be integers'))

Maybe needs to specify the default min/max values to e.g. possible min/max
values or `None`.

> +        if minsize != maxsize:
> +            size = (minsize, maxsize)
> +        else:
> +            size = minsize
> +    if size is None or 'set' not in args:
> +        raise error.ParseError(_('invalid set of arguments'))
> +    rev = getset(repo, fullreposet(repo), args['set'], order=order)
> +    if isinstance(size, tuple):
> +        if len(rev) < minsize or len(rev) > maxsize:
> +            raise error.RepoLookupError(
> +                _('revset size mismatch.'
> +                ' expected between %d and %d, got %d') % (minsize,
> +                                                          maxsize,
> +                                                          len(rev)))
> +    if isinstance(size, int):
> +        if len(rev) != size:
> +            raise error.RepoLookupError(
> +                _('revset size mismatch.'
> +                ' expected %d, got %d') % (size, len(rev)))

There's no point to duplicate these "if"s because both minsize/maxsize should
be set. We could switch the error messages by `minsize == maxsize`, but which
doesn't mean we need different `size` types depending on minsize/maxsize values.

> +    # filter rev by subset. since we'll probably want to get an ordered
> +    # result from expectsize(<set>), we'll have to conditionalize the
> +    # filtering direction

Nit: this comment seems unnecessary since it just rephrase the code.

> +    if order == followorder:
> +        return subset & rev
> +    else:
> +        return rev & subset


More information about the Mercurial-devel mailing list