2007-06-18 22:24:34 +04:00
|
|
|
# hook.py - hook support for mercurial
|
|
|
|
#
|
|
|
|
# Copyright 2007 Matt Mackall <mpm@selenic.com>
|
|
|
|
#
|
2009-04-26 03:08:54 +04:00
|
|
|
# This software may be used and distributed according to the terms of the
|
2010-01-20 07:20:08 +03:00
|
|
|
# GNU General Public License version 2 or any later version.
|
2007-06-18 22:24:34 +04:00
|
|
|
|
2015-08-09 05:24:40 +03:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from .i18n import _
|
|
|
|
from . import (
|
|
|
|
demandimport,
|
2017-06-02 08:05:21 +03:00
|
|
|
encoding,
|
2015-08-09 05:24:40 +03:00
|
|
|
error,
|
|
|
|
extensions,
|
2016-11-22 21:33:11 +03:00
|
|
|
pycompat,
|
2015-08-09 05:24:40 +03:00
|
|
|
util,
|
|
|
|
)
|
2007-06-18 22:24:34 +04:00
|
|
|
|
2017-03-31 11:59:37 +03:00
|
|
|
def _pythonhook(ui, repo, htype, hname, funcname, args, throw):
|
2007-06-18 22:24:34 +04:00
|
|
|
'''call python hook. hook is callable object, looked up as
|
|
|
|
name in python module. if callable returns "true", hook
|
|
|
|
fails, else passes. if hook raises exception, treated as
|
|
|
|
hook failure. exception propagates if throw is "true".
|
|
|
|
|
|
|
|
reason for "true" meaning "hook failed" is so that
|
|
|
|
unmodified commands (e.g. mercurial.commands.update) can
|
|
|
|
be run as hooks without wrappers to convert return values.'''
|
|
|
|
|
2014-06-23 17:24:38 +04:00
|
|
|
if callable(funcname):
|
2014-02-15 04:23:12 +04:00
|
|
|
obj = funcname
|
2017-05-31 23:11:34 +03:00
|
|
|
funcname = pycompat.sysbytes(obj.__module__ + r"." + obj.__name__)
|
2014-02-15 04:23:12 +04:00
|
|
|
else:
|
2007-06-18 22:24:34 +04:00
|
|
|
d = funcname.rfind('.')
|
|
|
|
if d == -1:
|
2015-10-13 04:49:23 +03:00
|
|
|
raise error.HookLoadError(
|
2016-02-12 22:34:04 +03:00
|
|
|
_('%s hook is invalid: "%s" not in a module')
|
2015-10-13 04:49:23 +03:00
|
|
|
% (hname, funcname))
|
2007-06-18 22:24:34 +04:00
|
|
|
modname = funcname[:d]
|
2009-12-21 18:12:43 +03:00
|
|
|
oldpaths = sys.path
|
2011-07-26 01:14:02 +04:00
|
|
|
if util.mainfrozen():
|
2009-08-06 06:45:54 +04:00
|
|
|
# binary installs require sys.path manipulation
|
2009-12-21 18:12:43 +03:00
|
|
|
modpath, modfile = os.path.split(modname)
|
|
|
|
if modpath and modfile:
|
|
|
|
sys.path = sys.path[:] + [modpath]
|
|
|
|
modname = modfile
|
2015-05-28 23:42:04 +03:00
|
|
|
with demandimport.deactivated():
|
2007-06-18 22:24:34 +04:00
|
|
|
try:
|
2015-05-28 23:42:04 +03:00
|
|
|
obj = __import__(modname)
|
2016-02-13 01:50:10 +03:00
|
|
|
except (ImportError, SyntaxError):
|
2016-02-12 09:02:52 +03:00
|
|
|
e1 = sys.exc_info()
|
2015-05-28 23:42:04 +03:00
|
|
|
try:
|
|
|
|
# extensions are loaded with hgext_ prefix
|
|
|
|
obj = __import__("hgext_%s" % modname)
|
2016-02-13 01:50:10 +03:00
|
|
|
except (ImportError, SyntaxError):
|
2016-02-12 09:02:52 +03:00
|
|
|
e2 = sys.exc_info()
|
2015-05-28 23:42:04 +03:00
|
|
|
if ui.tracebackflag:
|
|
|
|
ui.warn(_('exception from first failed import '
|
|
|
|
'attempt:\n'))
|
|
|
|
ui.traceback(e1)
|
|
|
|
if ui.tracebackflag:
|
|
|
|
ui.warn(_('exception from second failed import '
|
|
|
|
'attempt:\n'))
|
|
|
|
ui.traceback(e2)
|
2016-02-12 09:52:23 +03:00
|
|
|
|
|
|
|
if not ui.tracebackflag:
|
|
|
|
tracebackhint = _(
|
|
|
|
'run with --traceback for stack trace')
|
|
|
|
else:
|
|
|
|
tracebackhint = None
|
2015-10-13 04:49:23 +03:00
|
|
|
raise error.HookLoadError(
|
2016-02-12 09:41:20 +03:00
|
|
|
_('%s hook is invalid: import of "%s" failed') %
|
2016-02-12 09:52:23 +03:00
|
|
|
(hname, modname), hint=tracebackhint)
|
2009-08-06 06:45:54 +04:00
|
|
|
sys.path = oldpaths
|
2007-06-18 22:24:34 +04:00
|
|
|
try:
|
|
|
|
for p in funcname.split('.')[1:]:
|
|
|
|
obj = getattr(obj, p)
|
2008-10-28 21:25:26 +03:00
|
|
|
except AttributeError:
|
2015-10-13 04:49:23 +03:00
|
|
|
raise error.HookLoadError(
|
2016-02-12 09:41:20 +03:00
|
|
|
_('%s hook is invalid: "%s" is not defined')
|
2015-10-13 04:49:23 +03:00
|
|
|
% (hname, funcname))
|
2014-06-23 17:24:38 +04:00
|
|
|
if not callable(obj):
|
2015-10-13 04:49:23 +03:00
|
|
|
raise error.HookLoadError(
|
2016-02-12 09:41:20 +03:00
|
|
|
_('%s hook is invalid: "%s" is not callable')
|
2015-10-13 04:49:23 +03:00
|
|
|
% (hname, funcname))
|
2014-02-15 04:23:12 +04:00
|
|
|
|
|
|
|
ui.note(_("calling hook %s: %s\n") % (hname, funcname))
|
2017-02-16 00:17:39 +03:00
|
|
|
starttime = util.timer()
|
2014-02-15 04:23:12 +04:00
|
|
|
|
2007-06-18 22:24:34 +04:00
|
|
|
try:
|
2017-12-10 02:16:50 +03:00
|
|
|
r = obj(ui=ui, repo=repo, hooktype=htype, **pycompat.strkwargs(args))
|
2015-06-24 08:20:08 +03:00
|
|
|
except Exception as exc:
|
2015-10-08 22:55:45 +03:00
|
|
|
if isinstance(exc, error.Abort):
|
2015-05-15 17:56:43 +03:00
|
|
|
ui.warn(_('error: %s hook failed: %s\n') %
|
|
|
|
(hname, exc.args[0]))
|
|
|
|
else:
|
|
|
|
ui.warn(_('error: %s hook raised an exception: '
|
2017-06-02 08:05:21 +03:00
|
|
|
'%s\n') % (hname, encoding.strtolocal(str(exc))))
|
2015-05-15 17:56:43 +03:00
|
|
|
if throw:
|
2007-06-18 22:24:34 +04:00
|
|
|
raise
|
2016-02-12 22:44:35 +03:00
|
|
|
if not ui.tracebackflag:
|
|
|
|
ui.warn(_('(run with --traceback for stack trace)\n'))
|
2015-05-15 17:56:43 +03:00
|
|
|
ui.traceback()
|
2015-10-15 02:27:10 +03:00
|
|
|
return True, True
|
2011-07-09 20:06:59 +04:00
|
|
|
finally:
|
2017-02-16 00:17:39 +03:00
|
|
|
duration = util.timer() - starttime
|
2013-02-14 00:51:30 +04:00
|
|
|
ui.log('pythonhook', 'pythonhook-%s: %s finished in %0.2f seconds\n',
|
2017-03-31 11:59:37 +03:00
|
|
|
htype, funcname, duration)
|
2007-06-18 22:24:34 +04:00
|
|
|
if r:
|
|
|
|
if throw:
|
2014-11-30 06:13:35 +03:00
|
|
|
raise error.HookAbort(_('%s hook failed') % hname)
|
2007-06-18 22:24:34 +04:00
|
|
|
ui.warn(_('warning: %s hook failed\n') % hname)
|
2015-10-15 02:27:10 +03:00
|
|
|
return r, False
|
2007-06-18 22:24:34 +04:00
|
|
|
|
2017-03-31 12:08:11 +03:00
|
|
|
def _exthook(ui, repo, htype, name, cmd, args, throw):
|
2007-06-18 22:24:34 +04:00
|
|
|
ui.note(_("running hook %s: %s\n") % (name, cmd))
|
2009-02-17 04:35:07 +03:00
|
|
|
|
2017-02-16 00:17:39 +03:00
|
|
|
starttime = util.timer()
|
2009-02-17 04:35:07 +03:00
|
|
|
env = {}
|
2015-10-16 19:15:34 +03:00
|
|
|
|
|
|
|
# make in-memory changes visible to external process
|
2015-12-03 03:38:21 +03:00
|
|
|
if repo is not None:
|
|
|
|
tr = repo.currenttransaction()
|
|
|
|
repo.dirstate.write(tr)
|
|
|
|
if tr and tr.writepending():
|
|
|
|
env['HG_PENDING'] = repo.root
|
2017-03-31 12:08:11 +03:00
|
|
|
env['HG_HOOKTYPE'] = htype
|
2017-03-31 12:53:56 +03:00
|
|
|
env['HG_HOOKNAME'] = name
|
2015-10-16 19:15:34 +03:00
|
|
|
|
2009-02-17 04:35:07 +03:00
|
|
|
for k, v in args.iteritems():
|
2014-06-23 17:24:38 +04:00
|
|
|
if callable(v):
|
2009-02-17 04:35:07 +03:00
|
|
|
v = v()
|
2010-12-24 17:22:00 +03:00
|
|
|
if isinstance(v, dict):
|
|
|
|
# make the dictionary element order stable across Python
|
|
|
|
# implementations
|
|
|
|
v = ('{' +
|
|
|
|
', '.join('%r: %r' % i for i in sorted(v.iteritems())) +
|
|
|
|
'}')
|
2009-02-17 04:35:07 +03:00
|
|
|
env['HG_' + k.upper()] = v
|
|
|
|
|
2008-01-18 18:03:17 +03:00
|
|
|
if repo:
|
|
|
|
cwd = repo.root
|
|
|
|
else:
|
2016-11-22 21:33:11 +03:00
|
|
|
cwd = pycompat.getcwd()
|
2017-03-06 14:09:15 +03:00
|
|
|
r = ui.system(cmd, environ=env, cwd=cwd, blockedtag='exthook-%s' % (name,))
|
2013-02-09 21:04:32 +04:00
|
|
|
|
2017-02-16 00:17:39 +03:00
|
|
|
duration = util.timer() - starttime
|
2013-02-14 00:51:30 +04:00
|
|
|
ui.log('exthook', 'exthook-%s: %s finished in %0.2f seconds\n',
|
2013-02-09 21:04:32 +04:00
|
|
|
name, cmd, duration)
|
2007-06-18 22:24:34 +04:00
|
|
|
if r:
|
2011-05-06 17:31:09 +04:00
|
|
|
desc, r = util.explainexit(r)
|
2007-06-18 22:24:34 +04:00
|
|
|
if throw:
|
2014-11-30 06:13:35 +03:00
|
|
|
raise error.HookAbort(_('%s hook %s') % (name, desc))
|
2007-06-18 22:24:34 +04:00
|
|
|
ui.warn(_('warning: %s hook %s\n') % (name, desc))
|
|
|
|
return r
|
|
|
|
|
2016-04-14 12:41:15 +03:00
|
|
|
# represent an untrusted hook command
|
|
|
|
_fromuntrusted = object()
|
|
|
|
|
2012-01-16 00:50:12 +04:00
|
|
|
def _allhooks(ui):
|
2016-04-15 03:03:49 +03:00
|
|
|
"""return a list of (hook-id, cmd) pairs sorted by priority"""
|
|
|
|
hooks = _hookitems(ui)
|
2016-04-14 12:41:15 +03:00
|
|
|
# Be careful in this section, propagating the real commands from untrusted
|
|
|
|
# sources would create a security vulnerability, make sure anything altered
|
|
|
|
# in that section uses "_fromuntrusted" as its command.
|
|
|
|
untrustedhooks = _hookitems(ui, _untrusted=True)
|
|
|
|
for name, value in untrustedhooks.items():
|
|
|
|
trustedvalue = hooks.get(name, (None, None, name, _fromuntrusted))
|
|
|
|
if value != trustedvalue:
|
|
|
|
(lp, lo, lk, lv) = trustedvalue
|
|
|
|
hooks[name] = (lp, lo, lk, _fromuntrusted)
|
|
|
|
# (end of the security sensitive section)
|
2016-04-15 03:03:49 +03:00
|
|
|
return [(k, v) for p, o, k, v in sorted(hooks.values())]
|
|
|
|
|
2016-04-14 12:41:15 +03:00
|
|
|
def _hookitems(ui, _untrusted=False):
|
2016-04-15 03:03:49 +03:00
|
|
|
"""return all hooks items ready to be sorted"""
|
2016-04-14 12:28:46 +03:00
|
|
|
hooks = {}
|
2016-04-14 12:41:15 +03:00
|
|
|
for name, cmd in ui.configitems('hooks', untrusted=_untrusted):
|
2012-01-16 00:50:12 +04:00
|
|
|
if not name.startswith('priority'):
|
|
|
|
priority = ui.configint('hooks', 'priority.%s' % name, 0)
|
2016-04-14 12:28:46 +03:00
|
|
|
hooks[name] = (-priority, len(hooks), name, cmd)
|
2016-04-15 03:03:49 +03:00
|
|
|
return hooks
|
2012-01-16 00:50:12 +04:00
|
|
|
|
2008-01-11 22:06:38 +03:00
|
|
|
_redirect = False
|
|
|
|
def redirect(state):
|
2008-03-15 03:57:46 +03:00
|
|
|
global _redirect
|
2008-01-11 22:06:38 +03:00
|
|
|
_redirect = state
|
|
|
|
|
2017-10-08 14:08:31 +03:00
|
|
|
def hashook(ui, htype):
|
|
|
|
"""return True if a hook is configured for 'htype'"""
|
|
|
|
if not ui.callhooks:
|
|
|
|
return False
|
|
|
|
for hname, cmd in _allhooks(ui):
|
|
|
|
if hname.split('.')[0] == htype and cmd:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2017-03-31 12:06:42 +03:00
|
|
|
def hook(ui, repo, htype, throw=False, **args):
|
2012-06-23 20:57:07 +04:00
|
|
|
if not ui.callhooks:
|
|
|
|
return False
|
|
|
|
|
2015-10-15 02:13:31 +03:00
|
|
|
hooks = []
|
|
|
|
for hname, cmd in _allhooks(ui):
|
2017-03-31 12:06:42 +03:00
|
|
|
if hname.split('.')[0] == htype and cmd:
|
2015-10-15 02:13:31 +03:00
|
|
|
hooks.append((hname, cmd))
|
|
|
|
|
2017-03-31 12:06:42 +03:00
|
|
|
res = runhooks(ui, repo, htype, hooks, throw=throw, **args)
|
2015-10-15 02:19:47 +03:00
|
|
|
r = False
|
|
|
|
for hname, cmd in hooks:
|
2015-10-15 02:27:10 +03:00
|
|
|
r = res[hname][0] or r
|
2015-10-15 02:19:47 +03:00
|
|
|
return r
|
2015-10-15 02:13:31 +03:00
|
|
|
|
2017-03-31 12:03:23 +03:00
|
|
|
def runhooks(ui, repo, htype, hooks, throw=False, **args):
|
2017-06-17 12:59:26 +03:00
|
|
|
args = pycompat.byteskwargs(args)
|
2015-10-15 02:19:47 +03:00
|
|
|
res = {}
|
2009-10-28 23:35:57 +03:00
|
|
|
oldstdout = -1
|
2008-01-11 22:06:38 +03:00
|
|
|
|
2008-11-26 01:40:54 +03:00
|
|
|
try:
|
2015-10-15 02:13:31 +03:00
|
|
|
for hname, cmd in hooks:
|
2012-11-27 02:14:22 +04:00
|
|
|
if oldstdout == -1 and _redirect:
|
|
|
|
try:
|
2016-10-20 17:53:36 +03:00
|
|
|
stdoutno = util.stdout.fileno()
|
|
|
|
stderrno = util.stderr.fileno()
|
2012-11-27 02:14:22 +04:00
|
|
|
# temporarily redirect stdout to stderr, if possible
|
|
|
|
if stdoutno >= 0 and stderrno >= 0:
|
2016-10-20 17:53:36 +03:00
|
|
|
util.stdout.flush()
|
2012-11-27 02:14:22 +04:00
|
|
|
oldstdout = os.dup(stdoutno)
|
|
|
|
os.dup2(stderrno, stdoutno)
|
2012-11-27 03:48:39 +04:00
|
|
|
except (OSError, AttributeError):
|
|
|
|
# files seem to be bogus, give up on redirecting (WSGI, etc)
|
2012-11-27 02:14:22 +04:00
|
|
|
pass
|
|
|
|
|
2016-04-14 12:41:15 +03:00
|
|
|
if cmd is _fromuntrusted:
|
|
|
|
if throw:
|
|
|
|
raise error.HookAbort(
|
2017-03-31 12:02:05 +03:00
|
|
|
_('untrusted hook %s not executed') % hname,
|
2016-04-14 12:41:15 +03:00
|
|
|
hint = _("see 'hg help config.trusted'"))
|
2017-03-31 12:02:05 +03:00
|
|
|
ui.warn(_('warning: untrusted hook %s not executed\n') % hname)
|
2016-04-14 12:41:15 +03:00
|
|
|
r = 1
|
|
|
|
raised = False
|
|
|
|
elif callable(cmd):
|
2017-03-31 12:03:23 +03:00
|
|
|
r, raised = _pythonhook(ui, repo, htype, hname, cmd, args,
|
|
|
|
throw)
|
2008-11-26 01:40:54 +03:00
|
|
|
elif cmd.startswith('python:'):
|
2009-08-06 06:45:54 +04:00
|
|
|
if cmd.count(':') >= 2:
|
|
|
|
path, cmd = cmd[7:].rsplit(':', 1)
|
2010-12-07 17:27:04 +03:00
|
|
|
path = util.expandpath(path)
|
2010-12-11 04:18:02 +03:00
|
|
|
if repo:
|
|
|
|
path = os.path.join(repo.root, path)
|
2012-07-06 20:41:25 +04:00
|
|
|
try:
|
|
|
|
mod = extensions.loadpath(path, 'hghook.%s' % hname)
|
|
|
|
except Exception:
|
|
|
|
ui.write(_("loading %s hook failed:\n") % hname)
|
|
|
|
raise
|
2009-03-27 02:28:09 +03:00
|
|
|
hookfn = getattr(mod, cmd)
|
|
|
|
else:
|
|
|
|
hookfn = cmd[7:].strip()
|
2017-03-31 12:03:23 +03:00
|
|
|
r, raised = _pythonhook(ui, repo, htype, hname, hookfn, args,
|
2015-10-15 02:27:10 +03:00
|
|
|
throw)
|
2008-11-26 01:40:54 +03:00
|
|
|
else:
|
2017-03-31 12:08:11 +03:00
|
|
|
r = _exthook(ui, repo, htype, hname, cmd, args, throw)
|
2015-10-15 02:27:10 +03:00
|
|
|
raised = False
|
2015-10-15 02:19:47 +03:00
|
|
|
|
2015-10-15 02:27:10 +03:00
|
|
|
res[hname] = r, raised
|
hook: forcibly flush stderr for Windows test stability
There are a handful of SSH related test failures on Windows.
--- c:/Users/Matt/Projects/hg/tests/test-bundle2-exchange.t
+++ c:/Users/Matt/Projects/hg/tests/test-bundle2-exchange.t.err
@@ -305,16 +305,16 @@
remote: added 1 changesets with 1 changes to 1 files
remote: 1 new obsolescence markers
updating bookmark book_5fdd
+ pre-close-tip:02de42196ebe draft book_02de
+ postclose-tip:02de42196ebe draft book_02de
+ txnclose hook: HG_SOURCE=push-response HG_TXNNAME=push-response
+ ssh://user@dummy/other HG_URL=ssh://user@dummy/other
remote: pre-close-tip:5fddd98957c8 draft book_5fdd
remote: pushkey: lock state after "bookmarks"
remote: lock: free
remote: wlock: free
remote: postclose-tip:5fddd98957c8 draft book_5fdd
remote: txnclose hook: (env vars truncated)
- pre-close-tip:02de42196ebe draft book_02de
- postclose-tip:02de42196ebe draft book_02de
- txnclose hook: HG_SOURCE=push-response HG_TXNNAME=push-response
- ssh://user@dummy/other HG_URL=ssh://user@dummy/other
$ hg -R other log -G
o 6:5fddd98957c8 draft Nicolas Dumazet <...> book_5fdd C
|
--- c:/Users/Matt/Projects/hg/tests/test-ssh.t
+++ c:/Users/Matt/Projects/hg/tests/test-ssh.t.err
@@ -438,12 +438,12 @@
$ hg push
pushing to ssh://user@dummy/remote
searching for changes
+ local stdout
remote: adding changesets
remote: adding manifests
remote: adding file changes
remote: added 1 changesets with 1 changes to 1 files
- remote: KABOOM
- local stdout
+ remote: KABOOM\r (esc)
$ cd ..
What is happening is that no data is available in 'sshpeer.pipee' while the
command is executing. As the command completes, local output is printed, and
then sshpeer.cleanup() is called. When it calls 'self.pipeo.close()', the child
process is shutdown, flushing stderr.
As an experiment, I printed a line to stdout and another to stderr instead this
flush(). The stdout data was immediately available to the hg client, and none
of the stderr data was until the child exited. At that point, pipee has all of
the buffered data, and it is read out and printed before the pipe is closed in
sshpeer.cleanup(). This is probably a known issue, since ui.write_err()
mentions that stderr may be buffered, and also flushes stderr.
It would be nice if there was a more general fix (there is one more test that
fails), but I'm not sure what it is. I've seen (ancient) references [1] to
setvbuf() "crashing spectacularly" on some systems if any I/O has been done
already, so it seems worth avoiding.
https://sourceware.org/ml/gdb-patches/2013-08/msg00422.html
[1] https://groups.google.com/forum/#!msg/comp.lang.python/JT8LiYzYDEY/Qg9d1HwyjScJ
2015-04-11 05:30:19 +03:00
|
|
|
|
|
|
|
# The stderr is fully buffered on Windows when connected to a pipe.
|
|
|
|
# A forcible flush is required to make small stderr data in the
|
|
|
|
# remote side available to the client immediately.
|
2016-10-20 17:53:36 +03:00
|
|
|
util.stderr.flush()
|
2008-11-26 01:40:54 +03:00
|
|
|
finally:
|
2009-10-28 23:35:57 +03:00
|
|
|
if _redirect and oldstdout >= 0:
|
2016-10-20 17:53:36 +03:00
|
|
|
util.stdout.flush() # write hook output to stderr fd
|
2009-10-28 23:35:57 +03:00
|
|
|
os.dup2(oldstdout, stdoutno)
|
2008-11-26 01:40:54 +03:00
|
|
|
os.close(oldstdout)
|
2008-03-15 03:57:46 +03:00
|
|
|
|
2015-10-15 02:19:47 +03:00
|
|
|
return res
|