Patchwork D3371: scmutil: make shortesthexnodeidprefix() take a full binary nodeid

login
register
mail settings
Submitter phabricator
Date April 14, 2018, 4:22 p.m.
Message ID <eda30cdaecf6a9789ed6e107d2550e5e@localhost.localdomain>
Download mbox | patch
Permalink /patch/31049/
State Not Applicable
Headers show

Comments

phabricator - April 14, 2018, 4:22 p.m.
martinvonz updated this revision to Diff 8286.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D3371?vs=8256&id=8286

REVISION DETAIL
  https://phab.mercurial-scm.org/D3371

AFFECTED FILES
  hgext/show.py
  mercurial/scmutil.py
  mercurial/templatefuncs.py
  tests/test-command-template.t

CHANGE DETAILS




To: martinvonz, #hg-reviewers
Cc: mercurial-devel

Patch

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
@@ -3900,6 +3900,21 @@ 
   $ hg log -r 'wdir()' -T '{node|shortest}\n'
   ffff
 
+  $ hg log --template '{shortest("f")}\n' -l1
+  f
+
+  $ hg log --template '{shortest("0123456789012345678901234567890123456789")}\n' -l1
+  0123456789012345678901234567890123456789
+
+  $ hg log --template '{shortest("01234567890123456789012345678901234567890123456789")}\n' -l1
+  01234567890123456789012345678901234567890123456789
+
+  $ hg log --template '{shortest("not a hex string")}\n' -l1
+  not a hex string
+
+  $ hg log --template '{shortest("not a hex string, but it'\''s 40 bytes long")}\n' -l1
+  not a hex string, but it's 40 bytes long
+
   $ cd ..
 
 Test shortest(node) with the repo having short hash collision:
diff --git a/mercurial/templatefuncs.py b/mercurial/templatefuncs.py
--- a/mercurial/templatefuncs.py
+++ b/mercurial/templatefuncs.py
@@ -26,6 +26,9 @@ 
     templateutil,
     util,
 )
+from .node import (
+    bin,
+)
 from .utils import (
     dateutil,
     stringutil,
@@ -579,15 +582,29 @@ 
         # i18n: "shortest" is a keyword
         raise error.ParseError(_("shortest() expects one or two arguments"))
 
-    node = evalstring(context, mapping, args[0])
+    hexnode = evalstring(context, mapping, args[0])
 
     minlength = 4
     if len(args) > 1:
         minlength = evalinteger(context, mapping, args[1],
                                 # i18n: "shortest" is a keyword
                                 _("shortest() expects an integer minlength"))
 
     repo = context.resource(mapping, 'ctx')._repo
+    if len(hexnode) > 40:
+        return hexnode
+    elif len(hexnode) == 40:
+        try:
+            node = bin(hexnode)
+        except TypeError:
+            return hexnode
+    else:
+        try:
+            node = scmutil.resolvehexnodeidprefix(repo, hexnode)
+        except (error.LookupError, error.WdirUnsupported):
+            return hexnode
+        if not node:
+            return hexnode
     return scmutil.shortesthexnodeidprefix(repo, node, minlength)
 
 @templatefunc('strip(text[, chars])')
diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -443,12 +443,12 @@ 
     repo.changelog.rev(node)  # make sure node isn't filtered
     return node
 
-def shortesthexnodeidprefix(repo, hexnode, minlength=1):
+def shortesthexnodeidprefix(repo, node, minlength=1):
     """Find the shortest unambiguous prefix that matches hexnode."""
     # _partialmatch() of filtered changelog could take O(len(repo)) time,
     # which would be unacceptably slow. so we look for hash collision in
     # unfiltered space, which means some hashes may be slightly longer.
-    return repo.unfiltered().changelog.shortest(hexnode, minlength)
+    return repo.unfiltered().changelog.shortest(hex(node), minlength)
 
 def isrevsymbol(repo, symbol):
     """Checks if a symbol exists in the repo.
diff --git a/hgext/show.py b/hgext/show.py
--- a/hgext/show.py
+++ b/hgext/show.py
@@ -29,7 +29,6 @@ 
 
 from mercurial.i18n import _
 from mercurial.node import (
-    hex,
     nullrev,
 )
 from mercurial import (
@@ -448,8 +447,8 @@ 
     if not revs:
         return minlen
     cl = repo.changelog
-    return max(len(scmutil.shortesthexnodeidprefix(repo, hex(cl.node(r)),
-                                                   minlen)) for r in revs)
+    return max(len(scmutil.shortesthexnodeidprefix(repo, cl.node(r), minlen))
+               for r in revs)
 
 # Adjust the docstring of the show command so it shows all registered views.
 # This is a bit hacky because it runs at the end of module load. When moved