##// END OF EJS Templates
dispatch: add generic pre- and post-command hooks
Matt Mackall -
r4630:e6d105a5 default
parent child Browse files
Show More
@@ -312,10 +312,20 hooks::
312 312 new parent is in $HG_PARENT1. If merge, ID of second new parent
313 313 is in $HG_PARENT2. If update succeeded, $HG_ERROR=0. If update
314 314 failed (e.g. because conflicts not resolved), $HG_ERROR=1.
315 pre-<command>;;
316 Run before executing the associated command. The contents of the
317 command line are passed as $HG_ARGS. If the hook returns failure,
318 the command doesn't execute and Mercurial returns the failure code.
319 post-<command>;;
320 Run after successful invocations of the associated command. The
321 contents of the command line are passed as $HG_ARGS and the result
322 code in $HG_RESULT. Hook failure is ignored.
315 323
316 Note: In earlier releases, the names of hook environment variables
317 did not have a "HG_" prefix. The old unprefixed names are no longer
318 provided in the environment.
324 Note: it is generally better to use standard hooks rather than the
325 generic pre- and post- command hooks as they are guaranteed to be
326 called in the appropriate contexts for influencing transactions.
327 Also, hooks like "commit" will be called in all contexts that
328 generate a commit (eg. tag) and not just the commit command.
319 329
320 330 The syntax for Python hooks is as follows:
321 331
@@ -9,7 +9,7 from node import *
9 9 from i18n import _
10 10 import os, sys, atexit, signal, pdb, traceback, socket, errno, shlex
11 11 import mdiff, bdiff, util, templater, patch, commands, hg, lock, time
12 import fancyopts, revlog, version, extensions
12 import fancyopts, revlog, version, extensions, hook
13 13
14 14 revrangesep = ':'
15 15
@@ -272,6 +272,7 def dispatch(ui, args):
272 272 if fallback:
273 273 util._fallbackencoding = fallback
274 274
275 fullargs = args
275 276 cmd, func, args, options, cmdoptions = parse(ui, args)
276 277
277 278 if options["encoding"]:
@@ -302,8 +303,8 def dispatch(ui, args):
302 303 elif not cmd:
303 304 return commands.help_(ui, 'shortlist')
304 305
306 repo = None
305 307 if cmd not in commands.norepo.split():
306 repo = None
307 308 try:
308 309 repo = hg.repository(ui, path=path)
309 310 ui = repo.ui
@@ -316,7 +317,15 def dispatch(ui, args):
316 317 else:
317 318 d = lambda: func(ui, *args, **cmdoptions)
318 319
319 return runcommand(ui, options, cmd, d)
320 # run pre-hook, and abort if it fails
321 ret = hook.hook(ui, repo, "pre-%s" % cmd, False, args=" ".join(fullargs))
322 if ret:
323 return ret
324 ret = runcommand(ui, options, cmd, d)
325 # run post-hook, passing command result
326 hook.hook(ui, repo, "post-%s" % cmd, False, args=" ".join(fullargs),
327 result = ret)
328 return ret
320 329
321 330 def runcommand(ui, options, cmd, cmdfunc):
322 331 def checkargs():
@@ -11,6 +11,9 echo 'commit.b = unset HG_LOCAL HG_TAG;
11 11 echo 'precommit = unset HG_LOCAL HG_NODE HG_TAG; python ../printenv.py precommit' >> .hg/hgrc
12 12 echo 'pretxncommit = unset HG_LOCAL HG_TAG; python ../printenv.py pretxncommit' >> .hg/hgrc
13 13 echo 'pretxncommit.tip = hg -q tip' >> .hg/hgrc
14 echo 'pre-identify = false' >> .hg/hgrc
15 echo 'pre-cat = echo "meow $HG_ARGS"' >> .hg/hgrc
16 echo 'post-cat = echo "purr $HG_RESULT"' >> .hg/hgrc
14 17 echo a > a
15 18 hg add a
16 19 hg commit -m a -d "1000000 0"
@@ -35,6 +38,10 hg commit -m b -d '1 0'
35 38 hg merge 1
36 39 hg commit -m merge -d '2 0'
37 40
41 # test generic hooks
42 hg id
43 hg cat b
44
38 45 cd ../b
39 46 hg pull ../a
40 47
@@ -22,6 +22,10 pretxncommit hook: HG_NODE=4c52fb2e40228
22 22 3:4c52fb2e4022
23 23 commit hook: HG_NODE=4c52fb2e402287dd5dc052090682536c8406c321 HG_PARENT1=1324a5531bac09b329c3845d35ae6a7526874edb HG_PARENT2=b702efe9688826e3a91283852b328b84dbf37bc2
24 24 commit.b hook: HG_NODE=4c52fb2e402287dd5dc052090682536c8406c321 HG_PARENT1=1324a5531bac09b329c3845d35ae6a7526874edb HG_PARENT2=b702efe9688826e3a91283852b328b84dbf37bc2
25 warning: pre-identify hook exited with status 1
26 meow cat b
27 purr 0
28 b
25 29 prechangegroup hook: HG_SOURCE=pull HG_URL=file:
26 30 changegroup hook: HG_NODE=b702efe9688826e3a91283852b328b84dbf37bc2 HG_SOURCE=pull HG_URL=file:
27 31 incoming hook: HG_NODE=b702efe9688826e3a91283852b328b84dbf37bc2 HG_SOURCE=pull HG_URL=file:
General Comments 0
You need to be logged in to leave comments. Login now