def revs(rev):
    """Normalize revision range.

    Takes a revision string representing one revision or a range of them
    and returns a pair (startrev, endrev).

    For example, the revision 43 starts at r42 and ends at r43:

      >>> revs('43')
      (42, '43')

    Sometimes it is convenient to copy and paste revision numbers
    from svn log output

      >>> revs('r43')
      (42, '43')

    Revision ranges are also supported:

      >>> revs('42-21252')
      (41, 21252)
      >>> revs('42-HEAD')
      (41, 'HEAD')

    SVN-compatible revision ranges also work

      >>> revs('41:21252')
      (41, 21252)
      >>> revs('41:HEAD')
      (41, 'HEAD')

    Even reverse diffs that undo certain revisions are supported
    correctly:

      >>> revs('42:41') # undo r42
      (42, 41)

    But not with the "simple rev" syntax

      >>> revs('42-41')
      Traceback (most recent call last):
        ...
      ValueError: empty range (42-41)

    """
    if rev.startswith('r'):
        rev = rev[1:]
    if '-' in rev:
        rev, endrev = rev.split('-')
        rev = int(rev) - 1
        if endrev != 'HEAD':
            endrev = int(endrev)
            if rev >= endrev:
                raise ValueError('empty range (%s-%s)' % (rev + 1, endrev))
    elif ':' in rev:
        rev, endrev = rev.split(':')
        rev = int(rev)
        if not endrev == 'HEAD':
            endrev = int(endrev)
    else:
        endrev = rev
        rev = int(rev) - 1
    return rev, endrev
