[PATCH 3 of 7] scmutil: introduce binnode(ctx) as paired function with intrev(ctx)

Yuya Nishihara yuya at tcha.org
Sat Jun 3 10:39:27 EDT 2017


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1496484721 -32400
#      Sat Jun 03 19:12:01 2017 +0900
# Node ID 4e1430d8750d271a5812b96243e1ec68d3e6ebb4
# Parent  aee024abcb7e2abcadd612cc736c93639cc275e0
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.

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -15,7 +15,6 @@ import tempfile
 
 from .i18n import _
 from .node import (
-    bin,
     hex,
     nullid,
     nullrev,
@@ -1349,7 +1348,7 @@ class changeset_printer(object):
             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')
diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -15,7 +15,11 @@ import re
 import socket
 
 from .i18n import _
-from .node import wdirrev
+from .node import (
+    wdirid,
+    wdirrev,
+)
+
 from . import (
     encoding,
     error,
@@ -376,6 +380,13 @@ def walkrepos(path, followsym=False, see
                         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"""
diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -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 @@ def _formatrevnode(ctx):
     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:


More information about the Mercurial-devel mailing list