changeset 46843:728d89f6f9b1

refactor: prefer checks against nullrev over nullid A common pattern is using a changeset context and obtaining the node to compare against nullid. Change this to obtain the nullrev instead. In the future, the nullid becomes a property of the repository and is no longer a global constant, so using nullrev is much easier to reason about. Python function call overhead makes the difference moot, but future changes will result in more dictionary lookups otherwise, so prefer the simpler pattern. Differential Revision: https://phab.mercurial-scm.org/D10290
author Joerg Sonnenberger <joerg@bec.de>
date Tue, 30 Mar 2021 02:32:30 +0200
parents ad878e3f282b
children 2fd5e0054dd9
files hgext/extdiff.py hgext/split.py mercurial/context.py mercurial/copies.py mercurial/logcmdutil.py mercurial/mergestate.py mercurial/shelve.py mercurial/simplemerge.py
diffstat 8 files changed, 19 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/extdiff.py	Tue Mar 30 02:33:12 2021 +0200
+++ b/hgext/extdiff.py	Tue Mar 30 02:32:30 2021 +0200
@@ -91,7 +91,7 @@
 
 from mercurial.i18n import _
 from mercurial.node import (
-    nullid,
+    nullrev,
     short,
 )
 from mercurial import (
@@ -565,18 +565,18 @@
             repo, [from_rev] + [to_rev], b'nowarn'
         )
         ctx1a = scmutil.revsingle(repo, from_rev, None)
-        ctx1b = repo[nullid]
+        ctx1b = repo[nullrev]
         ctx2 = scmutil.revsingle(repo, to_rev, None)
     else:
         ctx1a, ctx2 = scmutil.revpair(repo, revs)
         if not revs:
             ctx1b = repo[None].p2()
         else:
-            ctx1b = repo[nullid]
+            ctx1b = repo[nullrev]
 
     # Disable 3-way merge if there is only one parent
     if do3way:
-        if ctx1b.node() == nullid:
+        if ctx1b.rev() == nullrev:
             do3way = False
 
     matcher = scmutil.match(ctx2, pats, opts)
--- a/hgext/split.py	Tue Mar 30 02:33:12 2021 +0200
+++ b/hgext/split.py	Tue Mar 30 02:32:30 2021 +0200
@@ -12,7 +12,7 @@
 from mercurial.i18n import _
 
 from mercurial.node import (
-    nullid,
+    nullrev,
     short,
 )
 
@@ -80,12 +80,12 @@
                 raise error.InputError(_(b'cannot split multiple revisions'))
 
             rev = revs.first()
-            ctx = repo[rev]
-            # Handle nullid specially here (instead of leaving for precheck()
+            # Handle nullrev specially here (instead of leaving for precheck()
             # below) so we get a nicer message and error code.
-            if rev is None or ctx.node() == nullid:
+            if rev is None or rev == nullrev:
                 ui.status(_(b'nothing to split\n'))
                 return 1
+            ctx = repo[rev]
             if ctx.node() is None:
                 raise error.InputError(_(b'cannot split working directory'))
 
--- a/mercurial/context.py	Tue Mar 30 02:33:12 2021 +0200
+++ b/mercurial/context.py	Tue Mar 30 02:32:30 2021 +0200
@@ -2885,7 +2885,7 @@
         # "1 < len(self._parents)" can't be used for checking
         # existence of the 2nd parent, because "memctx._parents" is
         # explicitly initialized by the list, of which length is 2.
-        if p2.node() != nullid:
+        if p2.rev() != nullrev:
             man2 = p2.manifest()
             managing = lambda f: f in man1 or f in man2
         else:
@@ -2903,7 +2903,7 @@
         return scmutil.status(modified, added, removed, [], [], [], [])
 
     def parents(self):
-        if self._parents[1].node() == nullid:
+        if self._parents[1].rev() == nullrev:
             return [self._parents[0]]
         return self._parents
 
@@ -3052,7 +3052,7 @@
         # "1 < len(self._parents)" can't be used for checking
         # existence of the 2nd parent, because "metadataonlyctx._parents" is
         # explicitly initialized by the list, of which length is 2.
-        if p2.node() != nullid:
+        if p2.rev() != nullrev:
             man2 = p2.manifest()
             managing = lambda f: f in man1 or f in man2
         else:
--- a/mercurial/copies.py	Tue Mar 30 02:33:12 2021 +0200
+++ b/mercurial/copies.py	Tue Mar 30 02:32:30 2021 +0200
@@ -149,7 +149,7 @@
     # optimization, since the ctx.files() for a merge commit is not correct for
     # this comparison.
     forwardmissingmatch = match
-    if b.p1() == a and b.p2().node() == nullid:
+    if b.p1() == a and b.p2().rev() == nullrev:
         filesmatcher = matchmod.exact(b.files())
         forwardmissingmatch = matchmod.intersectmatchers(match, filesmatcher)
     if repo.ui.configbool(b'devel', b'copy-tracing.trace-all-files'):
--- a/mercurial/logcmdutil.py	Tue Mar 30 02:33:12 2021 +0200
+++ b/mercurial/logcmdutil.py	Tue Mar 30 02:32:30 2021 +0200
@@ -14,6 +14,7 @@
 from .i18n import _
 from .node import (
     nullid,
+    nullrev,
     wdirid,
     wdirrev,
 )
@@ -82,7 +83,7 @@
     If diff.merge is enabled, an overlayworkingctx of the auto-merged parents will be returned.
     """
     repo = ctx.repo()
-    if repo.ui.configbool(b"diff", b"merge") and ctx.p2().node() != nullid:
+    if repo.ui.configbool(b"diff", b"merge") and ctx.p2().rev() != nullrev:
         # avoid cycle context -> subrepo -> cmdutil -> logcmdutil
         from . import context
 
--- a/mercurial/mergestate.py	Tue Mar 30 02:33:12 2021 +0200
+++ b/mercurial/mergestate.py	Tue Mar 30 02:32:30 2021 +0200
@@ -11,6 +11,7 @@
     hex,
     nullhex,
     nullid,
+    nullrev,
 )
 from . import (
     error,
@@ -341,7 +342,7 @@
         flo = fco.flags()
         fla = fca.flags()
         if b'x' in flags + flo + fla and b'l' not in flags + flo + fla:
-            if fca.node() == nullid and flags != flo:
+            if fca.rev() == nullrev and flags != flo:
                 if preresolve:
                     self._repo.ui.warn(
                         _(
--- a/mercurial/shelve.py	Tue Mar 30 02:33:12 2021 +0200
+++ b/mercurial/shelve.py	Tue Mar 30 02:32:30 2021 +0200
@@ -534,7 +534,7 @@
     parent = parents[0]
     origbranch = wctx.branch()
 
-    if parent.node() != nullid:
+    if parent.rev() != nullrev:
         desc = b"changes to: %s" % parent.description().split(b'\n', 1)[0]
     else:
         desc = b'(changes in empty repository)'
--- a/mercurial/simplemerge.py	Tue Mar 30 02:33:12 2021 +0200
+++ b/mercurial/simplemerge.py	Tue Mar 30 02:32:30 2021 +0200
@@ -19,7 +19,7 @@
 from __future__ import absolute_import
 
 from .i18n import _
-from .node import nullid
+from .node import nullrev
 from . import (
     error,
     mdiff,
@@ -427,7 +427,7 @@
 def is_not_null(ctx):
     if not util.safehasattr(ctx, "node"):
         return False
-    return ctx.node() != nullid
+    return ctx.rev() != nullrev
 
 
 def _mergediff(m3, name_a, name_b, name_base):