mercurial/templatefuncs.py
changeset 37709 7b2955624777
parent 37708 8e8541610d85
child 37859 66dc9db6ed2c
child 38040 a3b4ccbec269
--- a/mercurial/templatefuncs.py	Fri Apr 13 10:36:03 2018 -0700
+++ b/mercurial/templatefuncs.py	Sat Apr 14 00:13:08 2018 -0700
@@ -10,6 +10,9 @@
 import re
 
 from .i18n import _
+from .node import (
+    bin,
+)
 from . import (
     color,
     encoding,
@@ -579,7 +582,7 @@
         # 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:
@@ -588,6 +591,20 @@
                                 _("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])')