[PATCH 1 of 2] hgversion: add hgversionint to make it easier to compare mercurial versions

4 views
Skip to first unread message

Angel Ezquerra

unread,
Apr 25, 2012, 8:03:51 AM4/25/12
to thg...@googlegroups.com
# HG changeset patch
# User Angel Ezquerra <angel.e...@gmail.com>
# Date 1335300236 -7200
# Branch stable
# Node ID c9c84ee8c38a5f7fb634d47d19bc4d8f79efd1db
# Parent 0233fd2f66840ad83a668e7b0a61a189f20ebf91
hgversion: add hgversionint to make it easier to compare mercurial versions

hgversionint is an integer representing mercurial's version number. It maps
3 digit version numbers into integers. For example, hgversion 2.2.1 corresponds
to hgversionint 221. If hgversion has less than 3 digits, a 0 is added to the
right side of the version, givin a power of 10 hgversion it (e.g. hgversion 2.2
corresponds to hgversionint 220).

If the hgversion is a revision id or it just does not follow the usual A.B,
A.B.C (etc) convention, hgversionint will be None.

The purpose of this patch is to make it easier to compare the current mercurial
version to a given fixed version number.

This is necessary when a mercurial version introduces some new functionality
that is not easy to detect by importing the corresponding module.

diff --git a/tortoisehg/util/hgversion.py b/tortoisehg/util/hgversion.py
--- a/tortoisehg/util/hgversion.py
+++ b/tortoisehg/util/hgversion.py
@@ -16,6 +16,20 @@
from mercurial import version
hgversion = version.get_version()

+def version2int(v):
+ v = v.split('+')[0]
+ if not v or v == 'unknown' or len(v) >= 12:
+ return None
+ vers = re.split(r'\.|-', v)[:3]
+ if len(vers) < 2:
+ return None
+ vers = [int(d) for d in vers]
+ if len(vers) < 3:
+ vers.append(0)
+ return 100 * vers[0] + 10 * vers[1] + vers[2]
+
+hgversionint = version2int(hgversion)
+
def checkhgversion(v):
"""range check the Mercurial version"""
reqver = ['2', '1']

thg_12767.patch

Steve Borho

unread,
May 3, 2012, 1:01:26 AM5/3/12
to thg...@googlegroups.com
On Wed, Apr 25, 2012 at 7:03 AM, Angel Ezquerra
<angel.e...@gmail.com> wrote:
> # HG changeset patch
> # User Angel Ezquerra <angel.e...@gmail.com>
> # Date 1335300236 -7200
> # Branch stable
> # Node ID c9c84ee8c38a5f7fb634d47d19bc4d8f79efd1db
> # Parent  0233fd2f66840ad83a668e7b0a61a189f20ebf91
> hgversion: add hgversionint to make it easier to compare mercurial versions
>
> hgversionint is an integer representing mercurial's version number. It maps
> 3 digit version numbers into integers. For example, hgversion 2.2.1 corresponds
> to hgversionint 221. If hgversion has less than 3 digits, a 0 is added to the
> right side of the version, givin a power of 10 hgversion it (e.g. hgversion 2.2
> corresponds to hgversionint 220).
>
> If the hgversion is a revision id or it just does not follow the usual A.B,
> A.B.C (etc) convention, hgversionint will be None.
>
> The purpose of this patch is to make it easier to compare the current mercurial
> version to a given fixed version number.
>
> This is necessary when a mercurial version introduces some new functionality
> that is not easy to detect by importing the corresponding module.

It's not clear why this is necessary. Python is perfectly happy
comparing two tuple objects:

('2', '1', '2') < ('2', '2')

> diff --git a/tortoisehg/util/hgversion.py b/tortoisehg/util/hgversion.py
> --- a/tortoisehg/util/hgversion.py
> +++ b/tortoisehg/util/hgversion.py
> @@ -16,6 +16,20 @@
>     from mercurial import version
>     hgversion = version.get_version()
>
> +def version2int(v):
> +    v = v.split('+')[0]
> +    if not v or v == 'unknown' or len(v) >= 12:
> +        return None
> +    vers = re.split(r'\.|-', v)[:3]
> +    if len(vers) < 2:
> +        return None
> +    vers = [int(d) for d in vers]
> +    if len(vers) < 3:
> +        vers.append(0)
> +    return 100 * vers[0] + 10 * vers[1] + vers[2]
> +
> +hgversionint = version2int(hgversion)
> +
>  def checkhgversion(v):
>     """range check the Mercurial version"""
>     reqver = ['2', '1']



--
Steve Borho

Angel Ezquerra

unread,
May 3, 2012, 1:19:14 AM5/3/12
to thg...@googlegroups.com
On Thu, May 3, 2012 at 7:01 AM, Steve Borho <st...@borho.org> wrote:
> On Wed, Apr 25, 2012 at 7:03 AM, Angel Ezquerra
> <angel.e...@gmail.com> wrote:
>> # HG changeset patch
>> # User Angel Ezquerra <angel.e...@gmail.com>
>> # Date 1335300236 -7200
>> # Branch stable
>> # Node ID c9c84ee8c38a5f7fb634d47d19bc4d8f79efd1db
>> # Parent  0233fd2f66840ad83a668e7b0a61a189f20ebf91
>> hgversion: add hgversionint to make it easier to compare mercurial versions
>>
>> hgversionint is an integer representing mercurial's version number. It maps
>> 3 digit version numbers into integers. For example, hgversion 2.2.1 corresponds
>> to hgversionint 221. If hgversion has less than 3 digits, a 0 is added to the
>> right side of the version, givin a power of 10 hgversion it (e.g. hgversion 2.2
>> corresponds to hgversionint 220).
>>
>> If the hgversion is a revision id or it just does not follow the usual A.B,
>> A.B.C (etc) convention, hgversionint will be None.
>>
>> The purpose of this patch is to make it easier to compare the current mercurial
>> version to a given fixed version number.
>>
>> This is necessary when a mercurial version introduces some new functionality
>> that is not easy to detect by importing the corresponding module.
>
> It's not clear why this is necessary.  Python is perfectly happy
> comparing two tuple objects:
>
> ('2', '1', '2') < ('2', '2')

That is a good point. I did not consider that.
However:

>>> ('2', '1', '0') == ('2', '1')
False

Which does not happen when using hgversionint.
This is a corner case though, so maybe it when can live with that...

Angel

Steve Borho

unread,
May 3, 2012, 1:23:02 AM5/3/12
to thg...@googlegroups.com
On Thu, May 3, 2012 at 12:19 AM, Angel Ezquerra
So long as you compare with a specific revision, it should be ok. ie:
hgver >= ('2', '2') will do the right thing.

--
Steve Borho
Reply all
Reply to author
Forward
0 new messages