# HG changeset patch # User Joerg Sonnenberger # Date 2021-03-30 00:32:30 # Node ID 728d89f6f9b1d180749d7678342c2a0eb8a72f42 # Parent ad878e3f282b2623840e66226b29d9fbebd3cfbc 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 diff --git a/hgext/extdiff.py b/hgext/extdiff.py --- a/hgext/extdiff.py +++ b/hgext/extdiff.py @@ -91,7 +91,7 @@ import subprocess from mercurial.i18n import _ from mercurial.node import ( - nullid, + nullrev, short, ) from mercurial import ( @@ -565,18 +565,18 @@ def dodiff(ui, repo, cmdline, pats, opts 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) diff --git a/hgext/split.py b/hgext/split.py --- a/hgext/split.py +++ b/hgext/split.py @@ -12,7 +12,7 @@ from __future__ import absolute_import from mercurial.i18n import _ from mercurial.node import ( - nullid, + nullrev, short, ) @@ -80,12 +80,12 @@ def split(ui, repo, *revs, **opts): 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')) diff --git a/mercurial/context.py b/mercurial/context.py --- a/mercurial/context.py +++ b/mercurial/context.py @@ -2885,7 +2885,7 @@ class memctx(committablectx): # "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 @@ class memctx(committablectx): 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 @@ class metadataonlyctx(committablectx): # "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: diff --git a/mercurial/copies.py b/mercurial/copies.py --- a/mercurial/copies.py +++ b/mercurial/copies.py @@ -149,7 +149,7 @@ def _committedforwardcopies(a, b, base, # 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'): diff --git a/mercurial/logcmdutil.py b/mercurial/logcmdutil.py --- a/mercurial/logcmdutil.py +++ b/mercurial/logcmdutil.py @@ -14,6 +14,7 @@ import posixpath from .i18n import _ from .node import ( nullid, + nullrev, wdirid, wdirrev, ) @@ -82,7 +83,7 @@ def diff_parent(ctx): 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 diff --git a/mercurial/mergestate.py b/mercurial/mergestate.py --- a/mercurial/mergestate.py +++ b/mercurial/mergestate.py @@ -11,6 +11,7 @@ from .node import ( hex, nullhex, nullid, + nullrev, ) from . import ( error, @@ -341,7 +342,7 @@ class _mergestate_base(object): 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( _( diff --git a/mercurial/shelve.py b/mercurial/shelve.py --- a/mercurial/shelve.py +++ b/mercurial/shelve.py @@ -534,7 +534,7 @@ def _docreatecmd(ui, repo, pats, opts): 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)' diff --git a/mercurial/simplemerge.py b/mercurial/simplemerge.py --- a/mercurial/simplemerge.py +++ b/mercurial/simplemerge.py @@ -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 _picklabels(defaults, overrides): 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):