##// 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 new parent is in $HG_PARENT1. If merge, ID of second new parent
312 new parent is in $HG_PARENT1. If merge, ID of second new parent
313 is in $HG_PARENT2. If update succeeded, $HG_ERROR=0. If update
313 is in $HG_PARENT2. If update succeeded, $HG_ERROR=0. If update
314 failed (e.g. because conflicts not resolved), $HG_ERROR=1.
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
324 Note: it is generally better to use standard hooks rather than the
317 did not have a "HG_" prefix. The old unprefixed names are no longer
325 generic pre- and post- command hooks as they are guaranteed to be
318 provided in the environment.
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 The syntax for Python hooks is as follows:
330 The syntax for Python hooks is as follows:
321
331
@@ -9,7 +9,7 from node import *
9 from i18n import _
9 from i18n import _
10 import os, sys, atexit, signal, pdb, traceback, socket, errno, shlex
10 import os, sys, atexit, signal, pdb, traceback, socket, errno, shlex
11 import mdiff, bdiff, util, templater, patch, commands, hg, lock, time
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 revrangesep = ':'
14 revrangesep = ':'
15
15
@@ -272,6 +272,7 def dispatch(ui, args):
272 if fallback:
272 if fallback:
273 util._fallbackencoding = fallback
273 util._fallbackencoding = fallback
274
274
275 fullargs = args
275 cmd, func, args, options, cmdoptions = parse(ui, args)
276 cmd, func, args, options, cmdoptions = parse(ui, args)
276
277
277 if options["encoding"]:
278 if options["encoding"]:
@@ -302,8 +303,8 def dispatch(ui, args):
302 elif not cmd:
303 elif not cmd:
303 return commands.help_(ui, 'shortlist')
304 return commands.help_(ui, 'shortlist')
304
305
306 repo = None
305 if cmd not in commands.norepo.split():
307 if cmd not in commands.norepo.split():
306 repo = None
307 try:
308 try:
308 repo = hg.repository(ui, path=path)
309 repo = hg.repository(ui, path=path)
309 ui = repo.ui
310 ui = repo.ui
@@ -316,7 +317,15 def dispatch(ui, args):
316 else:
317 else:
317 d = lambda: func(ui, *args, **cmdoptions)
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 def runcommand(ui, options, cmd, cmdfunc):
330 def runcommand(ui, options, cmd, cmdfunc):
322 def checkargs():
331 def checkargs():
@@ -11,6 +11,9 echo 'commit.b = unset HG_LOCAL HG_TAG;
11 echo 'precommit = unset HG_LOCAL HG_NODE HG_TAG; python ../printenv.py precommit' >> .hg/hgrc
11 echo 'precommit = unset HG_LOCAL HG_NODE HG_TAG; python ../printenv.py precommit' >> .hg/hgrc
12 echo 'pretxncommit = unset HG_LOCAL HG_TAG; python ../printenv.py pretxncommit' >> .hg/hgrc
12 echo 'pretxncommit = unset HG_LOCAL HG_TAG; python ../printenv.py pretxncommit' >> .hg/hgrc
13 echo 'pretxncommit.tip = hg -q tip' >> .hg/hgrc
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 echo a > a
17 echo a > a
15 hg add a
18 hg add a
16 hg commit -m a -d "1000000 0"
19 hg commit -m a -d "1000000 0"
@@ -35,6 +38,10 hg commit -m b -d '1 0'
35 hg merge 1
38 hg merge 1
36 hg commit -m merge -d '2 0'
39 hg commit -m merge -d '2 0'
37
40
41 # test generic hooks
42 hg id
43 hg cat b
44
38 cd ../b
45 cd ../b
39 hg pull ../a
46 hg pull ../a
40
47
@@ -22,6 +22,10 pretxncommit hook: HG_NODE=4c52fb2e40228
22 3:4c52fb2e4022
22 3:4c52fb2e4022
23 commit hook: HG_NODE=4c52fb2e402287dd5dc052090682536c8406c321 HG_PARENT1=1324a5531bac09b329c3845d35ae6a7526874edb HG_PARENT2=b702efe9688826e3a91283852b328b84dbf37bc2
23 commit hook: HG_NODE=4c52fb2e402287dd5dc052090682536c8406c321 HG_PARENT1=1324a5531bac09b329c3845d35ae6a7526874edb HG_PARENT2=b702efe9688826e3a91283852b328b84dbf37bc2
24 commit.b hook: HG_NODE=4c52fb2e402287dd5dc052090682536c8406c321 HG_PARENT1=1324a5531bac09b329c3845d35ae6a7526874edb HG_PARENT2=b702efe9688826e3a91283852b328b84dbf37bc2
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 prechangegroup hook: HG_SOURCE=pull HG_URL=file:
29 prechangegroup hook: HG_SOURCE=pull HG_URL=file:
26 changegroup hook: HG_NODE=b702efe9688826e3a91283852b328b84dbf37bc2 HG_SOURCE=pull HG_URL=file:
30 changegroup hook: HG_NODE=b702efe9688826e3a91283852b328b84dbf37bc2 HG_SOURCE=pull HG_URL=file:
27 incoming hook: HG_NODE=b702efe9688826e3a91283852b328b84dbf37bc2 HG_SOURCE=pull HG_URL=file:
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