[PATCH 1 of 2] template: add shortestnode keyword

Durham Goode durham at fb.com
Fri Jan 17 12:29:25 CST 2014


On 1/17/14 5:24 AM, "Mads Kiilerich" <mads at kiilerich.com> wrote:

>On 01/17/2014 09:45 AM, Durham Goode wrote:
>> # HG changeset patch
>> # User Durham Goode <durham at fb.com>
>> # Date 1389946237 28800
>> #      Fri Jan 17 00:10:37 2014 -0800
>> # Node ID b18359a70b640d2aeb3f4afd1c8e47d775402b8e
>> # Parent  6545770bd37991b4ff0400479455a6e3ffa5976b
>> template: add shortestnode keyword
>>
>> I wanted to do this as a filter, like '{node|shortest}', but filters
>>don't
>> have access to the repo.
>
>Could that be fixed so we could do it the preferred way instead?

I wasn't sure if filters were meant to be super simple string
manipulations. I also thought the current version would be more likely to
get accepted before the code freeze than a version that touched every
filter method.

>
>> diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
>> --- a/mercurial/templatekw.py
>> +++ b/mercurial/templatekw.py
>> @@ -307,6 +307,28 @@
>>       """
>>       return ctx.hex()
>>   
>> +def showshortestnode(repo, ctx, templ, **args):
>> +    """:shortestnode: String. The shortest changeset identification
>>hash that
>> +    uniquely identifies the changeset.
>> +    """
>> +    node = ctx.hex()
>> +
>> +    shortest = node
>> +    length = 6
>> +    cl = repo.changelog
>> +    while True:
>> +        test = node[:length]
>> +        try:
>> +            cl.index.partialmatch(test)
>> +            if length == 4:
>> +                return test
>> +            length -= 1
>> +            shortest = test
>> +        except error.RevlogError:
>> +            length += 1
>> +            if len(shortest) == length:
>> +                return shortest
>
>AFAICS, if the shortest hash has length 7, it will make a lookup with
>prefix 6 twice. A deliberate trade-off?

Yes, but it's unlikely that 7 will be needed (3% chance in our big repo),
so I figured the algorithmic cleanliness was preferrable.

>
>Wouldn't it be possible to do this much more efficiently using
>partialmatch internals?

Probably, but the performance of this implementation seems pretty good.
Logging 1000 commits in hg-crew using {shortestnode} takes only 0.03
seconds longer than using {node} (0.37s vs 0.34s).  Even on our internal
large repo it only adds 0.15 seconds.



More information about the Mercurial-devel mailing list