scmutil: introduce binnode(ctx) as paired function with intrev(ctx)
authorYuya Nishihara <yuya@tcha.org>
Sat, 03 Jun 2017 19:12:01 +0900
changeset 32658 55ff67ffcead
parent 32657 9fbd8ad398aa
child 32659 7b17f9de6d3e
scmutil: introduce binnode(ctx) as paired function with intrev(ctx) It seemed silly to convert ctx.hex() back to binary to use node.hex/short(), or to use [:12] instead of node.short() because ctx.node() could be None. Eventually I want to change wctx.rev() and wctx.node() to return wdirrev and wdirid respectively, but that's quite big API breakage and can't be achieved without some compatibility wrappers.
mercurial/cmdutil.py
mercurial/scmutil.py
mercurial/templatekw.py
--- a/mercurial/cmdutil.py	Sat Jun 03 19:01:19 2017 +0900
+++ b/mercurial/cmdutil.py	Sat Jun 03 19:12:01 2017 +0900
@@ -15,7 +15,6 @@
 
 from .i18n import _
 from .node import (
-    bin,
     hex,
     nullid,
     nullrev,
@@ -1349,7 +1348,7 @@
             hexfunc = short
         # as of now, wctx.node() and wctx.rev() return None, but we want to
         # show the same values as {node} and {rev} templatekw
-        revnode = (scmutil.intrev(ctx), hexfunc(bin(ctx.hex())))
+        revnode = (scmutil.intrev(ctx), hexfunc(scmutil.binnode(ctx)))
 
         if self.ui.quiet:
             self.ui.write("%d:%s\n" % revnode, label='log.node')
--- a/mercurial/scmutil.py	Sat Jun 03 19:01:19 2017 +0900
+++ b/mercurial/scmutil.py	Sat Jun 03 19:12:01 2017 +0900
@@ -15,7 +15,11 @@
 import socket
 
 from .i18n import _
-from .node import wdirrev
+from .node import (
+    wdirid,
+    wdirrev,
+)
+
 from . import (
     encoding,
     error,
@@ -376,6 +380,13 @@
                         newdirs.append(d)
             dirs[:] = newdirs
 
+def binnode(ctx):
+    """Return binary node id for a given basectx"""
+    node = ctx.node()
+    if node is None:
+        return wdirid
+    return node
+
 def intrev(ctx):
     """Return integer for a given basectx that can be used in comparison or
     arithmetic operation"""
--- a/mercurial/templatekw.py	Sat Jun 03 19:01:19 2017 +0900
+++ b/mercurial/templatekw.py	Sat Jun 03 19:12:01 2017 +0900
@@ -8,7 +8,12 @@
 from __future__ import absolute_import
 
 from .i18n import _
-from .node import hex, nullid
+from .node import (
+    hex,
+    nullid,
+    short,
+)
+
 from . import (
     encoding,
     error,
@@ -158,10 +163,10 @@
     template provided by cmdutil.changeset_templater"""
     repo = ctx.repo()
     if repo.ui.debugflag:
-        hexnode = ctx.hex()
+        hexfunc = hex
     else:
-        hexnode = ctx.hex()[:12]
-    return '%d:%s' % (scmutil.intrev(ctx), hexnode)
+        hexfunc = short
+    return '%d:%s' % (scmutil.intrev(ctx), hexfunc(scmutil.binnode(ctx)))
 
 def getfiles(repo, ctx, revcache):
     if 'files' not in revcache: