[PATCH 1 of 2] template: add shortestnode keyword

Mads Kiilerich mads at kiilerich.com
Fri Jan 17 07:24:47 CST 2014


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
>
> Adds a '{shortestnode}' template keyword that results in the shortest hex node
> that uniquely identifies the changeset at that time.

Nice.
(I could imagine that it could be nice to have a way specify that I want 
n extra digits to make it more likely that it stay unique in the future 
or with other developer's local changes. But I haven't tried this yet 
and do not if that would solve a real problem.)

> 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?

> 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?

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

/Mads


More information about the Mercurial-devel mailing list