provide pre- and post- hooks with parsed command line arguments.
python hooks are passed two new keyword arguments:
- opts: a dict of options; unsepcified options are set to their default
- pats: a list of arguments
shell hooks receive two new variables containing string representations
of the above data:
- $HG_OPTS
- $HG_PATS
for example, the opts and pats for 'hg -f v1.1' would be:
{'force': True, 'message': '', 'rev': '', 'user': '', 'date': '', 'local': None, 'remove': None, 'mq': None}
['v1.1']
--- a/doc/hgrc.5.txt Wed Jun 09 16:25:28 2010 -0500
+++ b/doc/hgrc.5.txt Thu Jun 10 09:32:19 2010 -0700
@@ -570,11 +570,19 @@
``post-<command>``
Run after successful invocations of the associated command. The
contents of the command line are passed as ``$HG_ARGS`` and the result
- code in ``$HG_RESULT``. Hook failure is ignored.
+ code in ``$HG_RESULT``. Parsed command line arguments are passed as
+ ``$HG_PATS`` and ``$HG_OPTS``. These contain string representations of
+ the python data internally passed to <command>. ``$HG_OPTS`` is a
+ dictionary of options (with unspecified options set to their defaults).
+ ``$HG_PATS`` is a list of arguments. Hook failure is ignored.
``pre-<command>``
Run before executing the associated command. The contents of the
- command line are passed as ``$HG_ARGS``. If the hook returns failure,
- the command doesn't execute and Mercurial returns the failure
+ command line are passed as ``$HG_ARGS``. Parsed command line arguments
+ are passed as ``$HG_PATS`` and ``$HG_OPTS``. These contain string
+ representations of the data internally passed to <command>. ``$HG_OPTS``
+ is a dictionary of options (with unspecified options set to their
+ defaults). ``$HG_PATS`` is a list of arguments. If the hook returns
+ failure, the command doesn't execute and Mercurial returns the failure
code.
``prechangegroup``
Run before a changegroup is added via push, pull or unbundle. Exit
--- a/mercurial/dispatch.py Wed Jun 09 16:25:28 2010 -0500
+++ b/mercurial/dispatch.py Thu Jun 10 09:32:19 2010 -0700
@@ -342,15 +342,16 @@
pos += 1
return values
-def runcommand(lui, repo, cmd, fullargs, ui, options, d):
+def runcommand(lui, repo, cmd, fullargs, ui, options, d, cmdpats, cmdoptions):
# run pre-hook, and abort if it fails
- ret = hook.hook(lui, repo, "pre-%s" % cmd, False, args=" ".join(fullargs))
+ ret = hook.hook(lui, repo, "pre-%s" % cmd, False, args=" ".join(fullargs),
+ pats=cmdpats, opts=cmdoptions)
if ret:
return ret
ret = _runcommand(ui, options, cmd, d)
# run post-hook, passing command result
hook.hook(lui, repo, "post-%s" % cmd, False, args=" ".join(fullargs),
- result = ret)
+ result=ret, pats=cmdpats, opts=cmdoptions)
return ret
_loaded = set()
@@ -454,6 +455,7 @@
return commands.help_(ui, 'shortlist')
repo = None
+ cmdpats = args[:]
if cmd not in commands.norepo.split():
try:
repo = hg.repository(ui, path=path)
@@ -477,7 +479,8 @@
ui.warn("warning: --repository ignored\n")
d = lambda: util.checksignature(func)(ui, *args, **cmdoptions)
- return runcommand(lui, repo, cmd, fullargs, ui, options, d)
+ return runcommand(lui, repo, cmd, fullargs, ui, options, d,
+ cmdpats, cmdoptions)
def _runcommand(ui, options, cmd, cmdfunc):
def checkargs():
--- a/tests/test-hook.out Wed Jun 09 16:25:28 2010 -0500
+++ b/tests/test-hook.out Thu Jun 10 09:32:19 2010 -0700
@@ -24,10 +24,10 @@
3:4c52fb2e4022
commit hook: HG_NODE=4c52fb2e402287dd5dc052090682536c8406c321 HG_PARENT1=1324a5531bac09b329c3845d35ae6a7526874edb HG_PARENT2=b702efe9688826e3a91283852b328b84dbf37bc2
commit.b hook: HG_NODE=4c52fb2e402287dd5dc052090682536c8406c321 HG_PARENT1=1324a5531bac09b329c3845d35ae6a7526874edb HG_PARENT2=b702efe9688826e3a91283852b328b84dbf37bc2
-pre-identify hook: HG_ARGS=id
+pre-identify hook: HG_ARGS=id HG_OPTS={'tags': None, 'rev': '', 'num': None, 'branch': None, 'id': None} HG_PATS=[]
warning: pre-identify hook exited with status 1
-pre-cat hook: HG_ARGS=cat b
-post-cat hook: HG_ARGS=cat b HG_RESULT=0
+pre-cat hook: HG_ARGS=cat b HG_OPTS={'rev': '', 'decode': None, 'exclude': [], 'output': '', 'include': []} HG_PATS=['b']
+post-cat hook: HG_ARGS=cat b HG_OPTS={'rev': '', 'decode': None, 'exclude': [], 'output': '', 'include': []} HG_PATS=['b'] HG_RESULT=0
b
prechangegroup hook: HG_SOURCE=pull HG_URL=file:
changegroup hook: HG_NODE=b702efe9688826e3a91283852b328b84dbf37bc2 HG_SOURCE=pull HG_URL=file: