From patchwork Fri Jan 17 08:45:29 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [1,of,2] template: add shortestnode keyword From: Durham Goode X-Patchwork-Id: 3365 Message-Id: To: mercurial-devel@selenic.com Date: Fri, 17 Jan 2014 00:45:29 -0800 # HG changeset patch # User Durham Goode # 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. I wanted to do this as a filter, like '{node|shortest}', but filters don't have access to the repo. 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 + def showp1rev(repo, ctx, templ, **args): """:p1rev: Integer. The repository-local revision number of the changeset's first parent, or -1 if the changeset has no parents.""" @@ -381,6 +403,7 @@ 'phase': showphase, 'phaseidx': showphaseidx, 'rev': showrev, + 'shortestnode': showshortestnode, 'tags': showtags, } diff --git a/tests/test-command-template.t b/tests/test-command-template.t --- a/tests/test-command-template.t +++ b/tests/test-command-template.t @@ -1626,3 +1626,12 @@ $ hg log -r 0 --template '{if(branches, "yes", "no")}\n' no + +Test shortestnode keyword: + + $ echo b > b + $ hg ci -qAm b + $ hg log --template '{shortestnode}\n' + d97c + f776 +