hook: make "mercurial" module available for Python hooks

Summary:
Some hooks (ex. mergedriver) are checked in old release branches. Provide
"mercurial" module compatibility so they won't break.

Reviewed By: mitrandir77

Differential Revision: D14366343

fbshipit-source-id: d47cc4fd512f63e4f6cdc5d7e5ab2c4398216b2f
This commit is contained in:
Jun Wu 2019-03-08 16:00:54 -08:00 committed by Facebook Github Bot
parent 46266b7436
commit c0d96cde7d
4 changed files with 33 additions and 3 deletions

View File

@ -291,7 +291,10 @@ def load(ui, name, path):
# "mercurial" and "hgext" were moved. Detect wrong module imports.
if ui.configbool("devel", "all-warnings"):
if "mercurial" in sys.modules or "hgext" in sys.modules:
if (
"mercurial" in sys.modules
and sys.modules["mercurial"] is not sys.modules["edenscm.mercurial"]
) or "hgext" in sys.modules:
ui.develwarn("extension %s imported incorrect modules" % name)
# Before we do anything with the extension, check against minimum stated

View File

@ -259,7 +259,8 @@ def runhooks(ui, repo, htype, hooks, throw=False, **args):
if repo:
path = os.path.join(repo.root, path)
try:
mod = extensions.loadpath(path, "hghook.%s" % hname)
with util.mercurialmodule():
mod = extensions.loadpath(path, "hghook.%s" % hname)
except Exception as e:
ui.write(_("loading %s hook failed: %s\n") % (hname, e))
raise
@ -270,7 +271,8 @@ def runhooks(ui, repo, htype, hooks, throw=False, **args):
# automatically.
if hookfn.startswith("hgext."):
hookfn = "edenscm." + hookfn
r, raised = _pythonhook(ui, repo, htype, hname, hookfn, args, throw)
with util.mercurialmodule():
r, raised = _pythonhook(ui, repo, htype, hname, hookfn, args, throw)
else:
r = _exthook(ui, repo, htype, hname, cmd, args, throw)
raised = False

View File

@ -793,6 +793,25 @@ def environoverride(name, value):
encoding.environ[name] = origvalue
class mercurialmodule(object):
"""Provide 'mercurial' module compatibility. Used by legacy Python hooks."""
def __init__(self):
self._needdel = False
def __enter__(self):
if sys.modules.get("mercurial") is not sys.modules["edenscm.mercurial"]:
assert "mercurial" not in sys.modules
sys.modules["mercurial"] = sys.modules["edenscm.mercurial"]
self._needdel = True
return self
def __exit__(self, exctype, excvalue, traceback):
if self._needdel:
del sys.modules["mercurial"]
self._needdel = False
class _lrucachenode(object):
"""A node in a doubly linked list.

View File

@ -20,8 +20,14 @@ and add B_add and C_add. Note: there are no conflicts.
$ setconfig experimental.mergedriver=python:$TESTTMP/mergedriver-test.py
$ cat > $TESTTMP/mergedriver-test.py << EOF
> from edenscm.mercurial import node
> from mercurial import node as node2
> assert node is node2
> import os
> def preprocess(ui, repo, hooktype, mergestate, wctx, labels):
> from edenscm.mercurial import util
> from mercurial import util as util2
> assert util is util2
> ui.write("merge driver preprocess\n")
> # Right now, need to mark at least one file to get mergedriver running
> mergestate.mark("A", "d") # driver-resovled